Skip to content

Commit 259eaf6

Browse files
committed
Support :ScratchBufferOpenFile
1 parent f8bb8c3 commit 259eaf6

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

autoload/scratch_buffer.vim

+18-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ scriptversion 3
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`
1111
function! scratch_buffer#open(...) abort
12-
const file_ext = get(a:000, 0, '--no-file-ext')
12+
return s:open_buffer(v:true, a:000)
13+
endfunction
14+
15+
function! scratch_buffer#open_file(...) abort
16+
return s:open_buffer(v:false, a:000)
17+
endfunction
18+
19+
function! s:open_buffer(temp_buffer, args) abort
20+
const file_ext = get(a:args, 0, '--no-file-ext')
1321
const file_pattern = (file_ext ==# '--no-file-ext' || file_ext ==# '')
1422
\ ? $'{g:scratch_buffer_tmp_file_pattern}'
1523
\ : $'{g:scratch_buffer_tmp_file_pattern}.{file_ext}'
@@ -19,13 +27,16 @@ function! scratch_buffer#open(...) abort
1927
throw 'No fresh scratch file found.'
2028
endif
2129

22-
const open_method = get(a:000, 1, 'vsp')
23-
const buffer_size = get(a:000, 2, v:null)
30+
const open_method = get(a:args, 1, 'vsp')
31+
const buffer_size = get(a:args, 2, v:null)
2432

2533
execute 'silent' open_method file_name
26-
setlocal noswapfile
27-
setlocal buftype=nofile
28-
setlocal bufhidden=hide
34+
35+
if a:temp_buffer
36+
setlocal noswapfile
37+
setlocal buftype=nofile
38+
setlocal bufhidden=hide
39+
endif
2940

3041
if buffer_size !=# v:null
3142
execute (open_method ==# 'vsp' ? 'vertical' : '') 'resize' buffer_size
@@ -47,16 +58,14 @@ endfunction
4758
" Clean up all scratch buffers and files
4859
function! scratch_buffer#clean() abort
4960
const all_buffer_names = scratch_buffer#helper#get_all_buffer_names()
50-
5161
const base_pattern = printf(g:scratch_buffer_tmp_file_pattern, '*')
5262
const files = glob(base_pattern .. '*', 0, 1)
63+
5364
for scratch in files
54-
" Delete file if exists
5565
if filereadable(scratch)
5666
call delete(scratch)
5767
endif
5868

59-
" Delete buffer if exists
6069
if all_buffer_names->scratch_buffer#helper#contains(scratch)
6170
execute ':bwipe' bufnr(scratch)
6271
endif

doc/vim-scratch-buffer.txt

+20-7
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ COMMANDS *scratch-buffer-commands*
8484

8585
*:ScratchBufferOpen*
8686
:ScratchBufferOpen [file-extension | --no-file-ext] [open-method] [buffer-size]
87-
Open a scratch buffer with a random file name.
88-
87+
Open a temporary scratch buffer with a random file name.
88+
8989
This opens buffers by the rule
9090
described in `g:scratch_buffer_tmp_file_pattern`.
91-
91+
9292
[file-extension] is an optional argument:
9393
- When omitted or --no-file-ext is specified:
9494
Creates a buffer without file extension and filetype
9595
- Otherwise:
9696
Uses the specified extension (e.g., 'md', 'ts') as filetype
97-
97+
9898
The buffer is opened as a temporary buffer.
9999
The following properties:
100100
>
@@ -116,6 +116,14 @@ COMMANDS *scratch-buffer-commands*
116116
or doesn't resize if this is omitted.
117117

118118

119+
*:ScratchBufferOpenFile*
120+
:ScratchBufferOpenFile [file-extension | --no-file-ext] [open-method] [buffer-size]
121+
Similar to `:ScratchBufferOpen` but creates a normal buffer instead of
122+
a temporary one. The buffer can be written and saved normally.
123+
124+
Uses the same arguments as `:ScratchBufferOpen`.
125+
126+
119127
*:ScratchBufferClean*
120128
:ScratchBufferClean
121129
Delete all scratch files and buffers that
@@ -126,17 +134,22 @@ COMMANDS *scratch-buffer-commands*
126134
FUNCTIONS *scratch-buffer-functions*
127135

128136
*scratch_buffer#open()*
129-
scratch_buffer#open([file-extension | --no-filetype][, open-method][, buffer-size])
137+
scratch_buffer#open([file-extension | --no-file-ext][, open-method][, buffer-size])
130138
Same as `:ScratchBufferOpen`. Creates a buffer with or without
131139
filetype based on the first argument:
132140
- When an empty string or`'--no-file-ext'`is specified:
133141
Creates a buffer without file extension and filetype
134142
- Otherwise:
135143
Uses the specified extension as filetype
136144

145+
*scratch_buffer#open_file()*
146+
scratch_buffer#open_file([file-extension | --no-file-ext][, open-method][, buffer-size])
147+
Same as `:ScratchBufferOpenFile`. Creates a normal buffer with the same naming
148+
convention as `:ScratchBufferOpen`, but without temporary buffer properties.
149+
137150
*scratch_buffer#clean()*
138-
scratch_buffer#clean()
139-
Same as `:ScratchBufferClean`.
151+
scratch_buffer#clean()
152+
Same as `:ScratchBufferClean`.
140153

141154

142155
==============================================================================

plugin/scratch_buffer.vim

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ let g:loaded_scratch_buffer = v:true
77

88
" Example:
99
" `:ScratchBufferOpen`
10-
" `:ScratchBufferOpen --no-filetype`
10+
" `:ScratchBufferOpen --no-file-ext`
1111
" `:ScratchBufferOpen sh`
1212
" `:ScratchBufferOpen ts vsp`
1313
" `:ScratchBufferOpen md sp 5`
1414
command! -bar -nargs=* ScratchBufferOpen call scratch_buffer#open(<f-args>)
1515

16-
command! -bar -nargs=0 ScratchBufferClean call scratch_buffer#clean()
16+
" Example:
17+
" `:ScratchBufferOpenFile md`
18+
" `:ScratchBufferOpenFile ts vsp`
19+
command! -bar -nargs=* ScratchBufferOpenFile call scratch_buffer#open_file(<f-args>)
20+
21+
command! -bar ScratchBufferClean call scratch_buffer#clean()
1722

1823
let g:scratch_buffer_tmp_file_pattern = '/tmp/vim-scratch-buffer-%d'

0 commit comments

Comments
 (0)