From 994a44999fea6167b1b2c67f5eb2d9dc2b01037a Mon Sep 17 00:00:00 2001 From: lambtho Date: Sun, 26 Jun 2022 17:49:19 +0000 Subject: [PATCH] Fix pcre2 issue with ripgrep (closes #145) (#148) * fix(tags): fix rg command if PCRE not available (closes #145) Some linux distro do not compile ripgrep using the --pcre2 flag. This commit checks if the --pcre2 flag is available and then tweaks the ripgrep command accordingly. At the moment, it will therefore not be possible to filter-out hexademical color codes from #tags for people that do not have the --pcre2 flag available. This will be documented in a later commit. **Important**: the detection of the PCRE availability is done by trying a simple rg search using _os.execute_. This function interface was changed in Lua 5.2, which means it will break when Neovim will update to Lua >5.1. The fix is already there in commented form. Fixes #145 * refact: cleanup * doc(tag): add note about rg --pcre2 * doc(tag): add note about rg --pcre2 in vim help --- README.md | 10 ++++++++++ doc/telekasten.txt | 17 ++++++++++++++--- lua/taglinks/tagutils.lua | 24 +++++++++++++++++++++--- lua/telekasten.lua | 11 +++++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 01fbde4..f85f291 100644 --- a/README.md +++ b/README.md @@ -704,6 +704,16 @@ The only symbols allowed are: Numbers are allowed in tags, as long as a tag is not purely numeric. For example, #1984 is not a valid tag, but `#y1984` is. +**Note**: When using the `#tag` notation, telekasten will try to differentiate +an actual tag from an hexadecimal code (e.g. `#FF0000`) to prevent false +matches. This is achieved by using the `--pcre2` flag of `ripgrep`. However, +some linux distribution (mostly the ones based on Debian) do not compile ripgrep +with this flag by default, making it impossible to use. If this is the case, the +tag functions of telekasten will not be able to differentiate color codes from +actual tags and will return everything. A workaround is to either use the +`:tag:` notation or to recompile ripgrep locally with the appropriate flag (see +issues # 115 and #145). + **Note**: For proper highlighting, the `auto_set_filetype` option is set to `true` by default. This automatically switches the filetype of opened notes from `markdown` to `telekasten`, and also registers the syntax with telescope previewers for `.md` files. diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 1af5621..c8077fa 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -157,12 +157,12 @@ telekasten.setup({opts}) Default: '~/zettelkasten' ---------------------- - Note to Windows users: + Note to Windows users: - If you must use Windows, avoid using the drive path. You may use it, + If you must use Windows, avoid using the drive path. You may use it, but telekasten will ignore the drive name and create your main markdown / zettelkasten folder in the same drive that neovim executes from. This - is because of how Telescope handles files paths. Windows paths must be + is because of how Telescope handles files paths. Windows paths must be stripped of the drive name or else the path will not be read correctly. This is done automatically but is advised that you avoid adding it. @@ -755,6 +755,17 @@ Note:~ default. This automatically sets the to `telekasten`, and also registers the syntax with telescope previewers for `.md` files. +Note: + When using the `#tag` notation, telekasten will try to differentiate an + actual tag from an hexadecimal code (e.g. `#FF0000`) to prevent false + matches. This is achieved by using the `--pcre2` flag of `ripgrep`. However, + some linux distribution (mostly the ones based on Debian) do not compile + ripgrep with this flag by default, making it impossible to use. If this is + the case, the tag functions of telekasten will not be able to differentiate + color codes from actual tags and will return everything. A workaround is to + either use the `:tag:` notation or to recompile ripgrep locally with the + appropriate flag (see issues # 115 and #145). + -------------------------------------------------------------------------------- Section 3.3: Templates *telekasten.templates* diff --git a/lua/taglinks/tagutils.lua b/lua/taglinks/tagutils.lua index 54a1f3f..95013da 100644 --- a/lua/taglinks/tagutils.lua +++ b/lua/taglinks/tagutils.lua @@ -1,7 +1,9 @@ local Job = require("plenary.job") local M = {} -local hashtag_re = +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 colon_re = "(^|\\s):[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*:" local yaml_re = @@ -11,6 +13,7 @@ local function command_find_all_tags(opts) opts = opts or {} opts.cwd = opts.cwd or "." opts.templateDir = opts.templateDir or "" + opts.rg_pcre = opts.rg_pcre or false -- do not list tags in the template directory local globArg = "" @@ -28,8 +31,20 @@ local function command_find_all_tags(opts) re = yaml_re end - return "rg", - { + local rg_args = { + "--vimgrep", + globArg, + "-o", + re, + "--", + opts.cwd, + } + + -- PCRE engine allows to remove hex color codes from #hastags + if opts.rg_pcre then + re = hashtag_re_pcre + + rg_args = { "--vimgrep", "--pcre2", globArg, @@ -38,6 +53,9 @@ local function command_find_all_tags(opts) "--", opts.cwd, } + end + + return "rg", rg_args end -- strips away leading ' or " , then trims whitespace diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 3f4acd7..a945f32 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -2734,6 +2734,7 @@ local function FindAllTags(opts) opts.tag_notation = M.Cfg.tag_notation local templateDir = Path:new(M.Cfg.templates):make_relative(M.Cfg.home) opts.templateDir = templateDir + opts.rg_pcre = M.Cfg.rg_pcre if not global_dir_check() then return @@ -2913,6 +2914,16 @@ local function Setup(cfg) M.Cfg.dailies = make_config_path_absolute(M.Cfg.dailies) M.Cfg.weeklies = make_config_path_absolute(M.Cfg.weeklies) M.Cfg.templates = make_config_path_absolute(M.Cfg.templates) + + -- Check if ripgrep is compiled with --pcre + -- ! This will need to be fixed when neovim moves to lua >=5.2 by the following: + -- M.Cfg.rg_pcre = os.execute("echo 'hello' | rg --pcr2 hello &> /dev/null") or false + + M.Cfg.rg_pcre = false + local has_pcre = os.execute("echo 'hello' | rg --pcre2 hello &> /dev/null") + if has_pcre == 0 then + M.Cfg.rg_pcre = true + end end M.find_notes = FindNotes