Skip to content
Merged
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
65 changes: 46 additions & 19 deletions autoload/wiki/fzf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ function! wiki#fzf#pages() abort "{{{1

let l:fzf_opts = join([
\ '-d"#####" --with-nth=-1 --print-query --prompt "WikiPages> "',
\ '--expect=' . get(g:, 'wiki_fzf_pages_force_create_key', 'alt-enter'),
\ '--expect=' . get(g:, 'wiki_fzf_force_create_key', 'alt-enter'),
\ g:wiki_fzf_pages_opts,
\])

call fzf#run(fzf#wrap({
\ 'source': map(
\ wiki#page#get_all(),
\ {_, x -> x[0] . '#####' . x[1] }),
\ {_, x -> x[0] . '#####' . substitute(x[1], '^/', '', '') }),
\ 'sink*': funcref('s:accept_page'),
\ 'options': l:fzf_opts
\}))
Expand Down Expand Up @@ -87,18 +87,19 @@ function! wiki#fzf#links(...) abort "{{{1
call wiki#log#warn('fzf must be installed for this to work')
return
endif
let l:mode = a:0 > 0 ? a:1 : ''
let l:mode = a:0 > 0 ? a:1 : 'normal'

let l:fzf_opts = join([
\ '-d"#####" --with-nth=-1 --print-query --prompt "WikiLinkAdd> "',
\ '--expect=' . get(g:, 'wiki_fzf_force_create_key', 'alt-enter'),
\ g:wiki_fzf_links_opts,
\])

call fzf#run(fzf#wrap({
\ 'source': map(
\ wiki#page#get_all(),
\ {_, x -> x[0] .. '#####' .. l:mode .. '#####' .. x[1] }),
\ 'sink*': funcref('s:accept_link'),
\ {_, x -> x[0] .. '#####' .. substitute(x[1], '^/', '', '') }),
\ 'sink*': funcref('s:accept_link_'..l:mode),
\ 'options': l:fzf_opts
\}))
endfunction
Expand All @@ -109,7 +110,7 @@ function! s:accept_page(lines) abort "{{{1
" a:lines is a list with two or three elements. Two if there were no matches,
" and three if there is one or more matching names. The first element is the
" search query; the second is either an empty string or the alternative key
" specified by g:wiki_fzf_pages_force_create_key (e.g. 'alt-enter') if this
" specified by g:wiki_fzf_force_create_key (e.g. 'alt-enter') if this
" was pressed; the third element contains the selected item.
if len(a:lines) < 2 | return | endif

Expand Down Expand Up @@ -151,20 +152,46 @@ function! s:accept_toc_entry(line) abort "{{{1
endfunction

"}}}1
function! s:accept_link(lines) abort "{{{1
" a:lines is a list with one or two elements. Two if there was a match, else
" one. The first element is the search query; the second element contains the
" selected item.
let l:path = len(a:lines) == 2
\ ? split(a:lines[1], '#####')[0]
\ : a:lines[0]
let l:mode = split(a:lines[1], '#####')[1]

call wiki#link#add(l:path, l:mode, { 'transform_relative': v:true })

if l:mode=='insert'
call feedkeys('a')
function! s:accept_link_visual(lines) abort "{{{1
" a:lines is a list with two or three elements. Two if there were no matches,
" and three if there is one or more matching names. The first element is the
" search query; the second is either an empty string or the alternative key
" specified by g:wiki_fzf_force_create_key (e.g. 'alt-enter') if this
" was pressed; the third element contains the selected item.
let l:path = s:get_path(a:lines)
call wiki#link#add(l:path, 'visual')
endfunction

" }}}1
function! s:get_path(lines) abort "{{{1
if len(a:lines) == 2 || !empty(a:lines[1])
return a:lines[0]
else
return split(a:lines[2], '#####')[0]
endif
endfunction

" }}}1
function! s:accept_link_insert(lines) abort "{{{1
" a:lines is a list with two or three elements. Two if there were no matches,
" and three if there is one or more matching names. The first element is the
" search query; the second is either an empty string or the alternative key
" specified by g:wiki_fzf_force_create_key (e.g. 'alt-enter') if this
" was pressed; the third element contains the selected item.
let l:path = s:get_path(a:lines)
call wiki#link#add(l:path, 'insert')
call feedkeys('a')
endfunction

" }}}1
function! s:accept_link_normal(lines) abort "{{{1
" a:lines is a list with two or three elements. Two if there were no matches,
" and three if there is one or more matching names. The first element is the
" search query; the second is either an empty string or the alternative key
" specified by g:wiki_fzf_force_create_key (e.g. 'alt-enter') if this
" was pressed; the third element contains the selected item.
let l:path = s:get_path(a:lines)
call wiki#link#add(l:path, '')
endfunction

" }}}1
21 changes: 17 additions & 4 deletions doc/wiki.txt
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ a reference of all available options.

Default: `['md']`

*g:wiki_fzf_pages_force_create_key*
*g:wiki_fzf_force_create_key*
Key combination that, when pressed while searching with |WikiPages|, 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
Expand Down Expand Up @@ -857,10 +857,11 @@ a reference of all available options.

- fzf (https://github.com/junegunn/fzf.vim):
- `wiki#fzf#pages` |g:wiki_fzf_pages_opts|
|g:wiki_fzf_pages_force_create_key|
|g:wiki_fzf_force_create_key|
- `wiki#fzf#tags` |g:wiki_fzf_tags_opts|
- `wiki#fzf#toc`
- `wiki#fzf#links` |g:wiki_fzf_links_opts|
|g:wiki_fzf_force_create_key|
- Lua backend that uses |vim.ui.select| (neovim only!)
- `require("wiki.ui_select").pages`
- `require("wiki.ui_select").tags`
Expand Down Expand Up @@ -1789,7 +1790,19 @@ MAPPINGS AND COMMANDS REFERENCE *wiki-mappings-reference*
*WikiLinkAdd*
List the wiki files in the current wiki. Select from the list to add a link
to that target page. This uses the UI select method defined by
|g:wiki_select_method|. The mapping also works from insert mode.
|g:wiki_select_method|. The mapping also works from insert and visual mode.

fzf specific behaviour ~
If |g:wiki_select_method| is `'fzf'`, then it is possible to create
a new page by pressing |g:wiki_fzf_force_create_key| (alt-enter,
by default). That is, a link whose name is equal to the fzf query will
be created.

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 create a
new link "My Fzf Page.wiki" (or similar, depending on the value of other
options).

*<plug>(wiki-link-next)*
*WikiLinkNext*
Expand Down Expand Up @@ -2032,7 +2045,7 @@ MAPPINGS AND COMMANDS REFERENCE *wiki-mappings-reference*

fzf specific behaviour ~
If |g:wiki_select_method| is `'fzf'`, then it is possible to create
a new page by pressing |g:wiki_fzf_pages_force_create_key| (alt-enter,
a new page by pressing |g:wiki_fzf_force_create_key| (alt-enter,
by default). That is, a page whose name is equal to the fzf query will
be created if it doesn't already exist. If the page exists, the existing
page will be opened.
Expand Down
1 change: 1 addition & 0 deletions test/example-select-methods/select-methods-fzf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ syntax enable

nnoremap q :qall!<cr>

let mapleader=" "
let g:wiki_cache_persistent = 0
let g:wiki_filetypes = ['wiki']
let g:wiki_root = '../wiki-basic'
Expand Down