Skip to content

Commit 24ac472

Browse files
committed
Support --no-file-ext for :ScratchBufferOpen
Closes #5
1 parent 8948ca4 commit 24ac472

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@
1111
## :wrench: Quick Start
1212

1313
```vim
14-
" Open a random file with Markdown filetype
14+
" Open a new scratch buffer without file extension and filetype
15+
:ScratchBufferOpen
16+
:ScratchBufferOpen --no-file-ext
17+
```
18+
19+
```vim
20+
" Open a new scratch buffer with Markdown filetype
1521
:ScratchBufferOpen md
1622
```
1723

1824
```vim
1925
" Open a small buffer at the top for quick notes
2026
:ScratchBufferOpen md sp 5
27+
:ScratchBufferOpen --no-file-ext sp 5
2128
```
2229

2330
Of course, you can open other file types too!

autoload/scratch_buffer.vim

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@ scriptencoding utf-8
22
scriptversion 3
33

44
" Params:
5-
" - file_ext {string} a file extension without '.', e.g., 'md', 'ts', or 'sh'
6-
" - (second argument) {'sp' | 'vsp' | undefined} An open method. How to open the new buffer
7-
" - (third argument) {number} A positive number to `:resize buffer_size`
8-
function! scratch_buffer#open(file_ext, ...) abort
9-
const file_name = s:find_fresh_tmp_file($'{g:scratch_buffer_tmp_file_pattern}.{a:file_ext}')
5+
" - (first argument) {string | undefined} (Optional)
6+
" - File extension without '.', e.g., 'md', 'ts', or 'sh'
7+
" - Or '--no-file-ext' to create a buffer without file extension
8+
" - If omitted, creates a buffer without file extension
9+
" - (second argument) {'sp' | 'vsp' | undefined} (Optional) An open method. How to open the new buffer
10+
" - (third argument) {number | undefined} (Optional) A positive number to `:resize buffer_size`
11+
function! scratch_buffer#open(...) abort
12+
const file_ext = get(a:000, 0, '--no-file-ext')
13+
const file_pattern = (file_ext ==# '--no-file-ext' || file_ext ==# '')
14+
\ ? $'{g:scratch_buffer_tmp_file_pattern}'
15+
\ : $'{g:scratch_buffer_tmp_file_pattern}.{file_ext}'
16+
17+
const file_name = s:find_fresh_tmp_file(file_pattern)
1018
if file_name is v:null
1119
throw 'No fresh scratch file found.'
1220
endif
1321

14-
const open_method = get(a:000, 0, 'vsp')
15-
const buffer_size = get(a:000, 1, v:null)
22+
const open_method = get(a:000, 1, 'vsp')
23+
const buffer_size = get(a:000, 2, v:null)
1624

1725
execute 'silent' open_method file_name
1826
setlocal noswapfile

doc/vim-scratch-buffer.txt

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ to execute code instantly!
2929
:ScratchBufferOpen ts
3030
3131
" Write TypeScript code
32+
" ...
3233
3334
" ...and run it immediately!
3435
:QuickRun
@@ -42,13 +43,17 @@ https://github.com/aiya000/vim-scratch-buffer
4243
==============================================================================
4344
USAGE *scratch-buffer-usage*
4445

45-
Open a scratch buffer. Please specify a file extension you want:
46+
Open a scratch buffer with or without a file extension:
4647
>
48+
" Open a new scratch buffer without file extension and filetype
49+
:ScratchBufferOpen
50+
:ScratchBufferOpen --no-file-ext
51+
4752
" Open a markdown buffer
4853
:ScratchBufferOpen md
4954
50-
" Open a markdown buffer with :sp
51-
:ScratchBufferOpen md sp
55+
" Open a buffer without file extension and filetype with :sp and 3 lines
56+
:ScratchBufferOpen --no-file-ext sp 3
5257
5358
" Open a markdown buffer with :sp and 3 lines
5459
:ScratchBufferOpen md sp 3
@@ -78,12 +83,18 @@ VARIABLES *scratch-buffer-variables*
7883
COMMANDS *scratch-buffer-commands*
7984

8085
*:ScratchBufferOpen*
81-
:ScratchBufferOpen {file-extension} [open-method] [buffer-size]
86+
:ScratchBufferOpen [file-extension | --no-file-ext] [open-method] [buffer-size]
8287
Open a scratch buffer with a random file name.
83-
88+
8489
This opens buffers by the rule
8590
described in `g:scratch_buffer_tmp_file_pattern`.
8691

92+
[file-extension] is an optional argument:
93+
- When omitted or --no-file-ext is specified:
94+
Creates a buffer without file extension and filetype
95+
- Otherwise:
96+
Uses the specified extension (e.g., 'md', 'ts') as filetype
97+
8798
The buffer is opened as a temporary buffer.
8899
The following properties:
89100
>
@@ -115,8 +126,13 @@ COMMANDS *scratch-buffer-commands*
115126
FUNCTIONS *scratch-buffer-functions*
116127

117128
*scratch_buffer#open()*
118-
scratch_buffer#open({file-extension}[, open-method][, buffer-size])
119-
Same as `:ScratchBufferOpen`.
129+
scratch_buffer#open([file-extension | --no-filetype][, open-method][, buffer-size])
130+
Same as `:ScratchBufferOpen`. Creates a buffer with or without
131+
filetype based on the first argument:
132+
- When an empty string or`'--no-file-ext'`is specified:
133+
Creates a buffer without file extension and filetype
134+
- Otherwise:
135+
Uses the specified extension as filetype
120136

121137
*scratch_buffer#clean_all_of()*
122138
scratch_buffer#clean_all_of({file-extension})

plugin/scratch_buffer.vim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ endif
66
let g:loaded_scratch_buffer = v:true
77

88
" Example:
9+
" `:ScratchBufferOpen`
10+
" `:ScratchBufferOpen --no-filetype`
911
" `:ScratchBufferOpen sh`
1012
" `:ScratchBufferOpen ts vsp`
1113
" `:ScratchBufferOpen md sp 5`
12-
command! -bar -nargs=+ ScratchBufferOpen call scratch_buffer#open(<f-args>)
14+
command! -bar -nargs=* ScratchBufferOpen call scratch_buffer#open(<f-args>)
1315

1416
" Example:
1517
" `:ScratchBufferCleanAllOf md`

0 commit comments

Comments
 (0)