zoxide/templates/elvish.txt
alaviss 0dfe1c4073
fix(elvish): before-chdir should assign to oldpwd upvalue (#818)
elvish uses lexical scoping for closure capture, as such all `oldpwd`
references within the script refers to the `oldpwd` defined at the top.
However, before-chdir hook is trying to assign to `oldpwd` within the
editor scope, which is not used by this script.

This manifested as a bug in which:

```
~
> mkdir -p /tmp/another

~
> z /tmp

/tmp
> z another

/tmp/another
> z -

~
> # The previous dir should be /tmp not ~!
```

Because the hook was updating a variable that was not used.

Fix the hook so that before-chdir assign to the proper upvalue.
2024-05-29 15:06:28 +05:30

121 lines
2.9 KiB
Plaintext

{%- let section = "# =============================================================================\n#" -%}
{%- let not_configured = "# -- not configured --" -%}
use builtin
use path
{{ section }}
# Utility functions for zoxide.
#
# cd + custom logic based on the value of _ZO_ECHO.
fn __zoxide_cd {|path|
builtin:cd $path
{%- if echo %}
builtin:echo $pwd
{%- endif %}
}
{{ section }}
# Hook configuration for zoxide.
#
# Initialize hook to track previous directory.
var oldpwd = $builtin:pwd
set builtin:before-chdir = [$@builtin:before-chdir {|_| set oldpwd = $builtin:pwd }]
# Initialize hook to add directories to zoxide.
{%- if hook == InitHook::None %}
{{ not_configured }}
{%- else %}
if (builtin:not (builtin:eq $E:__zoxide_shlvl $E:SHLVL)) {
set E:__zoxide_shlvl = $E:SHLVL
{%- if hook == InitHook::Prompt %}
set edit:before-readline = [$@edit:before-readline {|| zoxide add -- $pwd }]
{%- else if hook == InitHook::Pwd %}
set builtin:after-chdir = [$@builtin:after-chdir {|_| zoxide add -- $pwd }]
{%- endif %}
}
{%- endif %}
{{ section }}
# When using zoxide with --no-cmd, alias these internal functions as desired.
#
# Jump to a directory using only keywords.
fn __zoxide_z {|@rest|
if (builtin:eq [] $rest) {
__zoxide_cd ~
} elif (builtin:eq [-] $rest) {
__zoxide_cd $oldpwd
} elif (and ('builtin:==' (builtin:count $rest) 1) (path:is-dir &follow-symlink=$true $rest[0])) {
__zoxide_cd $rest[0]
} else {
var path
try {
set path = (zoxide query --exclude $pwd -- $@rest)
} catch {
} else {
__zoxide_cd $path
}
}
}
edit:add-var __zoxide_z~ $__zoxide_z~
# Jump to a directory using interactive search.
fn __zoxide_zi {|@rest|
var path
try {
set path = (zoxide query --interactive -- $@rest)
} catch {
} else {
__zoxide_cd $path
}
}
edit:add-var __zoxide_zi~ $__zoxide_zi~
{{ section }}
# Commands for zoxide. Disable these using --no-cmd.
#
{%- match cmd %}
{%- when Some with (cmd) %}
edit:add-var {{cmd}}~ $__zoxide_z~
edit:add-var {{cmd}}i~ $__zoxide_zi~
# Load completions.
{#-
zoxide-based completions are currently not possible, because Elvish only prints
a completion if the current token is a prefix of it.
#}
fn __zoxide_z_complete {|@rest|
if (!= (builtin:count $rest) 2) {
builtin:return
}
edit:complete-filename $rest[1] |
builtin:each {|completion|
var dir = $completion[stem]
if (path:is-dir $dir) {
builtin:put $dir
}
}
}
set edit:completion:arg-completer[{{cmd}}] = $__zoxide_z_complete~
{%- when None %}
{{ not_configured }}
{%- endmatch %}
{{ section }}
# To initialize zoxide, add this to your configuration (usually
# ~/.elvish/rc.elv):
#
# eval (zoxide init elvish | slurp)
#
# Note: zoxide only supports elvish v0.18.0 and above.