Skip to content

Commit 719fc6e

Browse files
committed
Merge branch 'feature/open-new-buffer'
Closes #7, #4, #2, #1
2 parents 538d528 + 5218c68 commit 719fc6e

File tree

6 files changed

+350
-138
lines changed

6 files changed

+350
-138
lines changed

CONTRIBUTING.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# How to run test
2+
3+
`.github/workflows/test.yml`は実際に動いているので、これを見るのが一番わかりやすいですが、一応記しておきます。
4+
5+
1. [vim-themis](https://github.com/thinca/vim-themis)をcloneする
6+
1. `themis``$PATH`に通す(以下の例では、`~/bin``$PATH`に含まれているとします)
7+
1. テストを実行する
8+
- `--reporter`は自由に指定できます
9+
10+
```shell-session
11+
$ git clone https://github.com/thinca/vim-themis ~/bin/vim-themis
12+
$ themis test --reporter spec
13+
```

README.md

Lines changed: 83 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,28 @@ This Vim plugin is created by **Cline (Roo Code)** and me!
1414

1515
- - -
1616

17-
## :wrench: Quick Start
17+
## Table of Contents
1818

19-
```vim
20-
" Open a new scratch buffer without file extension and filetype
21-
:ScratchBufferOpen
22-
:ScratchBufferOpen --no-file-ext
23-
```
19+
- [:sparkles: scratch-buffer.vim :sparkles:](#sparkles-scratch-buffervim-sparkles)
20+
- [:wrench: Quick Start](#wrench-quick-start)
21+
- [:fire: Why scratch-buffer.vim?](#fire-why-scratch-buffervim)
22+
- [:zap: Supercharge with vim-quickrun!](#zap-supercharge-with-vim-quickrun)
23+
- [:balance_scale: Comparison with scratch.vim](#balance_scale-comparison-with-scratchvim)
24+
- [:gear: Detailed Usage](#gear-detailed-usage)
25+
- [:keyboard: Default Keymappings](#keyboard-default-keymappings)
26+
- [:sparkles: scratch.vim Compatibility](#sparkles-scratchvim-compatibility)
2427

25-
```vim
26-
" Open a new scratch buffer with Markdown filetype
27-
:ScratchBufferOpen md
28-
```
28+
## :wrench: Quick Start
2929

3030
```vim
31-
" Open a small buffer at the top for quick notes
32-
:ScratchBufferOpen md sp 5
33-
:ScratchBufferOpen --no-file-ext sp 5
31+
:ScratchBufferOpen " Open a temporary buffer using default options
32+
:ScratchBufferOpen md sp 5 " Open a temporary Markdown buffer with :sp and height 5
33+
:ScratchBufferOpenFile ts vsp 100 " Open a persistent TypeScript buffer with :vsp and width 100
34+
:ScratchBufferOpenNext " Open next temporary buffer
35+
:ScratchBufferOpenFileNext " Open next persistent buffer
3436
```
3537

36-
Of course, you can open other file types too!
37-
38-
```vim
39-
" Open a TypeScript buffer
40-
:ScratchBufferOpen ts
41-
```
38+
Please see '[Detailed Usage](#gear-detailed-usage)' section for more information.
4239

4340
## :fire: Why scratch-buffer.vim?
4441

@@ -65,15 +62,60 @@ However, vim-scratch-buffer adds more features.
6562

6663
Compared to scratch.vim, vim-scratch-buffer provides these additional features:
6764

68-
- Open multiple buffers with sequential numbering
65+
- Flexible buffer management
66+
- Open multiple buffers with sequential numbering (`:ScratchBufferOpenNext`)
67+
- Quick access to recently used buffers (`:ScratchBufferOpen`)
6968
- When you want to take notes on different topics, scratch.vim only allows one buffer
70-
- See [`:ScratchBufferOpen`](https://github.com/aiya000/vim-scratch-buffer/blob/736eef08b531b91c95497917ddb97ffbc2047c73/doc/vim-scratch-buffer.txt#L86)
71-
- Choose between writeable buffers or temporary buffers
72-
- See [`:ScratchBufferOpen`](https://github.com/aiya000/vim-scratch-buffer/blob/736eef08b531b91c95497917ddb97ffbc2047c73/doc/vim-scratch-buffer.txt#L86) and [`:ScratchBufferOpenFile`](https://github.com/aiya000/vim-scratch-buffer/blob/736eef08b531b91c95497917ddb97ffbc2047c73/doc/vim-scratch-buffer.txt#L120)
73-
- Specify filetype, opening method (`:split` or `:vsplit`), and buffer height (width)
74-
- Filetype specification enables syntax highlighting
69+
- See `:help :ScratchBufferOpen` and `:help :ScratchBufferOpenNext`
70+
71+
- Buffer type options
72+
- Choose between writeable buffers or temporary buffers
73+
- Automatic saving for file buffers when enabled
74+
- Convert temporary buffers to persistent ones when needed
75+
- See `:help :ScratchBufferOpen` and `:help :ScratchBufferOpenFile`
76+
77+
- Customization options
78+
- Specify filetype for syntax highlighting
79+
- Choose opening method (`:split` or `:vsplit`)
80+
- Control buffer height/width
81+
- Configurable auto-hiding behavior
82+
83+
### :gear: Detailed Usage
84+
85+
```vim
86+
" Basic Usage
87+
88+
" Open a temporary buffer using default settings
89+
:ScratchBufferOpen
90+
91+
" Same as :ScratchBufferOpen but opens a writable persistent buffer
92+
:ScratchBufferOpenFile
93+
```
94+
95+
```vim
96+
" Open a new scratch buffer with a specific filetype
97+
98+
" Example: Markdown
99+
:ScratchBufferOpen md
100+
101+
" Example: TypeScript
102+
:ScratchBufferOpen ts
103+
104+
" Example: No filetype
105+
:ScratchBufferOpen --no-file-ext
106+
```
107+
108+
```vim
109+
" Open multiple scratch buffers
110+
:ScratchBufferOpen md " Opens most recently used buffer
111+
:ScratchBufferOpenNext md " Always creates a new buffer
112+
```
75113

76-
## :gear: Other Features
114+
```vim
115+
" Open a small buffer at the top for quick notes
116+
:ScratchBufferOpen md sp 5
117+
:ScratchBufferOpen --no-file-ext sp 5
118+
```
77119

78120
```vim
79121
" Delete all scratch files and buffers
@@ -82,21 +124,34 @@ Compared to scratch.vim, vim-scratch-buffer provides these additional features:
82124

83125
## :keyboard: Default Keymappings
84126

85-
By default (`g:scratch_buffer_use_default_keymappings` is enabled), the following keymappings are available:
127+
When `g:scratch_buffer_use_default_keymappings` is enabled (default: `v:false`), the following keymappings are available:
86128

87129
```vim
130+
" Quick open commands (execute immediately)
88131
nnoremap <silent> <leader>b <Cmd>ScratchBufferOpen<CR>
89132
nnoremap <silent> <leader>B <Cmd>ScratchBufferOpenFile<CR>
133+
134+
" Interactive commands (allows adding arguments)
90135
nnoremap <leader><leader>b :<C-u>ScratchBufferOpen<Space>
91136
nnoremap <leader><leader>B :<C-u>ScratchBufferOpenFile<Space>
92137
```
93138

94-
You can disable these default keymappings by setting:
139+
The quick open commands create buffers with default settings, while the interactive commands let you specify file extension, open method, and buffer size.
140+
141+
You can customize these mappings by disabling the defaults:
95142

96143
```vim
97144
let g:scratch_buffer_use_default_keymappings = v:false
98145
```
99146

147+
And then defining your own:
148+
149+
```vim
150+
" Example custom mappings
151+
nnoremap <silent> <leader>s <Cmd>ScratchBufferOpen<CR>
152+
nnoremap <silent> <leader>S <Cmd>ScratchBufferOpenFile<CR>
153+
```
154+
100155
## :sparkles: scratch.vim Compatibility
101156

102157
To make the plugin behave like scratch.vim, you can enable automatic buffer hiding!

autoload/scratch_buffer.vim

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,94 @@ scriptversion 3
88
" - If omitted, creates a buffer without file extension
99
" - (second argument) {'sp' | 'vsp' | undefined} (Optional) An open method. How to open the new buffer
1010
" - (third argument) {number | undefined} (Optional) A positive number to `:resize buffer_size`
11-
function! scratch_buffer#open(...) abort
12-
return s:open_buffer(v:true, a:000)
11+
function! scratch_buffer#open(opening_next_fresh_buffer, ...) abort
12+
return s:open_buffer(#{
13+
\ opening_temporary_buffer: v:true,
14+
\ opening_next_fresh_buffer: a:opening_next_fresh_buffer,
15+
\ args: a:000,
16+
\ })
1317
endfunction
1418

15-
function! scratch_buffer#open_file(...) abort
16-
return s:open_buffer(v:false, a:000)
19+
function! scratch_buffer#open_file(opening_next_fresh_buffer, ...) abort
20+
return s:open_buffer(#{
21+
\ opening_temporary_buffer: v:false,
22+
\ opening_next_fresh_buffer: a:opening_next_fresh_buffer,
23+
\ args: a:000,
24+
\ })
1725
endfunction
1826

19-
function! s:open_buffer(temp_buffer, args) abort
20-
const file_ext = get(a:args, 0, g:scratch_buffer_default_file_ext)
27+
function! s:open_buffer(options) abort
28+
const args = a:options.args
29+
const opening_temporary_buffer = a:options.opening_temporary_buffer
30+
const opening_next_fresh_buffer = a:options.opening_next_fresh_buffer
31+
32+
const file_ext = get(args, 0, g:scratch_buffer_default_file_ext)
2133
const file_pattern = (file_ext ==# '--no-file-ext' || file_ext ==# '')
2234
\ ? $'{g:scratch_buffer_tmp_file_pattern}'
2335
\ : $'{g:scratch_buffer_tmp_file_pattern}.{file_ext}'
2436

25-
const file_name = s:find_fresh_tmp_file(file_pattern)
26-
if file_name is v:null
27-
throw 'No fresh scratch file found.'
28-
endif
37+
const index = s:find_current_index(file_pattern) + (opening_next_fresh_buffer ? 1 : 0)
38+
const file_name = expand(printf(file_pattern, index))
2939

30-
const open_method = get(a:args, 1, g:scratch_buffer_default_open_method)
31-
const buffer_size = get(a:args, 2, g:scratch_buffer_default_buffer_size)
40+
const open_method = get(args, 1, g:scratch_buffer_default_open_method)
41+
const buffer_size = get(args, 2, g:scratch_buffer_default_buffer_size)
3242

3343
execute 'silent' open_method file_name
3444

35-
if a:temp_buffer
36-
setlocal noswapfile
45+
if opening_temporary_buffer
3746
setlocal buftype=nofile
3847
setlocal bufhidden=hide
48+
else
49+
setlocal buftype=
50+
setlocal bufhidden=
3951
endif
4052

4153
if buffer_size !=# v:null
4254
execute (open_method ==# 'vsp' ? 'vertical' : '') 'resize' buffer_size
4355
endif
4456
endfunction
4557

46-
function! s:find_fresh_tmp_file(pattern) abort
47-
const all_buffer_names = scratch_buffer#helper#get_all_buffer_names()
58+
function! s:find_current_index(pattern) abort
59+
" NOTE: `[]->max()` returns 0
60+
const max_buffer_index = scratch_buffer#helper#get_all_buffer_names()
61+
\ ->mapnew({ _, buffer_name -> s:extract_index_from_name(buffer_name, a:pattern)})
62+
\ ->max()
63+
const max_file_index = glob(substitute(a:pattern, '%d', '*', ''), v:false, v:true)
64+
\ ->mapnew({ _, file_name -> s:extract_index_from_name(file_name, a:pattern) })
65+
\ ->max()
66+
return [max_buffer_index, max_file_index]->max()
67+
endfunction
4868

49-
for i in range(0, 100000)
50-
let scratch = expand(printf(a:pattern, i))
51-
if !filereadable(scratch) && !all_buffer_names->scratch_buffer#helper#contains(scratch)
52-
return scratch
53-
endif
54-
endfor
55-
return v:null
69+
function! s:extract_index_from_name(name, pattern) abort
70+
const getting_index_regex = substitute(a:pattern, '%d', '\\(\\d\\+\\)', '')
71+
const matches = matchlist(a:name, getting_index_regex)
72+
return len(matches) > 1 ? str2nr(matches[1]) : v:null
73+
endfunction
74+
75+
function! s:find_next_index(pattern) abort
76+
return + 1
5677
endfunction
5778

5879
" Clean up all scratch buffers and files
5980
function! scratch_buffer#clean() abort
60-
const all_buffer_names = scratch_buffer#helper#get_all_buffer_names()
61-
const base_pattern = printf(g:scratch_buffer_tmp_file_pattern, '*')
62-
const files = glob(base_pattern .. '*', 0, 1)
63-
64-
for scratch in files
65-
if filereadable(scratch)
66-
call delete(scratch)
67-
endif
81+
const files = glob(
82+
\ substitute(g:scratch_buffer_tmp_file_pattern, '%d', '*', ''),
83+
\ v:false,
84+
\ v:true,
85+
\ )
86+
for file in files
87+
call delete(file)
6888
endfor
6989

70-
for scratch in all_buffer_names
71-
if scratch =~# base_pattern
72-
execute ':bwipe!' bufnr(scratch)
73-
endif
90+
const scratch_prefix = '^' .. substitute(
91+
\ g:scratch_buffer_tmp_file_pattern,
92+
\ '%d',
93+
\ '',
94+
\ '',
95+
\ )
96+
const buffers = scratch_buffer#helper#get_all_buffer_names()
97+
\ ->filter({ _, buffer_name -> buffer_name =~# scratch_prefix })
98+
for buffer in buffers
99+
execute ':bwipe!' bufnr(buffer)
74100
endfor
75101
endfunction

0 commit comments

Comments
 (0)