Skip to content

Commit 5faecac

Browse files
committed
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.
1 parent 5d1d1ef commit 5faecac

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

autoload/dirvish.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ let s:noau = 'silent noautocmd keepjumps'
55
let s:cb_map = {} " callback map
66
let s:rel = get(g:, 'dirvish_relative_paths', 0)
77

8+
augroup dirvish_dummy_events
9+
autocmd!
10+
autocmd User Dirvish* "
11+
augroup END
12+
813
" Debug:
914
" echo '' > dirvish.log ; tail -F dirvish.log
1015
" nvim +"let g:dirvish_dbg=1" -- b1 b2
@@ -169,6 +174,9 @@ func! dirvish#shdo(paths, cmd) abort
169174
augroup END
170175

171176
nnoremap <buffer><silent> Z! :silent write<Bar>exe '!'.(has('win32')?fnameescape(escape(expand('%:p:gs?\\?/?'), '&\')):join(map(split(&shell), 'shellescape(v:val)')).' %')<Bar>if !v:shell_error<Bar>close<Bar>endif<CR>
177+
178+
let b:dirvish_dir = escape(shellescape(head),'&\')
179+
doautocmd <nomodeline> User DirvishShdoCreated
172180
endf
173181

174182
" Returns true if the buffer was modified by the user.

doc/dirvish.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,28 @@ dirvish#remove_icon_fn(fn_id)
126126
Unregisters the function associated with {fn_id} returned from
127127
|dirvish#add_icon_fn()|.
128128

129+
==============================================================================
130+
AUTOCOMMANDS *dirvish-autocommands*
131+
132+
DirvishShdoCreated *User_DirvishShdoCreated*
133+
A |User| |autocommand| invoked after creating a |:Shdo| buffer.
134+
b:dirvish_dir is set to the previous Dirvish buffer's path.
135+
136+
To ensure scripts always cd to the current dirvish directory, you could
137+
add this to your vimrc: >
138+
139+
autocmd User DirvishShdoCreated call OnShdoCreated()
140+
function! OnShdoCreated() abort
141+
if has('win32')
142+
" work across hard drives
143+
let chdir = 'pushd '
144+
else
145+
let chdir = 'cd '
146+
endif
147+
call append(0, [chdir .. b:dirvish_dir, ''])
148+
endf
149+
<
150+
129151
==============================================================================
130152
OPTIONS *dirvish-options*
131153

0 commit comments

Comments
 (0)