|
1 | 1 | local M = {}
|
2 | 2 |
|
3 |
| --- Returns shorten path |
4 |
| -local function filter(path) |
5 |
| - local root = vim.g.wiki_root .. "/" |
6 |
| - return path:gsub(root, "") |
7 |
| -end |
8 |
| - |
9 |
| --- Returns full path |
10 |
| -local function unfilter(path) |
11 |
| - local root = vim.g.wiki_root .. "/" |
12 |
| - return root .. path |
13 |
| -end |
14 |
| - |
15 | 3 | function M.get_pages()
|
16 |
| - local res = {} |
17 |
| - local pages = vim.fn["wiki#page#get_all"]() |
18 |
| - for _, p in pairs(pages) do |
19 |
| - local page = filter(p[1]) |
20 |
| - table.insert(res, page) |
21 |
| - end |
22 |
| - vim.ui.select(res, { prompt = "WikiPages> " }, function(f) |
23 |
| - f = unfilter(f) |
24 |
| - vim.cmd("edit " .. f) |
| 4 | + -- wiki#page#get_all returns a list of path pairs, where the first element is |
| 5 | + -- the absolute path and the second element is the path relative to wiki |
| 6 | + -- root. |
| 7 | + vim.ui.select(vim.fn["wiki#page#get_all"](), { |
| 8 | + prompt = "WikiPages> ", |
| 9 | + format_item = function(item) |
| 10 | + return item[2] |
| 11 | + end, |
| 12 | + }, function(item) |
| 13 | + if item then |
| 14 | + vim.cmd.edit(item[1]) |
| 15 | + end |
25 | 16 | end)
|
26 | 17 | end
|
27 | 18 |
|
28 | 19 | function M.get_tags()
|
29 |
| - local res = {} |
30 |
| - local tags = vim.fn["wiki#tags#get_all"]() |
31 |
| - for key, val in pairs(tags) do |
32 |
| - for _, file in pairs(val) do |
33 |
| - local str = string.format("%s:%s:%s", key, file[2], filter(file[1])) |
34 |
| - table.insert(res, str) |
| 20 | + local tags_with_locations = vim.fn["wiki#tags#get_all"]() |
| 21 | + |
| 22 | + local length = 0 |
| 23 | + for tag, _ in pairs(tags_with_locations) do |
| 24 | + if #tag > length then |
| 25 | + length = #tag |
35 | 26 | end
|
36 | 27 | end
|
37 |
| - vim.ui.select(res, { prompt = "WikiTags> " }, function(t) |
38 |
| - t = unfilter(vim.split(t, ":")[3]) |
39 |
| - vim.cmd("edit " .. t) |
| 28 | + local frmt = "%-" .. length .. "s %s:%s" |
| 29 | + |
| 30 | + local root = vim.fn["wiki#get_root"]() |
| 31 | + local items = {} |
| 32 | + for tag, locations in pairs(tags_with_locations) do |
| 33 | + for _, loc in pairs(locations) do |
| 34 | + local path_rel = vim.fn["wiki#paths#relative"](loc[1], root) |
| 35 | + local str = string.format(frmt, tag, path_rel, loc[2]) |
| 36 | + table.insert(items, { str, loc[1] }) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + vim.ui.select(items, { |
| 41 | + prompt = "WikiTags> ", |
| 42 | + format_item = function(item) |
| 43 | + return item[1] |
| 44 | + end, |
| 45 | + }, function(item) |
| 46 | + if item then |
| 47 | + vim.cmd.edit(item[2]) |
| 48 | + end |
40 | 49 | end)
|
41 |
| - return res |
42 | 50 | end
|
43 | 51 |
|
44 | 52 | function M.toc()
|
45 |
| - local res = {} |
46 | 53 | local toc = vim.fn["wiki#toc#gather_entries"]()
|
| 54 | + |
| 55 | + local items = {} |
47 | 56 | for _, hd in pairs(toc) do
|
48 | 57 | local indent = vim.fn["repeat"](".", hd.level - 1)
|
49 | 58 | local line = hd.lnum .. "|" .. indent .. hd.header
|
50 |
| - table.insert(res, line) |
| 59 | + table.insert(items, line) |
51 | 60 | end
|
52 |
| - vim.ui.select(res, { prompt = "WikiToc> " }, function(t) |
53 |
| - t = vim.split(t, "|")[1] |
54 |
| - vim.cmd("execute " .. t) |
| 61 | + |
| 62 | + vim.ui.select(items, { prompt = "WikiToc> " }, function(item) |
| 63 | + if item then |
| 64 | + item = vim.split(item, "|")[1] |
| 65 | + vim.cmd.execute(item) |
| 66 | + end |
55 | 67 | end)
|
56 | 68 | end
|
57 | 69 |
|
|
0 commit comments