From d76b92bc80eed76324ed7f4faa0e747bad81916e Mon Sep 17 00:00:00 2001 From: Justin Eldridge Date: Tue, 13 Apr 2021 21:05:54 -0700 Subject: [PATCH 1/3] Create new page with fzf even if there is a match Previously, fzf#pages was only capable of creating a new wiki page if the query resulted in no matches. This commit allows for the creation of a new page even when the query does match one or more existing pages by pressing alt-enter while in the fzf prompt. --- autoload/wiki/fzf.vim | 17 ++++++++++++----- doc/wiki.txt | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/autoload/wiki/fzf.vim b/autoload/wiki/fzf.vim index eaac9ce7..4463070c 100644 --- a/autoload/wiki/fzf.vim +++ b/autoload/wiki/fzf.vim @@ -17,11 +17,14 @@ function! wiki#fzf#pages() abort "{{{1 let l:pages = globpath(l:root, l:pattern, v:false, v:true) call map(l:pages, '"/" . substitute(v:val, l:root . "/" , "", "")') call map(l:pages, {_, x -> x . "¤" . fnamemodify(x, ':r')}) - + let fzf_opts = join([ + \ '-d"¤" --with-nth=-1 --print-query --prompt "WikiPages> "', + \ '--expect=alt-enter' + \]) call fzf#run(fzf#wrap({ \ 'source': l:pages, \ 'sink*': funcref('s:accept_page'), - \ 'options': '-d"¤" --with-nth=-1 --print-query --prompt "WikiPages> " ' + \ 'options': fzf_opts \})) endfunction @@ -78,14 +81,18 @@ endfunction "}}}1 function! s:accept_page(lines) abort "{{{1 - if len(a:lines) == 1 + " if the query was so narrow that no page names matched, there will be two + " lines -- otherwise, three. The first line is the query, the second is + " either empty or alt-enter, depending on if enter or alt-enter was used to + " select, and the third line (possibly) contains the selection + if len(a:lines) == 2 || !empty(a:lines[1]) let l:page = a:lines[0] redraw! - call wiki#log#info('Opening new page "' . l:page . '"') + call wiki#log#info('Opening page "' . l:page . '"') sleep 1 call wiki#page#open(l:page) else - let l:file = split(a:lines[1], '¤')[0] + let l:file = split(a:lines[2], '¤')[0] execute 'edit ' . wiki#get_root() . l:file endif endfunction diff --git a/doc/wiki.txt b/doc/wiki.txt index 1631487a..c5d006cb 100644 --- a/doc/wiki.txt +++ b/doc/wiki.txt @@ -750,11 +750,18 @@ the commands are also available as mappings of the form `(wiki-[name])`. Open |fzf| in find file mode for wiki files in current wiki or in the main wiki defined by |g:wiki_root|. - This can also also be used to create a new page similar to |WikiOpen|, if - the input search term does not have any results. E.g., if you searched for - "My Fzf Page", and this term does not exist, then it will instead open a new - page "My Fzf Page.wiki" (or similar, depending on the value of other - options). + This can also also be used to create a new page. If a query is typed and + alt-enter is pressed, a new page with name equal to the query will be + created if it doesn't already exist. For example, suppose you search for "My + Fzf Page", and there is already a page named "My Favorite Fzf Page". fzf + will show the latter as a possible selection. If the enter key is pressed, + the existing "My Favorite Fzf Page" will be opened. If alt-enter is pressed + instead, a new page titled "My Fzf Page" will be created and opened. + + For convenience, if the input query does not have any results, simply + pressing enter will also create a page. E.g., if you searched for "My Fzf + Page", and this term does not exist, then it will instead open a new page + "My Fzf Page.wiki" (or similar, depending on the value of other options). *(wiki-fzf-tags)* *WikiFzfTags* From 6163126648027e001fc0da05b300690430f11ac0 Mon Sep 17 00:00:00 2001 From: Justin Eldridge Date: Fri, 16 Apr 2021 17:08:08 -0700 Subject: [PATCH 2/3] style changes; docs; added option --- autoload/wiki/fzf.vim | 10 ++++------ autoload/wiki/page.vim | 10 +++++++++- doc/wiki.txt | 27 ++++++++++++++++++++------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/autoload/wiki/fzf.vim b/autoload/wiki/fzf.vim index 4463070c..d01a4dac 100644 --- a/autoload/wiki/fzf.vim +++ b/autoload/wiki/fzf.vim @@ -17,10 +17,12 @@ function! wiki#fzf#pages() abort "{{{1 let l:pages = globpath(l:root, l:pattern, v:false, v:true) call map(l:pages, '"/" . substitute(v:val, l:root . "/" , "", "")') call map(l:pages, {_, x -> x . "¤" . fnamemodify(x, ':r')}) + let fzf_opts = join([ \ '-d"¤" --with-nth=-1 --print-query --prompt "WikiPages> "', - \ '--expect=alt-enter' + \ '--expect=' . get(g:, 'wiki_fzf_pages_force_create_key', 'alt-enter') \]) + call fzf#run(fzf#wrap({ \ 'source': l:pages, \ 'sink*': funcref('s:accept_page'), @@ -86,11 +88,7 @@ function! s:accept_page(lines) abort "{{{1 " either empty or alt-enter, depending on if enter or alt-enter was used to " select, and the third line (possibly) contains the selection if len(a:lines) == 2 || !empty(a:lines[1]) - let l:page = a:lines[0] - redraw! - call wiki#log#info('Opening page "' . l:page . '"') - sleep 1 - call wiki#page#open(l:page) + call wiki#page#open(a:lines[0]) else let l:file = split(a:lines[2], '¤')[0] execute 'edit ' . wiki#get_root() . l:file diff --git a/autoload/wiki/page.vim b/autoload/wiki/page.vim index 883b4ad0..19e32ff5 100644 --- a/autoload/wiki/page.vim +++ b/autoload/wiki/page.vim @@ -9,7 +9,15 @@ function! wiki#page#open(page) abort "{{{1 \ !empty(g:wiki_map_create_page) && exists('*' . g:wiki_map_create_page) \ ? call(g:wiki_map_create_page, [a:page]) \ : a:page - call wiki#url#parse('wiki:/' . l:page).open() + let l:url = wiki#url#parse('wiki:/' . l:page) + + if !filereadable(l:url.path) + redraw! + call wiki#log#info('Opening new page "' . l:page . '"') + sleep 1 + end + + call url.open() endfunction "}}}1 diff --git a/doc/wiki.txt b/doc/wiki.txt index c5d006cb..fcdef35b 100644 --- a/doc/wiki.txt +++ b/doc/wiki.txt @@ -505,6 +505,14 @@ OPTIONS *wiki-config-options* Default: 0 +*g:wiki_fzf_pages_force_create_key* + Key combination that, when pressed while searching with |WikiFzfPages|, will + create a new page with the name of the query. The value must be a string + recognized by fzf's `--expect` argument; see fzf's manual page for a list of + available keys. + + Default: `'alt-enter'` + ------------------------------------------------------------------------------ EVENTS *wiki-config-events* @@ -750,13 +758,18 @@ the commands are also available as mappings of the form `(wiki-[name])`. Open |fzf| in find file mode for wiki files in current wiki or in the main wiki defined by |g:wiki_root|. - This can also also be used to create a new page. If a query is typed and - alt-enter is pressed, a new page with name equal to the query will be - created if it doesn't already exist. For example, suppose you search for "My - Fzf Page", and there is already a page named "My Favorite Fzf Page". fzf - will show the latter as a possible selection. If the enter key is pressed, - the existing "My Favorite Fzf Page" will be opened. If alt-enter is pressed - instead, a new page titled "My Fzf Page" will be created and opened. + This can also be used to create a new page. If a query is typed and the key + specified by |g:wiki_fzf_pages_force_create_key| is pressed (alt-enter, by + default), a page whose name is equal to the query will be created if it + doesn't already exist. If the page does already exist, it will be opened. + If |g:wiki_map_create_page| is defined, the new page name is determined by + first applying that function to the query. + + For example, suppose you search for "foo", and there is one result: an + already-existing page named "foobar". If the enter key is pressed, the + existing "foobar" will be opened, as usual. However, if + |g:wiki_fzf_pages_force_create_key| is pressed instead, a new page titled + "foo" will be created and opened. For convenience, if the input query does not have any results, simply pressing enter will also create a page. E.g., if you searched for "My Fzf From db36957c18acd31ee5aa07b7e56234a76608a1fe Mon Sep 17 00:00:00 2001 From: Justin Eldridge Date: Fri, 16 Apr 2021 17:15:59 -0700 Subject: [PATCH 3/3] Marked variables as local --- autoload/wiki/fzf.vim | 4 ++-- autoload/wiki/page.vim | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/autoload/wiki/fzf.vim b/autoload/wiki/fzf.vim index d01a4dac..ff1e81f7 100644 --- a/autoload/wiki/fzf.vim +++ b/autoload/wiki/fzf.vim @@ -18,7 +18,7 @@ function! wiki#fzf#pages() abort "{{{1 call map(l:pages, '"/" . substitute(v:val, l:root . "/" , "", "")') call map(l:pages, {_, x -> x . "¤" . fnamemodify(x, ':r')}) - let fzf_opts = join([ + let l:fzf_opts = join([ \ '-d"¤" --with-nth=-1 --print-query --prompt "WikiPages> "', \ '--expect=' . get(g:, 'wiki_fzf_pages_force_create_key', 'alt-enter') \]) @@ -26,7 +26,7 @@ function! wiki#fzf#pages() abort "{{{1 call fzf#run(fzf#wrap({ \ 'source': l:pages, \ 'sink*': funcref('s:accept_page'), - \ 'options': fzf_opts + \ 'options': l:fzf_opts \})) endfunction diff --git a/autoload/wiki/page.vim b/autoload/wiki/page.vim index 19e32ff5..3444e525 100644 --- a/autoload/wiki/page.vim +++ b/autoload/wiki/page.vim @@ -17,7 +17,7 @@ function! wiki#page#open(page) abort "{{{1 sleep 1 end - call url.open() + call l:url.open() endfunction "}}}1