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
6 changes: 1 addition & 5 deletions autoload/wiki/buffer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ function! s:init_buffer_commands() abort " {{{1
command! -buffer -nargs=+ -complete=customlist,wiki#complete#tag_names
\ WikiTagRename call wiki#tags#rename_ask(<f-args>)

if has('nvim') && g:wiki_select_method == 'ui_select'
command! -buffer WikiToc lua require('wiki').toc()
else
command! -buffer WikiToc call wiki#fzf#toc()
endif
command! -buffer WikiToc call g:wiki_select_method.toc()
command! -buffer -nargs=1 WikiClearCache call wiki#cache#clear(<q-args>)

if b:wiki.in_journal
Expand Down
147 changes: 131 additions & 16 deletions doc/wiki.txt
Original file line number Diff line number Diff line change
Expand Up @@ -819,23 +819,62 @@ a reference of all available options.
Default: 1

*g:wiki_select_method*
A string to represent the UI select method used for the following commands:

- |WikiPages|
- |WikiTags|
- |WikiToc|

Possible values:

- `'ui_select'` Use |vim.ui.select| (neovim only!)
- `'fzf'` Use fzf (https://github.com/junegunn/fzf.vim). This has
a few additional options for more fine tuned control:
- |g:wiki_fzf_pages_opts|
- |g:wiki_fzf_tags_opts|
- |g:wiki_fzf_pages_force_create_key|
A dictionary of functions that lets allow the user to choose his preferred
the behaviour for the builtin commands: |WikiPage|, |WikiTags| and
|WikiToc|.

This allows the user quite a lot of flexibility in specifying how which
command will behave by letting him deciding wich builtin function use or
let him create one himself (see |wiki-advanced-config-3|).

The following table shows the option keys and the related commands:

key command
--- -------
`pages` |WikiPages|
`tags` |WikiTags|
`toc` |WikiToc|

The dictionary values can be one of two types:

- |String|: The name of a function to be executed;
- |Funcref|: A function that will be executed.

The following builtin alternatives can be used:

- fzf (https://github.com/junegunn/fzf.vim):
- `wiki#fzf#pages()`
- `wiki#fzf#tags()`
- `wiki#fzf#toc()`
- Note: This has a few additional options for more fine tuned control:
- |g:wiki_fzf_pages_opts|
- |g:wiki_fzf_tags_opts|
- |g:wiki_fzf_pages_force_create_key|
- Lua backend that uses |vim.ui.select| (neovim only!)
- `require("wiki.ui_select").pages()`
- `require("wiki.ui_select").tags()`
- `require("wiki.ui_select").toc()`
- Telescope (https://github.com/nvim-telescope/telescope.nvim)
- `require("wiki.telescope").pages()
- `require("wiki.telescope").tags()
- `require("wiki.telescope").toc()

Default (neovim): >lua

vim.g.wiki_select_method = {
pages = require("wiki.ui_select").pages,
tags = 'equire("wiki.ui_select").tags,
toc = require("wiki.ui_select").toc,
}
<
Default (Vim): >vim

Default (neovim): `'ui_select'`
Default (Vim): `'fzf'`
let g:wiki_select_method = {
\ 'pages': 'wiki#fzf#pages',
\ 'tags': 'wiki#fzf#tags',
\ 'toc': 'wiki#fzf#toc',
\}
<

*g:wiki_index_name*
A string with the index page name for the wiki. The value should not include
Expand Down Expand Up @@ -2265,5 +2304,81 @@ to be unencoded, then you could do something like this: >vim
\ },
\}

Example 3: Customize the `wiki` select method ~
*wiki-advanced-config-3*
We support officially: Telescope, vim.ui.select and fzf.vim. But, if you want,
you can use watever fuzzy finder you want. The following is a setup using
`fzf-lua`: >lua

local fzf = require "fzf-lua"
local fzf_data = require "fzf-lua".config.__resume_data

local function fzf_pages()
fzf.files({
prompt = "Wiki files>",
cwd = vim.g.wiki_root,
actions = {
['default'] = function(selected)
local note = selected[1]
if not note then
if fzf_data.last_query then
note = fzf_data.last_query
end
end
vim.fn["wiki#page#open"](note)
end,
}
})
end

local function fzf_tags()
local tags_with_locations = vim.fn["wiki#tags#get_all"]()
local root = vim.fn["wiki#get_root"]()
local items = {}
for tag, locations in pairs(tags_with_locations) do
for _, loc in pairs(locations) do
local path = vim.fn["wiki#paths#relative"](loc[1], root)
local str = string.format("%s:%d:%s", tag, loc[2], path)
table.insert(items, str)
end
end
fzf.fzf_exec(items, {
actions = {
['default'] = function(selected)
local note = vim.split(selected[1], ':')[3]
if note then
vim.fn["wiki#page#open"](note)
end
end
}
})
end

local function fzf_toc()
local toc = vim.fn["wiki#toc#gather_entries"]()
local items = {}
for _, hd in pairs(toc) do
local indent = vim.fn["repeat"](".", hd.level - 1)
local line = indent .. hd.header
table.insert(items, string.format("%d:%s", hd.lnum, line))
end
fzf.fzf_exec(items, {
actions = {
['default'] = function(selected)
local ln = vim.split(selected[1], ':')[1]
if ln then
vim.fn.execute(ln)
end
end
}
})
end

vim.g.wiki_select_method = {
pages = fzf_pages,
tags = fzf_tags,
toc = fzf_toc,
}

==============================================================================
vim:tw=78:ts=8:ft=help:norl:fdm=marker:cole=2:
120 changes: 120 additions & 0 deletions lua/wiki/telescope.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
local conf = require("telescope.config").values
local pickers = require "telescope.pickers"
local finders = require "telescope.finders"
local builtin = require "telescope.builtin"
local actions = require "telescope.actions"
local action_state = require "telescope.actions.state"
local M = {}

function M.pages(opts)
builtin.find_files(vim.tbl_deep_extend("force", {
prompt_title = "Wiki files",
cwd = vim.g.wiki_root,
file_ignore_patterns = {
"%.stversions/",
"%.git/",
},
path_display = function(_, path)
local name = path:match "(.+)%.[^.]+$"
return name or path
end,
attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace_if(function()
return action_state.get_selected_entry() == nil
end, function()
actions.close(prompt_bufnr)
local new_name = action_state.get_current_line()
if vim.fn.empty(new_name) ~= 1 then
return
end
vim.fn["wiki#page#open"](new_name)
end)
return true
end,
}, opts or {}))
end

function M.tags(opts)
local tags_with_locations = vim.fn["wiki#tags#get_all"]()

local length = 0
for tag, _ in pairs(tags_with_locations) do
if #tag > length then
length = #tag
end
end

local root = vim.fn["wiki#get_root"]()
local items = {}
for tag, locations in pairs(tags_with_locations) do
for _, loc in pairs(locations) do
local path_rel = vim.fn["wiki#paths#relative"](loc[1], root)
local str = string.format("%-" .. length .. "s %s:%s", tag, path_rel, loc[2])
table.insert(items, { str, loc[1], loc[2] })
end
end

opts = opts or {}

local telescope_opts = vim.tbl_deep_extend("force", {
prompt_title = "Wiki Tags",
sorter = conf.generic_sorter(opts),
previewer = conf.grep_previewer(opts),
}, opts)

pickers.new(telescope_opts, {
finder = finders.new_table {
results = items,
entry_maker = function(entry)
return {
value = entry[2],
display = entry[1],
ordinal = entry[1],
lnum = entry[3],
}
end
},
}):find()
end

function M.toc(opts)
local toc = vim.fn["wiki#toc#gather_entries"]()

local items = {}
for _, hd in pairs(toc) do
local indent = vim.fn["repeat"](".", hd.level - 1)
local line = indent .. hd.header
table.insert(items, { line, hd.lnum })
end

opts = opts or {}

local telescope_opts = vim.tbl_deep_extend("force", {
prompt_title = "Wiki Toc",
sorter = conf.generic_sorter(opts),
previewer = false,
}, opts)

pickers.new(telescope_opts, {
finder = finders.new_table {
results = items,
entry_maker = function(entry)
return {
value = entry[2],
display = entry[1],
ordinal = entry[1],
lnum = entry[2],
}
end,
attach_mappings = function(prompt_buf, _)
actions.select_default:replace(function()
actions.close(prompt_buf)
local entry = action_state.get_selected_entry()
vim.cmd.execute(entry)
end)
end
},
}):find()
end

return M
4 changes: 2 additions & 2 deletions lua/wiki/init.lua → lua/wiki/ui_select.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local M = {}

function M.get_pages()
function M.pages()
-- wiki#page#get_all returns a list of path pairs, where the first element is
-- the absolute path and the second element is the path relative to wiki
-- root.
Expand All @@ -20,7 +20,7 @@ function M.get_pages()
end)
end

function M.get_tags()
function M.tags()
local tags_with_locations = vim.fn["wiki#tags#get_all"]()

local length = 0
Expand Down
28 changes: 18 additions & 10 deletions plugin/wiki.vim
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,19 @@ call wiki#init#option('wiki_template_month_names', [
\])
call wiki#init#option('wiki_root', '')
if has('nvim')
call wiki#init#option('wiki_select_method', 'ui_select')
lua << EOF
vim.fn['wiki#init#option']('wiki_select_method', {
pages = require("wiki.ui_select").pages,
tags = require("wiki.ui_select").tags,
toc = require("wiki.ui_select").toc,
})
EOF
else
call wiki#init#option('wiki_select_method', 'fzf')
call wiki#init#option('wiki_select_method', {
\ 'pages': 'wiki#wiki#fzf#pages',
\ 'tags': 'wiki#wiki#fzf#tags',
\ 'toc': 'wiki#wiki#fzf#toc',
\})
endif
call wiki#init#option('wiki_tag_list', { 'output' : 'loclist' })
call wiki#init#option('wiki_tag_search', { 'output' : 'loclist' })
Expand Down Expand Up @@ -130,14 +140,8 @@ command! WikiIndex call wiki#goto_index()
command! -nargs=? WikiOpen call wiki#page#open(<f-args>)
command! WikiReload call wiki#reload()
command! WikiJournal call wiki#journal#open()
if has('nvim') && g:wiki_select_method == 'ui_select'
command! WikiPages lua require('wiki').get_pages()
command! WikiTags lua require('wiki').get_tags()
else
command! WikiPages call wiki#fzf#pages()
command! WikiTags call wiki#fzf#tags()
endif

command! WikiPages call g:wiki_select_method.pages()
command! WikiTags call g:wiki_select_method.tags()
" Initialize mappings
nnoremap <silent> <plug>(wiki-index) :WikiIndex<cr>
nnoremap <silent> <plug>(wiki-open) :WikiOpen<cr>
Expand All @@ -157,6 +161,10 @@ let s:mappings = index(['all', 'global'], g:wiki_mappings_use_defaults) >= 0
call extend(s:mappings, get(g:, 'wiki_mappings_global', {}))
call wiki#init#apply_mappings_from_dict(s:mappings, '')

if type(g:wiki_select_method) == 1
echoerr 'Deprecation notice: wiki_select_method wants different arguments, see :h wiki_select_method to learn more'
endif

" Enable on desired filetypes
augroup wiki
autocmd!
Expand Down