2021-04-08 18:35:42 +00:00
|
|
|
{%- 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.
|
2022-01-12 07:03:58 +00:00
|
|
|
fn __zoxide_cd {|path|
|
2021-04-08 18:35:42 +00:00
|
|
|
builtin:cd $path
|
|
|
|
{%- if echo %}
|
|
|
|
builtin:echo $pwd
|
|
|
|
{%- endif %}
|
|
|
|
}
|
|
|
|
|
|
|
|
{{ section }}
|
|
|
|
# Hook configuration for zoxide.
|
|
|
|
#
|
|
|
|
|
2021-08-31 10:58:49 +00:00
|
|
|
# Initialize hook to track previous directory.
|
2021-09-04 10:31:50 +00:00
|
|
|
var oldpwd = $builtin:pwd
|
2022-01-12 07:03:58 +00:00
|
|
|
set builtin:before-chdir = [$@builtin:before-chdir {|_| edit:add-var oldpwd $builtin:pwd }]
|
2021-04-08 18:35:42 +00:00
|
|
|
|
2021-08-31 10:58:49 +00:00
|
|
|
# Initialize hook to add directories to zoxide.
|
2021-09-06 21:11:17 +00:00
|
|
|
{%- 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 %}
|
2022-01-12 07:03:58 +00:00
|
|
|
set edit:before-readline = [$@edit:before-readline {|| zoxide add -- $pwd }]
|
2021-09-06 21:11:17 +00:00
|
|
|
{%- else if hook == InitHook::Pwd %}
|
2022-01-12 07:03:58 +00:00
|
|
|
set builtin:after-chdir = [$@builtin:after-chdir {|_| zoxide add -- $pwd }]
|
2021-09-06 21:11:17 +00:00
|
|
|
{%- endif %}
|
2021-09-04 10:31:50 +00:00
|
|
|
}
|
2021-04-08 18:35:42 +00:00
|
|
|
|
2021-09-06 21:11:17 +00:00
|
|
|
{%- endif %}
|
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2022-04-22 07:41:11 +00:00
|
|
|
# When using zoxide with --no-cmd, alias these internal functions as desired.
|
2021-04-08 18:35:42 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
# Jump to a directory using only keywords.
|
2022-01-12 07:03:58 +00:00
|
|
|
fn __zoxide_z {|@rest|
|
2021-04-08 18:35:42 +00:00
|
|
|
if (builtin:eq [] $rest) {
|
|
|
|
__zoxide_cd ~
|
|
|
|
} elif (builtin:eq [-] $rest) {
|
2021-09-04 10:31:50 +00:00
|
|
|
__zoxide_cd $oldpwd
|
2021-08-23 20:30:34 +00:00
|
|
|
} elif (and ('builtin:==' (builtin:count $rest) 1) (path:is-dir &follow-symlink=$true $rest[0])) {
|
2021-04-08 18:35:42 +00:00
|
|
|
__zoxide_cd $rest[0]
|
|
|
|
} else {
|
2021-08-23 20:30:34 +00:00
|
|
|
var path
|
|
|
|
try {
|
2021-09-04 10:31:50 +00:00
|
|
|
set path = (zoxide query --exclude $pwd -- $@rest)
|
2022-03-31 11:20:29 +00:00
|
|
|
} catch {
|
2021-08-23 20:30:34 +00:00
|
|
|
} else {
|
|
|
|
__zoxide_cd $path
|
|
|
|
}
|
2021-04-08 18:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
edit:add-var __zoxide_z~ $__zoxide_z~
|
|
|
|
|
|
|
|
# Jump to a directory using interactive search.
|
2022-01-12 07:03:58 +00:00
|
|
|
fn __zoxide_zi {|@rest|
|
2021-08-23 20:30:34 +00:00
|
|
|
var path
|
|
|
|
try {
|
2021-09-04 10:31:50 +00:00
|
|
|
set path = (zoxide query -i -- $@rest)
|
2022-03-31 11:20:29 +00:00
|
|
|
} catch {
|
2021-08-23 20:30:34 +00:00
|
|
|
} else {
|
|
|
|
__zoxide_cd $path
|
|
|
|
}
|
2021-04-08 18:35:42 +00:00
|
|
|
}
|
|
|
|
edit:add-var __zoxide_zi~ $__zoxide_zi~
|
|
|
|
|
|
|
|
{{ section }}
|
2022-04-10 22:11:51 +00:00
|
|
|
# Commands for zoxide. Disable these using --no-cmd.
|
2021-04-08 18:35:42 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
{%- match cmd %}
|
|
|
|
{%- when Some with (cmd) %}
|
|
|
|
|
2021-09-04 10:31:50 +00:00
|
|
|
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. -#}
|
2022-01-12 07:03:58 +00:00
|
|
|
fn __zoxide_z_complete {|@rest|
|
2021-09-04 10:31:50 +00:00
|
|
|
if (!= (builtin:count $rest) 2) {
|
|
|
|
builtin:return
|
|
|
|
}
|
|
|
|
edit:complete-filename $rest[1] |
|
2022-01-12 07:03:58 +00:00
|
|
|
builtin:each {|completion|
|
2021-09-04 10:31:50 +00:00
|
|
|
var dir = $completion[stem]
|
|
|
|
if (path:is-dir $dir) {
|
|
|
|
builtin:put $dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set edit:completion:arg-completer[{{cmd}}] = $__zoxide_z_complete~
|
2021-04-08 18:35:42 +00:00
|
|
|
|
|
|
|
{%- when None %}
|
|
|
|
|
|
|
|
{{ not_configured }}
|
|
|
|
|
|
|
|
{%- endmatch %}
|
|
|
|
|
|
|
|
{{ section }}
|
2021-04-27 12:07:38 +00:00
|
|
|
# To initialize zoxide, add this to your configuration (usually
|
|
|
|
# ~/.elvish/rc.elv):
|
2021-04-08 18:35:42 +00:00
|
|
|
#
|
2021-08-23 20:30:34 +00:00
|
|
|
# eval (zoxide init elvish | slurp)
|
|
|
|
#
|
2022-03-31 11:20:29 +00:00
|
|
|
# Note: zoxide only supports elvish v0.18.0 and above.
|