Skip to content

Commit 9921340

Browse files
authored
Merge pull request #196 from ilyagr/hyphens
Make z.lua try treating `-` as a normal character if there are no results
2 parents 7f2bfcb + b62784b commit 9921340

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,12 @@ z -b foo bar # replace foo with bar in cwd and cd there
160160
- set `$_ZL_ECHO` to 1 to display new directory name after cd.
161161
- set `$_ZL_MATCH_MODE` to 1 to enable enhanced matching.
162162
- set `$_ZL_NO_CHECK` to 1 to disable path validation, use `z --purge` to clean
163-
- set `$_ZL_HYPHEN` to 1 to treat hyphon (-) as a normal character not a lua regexp keyword.
163+
- set `$_ZL_HYPHEN` to 0 to treat a hyphen (`-`) as a
164+
[lua regexp special character](https://www.lua.org/pil/20.2.html),
165+
set `$_ZL_HYPHEN` to 1 to treat a hyphen as a normal character.
166+
If `$_ZL_HYPHEN` is not set or if it is set to `auto`, z.lua tries to treat `-`
167+
as a lua regexp special character first. If there are no matches, z.lua tries
168+
again, this time treating `-` as a normal character.
164169
- set `$_ZL_CLINK_PROMPT_PRIORITY` change clink prompt register priority (default 99).
165170

166171
## Aging

z.lua

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Z_CMD = 'z'
127127
Z_MATCHMODE = 0
128128
Z_MATCHNAME = false
129129
Z_SKIPPWD = false
130-
Z_HYPHEN = false
130+
Z_HYPHEN = "auto"
131131

132132
os.LOG_NAME = os.getenv('_ZL_LOG_NAME')
133133

@@ -1292,13 +1292,16 @@ end
12921292
-----------------------------------------------------------------------
12931293
-- select matched pathnames
12941294
-----------------------------------------------------------------------
1295-
function data_select(M, patterns, matchlast)
1295+
-- z_hyphen must be `true`, `false``, or `"auto"`.
1296+
function data_select(M, patterns, matchlast, z_hyphen)
12961297
local N = {}
12971298
local i = 1
12981299
local pats = {}
1300+
local hyphens = false
12991301
for i = 1, #patterns do
13001302
local p = patterns[i]
1301-
if Z_HYPHEN then
1303+
hyphens = hyphens or string.match(p, "%-")
1304+
if z_hyphen == true then
13021305
p = p:gsub('-', '%%-')
13031306
end
13041307
table.insert(pats, case_insensitive_pattern(p))
@@ -1309,6 +1312,9 @@ function data_select(M, patterns, matchlast)
13091312
table.insert(N, item)
13101313
end
13111314
end
1315+
if (hyphens and z_hyphen == "auto" and #N == 0) then
1316+
N = data_select(M, patterns, matchlast, true)
1317+
end
13121318
return N
13131319
end
13141320

@@ -1458,10 +1464,10 @@ function z_match(patterns, method, subdir)
14581464
method = method ~= nil and method or 'frecent'
14591465
subdir = subdir ~= nil and subdir or false
14601466
local M = data_load(DATA_FILE)
1461-
M = data_select(M, patterns, false)
1467+
M = data_select(M, patterns, false, Z_HYPHEN)
14621468
M = data_filter(M)
14631469
if Z_MATCHNAME then
1464-
local N = data_select(M, patterns, true)
1470+
local N = data_select(M, patterns, true, Z_HYPHEN)
14651471
N = data_filter(N)
14661472
if #N > 0 then
14671473
M = N
@@ -2067,10 +2073,13 @@ function z_init()
20672073
Z_SKIPPWD = true
20682074
end
20692075
end
2076+
assert(Z_HYPHEN == "auto", "Z_HYPHEN initialized to an unexpected value")
20702077
if _zl_hyphen ~= nil then
20712078
local m = string.lower(_zl_hyphen)
20722079
if (m == '1' or m == 'yes' or m == 'true' or m == 't') then
20732080
Z_HYPHEN = true
2081+
elseif (m == '0' or m == 'no' or m == 'false' or m == 'f') then
2082+
Z_HYPHEN = false
20742083
end
20752084
end
20762085
end

0 commit comments

Comments
 (0)