From b62784b66d30639fee0e3513dda9199a9569ab7c Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Fri, 8 Dec 2023 21:36:38 -0800 Subject: [PATCH] Make z.lua try treating `-` as a normal character if there are no results Previously, you needed to set `_ZL_HYPHEN=1` to treat `-` as a normal character. Otherwise, it was treated as a Lua regexp special character, see https://www.lua.org/pil/20.2.html. Note that it is not super-useful to treat `-` as a special character; it is almost the same as `*` and the difference is not very useful in the context of fuzzy matching. Now, if `_ZL_HYPHEN` is not set, z.lua first tries to treat it as a regexp character. If there are no results (which is likely if the user does not know it's a special character), z.lua tries again, treating `-` as a normal character this time. If `_ZL_HYPHEN=0` or `_ZL_HYPHEN=1`, z.lua will always treat `-` as either a regex symbol or as a normal character (respectively). Hopefully, this will make the FAQ at https://github.com/skywind3000/z.lua/wiki/FAQ#how-to-input-a-hyphen---in-the-keyword- unnecessary. It took me weeks to look into the question of why `z home-manager` refused to work and to find that FAQ. I only tested this briefly, but it seems to work. --- README.md | 7 ++++++- z.lua | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 301fa76..639b021 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,12 @@ z -b foo bar # replace foo with bar in cwd and cd there - set `$_ZL_ECHO` to 1 to display new directory name after cd. - set `$_ZL_MATCH_MODE` to 1 to enable enhanced matching. - set `$_ZL_NO_CHECK` to 1 to disable path validation, use `z --purge` to clean -- set `$_ZL_HYPHEN` to 1 to treat hyphon (-) as a normal character not a lua regexp keyword. +- set `$_ZL_HYPHEN` to 0 to treat a hyphen (`-`) as a + [lua regexp special character](https://www.lua.org/pil/20.2.html), + set `$_ZL_HYPHEN` to 1 to treat a hyphen as a normal character. + If `$_ZL_HYPHEN` is not set or if it is set to `auto`, z.lua tries to treat `-` + as a lua regexp special character first. If there are no matches, z.lua tries + again, this time treating `-` as a normal character. - set `$_ZL_CLINK_PROMPT_PRIORITY` change clink prompt register priority (default 99). ## Aging diff --git a/z.lua b/z.lua index 1897a21..224c4da 100755 --- a/z.lua +++ b/z.lua @@ -127,7 +127,7 @@ Z_CMD = 'z' Z_MATCHMODE = 0 Z_MATCHNAME = false Z_SKIPPWD = false -Z_HYPHEN = false +Z_HYPHEN = "auto" os.LOG_NAME = os.getenv('_ZL_LOG_NAME') @@ -1292,13 +1292,16 @@ end ----------------------------------------------------------------------- -- select matched pathnames ----------------------------------------------------------------------- -function data_select(M, patterns, matchlast) +-- z_hyphen must be `true`, `false``, or `"auto"`. +function data_select(M, patterns, matchlast, z_hyphen) local N = {} local i = 1 local pats = {} + local hyphens = false for i = 1, #patterns do local p = patterns[i] - if Z_HYPHEN then + hyphens = hyphens or string.match(p, "%-") + if z_hyphen == true then p = p:gsub('-', '%%-') end table.insert(pats, case_insensitive_pattern(p)) @@ -1309,6 +1312,9 @@ function data_select(M, patterns, matchlast) table.insert(N, item) end end + if (hyphens and z_hyphen == "auto" and #N == 0) then + N = data_select(M, patterns, matchlast, true) + end return N end @@ -1458,10 +1464,10 @@ function z_match(patterns, method, subdir) method = method ~= nil and method or 'frecent' subdir = subdir ~= nil and subdir or false local M = data_load(DATA_FILE) - M = data_select(M, patterns, false) + M = data_select(M, patterns, false, Z_HYPHEN) M = data_filter(M) if Z_MATCHNAME then - local N = data_select(M, patterns, true) + local N = data_select(M, patterns, true, Z_HYPHEN) N = data_filter(N) if #N > 0 then M = N @@ -2067,10 +2073,13 @@ function z_init() Z_SKIPPWD = true end end + assert(Z_HYPHEN == "auto", "Z_HYPHEN initialized to an unexpected value") if _zl_hyphen ~= nil then local m = string.lower(_zl_hyphen) if (m == '1' or m == 'yes' or m == 'true' or m == 't') then Z_HYPHEN = true + elseif (m == '0' or m == 'no' or m == 'false' or m == 'f') then + Z_HYPHEN = false end end end