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