Skip to content

Commit

Permalink
show_tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene Schallner committed Dec 11, 2021
1 parent 1c2bece commit 9e97e8a
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 20 deletions.
3 changes: 2 additions & 1 deletion BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

- [ ] maybe a virtual line in the 1st line that shows number of backlinks and maybe other interesting stuff
- or put it as an extmark at the end of the first line, meh.
- [ ] better support for #tags [see also this comment](https://github.com/renerocksai/telekasten.nvim/discussions/23#discussioncomment-1754511)
- [ ] some cool buffer showing backlinks (and stuff?) [see also this comment](https://github.com/renerocksai/telekasten.nvim/discussions/23#discussioncomment-1754511)
- maybe another one where we dot-render a graph of linked notes and
display it via vimg from telescope_media_files or sth similar
Expand All @@ -14,6 +13,8 @@
- [ ] yt video

## Dones
- [x] better support for #tags [see also this comment](https://github.com/renerocksai/telekasten.nvim/discussions/23#discussioncomment-1754511)
- at least we have a tag picker now
- [x] follow external URLs
- [x] telekasten filetype
- [x] Telekasten command with completion, command palette
Expand Down
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mixing it with a journal, based on [telescope.nvim](https://github.com/nvim-tele

#### Highlights:

- Find notes by name, daily and weekly notes by date
- Find notes by name, #tag, and daily, weekly notes by date
- search within all notes
- place and follow links to your notes or create new ones, with templates
- current daily and weekly notes are (optionally) created if not present when searching for dailies or weeklies
Expand Down Expand Up @@ -247,6 +247,10 @@ require('telekasten').setup({
-- command palette theme: dropdown (window) or ivy (bottom panel)
command_palette_theme = "ivy",

-- tag list theme:
-- get_cursor: small tag list at cursor; ivy and dropdown like above
show_tags_theme = "ivy",

})
END
```
Expand Down Expand Up @@ -285,6 +289,10 @@ END
| `command_palette_theme` | theme (layout) of the command palette| ivy |
| | - `ivy` (default): bottom panel overlay | |
| | - `dropdown`: floating popup window ||
| `show_tags_theme` | theme (layout) for the tag list| ivy |
| | - `ivy` (default): bottom panel overlay | |
| | - `dropdown`: floating popup window ||
| | - `get_cursor`: floating popup window at cursor position ||


The calendar support has its own options, contained in `calendar_opts`:
Expand Down Expand Up @@ -372,6 +380,7 @@ the list for a more detailed description:

- `panel` : brings up the [command palette](#21-telekasten-command-palette)
- `find_notes` : Find notes by title (filename)
- `show_tags` : Search through all tags
- `find_daily_notes` : Find daily notes by title (date)
- `search_notes` : Search (grep) in all notes
- `insert_link` : Insert a link to a note
Expand Down Expand Up @@ -484,6 +493,7 @@ The plugin defines the following functions:
- **note**: this requires the `telescope-media-files.nvim` plugin to be installed.
- `setup(opts)`: used for configuring paths, file extension, etc.
- `panel()` : brings up the command palette
- `show_tags()` : brings up the tag list. From there you can select a tag to search for tagged notes - or yank or insert the tag

To use one of the functions above, just run them with the `:lua ...` command.

Expand Down Expand Up @@ -719,15 +729,18 @@ nnoremap <leader>zF :lua require('telekasten').find_friends()<CR>
nnoremap <leader>zI :lua require('telekasten').insert_img_link({ i=true })<CR>
nnoremap <leader>zp :lua require('telekasten').preview_img()<CR>
nnoremap <leader>zm :lua require('telekasten').browse_media()<CR>
nnoremap <leader>za :lua require('telekasten').show_tags()<CR>
nnoremap <leader># :lua require('telekasten').show_tags()<CR>
" on hesitation, bring up the panel
nnoremap <leader>z :lua require('telekasten').panel()<CR>
" we could define [[ in **insert mode** to call insert link
" inoremap [[ <ESC>:lua require('telekasten').insert_link()<CR>
" inoremap [[ <cmd>:lua require('telekasten').insert_link()<CR>
" alternatively: leader [
inoremap <leader>[ <ESC>:lua require('telekasten').insert_link({ i=true })<CR>
inoremap <leader>zt <ESC>:lua require('telekasten').toggle_todo({ i=true })<CR>
inoremap <leader>[ <cmd>:lua require('telekasten').insert_link({ i=true })<CR>
inoremap <leader>zt <cmd>:lua require('telekasten').toggle_todo({ i=true })<CR>
inoremap <leader># <cmd>lua require('telekasten').show_tags({i = true})<cr>
" ----- the following are for syntax-coloring [[links]] and ==highlighted text==
" ----- (see the section about coloring in README.md)
Expand Down
9 changes: 9 additions & 0 deletions doc/telekasten.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ telekasten.setup({opts})
-- tag notation: '#tag', ':tag:', 'yaml-bare'
tag_notation = "#tag",
-- command palette theme: dropdown (window) or ivy (bottom panel)
command_palette_theme = "ivy",
-- tag list theme:
-- get_cursor: small tag list at cursor; ivy and dropdown like above
show_tags_theme = "ivy",
}
<

Expand Down Expand Up @@ -765,12 +772,14 @@ However, here are some suggestions:
nnoremap <leader>zI :lua require('telekasten').insert_img_link({ i=true })<CR>
nnoremap <leader>zp :lua require('telekasten').preview_img()<CR>
nnoremap <leader>zm :lua require('telekasten').browse_media()<CR>
nnoremap <leader># :lua require('telekasten').show_tags()<CR>
" we could define [[ in **insert mode** to call insert link
" inoremap [[ <ESC>:lua require('telekasten').insert_link()<CR>
" alternatively: leader [
inoremap <leader>[ <ESC>:lua require('telekasten').insert_link({ i=true })<CR>
inoremap <leader>zt <ESC>:lua require('telekasten').toggle_todo({ i=true })<CR>
inoremap <leader># <cmd>lua require('telekasten').show_tags({i = true})<cr>
" the following are for syntax-coloring [[links]] and ==highlighted text==
" (see the section about coloring in README.md)
Expand Down
123 changes: 123 additions & 0 deletions lua/taglinks/tagutils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
local Job = require("plenary.job")

local M = {}
local hashtag_re = "(^|\\s)#[a-zA-Z]+[a-zA-Z0-9/\\-_]*"
local colon_re = "(^|\\s):[a-zA-Z]+[a-zA-Z0-9/\\-_]*:"
local yaml_re = "(^|\\s)tags:\\s*\\[([a-zA-Z]+[a-zA-Z0-9/\\-_]*(,\\s)*)*]"

local function command_find_all_tags(opts)
opts = opts or {}
opts.cwd = opts.cwd or "."
local re = hashtag_re

if opts.tag_notation == ":tag:" then
re = colon_re
end

if opts.tag_notation == "yaml-bare" then
re = yaml_re
end

return "rg", { "--vimgrep", "-o", re, "--", opts.cwd }
end

local function trim(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

local function insert_tag(tbl, tag, entry)
entry.t = tag
if tbl[tag] == nil then
tbl[tag] = { entry }
else
tbl[tag][#tbl[tag] + 1] = entry
end
end

local function split(line, sep, n)
local startpos = 0
local endpos
local ret = {}
for _ = 1, n - 1 do
endpos = line:find(sep, startpos + 1)
ret[#ret + 1] = line:sub(startpos + 1, endpos - 1)
startpos = endpos
end
-- now the remainder
ret[n] = line:sub(startpos + 1)
return ret
end

local function yaml_to_tags(line, entry, ret)
local _, startpos = line:find("tags%s*:%s*%[")
local global_end = line:find("%]")

line = line:sub(startpos + 1, global_end)

local i = 1
local j
local prev_i
local tag
while true do
i, j = line:find("%s*.*%s*,", i)
if i == nil then
tag = line:sub(prev_i)
tag = tag:gsub("%s*(.*)%s*", "%1")
else
tag = line:sub(i, j)
tag = tag:gsub("%s*(.*)%s*,", "%1")
end

local new_entry = {}
new_entry.t = tag
new_entry.l = entry.l
new_entry.fn = entry.fn
new_entry.c = startpos + (i or prev_i)
insert_tag(ret, tag, new_entry)
if i == nil then
break
end
i = j + 1
prev_i = i
end
end

local function parse_entry(opts, line, ret)
local s = split(line, ":", 4)
local fn, l, c, t = s[1], s[2], s[3], s[4]

t = trim(t)
local entry = { fn = fn, l = l, c = c }

if opts.tag_notation == "yaml-bare" then
yaml_to_tags(t, entry, ret)
elseif opts.tag_notation == ":tag:" then
insert_tag(ret, t, entry)
else
insert_tag(ret, t, entry)
end
end

M.do_find_all_tags = function(opts)
local cmd, args = command_find_all_tags(opts)
-- print(cmd .. " " .. vim.inspect(args))
local ret = {}
local _ = Job
:new({
command = cmd,
args = args,
enable_recording = true,
on_exit = function(j, return_val)
for _, line in pairs(j:result()) do
parse_entry(opts, line, ret)
end
end,
on_stderr = function(err, data, _)
print("error: " .. tostring(err) .. "data: " .. data)
end,
})
:sync()
-- print("final results: " .. vim.inspect(ret))
return ret
end
return M
Loading

0 comments on commit 9e97e8a

Please sign in to comment.