1
0
mirror of https://github.com/skywind3000/z.lua synced 2026-03-22 15:49:47 +00:00

25 Commits

Author SHA1 Message Date
skywind3000
0effba4021 polish: 1.8.3 2020-02-10 00:14:09 +08:00
skywind3000
cada42e5ea z.lua 1.8.3: polish interactive mode in z -b -i 2020-02-10 00:09:08 +08:00
skywind3000
b8b6d1afd6 z.lua 1.8.2 2020-02-09 23:46:35 +08:00
skywind3000
4abe97f989 jump backwards 2020-02-09 23:39:36 +08:00
skywind3000
756d13d8fa zlua 1.8.1: new -b -i and -b -I for interactive backwards cd 2020-02-09 23:34:37 +08:00
skywind3000
4900651af3 Merge branch 'master' of https://github.com/skywind3000/z.lua 2019-12-29 04:59:20 +08:00
skywind3000
836efd3973 1.7.4: new $_ZL_HYPHEN option 2019-12-29 04:59:08 +08:00
Linwei
3b8b4c1fbb Merge pull request #87 from davidosomething/no-aliases
Introduce _ZL_NO_ALIASES to skip creating aliases
2019-12-26 00:35:12 +08:00
David O'Trakoun
8eaebca04a Introduce _ZL_NO_ALIASES to skip creating aliases 2019-12-25 00:16:44 -05:00
Linwei
fdd037acf8 Merge pull request #85 from rongmu/_zl_fzf_flag-fix
[zsh plugin] allow the user to customize _ZL_FZF_FLAG beforehand
2019-12-10 17:45:04 +08:00
Shaoyun Yu
a817cf6ed2 allow the user to custom _ZL_FZF_FLAG beforehand
increase flexibility: allow the user to custom _ZL_FZF_FLAG before the zsh plugin is loaded
2019-12-10 10:50:55 +09:00
skywind3000
5c36d55698 use lua-filesystem package if possible when $_ZL_USE_LFS is 1 2019-09-07 01:02:48 +08:00
skywind3000
bdab27db1b fixed wrong return value from os.path.exists 2019-09-06 01:21:28 +08:00
skywind3000
e08f5beca4 update doc 2019-08-01 20:06:05 +08:00
skywind3000
a245db0d93 update doc 2019-08-01 19:54:34 +08:00
skywind3000
dd721703c3 1.7.2: improve bash / zsh shell compatibility 2019-08-01 19:46:23 +08:00
Linwei
7920d56c89 Merge pull request #71 from barlik/prompt-command
Do not append semicolon to PROMPT_COMMAND
2019-08-01 17:45:54 +08:00
Linwei
645818ccc8 Merge pull request #72 from barlik/zsh-hooks 2019-08-01 02:41:19 +08:00
Rastislav Barlik
b98911a227 Setting zsh-hooks to be unique is not necessary 2019-07-31 14:46:28 +01:00
Rastislav Barlik
671830059b Do not append semicolon to PROMPT_COMMAND 2019-07-31 13:47:55 +01:00
skywind3000
0fad96124b commit new README.md 2019-06-07 22:17:42 +08:00
skywind3000
465f2b8e62 fixed linux $_ZL_DATA failure 2019-06-07 22:13:29 +08:00
skywind3000
ff9d874ef4 update doc 2019-03-09 17:24:53 +08:00
skywind3000
86120d206e fix: ReplaceFile issue in luajit (windows). 2019-03-09 16:56:46 +08:00
skywind3000
1e0e3523b0 add ranger plugin for z.lua 2019-03-08 16:23:40 +08:00
5 changed files with 275 additions and 40 deletions

View File

@@ -302,6 +302,8 @@ export _ZL_ROOT_MARKERS=".git,.svn,.hg,.root,package.json"
**Bonus**`zb ..` 相当于 `cd ..``zb ...` 相当于 `cd ../..`,而 `zb ....` 相当于 `cd ../../..` 等等。 最后 `zb ..20` 等同于调用 `cd ..` 二十次。 **Bonus**`zb ..` 相当于 `cd ..``zb ...` 相当于 `cd ../..`,而 `zb ....` 相当于 `cd ../../..` 等等。 最后 `zb ..20` 等同于调用 `cd ..` 二十次。
**Bonus**: 试试 `z -b -i` 以及 `z -b -I`,推荐把他们取个别名成 `zbi` 和 `zbf`。
## 补全功能 ## 补全功能

View File

@@ -131,6 +131,7 @@ z -b foo # cd to the parent directory starting with foo
- set `$_ZL_ECHO` to 1 to display new directory name after cd. - set `$_ZL_ECHO` to 1 to display new directory name after cd.
- set `$_ZL_MATCH_MODE` to 1 to enable enhanced matching. - 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_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.
## Aging ## Aging
@@ -322,6 +323,7 @@ If you want `zb` jump back to a parent directory contains a `.root` or `package.
**Bonus**: `zb ..` equals to `cd ..`, `zb ...` equals to `cd ../..` and `zb ....` equals to `cd ../../..`, and so on. Finally, `zb ..20` equals to `cd (..)x20`. **Bonus**: `zb ..` equals to `cd ..`, `zb ...` equals to `cd ../..` and `zb ....` equals to `cd ../../..`, and so on. Finally, `zb ..20` equals to `cd (..)x20`.
**Bonus**: try `z -b -i` and `z -b -I` and you can alias them to `zbi` and `zbf`.
## Completion ## Completion
@@ -458,6 +460,12 @@ As you see, z.lua is the fastest one and requires less resource.
## History ## History
- 1.8.3 (2020-02-09): new: `z -b -i` and `z -b -I` to jump backwards in interactive mode.
- 1.7.4 (2019-12-29): new: `$_ZL_HYPHEN` to treat hyphen as a normal character, see [here](https://github.com/skywind3000/z.lua/wiki/FAQ#how-to-input-a-hyphen---in-the-keyword-).
- 1.7.3 (2019-09-07): use [lua-filesystem](http://keplerproject.github.io/luafilesystem/) package if possible when `$_ZL_USE_LFS` is `1`.
- 1.7.2 (2019-08-01): Improve bash/zsh shell compatibility by [@barlik](https://github.com/barlik).
- 1.7.1 (2019-06-07): Fixed: `$_ZL_DATA` failure on Linux sometimes.
- 1.7.0 (2019-03-09): Support [ranger](https://github.com/skywind3000/z.lua/wiki/FAQ#how-to-integrate-zlua-to-ranger-), fix ReplaceFile issue in luajit (windows).
- 1.6.0 (2019-03-04): optimize with ffi module (luajit builtin module). - 1.6.0 (2019-03-04): optimize with ffi module (luajit builtin module).
- 1.5.11 (2019-03-02): fixed: os.path.isdir doesn't work for symbol link folders. - 1.5.11 (2019-03-02): fixed: os.path.isdir doesn't work for symbol link folders.
- 1.5.10 (2019-03-01): Prevent writing file racing. - 1.5.10 (2019-03-01): Prevent writing file racing.
@@ -509,6 +517,7 @@ This project needs help for the tasks below:
- Thanks to [@TeddyDD](https://github.com/TeddyDD) for Fish Shell porting. - Thanks to [@TeddyDD](https://github.com/TeddyDD) for Fish Shell porting.
- Thanks to [@manhong2112](https://github.com/manhong2112) for Power Shell porting. - 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 [@BarbUk](https://github.com/BarbUk) for fzf completion in Bash.
- Thanks to [@barlik](https://github.com/barlik) for many improvements.
And many others. And many others.

91
ranger_zlua.py Normal file
View File

@@ -0,0 +1,91 @@
import time, sys, os
import ranger.api
import subprocess
old_hook_init = ranger.api.hook_init
PATH_LUA = os.environ.get('RANGER_LUA')
PATH_ZLUA = os.environ.get('RANGER_ZLUA')
if not PATH_LUA:
for path in os.environ.get('PATH', '').split(os.path.pathsep):
for name in ('lua', 'luajit', 'lua5.3', 'lua5.2', 'lua5.1'):
test = os.path.join(path, name)
test = test + (sys.platform[:3] == 'win' and ".exe" or "")
if os.path.exists(test):
PATH_LUA = test
break
if not PATH_LUA:
sys.stderr.write('Please install lua or set $RANGER_LUA.\n')
sys.exit()
if (not PATH_ZLUA) or (not os.path.exists(PATH_ZLUA)):
sys.stderr.write('Not find z.lua, please set $RANGER_ZLUA to absolute path of z.lua.\n')
sys.exit()
def hook_init(fm):
def update_zlua(signal):
import os, random
os.environ['_ZL_RANDOM'] = str(random.randint(0, 0x7fffffff))
p = subprocess.Popen([PATH_LUA, PATH_ZLUA, "--add", signal.new.path])
p.wait()
if PATH_ZLUA and PATH_LUA and os.path.exists(PATH_ZLUA):
fm.signal_bind('cd', update_zlua)
return old_hook_init(fm)
ranger.api.hook_init = hook_init
class z(ranger.api.commands.Command):
def execute (self):
import sys, os, time
args = self.args[1:]
if args:
mode = ''
for arg in args:
if arg in ('-l', '-e', '-x', '-h', '--help', '--'):
mode = arg
break
elif arg in ('-I', '-i'):
mode = arg
elif arg[:1] != '-':
break
if mode:
cmd = '"%s" "%s" '%(PATH_LUA, PATH_ZLUA)
if mode in ('-I', '-i', '--'):
cmd += ' --cd'
for arg in args:
cmd += ' "%s"'%arg
if mode in ('-e', '-x'):
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
path = path.decode("utf-8", "ignore")
path = path.rstrip('\n')
self.fm.notify(path)
elif mode in ('-h', '-l', '--help'):
p = self.fm.execute_command(cmd + '| less +G', universal_newlines=True)
stdout, stderr = p.communicate()
elif mode == '--':
p = self.fm.execute_command(cmd + ' 2>&1 | less +G', universal_newlines=True)
stdout, stderr = p.communicate()
else:
if mode == '-I':
os.environ['_ZL_FZF_HEIGHT'] = '0'
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
self.fm.execute_console('redraw_window')
else:
p = self.fm.execute_command(cmd, universal_newlines=True, stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
path = stdout.rstrip('\n')
if path and os.path.exists(path):
self.fm.cd(path)
else:
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
path = path.decode("utf-8", "ignore")
path = path.rstrip('\n')
if path and os.path.exists(path):
self.fm.cd(path)
else:
self.fm.notify('No matching found', bad = True)
return True

163
z.lua
View File

@@ -1,10 +1,10 @@
#! /usr/bin/env lua #! /usr/bin/env lua
--===================================================================== --=====================================================================
-- --
-- z.lua - a cd command that learns, by skywind 2018, 2019 -- z.lua - a cd command that learns, by skywind 2018, 2019, 2020
-- Licensed under MIT license. -- Licensed under MIT license.
-- --
-- Version 1.6.0, Last Modified: 2019/03/04 14:47 -- Version 1.8.3, Last Modified: 2020/02/10 00:05
-- --
-- * 10x faster than fasd and autojump, 3x faster than z.sh -- * 10x faster than fasd and autojump, 3x faster than z.sh
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox -- * available for posix shells: bash, zsh, sh, ash, dash, busybox
@@ -74,6 +74,8 @@
-- set $_ZL_MAXAGE to define a aging threshold (default is 5000). -- set $_ZL_MAXAGE to define a aging threshold (default is 5000).
-- set $_ZL_MATCH_MODE to 1 to enable enhanced matching mode. -- set $_ZL_MATCH_MODE to 1 to enable enhanced matching mode.
-- set $_ZL_NO_CHECK to 1 to disable path validation. z --purge to clear. -- set $_ZL_NO_CHECK to 1 to disable path validation. z --purge to clear.
-- set $_ZL_USE_LFS to 1 to use lua-filesystem package
-- set $_ZL_HYPHEN to 1 to stop treating hyphen as a regexp keyword
-- --
--===================================================================== --=====================================================================
@@ -120,6 +122,7 @@ Z_CMD = 'z'
Z_MATCHMODE = 0 Z_MATCHMODE = 0
Z_MATCHNAME = false Z_MATCHNAME = false
Z_SKIPPWD = false Z_SKIPPWD = false
Z_HYPHEN = false
os.LOG_NAME = os.getenv('_ZL_LOG_NAME') os.LOG_NAME = os.getenv('_ZL_LOG_NAME')
@@ -548,6 +551,9 @@ end
-- file or path exists -- file or path exists
----------------------------------------------------------------------- -----------------------------------------------------------------------
function os.path.exists(name) function os.path.exists(name)
if name == '/' then
return true
end
local ok, err, code = os.rename(name, name) local ok, err, code = os.rename(name, name)
if not ok then if not ok then
if code == 13 then if code == 13 then
@@ -1106,7 +1112,10 @@ function data_save(filename, M)
fp:close() fp:close()
if tmpname ~= nil then if tmpname ~= nil then
if windows then if windows then
os.native.ReplaceFile(filename, tmpname) local ok, err, code = os.rename(tmpname, filename)
if not ok then
os.native.ReplaceFile(filename, tmpname)
end
else else
os.rename(tmpname, filename) os.rename(tmpname, filename)
end end
@@ -1260,6 +1269,9 @@ function data_select(M, patterns, matchlast)
local pats = {} local pats = {}
for i = 1, #patterns do for i = 1, #patterns do
local p = patterns[i] local p = patterns[i]
if Z_HYPHEN then
p = p:gsub('-', '%%-')
end
table.insert(pats, case_insensitive_pattern(p)) table.insert(pats, case_insensitive_pattern(p))
end end
for i = 1, #M do for i = 1, #M do
@@ -1573,7 +1585,7 @@ function z_cd(patterns)
io.stderr:write('> ') io.stderr:write('> ')
io.stderr:flush() io.stderr:flush()
local input = io.read('*l') local input = io.read('*l')
if input == nil then if input == nil or input == '' then
return nil return nil
end end
local index = tonumber(input) local index = tonumber(input)
@@ -1755,6 +1767,91 @@ function cd_minus(args, options)
end end
-----------------------------------------------------------------------
-- cd breadcrumbs: z -b -i, z -b -I
-----------------------------------------------------------------------
function cd_breadcrumbs(pwd, interactive)
local pwd = (pwd == nil or pwd == '') and os.pwd() or pwd
local pwd = os.path.normpath(pwd)
local path, _ = os.path.split(pwd)
local elements = {}
local interactive = interactive and interactive or 1
local fullname = os.environ('_ZL_FULL_PATH', false)
while true do
local head, name = os.path.split(path)
if head == path then -- reached root
table.insert(elements, {head, head})
break
elseif name ~= '' then
table.insert(elements, {name, path})
else
break
end
path = head
end
local tmpname = '/tmp/zlua.txt'
local fp = io.stderr
if interactive == 2 then
if not windows then
tmpname = os.tmpname()
else
tmpname = os.tmpname():gsub('\\', ''):gsub('%.', '')
tmpname = os.environ('TMP', '') .. '\\zlua_' .. tmpname .. '.txt'
end
fp = io.open(tmpname, 'w')
end
-- print table
local maxsize = string.len(tostring(#elements))
for i = #elements, 1, -1 do
local item = elements[i]
local name = item[1]
local text = string.rep(' ', maxsize - string.len(i)) .. tostring(i)
text = text .. ': ' .. (fullname and item[2] or item[1])
fp:write(text .. '\n')
end
if fp ~= io.stderr then
fp:close()
end
local retval = ''
-- select from stdin or fzf
if interactive == 1 then
io.stderr:write('> ')
io.stderr:flush()
retval = io.read('*l')
elseif interactive == 2 then
local fzf = os.environ('_ZL_FZF', 'fzf')
local cmd = '--reverse --inline-info --tac '
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
if not windows then
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
cmd = 'type "' .. tmpname .. '" | ' .. cmd
end
retval = os.call(cmd)
os.remove(tmpname)
if retval == '' or retval == nil then
return nil
end
local pos = retval:find(':')
if not pos then
return nil
end
retval = retval:sub(1, pos - 1):gsub('^%s*', '')
end
local index = tonumber(retval)
if index == nil or index < 1 or index > #elements then
return nil
end
return elements[index][2]
end
----------------------------------------------------------------------- -----------------------------------------------------------------------
-- main entry -- main entry
----------------------------------------------------------------------- -----------------------------------------------------------------------
@@ -1789,7 +1886,11 @@ function main(argv)
if options['--cd'] or options['-e'] then if options['--cd'] or options['-e'] then
local path = '' local path = ''
if options['-b'] then if options['-b'] then
path = cd_backward(args, options) if Z_INTERACTIVE == 0 then
path = cd_backward(args, options)
else
path = cd_breadcrumbs('', Z_INTERACTIVE)
end
elseif options['-'] then elseif options['-'] then
path = cd_minus(args, options) path = cd_minus(args, options)
elseif #args == 0 then elseif #args == 0 then
@@ -1861,16 +1962,14 @@ function z_init()
local _zl_matchname = os.getenv('_ZL_MATCH_NAME') local _zl_matchname = os.getenv('_ZL_MATCH_NAME')
local _zl_skippwd = os.getenv('_ZL_SKIP_PWD') local _zl_skippwd = os.getenv('_ZL_SKIP_PWD')
local _zl_matchmode = os.getenv('_ZL_MATCH_MODE') local _zl_matchmode = os.getenv('_ZL_MATCH_MODE')
local _zl_hyphen = os.getenv('_ZL_HYPHEN')
if _zl_data ~= nil and _zl_data ~= "" then if _zl_data ~= nil and _zl_data ~= "" then
if windows then if windows then
DATA_FILE = _zl_data DATA_FILE = _zl_data
else else
-- avoid windows environments affect cygwin & msys -- avoid windows environments affect cygwin & msys
if _zl_data:sub(2, 2) ~= ':' then if not string.match(_zl_data, '^%a:[/\\]') then
local t = _zl_data:sub(3, 3) DATA_FILE = _zl_data
if t ~= '/' and t ~= "\\" then
DATA_FILE = _zl_data
end
end end
end end
end end
@@ -1916,6 +2015,12 @@ function z_init()
Z_SKIPPWD = true Z_SKIPPWD = true
end end
end end
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
end
end
end end
@@ -2010,14 +2115,14 @@ alias ${_ZL_CMD:-z}='_zlua'
local script_init_bash = [[ local script_init_bash = [[
case "$PROMPT_COMMAND" in case "$PROMPT_COMMAND" in
*_zlua?--add*) ;; *_zlua?--add*) ;;
*) PROMPT_COMMAND="(_zlua --add \"\$(command pwd 2>/dev/null)\" &);$PROMPT_COMMAND" ;; *) PROMPT_COMMAND="(_zlua --add \"\$(command pwd 2>/dev/null)\" &)${PROMPT_COMMAND:+;$PROMPT_COMMAND}" ;;
esac esac
]] ]]
local script_init_bash_fast = [[ local script_init_bash_fast = [[
case "$PROMPT_COMMAND" in case "$PROMPT_COMMAND" in
*_zlua?--add*) ;; *_zlua?--add*) ;;
*) PROMPT_COMMAND="(_zlua --add \"\$PWD\" &);$PROMPT_COMMAND" ;; *) PROMPT_COMMAND="(_zlua --add \"\$PWD\" &)${PROMPT_COMMAND:+;$PROMPT_COMMAND}" ;;
esac esac
]] ]]
@@ -2029,7 +2134,7 @@ _zlua_precmd() {
} }
case "$PROMPT_COMMAND" in case "$PROMPT_COMMAND" in
*_zlua_precmd*) ;; *_zlua_precmd*) ;;
*) PROMPT_COMMAND="_zlua_precmd;$PROMPT_COMMAND" ;; *) PROMPT_COMMAND="_zlua_precmd${PROMPT_COMMAND:+;$PROMPT_COMMAND}" ;;
esac esac
]] ]]
@@ -2056,7 +2161,7 @@ local script_init_zsh = [[
_zlua_precmd() { _zlua_precmd() {
(_zlua --add "${PWD:a}" &) (_zlua --add "${PWD:a}" &)
} }
typeset -gaU precmd_functions typeset -ga precmd_functions
[ -n "${precmd_functions[(r)_zlua_precmd]}" ] || { [ -n "${precmd_functions[(r)_zlua_precmd]}" ] || {
precmd_functions[$(($#precmd_functions+1))]=_zlua_precmd precmd_functions[$(($#precmd_functions+1))]=_zlua_precmd
} }
@@ -2066,7 +2171,7 @@ local script_init_zsh_once = [[
_zlua_precmd() { _zlua_precmd() {
(_zlua --add "${PWD:a}" &) (_zlua --add "${PWD:a}" &)
} }
typeset -gaU chpwd_functions typeset -ga chpwd_functions
[ -n "${chpwd_functions[(r)_zlua_precmd]}" ] || { [ -n "${chpwd_functions[(r)_zlua_precmd]}" ] || {
chpwd_functions[$(($#chpwd_functions+1))]=_zlua_precmd chpwd_functions[$(($#chpwd_functions+1))]=_zlua_precmd
} }
@@ -2539,7 +2644,33 @@ end
----------------------------------------------------------------------- -----------------------------------------------------------------------
-- testing case -- LFS optimize
-----------------------------------------------------------------------
os.lfs = {}
os.lfs.enable = os.getenv('_ZL_USE_LFS')
if os.lfs.enable ~= nil then
local m = string.lower(os.lfs.enable)
if (m == '1' or m == 'yes' or m == 'true' or m == 't') then
os.lfs.status, os.lfs.pkg = pcall(require, 'lfs')
if os.lfs.status then
local lfs = os.lfs.pkg
os.path.exists = function (name)
return lfs.attributes(name) and true or false
end
os.path.isdir = function (name)
local mode = lfs.attributes(name)
if not mode then
return false
end
return (mode.mode == 'directory') and true or false
end
end
end
end
-----------------------------------------------------------------------
-- program entry
----------------------------------------------------------------------- -----------------------------------------------------------------------
if not pcall(debug.getlocal, 4, 1) then if not pcall(debug.getlocal, 4, 1) then
-- main script -- main script

View File

@@ -20,15 +20,17 @@ if [[ -z "$ZLUA_EXEC" ]]; then
fi fi
fi fi
export _ZL_FZF_FLAG="-e" export _ZL_FZF_FLAG=${_ZL_FZF_FLAG:-"-e"}
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)" eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)"
if [[ -z "$_ZL_NO_ALIASES" ]]; then
alias zz='z -i' alias zz='z -i'
alias zc='z -c' alias zc='z -c'
alias zf='z -I' alias zf='z -I'
alias zb='z -b' alias zb='z -b'
alias zh='z -I -t .' alias zbi='z -b -i'
alias zzc='zz -c' alias zbf='z -b -I'
alias zh='z -I -t .'
alias zzc='zz -c'
fi