Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions autoload/wiki/fzf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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])
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I agree it is good to have a comment here to explain.

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
Expand Down
10 changes: 9 additions & 1 deletion autoload/wiki/page.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions doc/wiki.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down Expand Up @@ -750,11 +758,23 @@ the commands are also available as mappings of the form `<plug>(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).

*<plug>(wiki-fzf-tags)*
*WikiFzfTags*
Expand Down