From e00662906e1ec0e242565ebb2f51dd1873b0222b Mon Sep 17 00:00:00 2001 From: cocopon Date: Sun, 1 Jan 2017 18:34:57 +0900 Subject: [PATCH 01/13] Show error if non-directory argument specified --- autoload/vaffle.vim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/autoload/vaffle.vim b/autoload/vaffle.vim index 8371d4a..41de35c 100644 --- a/autoload/vaffle.vim +++ b/autoload/vaffle.vim @@ -21,10 +21,16 @@ endfunction function! vaffle#init(...) abort + let path = (a:0 == 0) + \ ? getcwd() + \ : a:1 + if !isdirectory(path) + call vaffle#util#echo_error( + \ printf('Not a directory: ''%s''', path)) + return + endif + try - let path = (a:0 == 0) - \ ? getcwd() - \ : a:1 call vaffle#buffer#init(path) catch /:E37:/ call vaffle#util#echo_error( From e031340b77a06bff28f48b5930c89c808b2daae9 Mon Sep 17 00:00:00 2001 From: cocopon Date: Sun, 1 Jan 2017 18:58:08 +0900 Subject: [PATCH 02/13] Refactor buffer initialization --- autoload/vaffle/buffer.vim | 77 +++++++++++++++++++++++++------------- autoload/vaffle/env.vim | 21 ----------- 2 files changed, 51 insertions(+), 47 deletions(-) diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index 34b66ae..3fab03e 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -55,16 +55,7 @@ function! s:generate_unique_bufname(path) abort endfunction -function! vaffle#buffer#init(path) abort - let prev_bufnr = bufnr('%') - let path = vaffle#util#normalize_path(a:path) - - " Create new `nofile` buffer to avoid unwanted sync - " between different windows - enew - execute printf('silent file %s', - \ s:generate_unique_bufname(path)) - +function! s:store_options() abort let options = { \ 'bufhidden': { 'type': 'string', 'value': &bufhidden}, \ 'buftype': { 'type': 'string', 'value': &buftype}, @@ -75,6 +66,51 @@ function! vaffle#buffer#init(path) abort call vaffle#env#set( \ 'initial_options', \ options) +endfunction + + +function! s:restore_options() abort + let options = vaffle#env#get().initial_options + for option_name in keys(options) + let option = options[option_name] + let command = (option.type ==? 'bool') + \ ? printf('setlocal %s%s', + \ (option.value ? '' : 'no'), + \ option_name) + \ : printf('setlocal %s=%s', + \ option_name, + \ option.value) + execute command + endfor +endfunction + + +function! s:should_restore() abort + if &filetype ==? 'vaffle' + " Active Vaffle buffer + return 0 + endif + + if !has_key(vaffle#env#get(), 'restored') + " Non-vaffle buffer + return 0 + endif + + return !vaffle#env#get().restored +endfunction + + +function! vaffle#buffer#init(path) abort + let prev_bufnr = bufnr('%') + let new_path = vaffle#util#normalize_path(a:path) + + " Create new `nofile` buffer to avoid unwanted sync + " between different windows + enew + execute printf('silent file %s', + \ s:generate_unique_bufname(new_path)) + + call s:store_options() setlocal bufhidden=wipe setlocal buftype=nowrite setlocal filetype=vaffle @@ -88,7 +124,7 @@ function! vaffle#buffer#init(path) abort \ prev_bufnr) endif - call vaffle#env#set_up(path) + call vaffle#env#set_up(new_path) call vaffle#buffer#redraw() if g:vaffle_use_default_mappings @@ -97,12 +133,12 @@ function! vaffle#buffer#init(path) abort if g:vaffle_auto_cd try - execute printf('lcd %s', fnameescape(path)) + execute printf('lcd %s', fnameescape(new_path)) catch /:E472:/ " E472: Command failed " Permission denied, etc. call vaffle#util#echo_error( - \ printf('Changing directory failed: ''%s''', path)) + \ printf('Changing directory failed: ''%s''', new_path)) return endtry endif @@ -110,22 +146,11 @@ endfunction function! vaffle#buffer#restore_if_needed() abort - if !vaffle#env#should_restore() + if !s:should_restore() return 0 endif - let options = vaffle#env#get().initial_options - for option_name in keys(options) - let option = options[option_name] - let command = (option.type ==? 'bool') - \ ? printf('setlocal %s%s', - \ (option.value ? '' : 'no'), - \ option_name) - \ : printf('setlocal %s=%s', - \ option_name, - \ option.value) - execute command - endfor + call s:restore_options() setlocal modifiable call vaffle#env#set('restored', 1) diff --git a/autoload/vaffle/env.vim b/autoload/vaffle/env.vim index 6bde0a5..6e2b6ee 100644 --- a/autoload/vaffle/env.vim +++ b/autoload/vaffle/env.vim @@ -101,25 +101,4 @@ function! vaffle#env#restore_from_buffer() abort endfunction -function! vaffle#env#should_restore() abort - if &filetype ==? 'vaffle' - " Active Vaffle buffer - return 0 - endif - - if !exists('w:vaffle') - " Buffer not for Vaffle - return 0 - endif - - if !exists('w:vaffle.restored') - \ || w:vaffle.restored - " Already restored - return 0 - endif - - return 1 -endfunction - - let &cpo = s:save_cpo From acb258e8f13f5eed8195088a25723f4950d9133c Mon Sep 17 00:00:00 2001 From: cocopon Date: Tue, 3 Jan 2017 13:39:04 +0900 Subject: [PATCH 03/13] Reuse Vaffle buffer --- autoload/vaffle.vim | 12 ++++++++ autoload/vaffle/buffer.vim | 57 ++++++++++++++++++++++---------------- autoload/vaffle/event.vim | 2 +- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/autoload/vaffle.vim b/autoload/vaffle.vim index 41de35c..6ce7274 100644 --- a/autoload/vaffle.vim +++ b/autoload/vaffle.vim @@ -30,6 +30,18 @@ function! vaffle#init(...) abort return endif + if &filetype ==? 'vaffle' + call vaffle#buffer#reuse(path) + return + endif + + if !isdirectory(bufname('%')) + " Create new buffer for non-directory buffer + let g:vaffle_creating_vaffle_buffer = 1 + enew + unlet g:vaffle_creating_vaffle_buffer + endif + try call vaffle#buffer#init(path) catch /:E37:/ diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index 3fab03e..3d50b6f 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -100,15 +100,30 @@ function! s:should_restore() abort endfunction +function! s:perform_auto_cd_if_needed(path) abort + if !g:vaffle_auto_cd + return + endif + + try + execute printf('lcd %s', fnameescape(a:path)) + catch /:E472:/ + " E472: Command failed + " Permission denied, etc. + call vaffle#util#echo_error( + \ printf('Changing directory failed: ''%s''', a:path)) + return + endtry +endfunction + + function! vaffle#buffer#init(path) abort - let prev_bufnr = bufnr('%') - let new_path = vaffle#util#normalize_path(a:path) + let path = vaffle#util#normalize_path(a:path) - " Create new `nofile` buffer to avoid unwanted sync + " Give unique name to buffer to avoid unwanted sync " between different windows - enew execute printf('silent file %s', - \ s:generate_unique_bufname(new_path)) + \ s:generate_unique_bufname(path)) call s:store_options() setlocal bufhidden=wipe @@ -118,30 +133,24 @@ function! vaffle#buffer#init(path) abort setlocal noswapfile setlocal nowrap - " Delete unused directory buffer - if isdirectory(bufname(prev_bufnr)) - execute printf('bwipeout! %d', - \ prev_bufnr) + if g:vaffle_use_default_mappings + call s:set_up_default_mappings() endif - call vaffle#env#set_up(new_path) + call vaffle#env#set_up(path) call vaffle#buffer#redraw() - if g:vaffle_use_default_mappings - call s:set_up_default_mappings() - endif + call s:perform_auto_cd_if_needed(path) +endfunction - if g:vaffle_auto_cd - try - execute printf('lcd %s', fnameescape(new_path)) - catch /:E472:/ - " E472: Command failed - " Permission denied, etc. - call vaffle#util#echo_error( - \ printf('Changing directory failed: ''%s''', new_path)) - return - endtry - endif + +function! vaffle#buffer#reuse(path) abort + let path = vaffle#util#normalize_path(a:path) + + call vaffle#env#set_up(path) + call vaffle#buffer#redraw() + + call s:perform_auto_cd_if_needed(path) endfunction diff --git a/autoload/vaffle/event.vim b/autoload/vaffle/event.vim index af41e24..f29cfa1 100644 --- a/autoload/vaffle/event.vim +++ b/autoload/vaffle/event.vim @@ -25,7 +25,7 @@ function! vaffle#event#on_bufenter() abort call vaffle#buffer#restore_if_needed() " Store bufnr of non-vaffle buffer to restore initial state - if &filetype !=? 'vaffle' + if &filetype !=? 'vaffle' && !get(g:, 'vaffle_creating_vaffle_buffer', 0) call vaffle#env#set('non_vaffle_bufnr', bufnr('%')) endif From b4fda9976fac850f6391576a2d08d5c5c8a98e77 Mon Sep 17 00:00:00 2001 From: cocopon Date: Wed, 4 Jan 2017 10:52:45 +0900 Subject: [PATCH 04/13] Reuse Vaffle buffer --- autoload/vaffle/file.vim | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/autoload/vaffle/file.vim b/autoload/vaffle/file.vim index bf61dc2..d336d36 100644 --- a/autoload/vaffle/file.vim +++ b/autoload/vaffle/file.vim @@ -8,7 +8,12 @@ set cpo&vim function! vaffle#file#open(env, items) abort if len(a:items) == 1 - execute printf('edit %s', fnameescape(a:items[0].path)) + let path = a:items[0].path + if isdirectory(path) + call vaffle#init(path) + else + execute printf('edit %s', fnameescape(a:items[0].path)) + endif return endif From 8400158f80e8a8751765a9b30a04e75b979be88f Mon Sep 17 00:00:00 2001 From: cocopon Date: Thu, 5 Jan 2017 23:58:16 +0900 Subject: [PATCH 05/13] Change format of buffer name --- autoload/vaffle/buffer.vim | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index 3d50b6f..c08a6e4 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -38,16 +38,18 @@ endfunction function! s:generate_unique_bufname(path) abort - " Add prefix `#:` (waffle!) to avoid truncating path - " with statusline item `%t` - let bufname = fnameescape(printf('#:%s', a:path)) + let bufname = '' + let index = 0 - let index = 2 - while bufnr(bufname) >= 0 + while 1 " Add index to avoid duplicated buffer name - let bufname = fnameescape(printf('#%d:%s', + let bufname = fnameescape(printf('vaffle://%d/%s', \ index, \ a:path)) + if bufnr(bufname) < 0 + break + endif + let index += 1 endwhile From 7d4c04ea93d72122183c785e38786dc5db7a8473 Mon Sep 17 00:00:00 2001 From: cocopon Date: Fri, 6 Jan 2017 00:58:12 +0900 Subject: [PATCH 06/13] Restore status in BufLeave --- autoload/vaffle/buffer.vim | 32 ++++++++++++++------------------ autoload/vaffle/env.vim | 1 - autoload/vaffle/event.vim | 30 +++++++++++++++++++----------- plugin/vaffle.vim | 1 + 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index c08a6e4..6cc87d1 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -87,21 +87,6 @@ function! s:restore_options() abort endfunction -function! s:should_restore() abort - if &filetype ==? 'vaffle' - " Active Vaffle buffer - return 0 - endif - - if !has_key(vaffle#env#get(), 'restored') - " Non-vaffle buffer - return 0 - endif - - return !vaffle#env#get().restored -endfunction - - function! s:perform_auto_cd_if_needed(path) abort if !g:vaffle_auto_cd return @@ -156,16 +141,27 @@ function! vaffle#buffer#reuse(path) abort endfunction +function! vaffle#buffer#is_for_vaffle(bufnr) abort + let bufname = bufname(a:bufnr) + return (match(bufname, '^vaffle://\d\+/') >= 0) +endfunction + + +function! vaffle#buffer#extract_path_from_bufname(bufnr) abort + let bufname = bufname(a:bufnr) + let matches = matchlist(bufname, '^vaffle://\d\+/\(.*\)$') + return get(matches, 1, '') +endfunction + + function! vaffle#buffer#restore_if_needed() abort - if !s:should_restore() + if !vaffle#buffer#is_for_vaffle(bufnr('%')) return 0 endif call s:restore_options() setlocal modifiable - call vaffle#env#set('restored', 1) - return 1 endfunction diff --git a/autoload/vaffle/env.vim b/autoload/vaffle/env.vim index 6e2b6ee..afc989d 100644 --- a/autoload/vaffle/env.vim +++ b/autoload/vaffle/env.vim @@ -9,7 +9,6 @@ set cpo&vim function! vaffle#env#set_up(path) abort let w:vaffle = get(w:, 'vaffle', {}) - let w:vaffle.restored = 0 let w:vaffle.dir = vaffle#util#normalize_path(a:path) let w:vaffle.cursor_paths = get( \ w:vaffle, diff --git a/autoload/vaffle/event.vim b/autoload/vaffle/event.vim index f29cfa1..2d06c24 100644 --- a/autoload/vaffle/event.vim +++ b/autoload/vaffle/event.vim @@ -16,17 +16,20 @@ endfunction function! vaffle#event#on_bufenter() abort call s:newtralize_netrw() - let path = expand('%:p') - - let is_nofile_buffer = empty(path) - let is_normal_buffer_for_file = !empty(path) && !isdirectory(path) - if is_nofile_buffer - \ || is_normal_buffer_for_file - call vaffle#buffer#restore_if_needed() - - " Store bufnr of non-vaffle buffer to restore initial state - if &filetype !=? 'vaffle' && !get(g:, 'vaffle_creating_vaffle_buffer', 0) - call vaffle#env#set('non_vaffle_bufnr', bufnr('%')) + let bufnr = bufnr('%') + let bufname = bufname(bufnr) + let is_vaffle_buffer = vaffle#buffer#is_for_vaffle(bufnr) + let path = is_vaffle_buffer + \ ? vaffle#buffer#extract_path_from_bufname(bufname) + \ : expand('%:p') + + let should_init = is_vaffle_buffer + \ || isdirectory(path) + + if !should_init + if !get(g:, 'vaffle_creating_vaffle_buffer', 0) + " Store bufnr of non-vaffle buffer to restore initial state + call vaffle#env#set('non_vaffle_bufnr', bufnr) endif return @@ -36,4 +39,9 @@ function! vaffle#event#on_bufenter() abort endfunction +function! vaffle#event#on_bufleave() abort + call vaffle#buffer#restore_if_needed() +endfunction + + let &cpo = s:save_cpo diff --git a/plugin/vaffle.vim b/plugin/vaffle.vim index f0ead38..f7c25bd 100644 --- a/plugin/vaffle.vim +++ b/plugin/vaffle.vim @@ -15,6 +15,7 @@ let g:loaded_vaffle = 1 augroup vaffle_vim autocmd! autocmd BufEnter * call vaffle#event#on_bufenter() + autocmd BufLeave * call vaffle#event#on_bufleave() augroup END From 9e5e05f68016f3ce78d7c7a37987f635bd3372d3 Mon Sep 17 00:00:00 2001 From: cocopon Date: Fri, 6 Jan 2017 01:25:38 +0900 Subject: [PATCH 07/13] Refactor vaffle#env --- autoload/vaffle.vim | 3 ++- autoload/vaffle/env.vim | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/autoload/vaffle.vim b/autoload/vaffle.vim index 6ce7274..0e4a175 100644 --- a/autoload/vaffle.vim +++ b/autoload/vaffle.vim @@ -320,7 +320,8 @@ function! vaffle#toggle_hidden() abort call vaffle#env#save_cursor(item) endif - call vaffle#env#set_up_items() + call vaffle#env#set('items', + \ vaffle#env#create_items(vaffle#env#get())) call vaffle#buffer#redraw() endfunction diff --git a/autoload/vaffle/env.vim b/autoload/vaffle/env.vim index afc989d..92769b0 100644 --- a/autoload/vaffle/env.vim +++ b/autoload/vaffle/env.vim @@ -6,6 +6,10 @@ let s:save_cpo = &cpo set cpo&vim +function! vaffle#env#create(path) abort +endfunction + + function! vaffle#env#set_up(path) abort let w:vaffle = get(w:, 'vaffle', {}) @@ -28,32 +32,34 @@ function! vaffle#env#set_up(path) abort \ 'shows_hidden_files', \ g:vaffle_show_hidden_files) - call vaffle#env#set_up_items() + let w:vaffle.items = vaffle#env#create_items(w:vaffle) + + let b:vaffle = w:vaffle endfunction -function! vaffle#env#set_up_items() abort - let paths = vaffle#compat#glob_list(w:vaffle.dir . '/*') - if w:vaffle.shows_hidden_files - let hidden_paths = vaffle#compat#glob_list(w:vaffle.dir . '/.*') +function! vaffle#env#create_items(env) abort + let paths = vaffle#compat#glob_list(a:env.dir . '/*') + if a:env.shows_hidden_files + let hidden_paths = vaffle#compat#glob_list(a:env.dir . '/.*') " Exclude '.' & '..' call filter(hidden_paths, 'match(v:val, ''/\.\.\?$'') < 0') call extend(paths, hidden_paths) end - let w:vaffle.items = map( + let items = map( \ copy(paths), \ 'vaffle#item#create(v:val)') - call sort(w:vaffle.items, 'vaffle#sorter#default#compare') + call sort(items, 'vaffle#sorter#default#compare') let index = 0 - for item in w:vaffle.items + for item in items let item.index = index let index += 1 endfor - let b:vaffle = w:vaffle + return items endfunction From f7b71c0381dde1a436bb40d9173e691951cd4464 Mon Sep 17 00:00:00 2001 From: cocopon Date: Fri, 6 Jan 2017 01:35:16 +0900 Subject: [PATCH 08/13] Move env#get to buffer#get_env --- autoload/vaffle.vim | 28 ++++++++++++++-------------- autoload/vaffle/buffer.vim | 18 +++++++++++++++--- autoload/vaffle/env.vim | 10 ++-------- autoload/vaffle/item.vim | 4 ++-- autoload/vaffle/rename_buffer.vim | 2 +- 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/autoload/vaffle.vim b/autoload/vaffle.vim index 0e4a175..843019a 100644 --- a/autoload/vaffle.vim +++ b/autoload/vaffle.vim @@ -60,7 +60,7 @@ function! vaffle#refresh() abort call vaffle#env#save_cursor(cursor_items[0]) endif - let cwd = vaffle#env#get().dir + let cwd = vaffle#buffer#get_env().dir call vaffle#env#set_up(cwd) call vaffle#buffer#redraw() endfunction @@ -80,7 +80,7 @@ function! vaffle#open_current() abort call vaffle#env#save_cursor(item) call vaffle#file#open( - \ vaffle#env#get(), + \ vaffle#buffer#get_env(), \ [item]) endfunction @@ -96,7 +96,7 @@ function! vaffle#open_selected() abort call vaffle#env#save_cursor(items[0]) call vaffle#file#open( - \ vaffle#env#get(), + \ vaffle#buffer#get_env(), \ items) endfunction @@ -104,7 +104,7 @@ endfunction function! vaffle#open_parent() abort call s:keep_buffer_singularity() - let env = vaffle#env#get() + let env = vaffle#buffer#get_env() let parent_dir = fnamemodify(env.dir, ':h') let cursor_items = vaffle#item#get_cursor_items('n') @@ -148,7 +148,7 @@ endfunction function! vaffle#toggle_all() abort call s:keep_buffer_singularity() - let items = vaffle#env#get().items + let items = vaffle#buffer#get_env().items if empty(items) return endif @@ -161,7 +161,7 @@ endfunction function! vaffle#set_selected_all(selected) abort call s:keep_buffer_singularity() - for item in vaffle#env#get().items + for item in vaffle#buffer#get_env().items let item.selected = a:selected endfor @@ -173,7 +173,7 @@ function! vaffle#quit() abort call s:keep_buffer_singularity() " Try restoring previous buffer - let bufnr = vaffle#env#get().non_vaffle_bufnr + let bufnr = vaffle#buffer#get_env().non_vaffle_bufnr if bufexists(bufnr) execute printf('buffer! %d', bufnr) return @@ -212,7 +212,7 @@ function! vaffle#delete_selected() abort endif call vaffle#file#delete( - \ vaffle#env#get(), + \ vaffle#buffer#get_env(), \ items) call vaffle#refresh() endfunction @@ -237,7 +237,7 @@ function! vaffle#move_selected() abort endif call vaffle#file#move( - \ vaffle#env#get(), + \ vaffle#buffer#get_env(), \ items, dst_name) call vaffle#refresh() endfunction @@ -254,7 +254,7 @@ function! vaffle#mkdir() abort endif call vaffle#file#mkdir( - \ vaffle#env#get(), + \ vaffle#buffer#get_env(), \ name) call vaffle#refresh() endfunction @@ -271,7 +271,7 @@ function! vaffle#new_file() abort endif call vaffle#file#edit( - \ vaffle#env#get(), + \ vaffle#buffer#get_env(), \ name) endfunction @@ -295,7 +295,7 @@ function! vaffle#rename_selected() abort endif call vaffle#file#rename( - \ vaffle#env#get(), + \ vaffle#buffer#get_env(), \ items, [new_basename]) call vaffle#refresh() return @@ -310,7 +310,7 @@ function! vaffle#toggle_hidden() abort call vaffle#env#set( \ 'shows_hidden_files', - \ !vaffle#env#get().shows_hidden_files) + \ !vaffle#buffer#get_env().shows_hidden_files) let item = get( \ vaffle#item#get_cursor_items('n'), @@ -321,7 +321,7 @@ function! vaffle#toggle_hidden() abort endif call vaffle#env#set('items', - \ vaffle#env#create_items(vaffle#env#get())) + \ vaffle#env#create_items(vaffle#buffer#get_env())) call vaffle#buffer#redraw() endfunction diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index 6cc87d1..6f3dc8b 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -72,7 +72,7 @@ endfunction function! s:restore_options() abort - let options = vaffle#env#get().initial_options + let options = vaffle#buffer#get_env().initial_options for option_name in keys(options) let option = options[option_name] let command = (option.type ==? 'bool') @@ -172,7 +172,7 @@ function! vaffle#buffer#redraw() abort " Clear buffer before drawing items silent keepjumps %d - let items = vaffle#env#get().items + let items = vaffle#buffer#get_env().items if !empty(items) let lnum = 1 for item in items @@ -210,9 +210,21 @@ endfunction function! vaffle#buffer#duplicate() abort call vaffle#env#restore_from_buffer() call vaffle#file#edit( - \ vaffle#env#get(), + \ vaffle#buffer#get_env(), \ '') endfunction +function! vaffle#buffer#get_env() abort + let w:vaffle = get(w:, 'vaffle', get(b:, 'vaffle', {})) + return w:vaffle +endfunction + + +function! vaffle#buffer#set_env(env) abort + let w:vaffle = a:env + let b:vaffle = w:vaffle +endfunction + + let &cpo = s:save_cpo diff --git a/autoload/vaffle/env.vim b/autoload/vaffle/env.vim index 92769b0..7e3425d 100644 --- a/autoload/vaffle/env.vim +++ b/autoload/vaffle/env.vim @@ -63,12 +63,6 @@ function! vaffle#env#create_items(env) abort endfunction -function! vaffle#env#get() abort - let w:vaffle = get(w:, 'vaffle', get(b:, 'vaffle', {})) - return w:vaffle -endfunction - - function! vaffle#env#set(key, value) abort let w:vaffle = get(w:, 'vaffle', get(b:, 'vaffle', {})) let w:vaffle[a:key] = a:value @@ -76,14 +70,14 @@ endfunction function! vaffle#env#save_cursor(item) abort - let cursor_paths = vaffle#env#get().cursor_paths + let cursor_paths = vaffle#buffer#get_env().cursor_paths let cursor_paths[w:vaffle.dir] = a:item.path call vaffle#env#set('cursor_paths', cursor_paths) endfunction function! vaffle#env#restore_cursor() abort - let cursor_paths = vaffle#env#get().cursor_paths + let cursor_paths = vaffle#buffer#get_env().cursor_paths let cursor_path = get(cursor_paths, w:vaffle.dir, '') if empty(cursor_path) return {} diff --git a/autoload/vaffle/item.vim b/autoload/vaffle/item.vim index 8f015ad..eed7af2 100644 --- a/autoload/vaffle/item.vim +++ b/autoload/vaffle/item.vim @@ -7,7 +7,7 @@ set cpo&vim function! vaffle#item#get_cursor_items(mode) abort - let items = vaffle#env#get().items + let items = vaffle#buffer#get_env().items if empty(items) return [] endif @@ -23,7 +23,7 @@ endfunction function! vaffle#item#get_selected_items() abort - let items = vaffle#env#get().items + let items = vaffle#buffer#get_env().items let selected_items = filter( \ copy(items), \ 'v:val.selected') diff --git a/autoload/vaffle/rename_buffer.vim b/autoload/vaffle/rename_buffer.vim index a35ab83..ed52b56 100644 --- a/autoload/vaffle/rename_buffer.vim +++ b/autoload/vaffle/rename_buffer.vim @@ -62,7 +62,7 @@ function! vaffle#rename_buffer#new(items) abort execute printf('bwipeout %d', bufnr) endif - let parent_env = vaffle#env#get() + let parent_env = vaffle#buffer#get_env() let parent_bufnr = bufnr('%') vnew From 1e4ab36f5fad9eeede0d7770f84ebb6b9800ef36 Mon Sep 17 00:00:00 2001 From: cocopon Date: Fri, 6 Jan 2017 01:44:07 +0900 Subject: [PATCH 09/13] Refactor env --- autoload/vaffle.vim | 28 ++++++++++++++-------------- autoload/vaffle/buffer.vim | 5 +++-- autoload/vaffle/env.vim | 14 +++++++------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/autoload/vaffle.vim b/autoload/vaffle.vim index 843019a..11a25f3 100644 --- a/autoload/vaffle.vim +++ b/autoload/vaffle.vim @@ -55,12 +55,14 @@ endfunction function! vaffle#refresh() abort call s:keep_buffer_singularity() + let env = vaffle#buffer#get_env() + let cursor_items = vaffle#item#get_cursor_items('n') if !empty(cursor_items) - call vaffle#env#save_cursor(cursor_items[0]) + call vaffle#env#save_cursor(env, cursor_items[0]) endif - let cwd = vaffle#buffer#get_env().dir + let cwd = env.dir call vaffle#env#set_up(cwd) call vaffle#buffer#redraw() endfunction @@ -77,11 +79,9 @@ function! vaffle#open_current() abort return endif - call vaffle#env#save_cursor(item) - - call vaffle#file#open( - \ vaffle#buffer#get_env(), - \ [item]) + let env = vaffle#buffer#get_env() + call vaffle#env#save_cursor(env, item) + call vaffle#file#open(env, [item]) endfunction @@ -93,11 +93,10 @@ function! vaffle#open_selected() abort return endif - call vaffle#env#save_cursor(items[0]) + let env = vaffle#buffer#get_env() + call vaffle#env#save_cursor(env, items[0]) - call vaffle#file#open( - \ vaffle#buffer#get_env(), - \ items) + call vaffle#file#open(env, items) endfunction @@ -109,7 +108,7 @@ function! vaffle#open_parent() abort let cursor_items = vaffle#item#get_cursor_items('n') if !empty(cursor_items) - call vaffle#env#save_cursor(cursor_items[0]) + call vaffle#env#save_cursor(env, cursor_items[0]) endif let item = vaffle#item#create(parent_dir) @@ -312,16 +311,17 @@ function! vaffle#toggle_hidden() abort \ 'shows_hidden_files', \ !vaffle#buffer#get_env().shows_hidden_files) + let env = vaffle#buffer#get_env() let item = get( \ vaffle#item#get_cursor_items('n'), \ 0, \ {}) if !empty(item) - call vaffle#env#save_cursor(item) + call vaffle#env#save_cursor(env, item) endif call vaffle#env#set('items', - \ vaffle#env#create_items(vaffle#buffer#get_env())) + \ vaffle#env#create_items(env)) call vaffle#buffer#redraw() endfunction diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index 6f3dc8b..9fcbbfa 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -172,7 +172,8 @@ function! vaffle#buffer#redraw() abort " Clear buffer before drawing items silent keepjumps %d - let items = vaffle#buffer#get_env().items + let env = vaffle#buffer#get_env() + let items = env.items if !empty(items) let lnum = 1 for item in items @@ -188,7 +189,7 @@ function! vaffle#buffer#redraw() abort setlocal nomodified let initial_lnum = 1 - let cursor_item = vaffle#env#restore_cursor() + let cursor_item = vaffle#env#restore_cursor(env) if !empty(cursor_item) let initial_lnum = index(items, cursor_item) + 1 endif diff --git a/autoload/vaffle/env.vim b/autoload/vaffle/env.vim index 7e3425d..52ec411 100644 --- a/autoload/vaffle/env.vim +++ b/autoload/vaffle/env.vim @@ -69,22 +69,22 @@ function! vaffle#env#set(key, value) abort endfunction -function! vaffle#env#save_cursor(item) abort - let cursor_paths = vaffle#buffer#get_env().cursor_paths - let cursor_paths[w:vaffle.dir] = a:item.path +function! vaffle#env#save_cursor(env, item) abort + let cursor_paths = a:env.cursor_paths + let cursor_paths[a:env.dir] = a:item.path call vaffle#env#set('cursor_paths', cursor_paths) endfunction -function! vaffle#env#restore_cursor() abort - let cursor_paths = vaffle#buffer#get_env().cursor_paths - let cursor_path = get(cursor_paths, w:vaffle.dir, '') +function! vaffle#env#restore_cursor(env) abort + let cursor_paths = a:env.cursor_paths + let cursor_path = get(cursor_paths, a:env.dir, '') if empty(cursor_path) return {} endif let items = filter( - \ copy(w:vaffle.items), + \ copy(a:env.items), \ 'v:val.path ==# cursor_path') if empty(items) return {} From b87700718a61e9fbc88644a36ba2323ad881de68 Mon Sep 17 00:00:00 2001 From: cocopon Date: Fri, 6 Jan 2017 01:54:15 +0900 Subject: [PATCH 10/13] Refactor env --- autoload/vaffle.vim | 9 +++++---- autoload/vaffle/buffer.vim | 8 ++++---- autoload/vaffle/env.vim | 8 +------- autoload/vaffle/event.vim | 2 +- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/autoload/vaffle.vim b/autoload/vaffle.vim index 11a25f3..254ccab 100644 --- a/autoload/vaffle.vim +++ b/autoload/vaffle.vim @@ -307,11 +307,11 @@ endfunction function! vaffle#toggle_hidden() abort call s:keep_buffer_singularity() - call vaffle#env#set( + let env = vaffle#buffer#get_env() + call vaffle#buffer#set_env( \ 'shows_hidden_files', - \ !vaffle#buffer#get_env().shows_hidden_files) + \ !env.shows_hidden_files) - let env = vaffle#buffer#get_env() let item = get( \ vaffle#item#get_cursor_items('n'), \ 0, @@ -320,7 +320,8 @@ function! vaffle#toggle_hidden() abort call vaffle#env#save_cursor(env, item) endif - call vaffle#env#set('items', + call vaffle#buffer#set_env( + \ 'items', \ vaffle#env#create_items(env)) call vaffle#buffer#redraw() endfunction diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index 9fcbbfa..a80792b 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -65,7 +65,7 @@ function! s:store_options() abort \ 'swapfile': { 'type': 'bool', 'value': &swapfile}, \ 'wrap': { 'type': 'bool', 'value': &wrap}, \ } - call vaffle#env#set( + call vaffle#buffer#set_env( \ 'initial_options', \ options) endfunction @@ -222,9 +222,9 @@ function! vaffle#buffer#get_env() abort endfunction -function! vaffle#buffer#set_env(env) abort - let w:vaffle = a:env - let b:vaffle = w:vaffle +function! vaffle#buffer#set_env(key, value) abort + let w:vaffle = get(w:, 'vaffle', get(b:, 'vaffle', {})) + let w:vaffle[a:key] = a:value endfunction diff --git a/autoload/vaffle/env.vim b/autoload/vaffle/env.vim index 52ec411..4b09343 100644 --- a/autoload/vaffle/env.vim +++ b/autoload/vaffle/env.vim @@ -63,16 +63,10 @@ function! vaffle#env#create_items(env) abort endfunction -function! vaffle#env#set(key, value) abort - let w:vaffle = get(w:, 'vaffle', get(b:, 'vaffle', {})) - let w:vaffle[a:key] = a:value -endfunction - - function! vaffle#env#save_cursor(env, item) abort let cursor_paths = a:env.cursor_paths let cursor_paths[a:env.dir] = a:item.path - call vaffle#env#set('cursor_paths', cursor_paths) + call vaffle#buffer#set_env('cursor_paths', cursor_paths) endfunction diff --git a/autoload/vaffle/event.vim b/autoload/vaffle/event.vim index 2d06c24..828983e 100644 --- a/autoload/vaffle/event.vim +++ b/autoload/vaffle/event.vim @@ -29,7 +29,7 @@ function! vaffle#event#on_bufenter() abort if !should_init if !get(g:, 'vaffle_creating_vaffle_buffer', 0) " Store bufnr of non-vaffle buffer to restore initial state - call vaffle#env#set('non_vaffle_bufnr', bufnr) + call vaffle#buffer#set_env('non_vaffle_bufnr', bufnr) endif return From a3ceda093b430e536720e157ba1a0fe681672d52 Mon Sep 17 00:00:00 2001 From: cocopon Date: Fri, 6 Jan 2017 02:25:27 +0900 Subject: [PATCH 11/13] Refactor env --- autoload/vaffle.vim | 19 +++++++++------- autoload/vaffle/buffer.vim | 28 ++++++++++++++---------- autoload/vaffle/env.vim | 44 ++++++++++++++++++-------------------- autoload/vaffle/event.vim | 4 +++- 4 files changed, 52 insertions(+), 43 deletions(-) diff --git a/autoload/vaffle.vim b/autoload/vaffle.vim index 254ccab..ea1227c 100644 --- a/autoload/vaffle.vim +++ b/autoload/vaffle.vim @@ -62,8 +62,11 @@ function! vaffle#refresh() abort call vaffle#env#save_cursor(env, cursor_items[0]) endif - let cwd = env.dir - call vaffle#env#set_up(cwd) + let new_env = vaffle#env#create(env.dir) + call vaffle#env#inherit(new_env, env) + let new_env.items = vaffle#env#create_items(new_env) + call vaffle#buffer#set_env(new_env) + call vaffle#buffer#redraw() endfunction @@ -308,9 +311,8 @@ function! vaffle#toggle_hidden() abort call s:keep_buffer_singularity() let env = vaffle#buffer#get_env() - call vaffle#buffer#set_env( - \ 'shows_hidden_files', - \ !env.shows_hidden_files) + let env.shows_hidden_files = !env.shows_hidden_files + call vaffle#buffer#set_env(env) let item = get( \ vaffle#item#get_cursor_items('n'), @@ -320,9 +322,10 @@ function! vaffle#toggle_hidden() abort call vaffle#env#save_cursor(env, item) endif - call vaffle#buffer#set_env( - \ 'items', - \ vaffle#env#create_items(env)) + let env = vaffle#buffer#get_env() + let env.items = vaffle#env#create_items(env) + call vaffle#buffer#set_env(env) + call vaffle#buffer#redraw() endfunction diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index a80792b..b13a6b4 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -57,17 +57,14 @@ function! s:generate_unique_bufname(path) abort endfunction -function! s:store_options() abort - let options = { +function! s:get_options_dict() abort + return { \ 'bufhidden': { 'type': 'string', 'value': &bufhidden}, \ 'buftype': { 'type': 'string', 'value': &buftype}, \ 'matchpairs': { 'type': 'string', 'value': &matchpairs}, \ 'swapfile': { 'type': 'bool', 'value': &swapfile}, \ 'wrap': { 'type': 'bool', 'value': &wrap}, \ } - call vaffle#buffer#set_env( - \ 'initial_options', - \ options) endfunction @@ -112,7 +109,7 @@ function! vaffle#buffer#init(path) abort execute printf('silent file %s', \ s:generate_unique_bufname(path)) - call s:store_options() + let initial_options = s:get_options_dict() setlocal bufhidden=wipe setlocal buftype=nowrite setlocal filetype=vaffle @@ -124,7 +121,12 @@ function! vaffle#buffer#init(path) abort call s:set_up_default_mappings() endif - call vaffle#env#set_up(path) + let env = vaffle#env#create(path) + call vaffle#env#inherit(env, vaffle#buffer#get_env()) + let env.initial_options = initial_options + let env.items = vaffle#env#create_items(env) + call vaffle#buffer#set_env(env) + call vaffle#buffer#redraw() call s:perform_auto_cd_if_needed(path) @@ -134,7 +136,11 @@ endfunction function! vaffle#buffer#reuse(path) abort let path = vaffle#util#normalize_path(a:path) - call vaffle#env#set_up(path) + let env = vaffle#env#create(path) + call vaffle#env#inherit(env, vaffle#buffer#get_env()) + let env.items = vaffle#env#create_items(env) + call vaffle#buffer#set_env(env) + call vaffle#buffer#redraw() call s:perform_auto_cd_if_needed(path) @@ -222,9 +228,9 @@ function! vaffle#buffer#get_env() abort endfunction -function! vaffle#buffer#set_env(key, value) abort - let w:vaffle = get(w:, 'vaffle', get(b:, 'vaffle', {})) - let w:vaffle[a:key] = a:value +function! vaffle#buffer#set_env(env) abort + let w:vaffle = a:env + let b:vaffle = w:vaffle endfunction diff --git a/autoload/vaffle/env.vim b/autoload/vaffle/env.vim index 4b09343..adc6dfe 100644 --- a/autoload/vaffle/env.vim +++ b/autoload/vaffle/env.vim @@ -7,34 +7,33 @@ set cpo&vim function! vaffle#env#create(path) abort + let env = {} + let env.dir = vaffle#util#normalize_path(a:path) + let env.initial_options = {} + let env.cursor_paths = {} + let env.non_vaffle_bufnr = -1 + let env.non_vaffle_bufnr = -1 + let env.shows_hidden_files = g:vaffle_show_hidden_files + let env.items = [] + return env endfunction -function! vaffle#env#set_up(path) abort - let w:vaffle = get(w:, 'vaffle', {}) - - let w:vaffle.dir = vaffle#util#normalize_path(a:path) - let w:vaffle.cursor_paths = get( - \ w:vaffle, +function! vaffle#env#inherit(env, old_env) abort + let a:env.cursor_paths = get( + \ a:old_env, \ 'cursor_paths', - \ {}) + \ a:env.cursor_paths) - let w:vaffle.non_vaffle_bufnr = get( - \ w:vaffle, + let a:env.non_vaffle_bufnr = get( + \ a:old_env, \ 'non_vaffle_bufnr', - \ -1) - if w:vaffle.non_vaffle_bufnr == bufnr('%') - let w:vaffle.non_vaffle_bufnr = -1 - endif + \ a:env.non_vaffle_bufnr) - let w:vaffle.shows_hidden_files = get( - \ w:vaffle, + let a:env.shows_hidden_files = get( + \ a:old_env, \ 'shows_hidden_files', - \ g:vaffle_show_hidden_files) - - let w:vaffle.items = vaffle#env#create_items(w:vaffle) - - let b:vaffle = w:vaffle + \ a:env.shows_hidden_files) endfunction @@ -64,9 +63,8 @@ endfunction function! vaffle#env#save_cursor(env, item) abort - let cursor_paths = a:env.cursor_paths - let cursor_paths[a:env.dir] = a:item.path - call vaffle#buffer#set_env('cursor_paths', cursor_paths) + let a:env.cursor_paths[a:env.dir] = a:item.path + call vaffle#buffer#set_env(a:env) endfunction diff --git a/autoload/vaffle/event.vim b/autoload/vaffle/event.vim index 828983e..dfba797 100644 --- a/autoload/vaffle/event.vim +++ b/autoload/vaffle/event.vim @@ -29,7 +29,9 @@ function! vaffle#event#on_bufenter() abort if !should_init if !get(g:, 'vaffle_creating_vaffle_buffer', 0) " Store bufnr of non-vaffle buffer to restore initial state - call vaffle#buffer#set_env('non_vaffle_bufnr', bufnr) + let env = vaffle#buffer#get_env() + let env.non_vaffle_bufnr = bufnr + call vaffle#buffer#set_env(env) endif return From 5bbb6bcc5d8f3b42e506e1bd13d1a7e52f92cd89 Mon Sep 17 00:00:00 2001 From: cocopon Date: Fri, 6 Jan 2017 02:41:18 +0900 Subject: [PATCH 12/13] Refactor env --- autoload/vaffle.vim | 13 +++++++------ autoload/vaffle/buffer.vim | 37 +++++++++++++++++++++++++++++++------ autoload/vaffle/env.vim | 30 ------------------------------ 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/autoload/vaffle.vim b/autoload/vaffle.vim index ea1227c..f6feba1 100644 --- a/autoload/vaffle.vim +++ b/autoload/vaffle.vim @@ -59,7 +59,7 @@ function! vaffle#refresh() abort let cursor_items = vaffle#item#get_cursor_items('n') if !empty(cursor_items) - call vaffle#env#save_cursor(env, cursor_items[0]) + call vaffle#buffer#save_cursor(cursor_items[0]) endif let new_env = vaffle#env#create(env.dir) @@ -82,8 +82,9 @@ function! vaffle#open_current() abort return endif + call vaffle#buffer#save_cursor(item) + let env = vaffle#buffer#get_env() - call vaffle#env#save_cursor(env, item) call vaffle#file#open(env, [item]) endfunction @@ -96,9 +97,9 @@ function! vaffle#open_selected() abort return endif - let env = vaffle#buffer#get_env() - call vaffle#env#save_cursor(env, items[0]) + call vaffle#buffer#save_cursor(items[0]) + let env = vaffle#buffer#get_env() call vaffle#file#open(env, items) endfunction @@ -111,7 +112,7 @@ function! vaffle#open_parent() abort let cursor_items = vaffle#item#get_cursor_items('n') if !empty(cursor_items) - call vaffle#env#save_cursor(env, cursor_items[0]) + call vaffle#buffer#save_cursor(cursor_items[0]) endif let item = vaffle#item#create(parent_dir) @@ -319,7 +320,7 @@ function! vaffle#toggle_hidden() abort \ 0, \ {}) if !empty(item) - call vaffle#env#save_cursor(env, item) + call vaffle#buffer#save_cursor(item) endif let env = vaffle#buffer#get_env() diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index b13a6b4..85f1288 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -101,6 +101,26 @@ function! s:perform_auto_cd_if_needed(path) abort endfunction +function! s:get_saved_cursor_lnum() abort + let env = vaffle#buffer#get_env() + let cursor_paths = env.cursor_paths + let cursor_path = get(cursor_paths, env.dir, '') + if empty(cursor_path) + return 1 + endif + + let items = filter( + \ copy(env.items), + \ 'v:val.path ==# cursor_path') + if empty(items) + return 1 + endif + + let cursor_item = items[0] + return index(env.items, cursor_item) + 1 +endfunction + + function! vaffle#buffer#init(path) abort let path = vaffle#util#normalize_path(a:path) @@ -194,11 +214,7 @@ function! vaffle#buffer#redraw() abort setlocal nomodifiable setlocal nomodified - let initial_lnum = 1 - let cursor_item = vaffle#env#restore_cursor(env) - if !empty(cursor_item) - let initial_lnum = index(items, cursor_item) + 1 - endif + let initial_lnum = s:get_saved_cursor_lnum() call cursor([initial_lnum, 1, 0, 1]) endfunction @@ -215,7 +231,9 @@ endfunction function! vaffle#buffer#duplicate() abort - call vaffle#env#restore_from_buffer() + " Split buffer doesn't have `w:vaffle` so restore it from `b:vaffle` + let w:vaffle = deepcopy(b:vaffle) + call vaffle#file#edit( \ vaffle#buffer#get_env(), \ '') @@ -234,4 +252,11 @@ function! vaffle#buffer#set_env(env) abort endfunction +function! vaffle#buffer#save_cursor(item) abort + let env = vaffle#buffer#get_env() + let env.cursor_paths[env.dir] = a:item.path + call vaffle#buffer#set_env(env) +endfunction + + let &cpo = s:save_cpo diff --git a/autoload/vaffle/env.vim b/autoload/vaffle/env.vim index adc6dfe..ac12558 100644 --- a/autoload/vaffle/env.vim +++ b/autoload/vaffle/env.vim @@ -62,34 +62,4 @@ function! vaffle#env#create_items(env) abort endfunction -function! vaffle#env#save_cursor(env, item) abort - let a:env.cursor_paths[a:env.dir] = a:item.path - call vaffle#buffer#set_env(a:env) -endfunction - - -function! vaffle#env#restore_cursor(env) abort - let cursor_paths = a:env.cursor_paths - let cursor_path = get(cursor_paths, a:env.dir, '') - if empty(cursor_path) - return {} - endif - - let items = filter( - \ copy(a:env.items), - \ 'v:val.path ==# cursor_path') - if empty(items) - return {} - endif - - return items[0] -endfunction - - -function! vaffle#env#restore_from_buffer() abort - " Split buffer doesn't have `w:vaffle` so restore it from `b:vaffle` - let w:vaffle = deepcopy(b:vaffle) -endfunction - - let &cpo = s:save_cpo From dcd56eec6e57d11e4ae27052df1c7ca60fd3e272 Mon Sep 17 00:00:00 2001 From: cocopon Date: Fri, 6 Jan 2017 11:34:52 +0900 Subject: [PATCH 13/13] Implement recycling buffer --- autoload/vaffle.vim | 31 +++++++++++++++---------------- autoload/vaffle/buffer.vim | 30 ++++++++++++++---------------- autoload/vaffle/env.vim | 1 - autoload/vaffle/event.vim | 15 +++++---------- 4 files changed, 34 insertions(+), 43 deletions(-) diff --git a/autoload/vaffle.vim b/autoload/vaffle.vim index f6feba1..0f19359 100644 --- a/autoload/vaffle.vim +++ b/autoload/vaffle.vim @@ -21,25 +21,24 @@ endfunction function! vaffle#init(...) abort - let path = (a:0 == 0) - \ ? getcwd() - \ : a:1 - if !isdirectory(path) - call vaffle#util#echo_error( - \ printf('Not a directory: ''%s''', path)) - return - endif + let bufnr = bufnr('%') + let is_vaffle_buffer = vaffle#buffer#is_for_vaffle(bufnr) - if &filetype ==? 'vaffle' - call vaffle#buffer#reuse(path) - return + let path = get(a:000, 0, '') + let extracted_path = vaffle#buffer#extract_path_from_bufname(path) + if !empty(extracted_path) + let path = extracted_path + endif + if empty(path) + let path = getcwd() endif - if !isdirectory(bufname('%')) - " Create new buffer for non-directory buffer - let g:vaffle_creating_vaffle_buffer = 1 - enew - unlet g:vaffle_creating_vaffle_buffer + let bufname = bufname('%') + if !is_vaffle_buffer && !isdirectory(bufname) + " Open new directory buffer and overwrite it + " (will be initialized by vaffle#event#on_bufenter) + execute printf('edit %s', fnameescape(path)) + return endif try diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index 85f1288..28bca70 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -143,22 +143,21 @@ function! vaffle#buffer#init(path) abort let env = vaffle#env#create(path) call vaffle#env#inherit(env, vaffle#buffer#get_env()) + let env.initial_options = initial_options let env.items = vaffle#env#create_items(env) - call vaffle#buffer#set_env(env) - - call vaffle#buffer#redraw() - - call s:perform_auto_cd_if_needed(path) -endfunction - - -function! vaffle#buffer#reuse(path) abort - let path = vaffle#util#normalize_path(a:path) + if env.non_vaffle_bufnr == bufnr('%') + " Exclude empty buffer used for Vaffle + " For example: + " :enew + " Created new empty buffer (bufnr: 2) + " Updated `non_vaffle_bufnr` (= 2) + " :Vaffle + " Used buffer (bufnr: 2) for Vaffle + " `non_vaffle_bufnr` is 2, but should not restore it + let env.non_vaffle_bufnr = -1 + endif - let env = vaffle#env#create(path) - call vaffle#env#inherit(env, vaffle#buffer#get_env()) - let env.items = vaffle#env#create_items(env) call vaffle#buffer#set_env(env) call vaffle#buffer#redraw() @@ -173,9 +172,8 @@ function! vaffle#buffer#is_for_vaffle(bufnr) abort endfunction -function! vaffle#buffer#extract_path_from_bufname(bufnr) abort - let bufname = bufname(a:bufnr) - let matches = matchlist(bufname, '^vaffle://\d\+/\(.*\)$') +function! vaffle#buffer#extract_path_from_bufname(bufname) abort + let matches = matchlist(a:bufname, '^vaffle://\d\+/\(.*\)$') return get(matches, 1, '') endfunction diff --git a/autoload/vaffle/env.vim b/autoload/vaffle/env.vim index ac12558..13bf6b7 100644 --- a/autoload/vaffle/env.vim +++ b/autoload/vaffle/env.vim @@ -12,7 +12,6 @@ function! vaffle#env#create(path) abort let env.initial_options = {} let env.cursor_paths = {} let env.non_vaffle_bufnr = -1 - let env.non_vaffle_bufnr = -1 let env.shows_hidden_files = g:vaffle_show_hidden_files let env.items = [] return env diff --git a/autoload/vaffle/event.vim b/autoload/vaffle/event.vim index dfba797..cd2412f 100644 --- a/autoload/vaffle/event.vim +++ b/autoload/vaffle/event.vim @@ -17,22 +17,17 @@ function! vaffle#event#on_bufenter() abort call s:newtralize_netrw() let bufnr = bufnr('%') - let bufname = bufname(bufnr) let is_vaffle_buffer = vaffle#buffer#is_for_vaffle(bufnr) - let path = is_vaffle_buffer - \ ? vaffle#buffer#extract_path_from_bufname(bufname) - \ : expand('%:p') + let path = expand('%:p') let should_init = is_vaffle_buffer \ || isdirectory(path) + " Store bufnr of non-directory buffer to back to initial buffer if !should_init - if !get(g:, 'vaffle_creating_vaffle_buffer', 0) - " Store bufnr of non-vaffle buffer to restore initial state - let env = vaffle#buffer#get_env() - let env.non_vaffle_bufnr = bufnr - call vaffle#buffer#set_env(env) - endif + let env = vaffle#buffer#get_env() + let env.non_vaffle_bufnr = bufnr + call vaffle#buffer#set_env(env) return endif