Skip to content

Commit

Permalink
Merge pull request #352 from lambdalisue/input-without-guard
Browse files Browse the repository at this point in the history
Add `=` action variants to several actions for providing default value
  • Loading branch information
lambdalisue authored Jul 3, 2021
2 parents 40f0987 + 0c3b831 commit 1b234d8
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 19 deletions.
9 changes: 9 additions & 0 deletions autoload/fern/mapping.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ function! fern#mapping#call(fn, ...) abort
endtry
endfunction

function! fern#mapping#call_without_guard(fn, ...) abort
try
call s:Promise.resolve(call('fern#helper#call', [a:fn] + a:000))
\.catch({ e -> fern#logger#error(e) })
catch
call fern#logger#error(v:exception)
endtry
endfunction

function! fern#mapping#init(scheme) abort
let disable_default_mappings = g:fern#disable_default_mappings
for name in g:fern#mapping#mappings
Expand Down
13 changes: 11 additions & 2 deletions autoload/fern/mapping/filter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ function! fern#mapping#filter#init(disable_default_mappings) abort
nnoremap <buffer><silent> <Plug>(fern-action-hidden:set) :<C-u>call <SID>call('hidden_set')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-hidden:unset) :<C-u>call <SID>call('hidden_unset')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-hidden:toggle) :<C-u>call <SID>call('hidden_toggle')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-include) :<C-u>call <SID>call('include')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-exclude) :<C-u>call <SID>call('exclude')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-include) :<C-u>call <SID>call('include')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-exclude) :<C-u>call <SID>call('exclude')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-include=) :<C-u>call <SID>call_without_guard('include')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-exclude=) :<C-u>call <SID>call_without_guard('exclude')<CR>
" Alias
nmap <buffer> <Plug>(fern-action-hidden) <Plug>(fern-action-hidden:toggle)
Expand All @@ -30,6 +32,13 @@ function! s:call(name, ...) abort
\)
endfunction

function! s:call_without_guard(name, ...) abort
return call(
\ 'fern#mapping#call_without_guard',
\ [funcref(printf('s:map_%s', a:name))] + a:000,
\)
endfunction

function! s:map_hidden_set(helper) abort
return a:helper.async.set_hidden(1)
\.then({ -> a:helper.async.redraw() })
Expand Down
8 changes: 8 additions & 0 deletions autoload/fern/mapping/node.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function! fern#mapping#node#init(disable_default_mappings) abort
nnoremap <buffer><silent> <Plug>(fern-action-expand:in) :<C-u>call <SID>call('expand_in')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-collapse) :<C-u>call <SID>call('collapse')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-reveal) :<C-u>call <SID>call('reveal')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-reveal=) :<C-u>call <SID>call_without_guard('reveal')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-focus:parent) :<C-u>call <SID>call('focus_parent')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-enter) :<C-u>call <SID>call('enter')<CR>
Expand Down Expand Up @@ -36,6 +37,13 @@ function! s:call(name, ...) abort
\)
endfunction

function! s:call_without_guard(name, ...) abort
return call(
\ 'fern#mapping#call_without_guard',
\ [funcref(printf('s:map_%s', a:name))] + a:000,
\)
endfunction

function! s:map_debug(helper) abort
let node = a:helper.sync.get_cursor_node()
redraw | echo fern#internal#node#debug(node)
Expand Down
24 changes: 17 additions & 7 deletions autoload/fern/scheme/file/mapping.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ let s:Promise = vital#fern#import('Async.Promise')
let s:Prompt = vital#fern#import('Prompt')

function! fern#scheme#file#mapping#init(disable_default_mappings) abort
nnoremap <buffer><silent> <Plug>(fern-action-new-path) :<C-u>call <SID>call('new_path')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-new-file) :<C-u>call <SID>call('new_file')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-new-dir) :<C-u>call <SID>call('new_dir')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-copy) :<C-u>call <SID>call('copy')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-move) :<C-u>call <SID>call('move')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-trash) :<C-u>call <SID>call('trash')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-remove) :<C-u>call <SID>call('remove')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-new-path) :<C-u>call <SID>call('new_path')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-new-file) :<C-u>call <SID>call('new_file')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-new-dir) :<C-u>call <SID>call('new_dir')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-new-path=) :<C-u>call <SID>call_without_guard('new_path')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-new-file=) :<C-u>call <SID>call_without_guard('new_file')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-new-dir=) :<C-u>call <SID>call_without_guard('new_dir')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-copy) :<C-u>call <SID>call('copy')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-move) :<C-u>call <SID>call('move')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-trash) :<C-u>call <SID>call('trash')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-remove) :<C-u>call <SID>call('remove')<CR>
if !a:disable_default_mappings
nmap <buffer><nowait> N <Plug>(fern-action-new-file)
Expand All @@ -26,6 +29,13 @@ function! s:call(name, ...) abort
\)
endfunction

function! s:call_without_guard(name, ...) abort
return call(
\ 'fern#mapping#call_without_guard',
\ [funcref(printf('s:map_%s', a:name))] + a:000,
\)
endfunction

function! s:map_cd(helper, command) abort
let path = a:helper.sync.get_cursor_node()._path
if a:command ==# 'tcd' && !exists(':tcd')
Expand Down
10 changes: 9 additions & 1 deletion autoload/fern/scheme/file/mapping/ex.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
let s:Promise = vital#fern#import('Async.Promise')

function! fern#scheme#file#mapping#ex#init(disable_default_mappings) abort
nnoremap <buffer><silent> <Plug>(fern-action-ex) :<C-u>call <SID>call('ex')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-ex) :<C-u>call <SID>call('ex')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-ex=) :<C-u>call <SID>call_without_guard('ex')<CR>
endfunction

function! s:call(name, ...) abort
Expand All @@ -11,6 +12,13 @@ function! s:call(name, ...) abort
\)
endfunction

function! s:call_without_guard(name, ...) abort
return call(
\ 'fern#mapping#call_without_guard',
\ [funcref(printf('s:map_%s', a:name))] + a:000,
\)
endfunction

function! s:map_ex(helper) abort
let nodes = a:helper.sync.get_selected_nodes()
let nodes = filter(copy(nodes), { -> v:val._path isnot# v:null })
Expand Down
10 changes: 9 additions & 1 deletion autoload/fern/scheme/file/mapping/grep.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ let s:Promise = vital#fern#import('Async.Promise')
let s:Process = vital#fern#import('Async.Promise.Process')

function! fern#scheme#file#mapping#grep#init(disable_default_mappings) abort
nnoremap <buffer><silent> <Plug>(fern-action-grep) :<C-u>call <SID>call('grep')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-grep) :<C-u>call <SID>call('grep')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-grep=) :<C-u>call <SID>call_without_guard('grep')<CR>
endfunction

function! s:call(name, ...) abort
Expand All @@ -13,6 +14,13 @@ function! s:call(name, ...) abort
\)
endfunction

function! s:call_without_guard(name, ...) abort
return call(
\ 'fern#mapping#call_without_guard',
\ [funcref(printf('s:map_%s', a:name))] + a:000,
\)
endfunction

function! s:map_grep(helper) abort
let pattern = input('Grep: ', '')
if empty(pattern)
Expand Down
80 changes: 72 additions & 8 deletions doc/fern.txt
Original file line number Diff line number Diff line change
Expand Up @@ -823,13 +823,29 @@ GLOBAL *fern-mapping-global*
DEPRECATED: Use |<Plug>(fern-action-hidden:toggle)| instead.

*<Plug>(fern-action-include)*
*<Plug>(fern-action-include=)*
Open a prompt to enter include filter. Users can type a |pattern| to
filter nodes recursively.

You can use a "=" variant to apply values to the prompt and/or submit
a value like:
>
" Automatically apply "foo" to the prompt and submit.
nmap <buffer>
\ <Plug>(my-include)
\ <Plug>(fern-action-include=)foo<CR>
<
*<Plug>(fern-action-exclude)*
*<Plug>(fern-action-exclude=)*
Open a prompt to enter exclude filter. Users can type a |pattern| to
filter nodes recursively.

You can use a "=" variant to apply values to the prompt and/or submit
a value like:
>
" Automatically apply "foo" to the prompt and submit.
nmap <buffer>
\ <Plug>(my-exclude)
\ <Plug>(fern-action-exclude=)foo<CR>
<
*<Plug>(fern-action-mark:clear)*
Clear existing marks.

Expand Down Expand Up @@ -895,8 +911,16 @@ GLOBAL *fern-mapping-global*
Collapse on a cursor node.

*<Plug>(fern-action-reveal)*
*<Plug>(fern-action-reveal=)*
Open a prompt to reveal a node in a tree.

You can use a "=" variant to apply values to the prompt and/or submit
a value like:
>
" Automatically apply "foo" to the prompt and submit.
nmap <buffer>
\ <Plug>(my-reveal)
\ <Plug>(fern-action-reveal=)foo<CR>
<
*<Plug>(fern-action-focus:parent)*
Focus the parent of the cursor node.

Expand Down Expand Up @@ -1103,26 +1127,58 @@ FILE *fern-mapping-file*
The following mappings/actions are only available on file:// scheme.

*<Plug>(fern-action-ex)*
*<Plug>(fern-action-ex=)*
Open a prompt to execute an Ex command with a path of cursor node or
paths of marked nodes.

You can use a "=" variant to apply values to the prompt and/or submit
a value like:
>
" Automatically apply "foo" to the prompt and submit.
nmap <buffer>
\ <Plug>(my-ex)
\ <Plug>(fern-action-ex=)foo<CR>
<
*<Plug>(fern-action-new-path)*
*<Plug>(fern-action-new-path=)*
Open a prompt to ask a path and create a file/directory of the input
path from the path of a cursor node.
Any intermediate directories of the destination will be created.
If the path ends with "/", it creates a directory. Otherwise it
creates a file.

You can use a "=" variant to apply values to the prompt and/or submit
a value like:
>
" Automatically apply "foo" to the prompt and submit.
nmap <buffer>
\ <Plug>(my-new-path)
\ <Plug>(fern-action-new-path=)foo<CR>
<
*<Plug>(fern-action-new-file)*
*<Plug>(fern-action-new-file=)*
Open a prompt to ask a path and create a file of the input path from
the path of a cursor node.
Any intermediate directories of the destination will be created.

You can use a "=" variant to apply values to the prompt and/or submit
a value like:
>
" Automatically apply "foo" to the prompt and submit.
nmap <buffer>
\ <Plug>(my-new-file)
\ <Plug>(fern-action-new-file=)foo<CR>
<
*<Plug>(fern-action-new-dir)*
*<Plug>(fern-action-new-dir=)*
Open a prompt to ask a path and create a directory of the input path
from the path of a cursor node.
Any intermediate directories of the destination will be created.

You can use a "=" variant to apply values to the prompt and/or submit
a value like:
>
" Automatically apply "foo" to the prompt and submit.
nmap <buffer>
\ <Plug>(my-new-dir)
\ <Plug>(fern-action-new-dir=)foo<CR>
<
*<Plug>(fern-action-copy)*
Open a prompt to ask a path and copy a file/directory of the cursor
node or marked node path(s) to the input path(s).
Expand Down Expand Up @@ -1195,9 +1251,17 @@ The following mappings/actions are only available on file:// scheme.
Note that this action has NO-relation with the system clipboard.

*<Plug>(fern-action-grep)*
*<Plug>(fern-action-grep=)*
Open a prompt to ask a grep pattern and execute grep command under the
cursor node. It respects the value of 'grepprg' and 'grepformat'.

You can use a "=" variant to apply values to the prompt and/or submit
a value like:
>
" Automatically apply "foo" to the prompt and submit.
nmap <buffer>
\ <Plug>(my-grep)
\ <Plug>(fern-action-grep=)foo<CR>
<
*<Plug>(fern-action-rename:select)*
*<Plug>(fern-action-rename:split)*
*<Plug>(fern-action-rename:vsplit)*
Expand Down

0 comments on commit 1b234d8

Please sign in to comment.