Skip to content

Support g:scratch_buffer_file_pattern & Fix test failing #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,22 @@ Compared to scratch.vim, vim-scratch-buffer provides these additional features:
- See `:help :ScratchBufferOpen` and `:help :ScratchBufferOpenFile`

- Customization options
- Specify filetype for syntax highlighting
- Specify filetype for syntax highlighting, for `:QuickRun`, and for etc
- Choose opening method (`:split` or `:vsplit`)
- Control buffer height/width
- Configurable auto-hiding behavior
- Configurable auto-hiding behavior: [scratch.vim compatibility](#sparkles-scratchvim-compatibility)
- Customize buffer file locations:
```vim
" Configure different paths for temporary and persistent buffers
let g:scratch_buffer_file_pattern = #{
\ when_tmp_buffer: '/tmp/scratch-tmp-%d', " For :ScratchBufferOpen
\ when_file_buffer: expand('~/scratch/%d'), " For :ScratchBufferOpenFile
\ }
" This is useful if you want to keep a file buffer directory
" (`~/tmp` in the above case) with `.prettier`, etc.
```

Please also see [doc/vim-scratch-buffer.txt](./doc/vim-scratch-buffer.txt) for other functions.

### :gear: Detailed Usage

Expand Down Expand Up @@ -122,6 +134,8 @@ Compared to scratch.vim, vim-scratch-buffer provides these additional features:
:ScratchBufferClean
```

Please also see [doc/vim-scratch-buffer.txt](./doc/vim-scratch-buffer.txt) for other usage.

## :keyboard: Default Keymappings

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

## :sparkles: scratch.vim Compatibility
## :sparkles: scratch.vim compatibility

To make the plugin behave like scratch.vim, you can enable automatic buffer hiding!
When enabled, scratch buffers will automatically hide when you leave the window.
Expand Down
67 changes: 48 additions & 19 deletions autoload/scratch_buffer.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
scriptencoding utf-8
scriptversion 3

" Params:
" - (first argument) {string | undefined} (Optional)
" - File extension without '.', e.g., 'md', 'ts', or 'sh'
Expand All @@ -10,29 +7,51 @@ scriptversion 3
" - (third argument) {number | undefined} (Optional) A positive number to `:resize buffer_size`
function! scratch_buffer#open(opening_next_fresh_buffer, ...) abort
return s:open_buffer(#{
\ opening_temporary_buffer: v:true,
\ opening_as_tmp_buffer: v:true,
\ opening_next_fresh_buffer: a:opening_next_fresh_buffer,
\ args: a:000,
\ })
endfunction

function! scratch_buffer#open_file(opening_next_fresh_buffer, ...) abort
return s:open_buffer(#{
\ opening_temporary_buffer: v:false,
\ opening_as_tmp_buffer: v:false,
\ opening_next_fresh_buffer: a:opening_next_fresh_buffer,
\ args: a:000,
\ })
endfunction

" Initialize augroup in case options were updated
function! scratch_buffer#initialize_augroup() abort
augroup VimScratchBuffer
autocmd!
execute
\ 'autocmd'
\ 'TextChanged'
\ substitute(g:scratch_buffer_file_pattern.when_file_buffer, '%d', '*', '')
\ 'call scratch_buffer#autocmd#save_file_buffer_if_enabled()'
execute
\ 'autocmd'
\ 'WinLeave'
\ substitute(g:scratch_buffer_file_pattern.when_tmp_buffer, '%d', '*', '')
\ 'call scratch_buffer#autocmd#hide_buffer_if_enabled()'
execute
\ 'autocmd'
\ 'WinLeave'
\ substitute(g:scratch_buffer_file_pattern.when_file_buffer, '%d', '*', '')
\ 'call scratch_buffer#autocmd#hide_buffer_if_enabled()'
augroup END
endfunction

function! s:open_buffer(options) abort
const args = a:options.args
const opening_temporary_buffer = a:options.opening_temporary_buffer
const opening_as_tmp_buffer = a:options.opening_as_tmp_buffer
const opening_next_fresh_buffer = a:options.opening_next_fresh_buffer

call scratch_buffer#initialize_augroup()

const file_ext = get(args, 0, g:scratch_buffer_default_file_ext)
const file_pattern = (file_ext ==# '--no-file-ext' || file_ext ==# '')
\ ? $'{g:scratch_buffer_tmp_file_pattern}'
\ : $'{g:scratch_buffer_tmp_file_pattern}.{file_ext}'
const file_pattern = s:get_file_pattern(opening_as_tmp_buffer, file_ext)

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

execute 'silent' open_method file_name

if opening_temporary_buffer
if opening_as_tmp_buffer
setlocal buftype=nofile
setlocal bufhidden=hide
else
Expand All @@ -55,6 +74,15 @@ function! s:open_buffer(options) abort
endif
endfunction

function! s:get_file_pattern(opening_as_tmp_buffer, file_ext) abort
const type = a:opening_as_tmp_buffer
\ ? 'when_tmp_buffer'
\ : 'when_file_buffer'
return a:file_ext ==# '--no-file-ext' || a:file_ext ==# ''
\ ? $'{g:scratch_buffer_file_pattern[type]}'
\ : $'{g:scratch_buffer_file_pattern[type]}.{a:file_ext}'
endfunction

function! s:find_current_index(pattern) abort
" NOTE: `[]->max()` returns 0
const max_buffer_index = scratch_buffer#helper#get_all_buffer_names()
Expand All @@ -72,23 +100,24 @@ function! s:extract_index_from_name(name, pattern) abort
return len(matches) > 1 ? str2nr(matches[1]) : v:null
endfunction

function! s:find_next_index(pattern) abort
return + 1
endfunction

" Clean up all scratch buffers and files
function! scratch_buffer#clean() abort
const files = glob(
\ substitute(g:scratch_buffer_tmp_file_pattern, '%d', '*', ''),
const persistent_files = glob(
\ substitute(g:scratch_buffer_file_pattern.when_file_buffer, '%d', '*', ''),
\ v:false,
\ v:true,
\ )
for file in files
call delete(file)
for persistent_file in persistent_files
call delete(persistent_file)
endfor

call s:wipe_buffers(g:scratch_buffer_file_pattern.when_tmp_buffer)
call s:wipe_buffers(g:scratch_buffer_file_pattern.when_file_buffer)
endfunction

function! s:wipe_buffers(file_pattern) abort
const scratch_prefix = '^' .. substitute(
\ g:scratch_buffer_tmp_file_pattern,
\ a:file_pattern,
\ '%d',
\ '',
\ '',
Expand Down
19 changes: 4 additions & 15 deletions autoload/scratch_buffer/autocmd.vim
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
scriptencoding utf-8
scriptversion 3

function! s:is_scratch_buffer() abort
return expand('%:p') =~# substitute(g:scratch_buffer_tmp_file_pattern, '%d', '*', '')
endfunction

function! scratch_buffer#autocmd#save_file_buffer() abort
if g:scratch_buffer_auto_save_file_buffer && (&buftype !=# 'nofile') && s:is_scratch_buffer()
silent! write
function! scratch_buffer#autocmd#save_file_buffer_if_enabled() abort
if g:scratch_buffer_auto_save_file_buffer && (&buftype !=# 'nofile')
silent write
endif
endfunction

function! scratch_buffer#autocmd#hide_buffer() abort
if !s:is_scratch_buffer()
return
endif

function! scratch_buffer#autocmd#hide_buffer_if_enabled() abort
if (&buftype ==# 'nofile') && g:scratch_buffer_auto_hide_buffer.when_tmp_buffer
quit
return
Expand Down
Loading