From 90032e3c1e664d7019a327fb91a51e91aabadaac Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Fri, 8 May 2020 14:28:42 -0700 Subject: [PATCH] Add option for preamble before shdo scripts 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. --- autoload/dirvish.vim | 5 +++++ doc/dirvish.txt | 14 ++++++++++++++ plugin/dirvish.vim | 2 ++ 3 files changed, 21 insertions(+) diff --git a/autoload/dirvish.vim b/autoload/dirvish.vim index fe310ea..d9e53a5 100644 --- a/autoload/dirvish.vim +++ b/autoload/dirvish.vim @@ -151,6 +151,11 @@ func! 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) diff --git a/doc/dirvish.txt b/doc/dirvish.txt index d058a1a..d4c79a7 100644 --- a/doc/dirvish.txt +++ b/doc/dirvish.txt @@ -163,6 +163,20 @@ can be overridden by handling that event. Example: > \ gh :silent keeppatterns g@\v/\.[^\/]+/?$@d _:setl cole=3 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* diff --git a/plugin/dirvish.vim b/plugin/dirvish.vim index e25e0e6..73f2b7d 100644 --- a/plugin/dirvish.vim +++ b/plugin/dirvish.vim @@ -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() command! -nargs=* -complete=file -range -bang Shdo call dirvish#shdo(0 ? argv() : getline(, ), )