Skip to content

Commit c7a46c1

Browse files
authored
Merge pull request #16 from aiya000/feature/tmp-file-pattern-when-tmp-buffer-and-when-file-buffer
Support g:scratch_buffer_file_pattern & Fix test failing
2 parents 8cd3ad4 + 57aec3b commit c7a46c1

File tree

6 files changed

+323
-125
lines changed

6 files changed

+323
-125
lines changed

README.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,22 @@ Compared to scratch.vim, vim-scratch-buffer provides these additional features:
7575
- See `:help :ScratchBufferOpen` and `:help :ScratchBufferOpenFile`
7676

7777
- Customization options
78-
- Specify filetype for syntax highlighting
78+
- Specify filetype for syntax highlighting, for `:QuickRun`, and for etc
7979
- Choose opening method (`:split` or `:vsplit`)
8080
- Control buffer height/width
81-
- Configurable auto-hiding behavior
81+
- Configurable auto-hiding behavior: [scratch.vim compatibility](#sparkles-scratchvim-compatibility)
82+
- Customize buffer file locations:
83+
```vim
84+
" Configure different paths for temporary and persistent buffers
85+
let g:scratch_buffer_file_pattern = #{
86+
\ when_tmp_buffer: '/tmp/scratch-tmp-%d', " For :ScratchBufferOpen
87+
\ when_file_buffer: expand('~/scratch/%d'), " For :ScratchBufferOpenFile
88+
\ }
89+
" This is useful if you want to keep a file buffer directory
90+
" (`~/tmp` in the above case) with `.prettier`, etc.
91+
```
92+
93+
Please also see [doc/vim-scratch-buffer.txt](./doc/vim-scratch-buffer.txt) for other functions.
8294
8395
### :gear: Detailed Usage
8496
@@ -122,6 +134,8 @@ Compared to scratch.vim, vim-scratch-buffer provides these additional features:
122134
:ScratchBufferClean
123135
```
124136

137+
Please also see [doc/vim-scratch-buffer.txt](./doc/vim-scratch-buffer.txt) for other usage.
138+
125139
## :keyboard: Default Keymappings
126140

127141
When `g:scratch_buffer_use_default_keymappings` is enabled (default: `v:false`), the following keymappings are available:
@@ -152,7 +166,7 @@ nnoremap <silent> <leader>s <Cmd>ScratchBufferOpen<CR>
152166
nnoremap <silent> <leader>S <Cmd>ScratchBufferOpenFile<CR>
153167
```
154168

155-
## :sparkles: scratch.vim Compatibility
169+
## :sparkles: scratch.vim compatibility
156170

157171
To make the plugin behave like scratch.vim, you can enable automatic buffer hiding!
158172
When enabled, scratch buffers will automatically hide when you leave the window.

autoload/scratch_buffer.vim

+48-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
scriptencoding utf-8
2-
scriptversion 3
3-
41
" Params:
52
" - (first argument) {string | undefined} (Optional)
63
" - File extension without '.', e.g., 'md', 'ts', or 'sh'
@@ -10,29 +7,51 @@ scriptversion 3
107
" - (third argument) {number | undefined} (Optional) A positive number to `:resize buffer_size`
118
function! scratch_buffer#open(opening_next_fresh_buffer, ...) abort
129
return s:open_buffer(#{
13-
\ opening_temporary_buffer: v:true,
10+
\ opening_as_tmp_buffer: v:true,
1411
\ opening_next_fresh_buffer: a:opening_next_fresh_buffer,
1512
\ args: a:000,
1613
\ })
1714
endfunction
1815

1916
function! scratch_buffer#open_file(opening_next_fresh_buffer, ...) abort
2017
return s:open_buffer(#{
21-
\ opening_temporary_buffer: v:false,
18+
\ opening_as_tmp_buffer: v:false,
2219
\ opening_next_fresh_buffer: a:opening_next_fresh_buffer,
2320
\ args: a:000,
2421
\ })
2522
endfunction
2623

24+
" Initialize augroup in case options were updated
25+
function! scratch_buffer#initialize_augroup() abort
26+
augroup VimScratchBuffer
27+
autocmd!
28+
execute
29+
\ 'autocmd'
30+
\ 'TextChanged'
31+
\ substitute(g:scratch_buffer_file_pattern.when_file_buffer, '%d', '*', '')
32+
\ 'call scratch_buffer#autocmd#save_file_buffer_if_enabled()'
33+
execute
34+
\ 'autocmd'
35+
\ 'WinLeave'
36+
\ substitute(g:scratch_buffer_file_pattern.when_tmp_buffer, '%d', '*', '')
37+
\ 'call scratch_buffer#autocmd#hide_buffer_if_enabled()'
38+
execute
39+
\ 'autocmd'
40+
\ 'WinLeave'
41+
\ substitute(g:scratch_buffer_file_pattern.when_file_buffer, '%d', '*', '')
42+
\ 'call scratch_buffer#autocmd#hide_buffer_if_enabled()'
43+
augroup END
44+
endfunction
45+
2746
function! s:open_buffer(options) abort
2847
const args = a:options.args
29-
const opening_temporary_buffer = a:options.opening_temporary_buffer
48+
const opening_as_tmp_buffer = a:options.opening_as_tmp_buffer
3049
const opening_next_fresh_buffer = a:options.opening_next_fresh_buffer
3150

51+
call scratch_buffer#initialize_augroup()
52+
3253
const file_ext = get(args, 0, g:scratch_buffer_default_file_ext)
33-
const file_pattern = (file_ext ==# '--no-file-ext' || file_ext ==# '')
34-
\ ? $'{g:scratch_buffer_tmp_file_pattern}'
35-
\ : $'{g:scratch_buffer_tmp_file_pattern}.{file_ext}'
54+
const file_pattern = s:get_file_pattern(opening_as_tmp_buffer, file_ext)
3655

3756
const index = s:find_current_index(file_pattern) + (opening_next_fresh_buffer ? 1 : 0)
3857
const file_name = expand(printf(file_pattern, index))
@@ -42,7 +61,7 @@ function! s:open_buffer(options) abort
4261

4362
execute 'silent' open_method file_name
4463

45-
if opening_temporary_buffer
64+
if opening_as_tmp_buffer
4665
setlocal buftype=nofile
4766
setlocal bufhidden=hide
4867
else
@@ -55,6 +74,15 @@ function! s:open_buffer(options) abort
5574
endif
5675
endfunction
5776

77+
function! s:get_file_pattern(opening_as_tmp_buffer, file_ext) abort
78+
const type = a:opening_as_tmp_buffer
79+
\ ? 'when_tmp_buffer'
80+
\ : 'when_file_buffer'
81+
return a:file_ext ==# '--no-file-ext' || a:file_ext ==# ''
82+
\ ? $'{g:scratch_buffer_file_pattern[type]}'
83+
\ : $'{g:scratch_buffer_file_pattern[type]}.{a:file_ext}'
84+
endfunction
85+
5886
function! s:find_current_index(pattern) abort
5987
" NOTE: `[]->max()` returns 0
6088
const max_buffer_index = scratch_buffer#helper#get_all_buffer_names()
@@ -72,23 +100,24 @@ function! s:extract_index_from_name(name, pattern) abort
72100
return len(matches) > 1 ? str2nr(matches[1]) : v:null
73101
endfunction
74102

75-
function! s:find_next_index(pattern) abort
76-
return + 1
77-
endfunction
78-
79103
" Clean up all scratch buffers and files
80104
function! scratch_buffer#clean() abort
81-
const files = glob(
82-
\ substitute(g:scratch_buffer_tmp_file_pattern, '%d', '*', ''),
105+
const persistent_files = glob(
106+
\ substitute(g:scratch_buffer_file_pattern.when_file_buffer, '%d', '*', ''),
83107
\ v:false,
84108
\ v:true,
85109
\ )
86-
for file in files
87-
call delete(file)
110+
for persistent_file in persistent_files
111+
call delete(persistent_file)
88112
endfor
89113

114+
call s:wipe_buffers(g:scratch_buffer_file_pattern.when_tmp_buffer)
115+
call s:wipe_buffers(g:scratch_buffer_file_pattern.when_file_buffer)
116+
endfunction
117+
118+
function! s:wipe_buffers(file_pattern) abort
90119
const scratch_prefix = '^' .. substitute(
91-
\ g:scratch_buffer_tmp_file_pattern,
120+
\ a:file_pattern,
92121
\ '%d',
93122
\ '',
94123
\ '',

autoload/scratch_buffer/autocmd.vim

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
scriptencoding utf-8
2-
scriptversion 3
3-
4-
function! s:is_scratch_buffer() abort
5-
return expand('%:p') =~# substitute(g:scratch_buffer_tmp_file_pattern, '%d', '*', '')
6-
endfunction
7-
8-
function! scratch_buffer#autocmd#save_file_buffer() abort
9-
if g:scratch_buffer_auto_save_file_buffer && (&buftype !=# 'nofile') && s:is_scratch_buffer()
10-
silent! write
1+
function! scratch_buffer#autocmd#save_file_buffer_if_enabled() abort
2+
if g:scratch_buffer_auto_save_file_buffer && (&buftype !=# 'nofile')
3+
silent write
114
endif
125
endfunction
136

14-
function! scratch_buffer#autocmd#hide_buffer() abort
15-
if !s:is_scratch_buffer()
16-
return
17-
endif
18-
7+
function! scratch_buffer#autocmd#hide_buffer_if_enabled() abort
198
if (&buftype ==# 'nofile') && g:scratch_buffer_auto_hide_buffer.when_tmp_buffer
209
quit
2110
return

0 commit comments

Comments
 (0)