From 5faecac4804dd03e895f34f0569df1da41f2d915 Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Fri, 8 May 2020 14:28:42 -0700 Subject: [PATCH] Add DirvishShdoCreated autocmd Allow users to process shdo scripts on creation. The example from the doc is especially useful with 'autochdir' so scripts start in the current dirvish working directory. Other users may tend to create scripts with arguments, want their commands to be inside a function, or output some diagnostic data. Using the same escaping that is applied on lines since that seems like the safest thing to do. dirvish_dummy_events ensures no error if users haven't setup an autocmd. --- autoload/dirvish.vim | 8 ++++++++ doc/dirvish.txt | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/autoload/dirvish.vim b/autoload/dirvish.vim index 10d0149..792a5ee 100644 --- a/autoload/dirvish.vim +++ b/autoload/dirvish.vim @@ -5,6 +5,11 @@ let s:noau = 'silent noautocmd keepjumps' let s:cb_map = {} " callback map let s:rel = get(g:, 'dirvish_relative_paths', 0) +augroup dirvish_dummy_events + autocmd! + autocmd User Dirvish* " +augroup END + " Debug: " echo '' > dirvish.log ; tail -F dirvish.log " nvim +"let g:dirvish_dbg=1" -- b1 b2 @@ -169,6 +174,9 @@ func! dirvish#shdo(paths, cmd) abort augroup END nnoremap Z! :silent writeexe '!'.(has('win32')?fnameescape(escape(expand('%:p:gs?\\?/?'), '&\')):join(map(split(&shell), 'shellescape(v:val)')).' %')if !v:shell_errorcloseendif + + let b:dirvish_dir = escape(shellescape(head),'&\') + doautocmd User DirvishShdoCreated endf " Returns true if the buffer was modified by the user. diff --git a/doc/dirvish.txt b/doc/dirvish.txt index 56fed1b..cdc4116 100644 --- a/doc/dirvish.txt +++ b/doc/dirvish.txt @@ -126,6 +126,28 @@ dirvish#remove_icon_fn(fn_id) Unregisters the function associated with {fn_id} returned from |dirvish#add_icon_fn()|. +============================================================================== +AUTOCOMMANDS *dirvish-autocommands* + +DirvishShdoCreated *User_DirvishShdoCreated* + A |User| |autocommand| invoked after creating a |:Shdo| buffer. + b:dirvish_dir is set to the previous Dirvish buffer's path. + + To ensure scripts always cd to the current dirvish directory, you could + add this to your vimrc: > + + autocmd User DirvishShdoCreated call OnShdoCreated() + function! OnShdoCreated() abort + if has('win32') + " work across hard drives + let chdir = 'pushd ' + else + let chdir = 'cd ' + endif + call append(0, [chdir .. b:dirvish_dir, '']) + endf +< + ============================================================================== OPTIONS *dirvish-options*