Skip to content

Commit

Permalink
Add option for preamble before shdo scripts
Browse files Browse the repository at this point in the history
I want all of my scripts to start in the current dirvish working
directory, so I use the example from the doc:
  let g:dirvish_shdo_before = 'cd {}'

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.
  • Loading branch information
idbrii committed Jul 12, 2021
1 parent ff3f5cd commit ac7427a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions autoload/dirvish.vim
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ function! dirvish#shdo(paths, cmd) abort
let f = !jagged && 2==exists(':lcd') ? fnamemodify(f, ':t') : lines[i]
let lines[i] = substitute(cmd, '\V{}', escape(shellescape(f),'&\'), 'g')
endfor

if !empty(g:dirvish_shdo_before)
let lines = [substitute(g:dirvish_shdo_before, '\V{}', escape(shellescape(head),'&\'), 'g'), ''] + lines
endif

execute 'silent split' tmpfile '|' (2==exists(':lcd')?('lcd '.dir):'')
setlocal bufhidden=wipe
silent keepmarks keepjumps call setline(1, lines)
Expand Down
14 changes: 14 additions & 0 deletions doc/dirvish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ can be overridden by handling that event. Example: >
\ gh :silent keeppatterns g@\v/\.[^\/]+/?$@d _<cr>:setl cole=3<cr>
augroup END
<
g:dirvish_shdo_before = '' *g:dirvish_shdo_before*
Inserts this text at the beginning of shell scripts generated with
|:Shdo|. The current dirvish directory is inserted wherever {} appears in
|g:dirvish_shdo_before|.
To ensure scripts always cd to the current dirvish directory, you could
add this to your vimrc: >
if has('win32')
" work across hard drives
let g:dirvish_shdo_before = 'pushd {}'
else
let g:dirvish_shdo_before = 'cd {}'
endif
<

==============================================================================
FAQ *dirvish-faq*
Expand Down
2 changes: 2 additions & 0 deletions plugin/dirvish.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ if exists('g:loaded_dirvish') || &cp || v:version < 700 || &cpo =~# 'C'
endif
let g:loaded_dirvish = 1

let g:dirvish_shdo_before = get(g:, 'dirvish_shdo_before', '')

command! -bar -nargs=? -complete=dir Dirvish call dirvish#open(<q-args>)
command! -nargs=* -complete=file -range -bang Shdo call dirvish#shdo(<bang>0 ? argv() : getline(<line1>, <line2>), <q-args>)

Expand Down

0 comments on commit ac7427a

Please sign in to comment.