From 8a8daa0ea1b288011f2118155b5620ec3cfc4ec6 Mon Sep 17 00:00:00 2001 From: "T. H. Wright" Date: Fri, 19 May 2023 15:04:30 -0400 Subject: [PATCH] Add @tag support (#247) Co-authored-by: T. H. Wright --- README.md | 5 ++-- doc/telekasten.txt | 2 +- lua/telekasten/utils/taglinks.lua | 3 ++ lua/telekasten/utils/tags.lua | 47 +++++++++++++++++++++---------- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 57d95a1..4e52f48 100644 --- a/README.md +++ b/README.md @@ -362,8 +362,9 @@ See the documentation for more details regarding the different types of links Telekasten supports the following tag notations: 1. `#tag` -2. `:tag:` -3. `yaml-bare`: bare tags in a tag collection in the yaml metadata: +2. `@tag` +3. `:tag:` +4. `yaml-bare`: bare tags in a tag collection in the yaml metadata: See the documentation for more details regarding the tag syntax (`:h diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 1b136a6..c956221 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -114,7 +114,7 @@ telekasten.setup({opts}) -- Make syntax available to markdown buffers and telescope previewers install_syntax = true, - -- Tag notation: '#tag', ':tag:', 'yaml-bare' + -- Tag notation: '#tag', '@tag', ':tag:', 'yaml-bare' tag_notation = "#tag", -- When linking to a note in subdir/, create a [[subdir/title]] link diff --git a/lua/telekasten/utils/taglinks.lua b/lua/telekasten/utils/taglinks.lua index 28ea5e6..54aed5c 100644 --- a/lua/telekasten/utils/taglinks.lua +++ b/lua/telekasten/utils/taglinks.lua @@ -52,6 +52,9 @@ M.is_tag_or_link_at = function(line, col, opts) if char == "#" then seen_hashtag = true end + if char == "@" then + seen_hashtag = true + end -- Tags should have a space before #, if not we are likely in a link if char == " " and seen_hashtag and opts.tag_notation == "#tag" then if not cannot_be_tag then diff --git a/lua/telekasten/utils/tags.lua b/lua/telekasten/utils/tags.lua index 6abcf44..faf39e8 100644 --- a/lua/telekasten/utils/tags.lua +++ b/lua/telekasten/utils/tags.lua @@ -5,6 +5,9 @@ local hashtag_re = "(^|\\s|'|\")#[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*" -- PCRE hashtag allows to remove the hex color codes from hastags local hashtag_re_pcre = "(^|\\s|'|\")((?!(#[a-fA-F0-9]{3})(\\W|$)|(#[a-fA-F0-9]{6})(\\W|$))#[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*)" +local atsign_re = "(^|\\s|'|\")@[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*" +local atsign_re_pcre = + "(^|\\s|'|\")((?!(@[a-fA-F0-9]{3})(\\W|$)|(@[a-fA-F0-9]{6})(\\W|$))@[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*)" local colon_re = "(^|\\s):[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*:" local yaml_re = "(^|\\s)tags:\\s*\\[\\s*([a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*(,\\s*)*)*\\s*]" @@ -23,11 +26,11 @@ local function command_find_all_tags(opts) local re = hashtag_re - if opts.tag_notation == ":tag:" then + if opts.tag_notation == "@tag" then + re = atsign_re + elseif opts.tag_notation == ":tag:" then re = colon_re - end - - if opts.tag_notation == "yaml-bare" then + elseif opts.tag_notation == "yaml-bare" then re = yaml_re end @@ -41,18 +44,32 @@ local function command_find_all_tags(opts) } -- PCRE engine allows to remove hex color codes from #hastags - if opts.rg_pcre and (re == hashtag_re) then - re = hashtag_re_pcre + if opts.rg_pcre then + if re == hashtag_re then + re = hashtag_re_pcre - rg_args = { - "--vimgrep", - "--pcre2", - globArg, - "-o", - re, - "--", - opts.cwd, - } + rg_args = { + "--vimgrep", + "--pcre2", + globArg, + "-o", + re, + "--", + opts.cwd, + } + elseif re == atsign_re then + re = atsign_re_pcre + + rg_args = { + "--vimgrep", + "--pcre2", + globArg, + "-o", + re, + "--", + opts.cwd, + } + end end return "rg", rg_args