diff --git a/plugins/z.lua/README.cn.md b/plugins/z.lua/README.cn.md index f092d02..0f47794 100644 --- a/plugins/z.lua/README.cn.md +++ b/plugins/z.lua/README.cn.md @@ -14,7 +14,7 @@ z.lua 是一个快速路径切换工具,它会跟踪你在 shell 下访问过 - 性能比 **z.sh** 快三倍,比 **fasd** / **autojump** 快十倍以上。 - 支持 Posix Shell:bash, zsh, dash, sh, ash, busybox 等等。 -- 支持 Fish Shell,Power Shell 和 Windows cmd。 +- 支持 Fish Shell,Nushell, Power Shell 和 Windows cmd。 - 使用增强匹配算法,更准确的带你去到你想去的地方。 - 低占用,能够仅在当前路径改变时才更新数据库(将 `$_ZL_ADD_ONCE` 设成 1)。 - 交互选择模式,如果有多个匹配结果的话,跳转前允许你进行选择。 @@ -84,6 +84,16 @@ z -b foo # 跳转到父目录中名称以 foo 开头的那一级 但是第二种方法需要记得在 z.lua 位置改变或者 lua 版本升级后需要重新生成。 +- Nushell: + + 在 `env.nu` 中加入如下代码: + + lua /path/to/z.lua --init nushell | save -f ~/.cache/zlua.nu + + 然后在 `config.nu` 中加入如下代码: + + source ~/.cache/zlua.nu + - Power Shell: 在你 Power Shell 的配置文件 `profile.ps1` 中放入下面语句: @@ -112,7 +122,7 @@ z -b foo # 跳转到父目录中名称以 foo 开头的那一级 - 设置 `$_ZL_NO_PROMPT_COMMAND` 为 1 来跳过钩子函数初始化(方便自己处理)。 - 设置 `$_ZL_EXCLUDE_DIRS` 逗号分隔的路径列表,列表内的路径不会被收集。 - 设置 `$_ZL_ADD_ONCE` 为 '1' 时,仅在当前路径 `$PWD` 改变时才更新数据库。 -- 设置 `$_ZL_MAXAGE` 来确定一个数据老化的阀值 (默认为 5000)。 +- 设置 `$_ZL_MAXAGE` 来确定一个数据老化的阈值 (默认为 5000)。 - 设置 `$_ZL_CD` 用来指定你想用的 cd 命令,比如有人用 cd_func 。 - 设置 `$_ZL_ECHO` 为 1 可以在跳转后显示目标路径名称。 - 设置 `$_ZL_MATCH_MODE` 为 1 可以打开 “增强匹配模式”。 @@ -201,7 +211,7 @@ Frecency 是一个由 'recent' 和 'frequency' 组成的合成词,这个术语 cd foo - 因此,在增强匹配算法中,你总可以象 cd 命令一样使用 z 命令,而不必当心目标路径是否被记录过。 + 因此,在增强匹配算法中,你总可以像 cd 命令一样使用 z 命令,而不必当心目标路径是否被记录过。 - 忽略当前路径: @@ -214,7 +224,7 @@ Frecency 是一个由 'recent' 和 'frequency' 组成的合成词,这个术语 我当然可以每次使用`z env gems` 来精确指明,但是每当我输入 `z xxx` 我必然是想进行路径跳转的,而不是呆在原地,所以使用增强匹配模式,即便当前目录是最佳匹配,它也能懂得你想跳转的心思。 -再我最初实现 z.lua 时,只有一个和 z.sh 类似的默认匹配算法,在网友的建议下,我陆续学习了来自 fasd / autojump 中的优秀理念,并加以完善改进,成为如今集三家之长的 “增强匹配算法” ,给它取个昵称,叫做 “更懂你的匹配算法”。 +在我最初实现 z.lua 时,只有一个和 z.sh 类似的默认匹配算法,在网友的建议下,我陆续学习了来自 fasd / autojump 中的优秀理念,并加以完善改进,成为如今集三家之长的 “增强匹配算法” ,给它取个昵称,叫做 “更懂你的匹配算法”。 ## Add once @@ -325,7 +335,13 @@ zsh/fish 的补全系统是比较完善的,使用 `z foo` 就能触发补 eval "$(lua /path/to/z.lua --init bash enhanced once echo fzf)" ``` -然后你在 bash 中,输入部分关键字后按 tab,就能把匹配的路径列出来: +如果你想在 zsh 中使用 fzf 补全,初始化时在 `--init` 后追加 `fzf` 关键字: + +```zsh +eval "$(lua /path/to/z.lua --init zsh enhanced once echo fzf)" +``` + +然后你在 bash/zsh 中,输入部分关键字后按 tab,就能把匹配的路径列出来: ![](images/complete-2.png) @@ -438,15 +454,13 @@ sys 0m0.030s 描述力强,可以更好的实现核心功能,同时速度更快,纯 shell 开发的话,太多语句是通过子进程 shell 的模式运行,所以性能很差,而 Python 开发的话启动速度又太慢,我在 Cygwin/msys 下用 z.sh 都觉得很卡,autojump/fasd 卡到不能用。 -最关键的一点,Lua 速度很快 200 KB 的可执行程序,启动速度是 python 的 3倍,perl 的 2 倍,很多命令行工具 go/rust 写成,动不动就 2MB / 3MB,他们都还没有完成加载,lua 脚本可能都运行完了。 +最关键的一点,Lua 速度很快 200 KB 的可执行程序,启动速度是 python 的 3 倍,perl 的 2 倍,很多命令行工具 go/rust 写成,动不动就 2MB / 3MB,他们都还没有完成加载,lua 脚本可能都运行完了。 ## Credit -Releated projects: - -- [rupa/z](https://github.com/rupa/z): origin z.sh implementation -- [JannesMeyer/z.ps](https://github.com/JannesMeyer/z.ps): z for powershell +我的推特:https://x.com/skywind3000 +个人博客: https://skywind.me/blog ## License diff --git a/plugins/z.lua/README.md b/plugins/z.lua/README.md index c4e7b6e..7143e3e 100644 --- a/plugins/z.lua/README.md +++ b/plugins/z.lua/README.md @@ -28,7 +28,7 @@ From people using z.lua: - **10x** times faster than **fasd** and **autojump**, **3x** times faster than **z.sh**. - Gain the ultimate speed with an optional [native module](https://github.com/skywind3000/czmod) written in C. - Available for **posix shells**: bash, zsh, dash, sh, ash, ksh, busybox and etc. -- Available for Fish Shell, Power Shell and Windows cmd. +- Available for Fish Shell, Nushell, Power Shell and Windows cmd. - [Enhanced matching algorithm](#enhanced-matching) takes you to where ever you want precisely. - Allow updating database only if `$PWD` changed with "$_ZL_ADD_ONCE" set to 1. - Interactive selection enables you to choose where to go before cd. @@ -78,6 +78,12 @@ z -b foo bar # replace foo with bar in cwd and cd there eval "$(lua /path/to/z.lua --init bash enhanced once fzf)" + NixOS users using home-manager can add this to their user profile: + + programs.z-lua.enable = true; + programs.z-lua.enableBashIntegration = true; + programs.z-lua.options = [ "enhanced" "once" "fzf" ]; # modify as needed + **NOTE**: For wsl-1 users, `lua-filesystem` must be installed: sudo apt-get install lua-filesystem @@ -90,7 +96,13 @@ z -b foo bar # replace foo with bar in cwd and cd there eval "$(lua /path/to/z.lua --init zsh)" - Options like "enhanced" and "once" can be used after `--init` too. It can also be initialized from "skywind3000/z.lua" with your zsh plugin managers (antigen / oh-my-zsh). + Options like "enhanced", "once" and "fzf" can be used after `--init` too. It can also be initialized from "skywind3000/z.lua" with your zsh plugin managers (antigen / oh-my-zsh). + + NixOS users using home-manager can add this to their user profile: + + programs.z-lua.enable = true; + programs.z-lua.enableZshIntegration = true; + programs.z-lua.options = [ "enhanced" "once" "fzf" ]; # modify as needed **NOTE**: for wsl-1 users, `lua-filesystem` must be installed. @@ -118,6 +130,24 @@ z -b foo bar # replace foo with bar in cwd and cd there into the same file. + NixOS users using home-manager can add this to their user profile: + + programs.z-lua.enable = true; + programs.z-lua.enableFishIntegration = true; + programs.z-lua.options = [ "enhanced" "once" "fzf" ]; # modify as needed + +- Nushell + + Put something like this in your `env.nu`: + + lua /path/to/z.lua --init nushell | save -f ~/.cache/zlua.nu + + Then put something like this in your `config.nu`: + + source ~/.cache/zlua.nu + + Note: Only Nushell v0.96+ is supported + - Power Shell: > ⚠️ **WARNING**: users of [Starship Prompt](https://starship.rs/) should add the following command *after* `starship init`. @@ -160,7 +190,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 @@ -380,6 +415,12 @@ Bash is not as powerful as zsh/fish, so we introduced fzf-completion for bash, i eval "$(lua /path/to/z.lua --init bash enhanced once echo fzf)" ``` +If you want use fzf completion in zsh, initalize your z.lua and append `fzf` keyword after `--init`: + +```zsh +eval "$(lua /path/to/z.lua --init zsh enhanced once echo fzf)" +``` + Then press `` after `z xxx`: ![](images/complete-2.png) @@ -576,6 +617,7 @@ This project needs help for the tasks below: - Thanks to [@manhong2112](https://github.com/manhong2112) for Power Shell porting. - Thanks to [@BarbUk](https://github.com/BarbUk) for fzf completion in Bash. - Thanks to [@barlik](https://github.com/barlik) for many improvements. +- Thanks to [@brglng](https://github.com/brglng) for nushell porting. And many others. diff --git a/plugins/z.lua/z.cmd b/plugins/z.lua/z.cmd index 8320456..6b0a621 100644 --- a/plugins/z.lua/z.cmd +++ b/plugins/z.lua/z.cmd @@ -1,10 +1,8 @@ @echo off setlocal EnableDelayedExpansion -set "HomeDir=%~dp0" -set "PathSave=%PATH%" set "LuaExe=lua" -set "LuaScript=%HomeDir%z.lua" +set "LuaScript=%~dp0z.lua" set "MatchType=-n" set "StrictSub=-n" set "RunMode=-n" @@ -91,7 +89,9 @@ if /i "%1"=="--purge" ( :check if /i "%1"=="" ( - set "RunMode=-l" + if /i "%InterMode%"=="" ( + set "RunMode=-l" + ) ) for /f "delims=" %%i in ('cd') do set "PWD=%%i" diff --git a/plugins/z.lua/z.lua b/plugins/z.lua/z.lua old mode 100644 new mode 100755 index 9a1d733..37d7ad0 --- a/plugins/z.lua/z.lua +++ b/plugins/z.lua/z.lua @@ -4,7 +4,7 @@ -- z.lua - a cd command that learns, by skywind 2018-2022 -- Licensed under MIT license. -- --- Version 1.8.17, Last Modified: 2022/09/29 15:47 +-- Version 1.8.25, Last Modified: 2026/03/09 21:15:46 -- -- * 10x faster than fasd and autojump, 3x faster than z.sh -- * available for posix shells: bash, zsh, sh, ash, dash, busybox @@ -127,7 +127,8 @@ Z_CMD = 'z' Z_MATCHMODE = 0 Z_MATCHNAME = false Z_SKIPPWD = false -Z_HYPHEN = false +Z_HYPHEN = "auto" +Z_DATA_SEPARATOR = "|" os.LOG_NAME = os.getenv('_ZL_LOG_NAME') @@ -234,7 +235,8 @@ end function dump(o) if type(o) == 'table' then local s = '{ ' - for k,v in pairs(o) do + for key, v in pairs(o) do + local k = key if type(k) ~= 'number' then k = '"'..k..'"' end s = s .. '['..k..'] = ' .. dump(v) .. ',' end @@ -882,6 +884,7 @@ function os.interpreter() local lua = os.argv[-1] if lua == nil then io.stderr:write("cannot get executable name, recompiled your lua\n") + return nil end if os.path.single(lua) then local path = os.path.which(lua) @@ -1061,6 +1064,26 @@ function path_case_insensitive() end +----------------------------------------------------------------------- +-- Read a line of the database and return a list of the 3 fields in it +----------------------------------------------------------------------- +function read_data_line(line) + local part = string.split(line, Z_DATA_SEPARATOR) + if #part <= 3 then + return part + end + -- If the part is made of more than 3 elements, it's probably because the + -- path element contains '|' that have been split. Thus, we want to + -- reconstruct it and keep the 2 last elements of part intact as the end + -- of the returned part. + local path = part[1] + for i=2,#part-2 do + path = path .. Z_DATA_SEPARATOR .. part[i] + end + return {path, part[#part-1], part[#part]} +end + + ----------------------------------------------------------------------- -- load and split data ----------------------------------------------------------------------- @@ -1073,7 +1096,7 @@ function data_load(filename) return {} end for line in fp:lines() do - local part = string.split(line, '|') + local part = read_data_line(line) local item = {} if part and part[1] and part[2] and part[3] then local key = insensitive and part[1]:lower() or part[1] @@ -1135,7 +1158,7 @@ function data_save(filename, M) end for i = 1, #M do local item = M[i] - local text = item.name .. '|' .. item.rank .. '|' .. item.time + local text = item.name .. Z_DATA_SEPARATOR .. item.rank .. Z_DATA_SEPARATOR .. item.time fp:write(text .. '\n') end fp:close() @@ -1292,13 +1315,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 +1335,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 +1487,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 @@ -1584,21 +1613,23 @@ function z_cd(patterns) if patterns == nil then return nil end - if #patterns == 0 then - return nil - end - local last = patterns[#patterns] - if last == '~' or last == '~/' then - return os.path.expand('~') - elseif windows and last == '~\\' then - return os.path.expand('~') - end - if os.path.isabs(last) and os.path.isdir(last) then - local size = #patterns - if size <= 1 then - return os.path.norm(last) - elseif last ~= '/' and last ~= '\\' then - return os.path.norm(last) + if Z_INTERACTIVE == 0 then + if #patterns == 0 then + return nil + end + local last = patterns[#patterns] + if last == '~' or last == '~/' then + return os.path.expand('~') + elseif windows and last == '~\\' then + return os.path.expand('~') + end + if os.path.isabs(last) and os.path.isdir(last) then + local size = #patterns + if size <= 1 then + return os.path.norm(last) + elseif last ~= '/' and last ~= '\\' then + return os.path.norm(last) + end end end local M = z_match(patterns, Z_METHOD, Z_SUBDIR) @@ -1640,15 +1671,15 @@ function z_cd(patterns) local flag = os.environ('_ZL_FZF_FLAG', '') flag = (flag == '' or flag == nil) and '+s -e' or flag cmd = ((fzf == '') and 'fzf' or fzf) .. ' ' .. cmd .. ' ' .. flag + local height = os.environ('_ZL_FZF_HEIGHT', '35%') + if height ~= nil and height ~= '' and height ~= '0' then + cmd = cmd .. ' --height ' .. height + end if not windows then tmpname = os.tmpname() - local height = os.environ('_ZL_FZF_HEIGHT', '35%') - if height ~= nil and height ~= '' and height ~= '0' then - cmd = cmd .. ' --height ' .. height - end cmd = cmd .. ' < "' .. tmpname .. '"' else - tmpname = os.tmpname():gsub('\\', ''):gsub('%.', '') + tmpname = os.tmpname():gsub('[\\:]', ''):gsub('%.', '') tmpname = os.environ('TMP', '') .. '\\zlua_' .. tmpname .. '.txt' cmd = 'type "' .. tmpname .. '" | ' .. cmd end @@ -1845,7 +1876,7 @@ function cd_breadcrumbs(pwd, interactive) if not windows then tmpname = os.tmpname() else - tmpname = os.tmpname():gsub('\\', ''):gsub('%.', '') + tmpname = os.tmpname():gsub('[\\:]', ''):gsub('%.', '') tmpname = os.environ('TMP', '') .. '\\zlua_' .. tmpname .. '.txt' end fp = io.open(tmpname, 'w') @@ -1943,13 +1974,13 @@ function main(argv) end elseif options['-'] then path = cd_minus(args, options) - elseif #args == 0 then + elseif #args == 0 and Z_INTERACTIVE == 0 then path = nil else path = z_cd(args) if path == nil and Z_MATCHMODE ~= 0 then local last = args[#args] - if os.path.isdir(last) then + if last and os.path.isdir(last) then path = os.path.abspath(last) path = os.path.norm(path) end @@ -1973,12 +2004,14 @@ function main(argv) for _, key in ipairs(args) do opts[key] = 1 end - if windows then + if opts.nushell then + z_nushell_init(opts) + elseif windows then z_windows_init(opts) elseif opts.fish then z_fish_init(opts) elseif opts.powershell then - z_windows_init(opts) + z_windows_init(opts) else z_shell_init(opts) end @@ -1990,9 +2023,14 @@ function main(argv) z_print(M, true, false) end elseif options['--complete'] then - local line = args[1] and args[1] or '' - local head = line:sub(Z_CMD:len()+1):gsub('^%s+', '') - local M = z_match({head}, Z_METHOD, Z_SUBDIR) + local M = {} + if options['-m1'] then + M = z_match(args and args or {}, Z_METHOD, Z_SUBDIR) + else + local line = args[1] and args[1] or '' + local head = line:sub(Z_CMD:len()+1):gsub('^%s+', '') + M = z_match({head}, Z_METHOD, Z_SUBDIR) + end for _, item in pairs(M) do print(item.name) end @@ -2067,10 +2105,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 @@ -2101,10 +2142,28 @@ function z_clink_init() end return {} end + local dirmatchfunc = function (word) + clink.matches_are_files(1) + return clink.match_files(word..'*', true, clink.find_dirs) + end + local dirmatchparser = clink.arg.new_parser():set_arguments({ dirmatchfunc }) local z_parser = clink.arg.new_parser() z_parser:set_arguments({ z_match_completion }) - z_parser:set_flags("-c", "-r", "-i", "--cd", "-e", "-b", "--add", "-x", "--purge", - "--init", "-l", "-s", "--complete", "--help", "-h") + z_parser:set_flags("-r", "-i", "-I", "-t", "-l", "-c", "-e", "-b", "-x"..dirmatchparser, "-h") + if z_parser.adddescriptions then + z_parser:adddescriptions({ + ['-r'] = "cd to highest ranked dir matching", + ['-i'] = "cd with interactive selection", + ['-I'] = "cd with interactive selection using fzf", + ['-t'] = "cd to most recently accessed dir matching", + ['-l'] = "list matches instead of cd", + ['-c'] = "restrict matches to subdirs of cwd (or %PWD% if set)", + ['-e'] = "echo the best match, don't cd", + ['-b'] = "jump backwards to given dir or to project root", + ['-x'] = { " dir", "remove path from history" }, + ['-h'] = "show help", + }) + end clink.arg.register_parser("z", z_parser) end @@ -2139,7 +2198,8 @@ _zlua() { -s) local arg_strip="-s" ;; -i) local arg_inter="-i" ;; -I) local arg_inter="-I" ;; - -h|--help) local arg_mode="-h" ;; + -h) local arg_mode="-h" ;; + --help) local arg_mode="-h" ;; --purge) local arg_mode="--purge" ;; *) break ;; esac @@ -2266,6 +2326,30 @@ if [ "${+functions[compdef]}" -ne 0 ]; then fi ]] +local script_fzf_complete_zsh = [[ +if command -v fzf >/dev/null 2>&1; then + # To redraw line after fzf closes (printf '\e[5n') + bindkey '\e[0n' kill-whole-line + _zlua_zsh_fzf_complete() { + local list=$(_zlua -l ${words[2,-1]}) + + if [ -n "$list" ]; then + local selected=$(print $list | ${=zlua_fzf} | sed 's/^[0-9,.]* *//') + + if [ -n "$selected" ]; then + cd ${selected} + printf '\e[5n' + fi + fi + } + + if [ "${+functions[compdef]}" -ne 0 ]; then + compdef _zlua_zsh_fzf_complete _zlua > /dev/null 2>&1 + compdef ${_ZL_CMD:-z}=_zlua + fi +fi +]] + ----------------------------------------------------------------------- -- initialize bash/zsh @@ -2303,7 +2387,7 @@ function z_shell_init(opts) end print(script_complete_bash) if opts.fzf ~= nil then - fzf_cmd = "fzf --nth 2.. --reverse --info=inline --tac " + local fzf_cmd = "fzf --nth 2.. --reverse --info=inline --tac " local height = os.environ('_ZL_FZF_HEIGHT', '35%') if height ~= nil and height ~= '' and height ~= '0' then fzf_cmd = fzf_cmd .. ' --height ' .. height .. ' ' @@ -2319,6 +2403,18 @@ function z_shell_init(opts) print(once and script_init_zsh_once or script_init_zsh) end print(script_complete_zsh) + if opts.fzf ~= nil then + local fzf_cmd = "fzf --nth 2.. --reverse --info=inline --tac " + local height = os.environ('_ZL_FZF_HEIGHT', '35%') + if height ~= nil and height ~= '' and height ~= '0' then + fzf_cmd = fzf_cmd .. ' --height ' .. height .. ' ' + end + local flag = os.environ('_ZL_FZF_FLAG', '') + flag = (flag == '' or flag == nil) and '+s -e' or flag + fzf_cmd = fzf_cmd .. ' ' .. flag .. ' ' + print('zlua_fzf="' .. fzf_cmd .. '"') + print(script_fzf_complete_zsh) + end elseif opts.posix ~= nil then if prompt_hook then local script = script_init_posix @@ -2423,7 +2519,9 @@ function _zlua end if test -z "$_ZL_CMD"; set -x _ZL_CMD z; end -alias "$_ZL_CMD"=_zlua +function $_ZL_CMD -w _zlua -d "alias $_ZL_CMD=_zlua" + _zlua $argv +end ]] script_init_fish = [[ @@ -2711,6 +2809,129 @@ function z_windows_init(opts) end +----------------------------------------------------------------------- +-- nushell +----------------------------------------------------------------------- +local script_zlua_nushell = [[ +export def _zlua --env --wrapped [...args: string] { + if ($args | length) != 0 and $args.0 == "--add" { + with-env { _ZL_RANDOM: (random int) } { ^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT --add ...($args | skip 1) } + } else if ($args | length) != 0 and $args.0 == "--complete" { + ^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT --complete ...($args | skip 1) + } else { + mut arg_mode = '' + mut arg_type = '' + mut arg_subdir = '' + mut arg_inter = '' + mut arg_strip = '' + mut count = 0 + for arg in $args { + match $arg { + '-l' => { $arg_mode = '-l' }, + '-e' => { $arg_mode = '-e' }, + '-x' => { $arg_mode = '-x' }, + '-t' => { $arg_type = '-t' }, + '-r' => { $arg_type = '-r' }, + '-c' => { $arg_subdir = '-c' }, + '-s' => { $arg_strip = '-s' }, + '-i' => { $arg_inter = '-i' }, + '-I' => { $arg_inter = '-I' }, + '-h' => { $arg_mode = '-h' }, + '--help' => { $arg_mode = '-h' }, + '--purge' => { $arg_mode = '--purge' }, + _ => break + } + $count += 1 + } + let args = $args | skip $count + if $arg_mode == '-h' or $arg_mode == '--purge' { + ^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT $arg_mode + } else if $arg_mode == '-l' or ($args | length) == 0 { + ^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT -l $arg_subdir $arg_type $arg_strip ...$args + } else if $arg_mode != '' { + ^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT $arg_mode $arg_subdir $arg_type $arg_inter ...$args + } else { + let zdest = (^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT --cd $arg_type $arg_subdir $arg_inter ...$args) + if $zdest != '' and ($zdest | path exists) { + cd $zdest + if _ZL_ECHO in $env and $env._ZL_ECHO != '' { + pwd + } + } + } + } +} +]] + +local script_init_nushell = [[ +$env.config = ($env | default {} config).config +$env.config = ($env.config | default {} hooks) +$env.config = ($env.config | update hooks ($env.config.hooks | default {} env_change)) +$env.config = ($env.config | update hooks.env_change ($env.config.hooks.env_change | default [] PWD)) +$env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.env_change.PWD | append {|_, dir| _zlua --add $dir })) +]] + +local script_complete_nushell = [[ +let zlua_completer = {|spans| $spans | skip 1 | _zlua --complete -m1 ...$in | lines | where {|x| $x != $env.PWD}} + +$env.config = ($env.config | default {} completions) +$env.config = ($env.config | update completions ($env.config.completions | default {} external)) +$env.config = ($env.config | update completions.external ($env.config.completions.external | default true enable)) +if completer in $env.config.completions.external { + let orig_completer = $env.config.completions.external.completer + $env.config = ($env.config | update completions.external.completer { + {|spans| + match $spans.0 { + z => $zlua_completer, + _zlua => $zlua_completer, + _ => $orig_completer + } | do $in $spans + } + }) +} else { + $env.config = ($env.config | update completions.external.completer { + {|spans| + match $spans.0 { + z => $zlua_completer, + _zlua => $zlua_completer, + } | do $in $spans + } + }) +} + +export alias z = _zlua + +]] + +----------------------------------------------------------------------- +-- initialize nushell +----------------------------------------------------------------------- +function z_nushell_init(opts) + print('$env.ZLUA_LUAEXE = \'' .. os.interpreter() .. '\'') + print('$env.ZLUA_SCRIPT = \'' .. os.scriptname() .. '\'') + local prompt_hook = (not os.environ("_ZL_NO_PROMPT_COMMAND", false)) + if opts.clean ~= nil then + prompt_hook = false + end + print(script_zlua_nushell) + if prompt_hook then + print(script_init_nushell) + end + print(script_complete_nushell) + if opts.enhanced ~= nil then + print('$env._ZL_MATCH_MODE = 1') + end + if opts.once ~= nil then + print('$env._ZL_ADD_ONCE = 1') + end + if opts.echo ~= nil then + print('$env._ZL_ECHO = 1') + end + if opts.nc ~= nil then + print('$env._ZL_NO_CHECK = 1') + end +end + ----------------------------------------------------------------------- -- help ----------------------------------------------------------------------- diff --git a/plugins/z.lua/z.lua.plugin.zsh b/plugins/z.lua/z.lua.plugin.zsh index 8109dbe..2ccac99 100644 --- a/plugins/z.lua/z.lua.plugin.zsh +++ b/plugins/z.lua/z.lua.plugin.zsh @@ -21,7 +21,11 @@ fi export _ZL_FZF_FLAG=${_ZL_FZF_FLAG:-"-e"} -eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)" +if [[ -z "$_ZL_ZSH_NO_FZF" ]]; then + eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced fzf)" +else + eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)" +fi if [[ -z "$_ZL_NO_ALIASES" ]]; then alias zz='z -i'