1
- scriptencoding utf- 8
2
- scriptversion 3
3
-
4
1
" Params:
5
2
" - (first argument) {string | undefined} (Optional)
6
3
" - File extension without '.', e.g., 'md', 'ts', or 'sh'
@@ -10,29 +7,51 @@ scriptversion 3
10
7
" - (third argument) {number | undefined} (Optional) A positive number to `:resize buffer_size`
11
8
function ! scratch_buffer#open (opening_next_fresh_buffer, ... ) abort
12
9
return s: open_buffer (#{
13
- \ opening_temporary_buffer : v: true ,
10
+ \ opening_as_tmp_buffer : v: true ,
14
11
\ opening_next_fresh_buffer: a: opening_next_fresh_buffer ,
15
12
\ args : a: 000 ,
16
13
\ })
17
14
endfunction
18
15
19
16
function ! scratch_buffer#open_file (opening_next_fresh_buffer, ... ) abort
20
17
return s: open_buffer (#{
21
- \ opening_temporary_buffer : v: false ,
18
+ \ opening_as_tmp_buffer : v: false ,
22
19
\ opening_next_fresh_buffer: a: opening_next_fresh_buffer ,
23
20
\ args : a: 000 ,
24
21
\ })
25
22
endfunction
26
23
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
+
27
46
function ! s: open_buffer (options ) abort
28
47
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
30
49
const opening_next_fresh_buffer = a: options .opening_next_fresh_buffer
31
50
51
+ call scratch_buffer#initialize_augroup ()
52
+
32
53
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)
36
55
37
56
const index = s: find_current_index (file_pattern) + (opening_next_fresh_buffer ? 1 : 0 )
38
57
const file_name = expand (printf (file_pattern, index ))
@@ -42,7 +61,7 @@ function! s:open_buffer(options) abort
42
61
43
62
execute ' silent' open_method file_name
44
63
45
- if opening_temporary_buffer
64
+ if opening_as_tmp_buffer
46
65
setlocal buftype = nofile
47
66
setlocal bufhidden = hide
48
67
else
@@ -55,6 +74,15 @@ function! s:open_buffer(options) abort
55
74
endif
56
75
endfunction
57
76
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
+
58
86
function ! s: find_current_index (pattern) abort
59
87
" NOTE: `[]->max()` returns 0
60
88
const max_buffer_index = scratch_buffer#helper#get_all_buffer_names ()
@@ -72,23 +100,24 @@ function! s:extract_index_from_name(name, pattern) abort
72
100
return len (matches) > 1 ? str2nr (matches[1 ]) : v: null
73
101
endfunction
74
102
75
- function ! s: find_next_index (pattern) abort
76
- return + 1
77
- endfunction
78
-
79
103
" Clean up all scratch buffers and files
80
104
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' , ' *' , ' ' ),
83
107
\ v: false ,
84
108
\ v: true ,
85
109
\ )
86
- for file in files
87
- call delete (file )
110
+ for persistent_file in persistent_files
111
+ call delete (persistent_file )
88
112
endfor
89
113
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
90
119
const scratch_prefix = ' ^' .. substitute (
91
- \ g: scratch_buffer_tmp_file_pattern ,
120
+ \ a: file_pattern ,
92
121
\ ' %d' ,
93
122
\ ' ' ,
94
123
\ ' ' ,
0 commit comments