diff --git a/autoload/wiki/fzf.vim b/autoload/wiki/fzf.vim index eaac9ce7..ff1e81f7 100644 --- a/autoload/wiki/fzf.vim +++ b/autoload/wiki/fzf.vim @@ -18,10 +18,15 @@ 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 l:fzf_opts = join([ + \ '-d"¤" --with-nth=-1 --print-query --prompt "WikiPages> "', + \ '--expect=' . get(g:, 'wiki_fzf_pages_force_create_key', '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': l:fzf_opts \})) endfunction @@ -78,14 +83,14 @@ endfunction "}}}1 function! s:accept_page(lines) abort "{{{1 - if len(a:lines) == 1 - let l:page = a:lines[0] - redraw! - call wiki#log#info('Opening new page "' . l:page . '"') - sleep 1 - call wiki#page#open(l:page) + " 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]) + call wiki#page#open(a:lines[0]) 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/autoload/wiki/page.vim b/autoload/wiki/page.vim index 883b4ad0..3444e525 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 l:url.open() endfunction "}}}1 diff --git a/doc/wiki.txt b/doc/wiki.txt index 1631487a..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,11 +758,23 @@ 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 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 + 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*