aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-02-23 12:39:10 -0800
committerKaz Kylheku <kaz@kylheku.com>2024-02-23 12:39:10 -0800
commit12fc4e20184060d3344b72b047dc31dc44065610 (patch)
tree5903a2a384fcbdc2639b102d96a0603c421b5799
parent8629957d6c727d3db36123dcc5675c4fd661f680 (diff)
downloadcdlog-12fc4e20184060d3344b72b047dc31dc44065610.tar.gz
cdlog-12fc4e20184060d3344b72b047dc31dc44065610.tar.bz2
cdlog-12fc4e20184060d3344b72b047dc31dc44065610.zip
Add tab completion for cd aliases.
* README.md: Documented. * cdlog.sh (cdlog.complete): Completion function that completes on directories, including CDPATH, as well as cdlog's cd aliases. (toplevel): Register the completion function.
-rw-r--r--README.md6
-rw-r--r--cdlog.sh29
2 files changed, 35 insertions, 0 deletions
diff --git a/README.md b/README.md
index 8c54ac4..2a248f1 100644
--- a/README.md
+++ b/README.md
@@ -105,6 +105,12 @@ the FIFO.
LRU mode keeps duplicate directories out of the FIFO and while
promoting recently used directories toward the top.
+## Completion
+
+`cdlog` provides its own Tab completion for the `cd` command, overriding
+the built-in completion. It includes the cd aliases, while completing
+on directories, taking into account `CDPATH`.
+
## How is this better?
* It's not hard-coded C code inside the shell; it consists of a small amount of
diff --git a/cdlog.sh b/cdlog.sh
index b7d814d..fd1b220 100644
--- a/cdlog.sh
+++ b/cdlog.sh
@@ -337,6 +337,33 @@ cdlog.alias()
return 0
}
+# Tab completion for cd aliases
+
+cdlog.complete()
+{
+ COMPREPLY=()
+ local cur=${COMP_WORDS[COMP_CWORD]}
+ local alias
+ local dir
+
+ COMPREPLY+=( $(compgen -d -- "$cur") )
+
+ for alias in "${!cdlog_alias[@]}"; do
+ case "$alias" in
+ ( "$cur"* )
+ COMPREPLY+=( "$alias" )
+ ;;
+ esac
+ done
+
+ IFS=':' set -- $CDPATH
+ for dir in "$@"; do
+ COMPREPLY+=( $(compgen -d -- "$dir/$cur") )
+ done
+
+ return 0
+}
+
# Aliases.
alias cd='cdlog.chdir -P'
alias pd='cdlog.pop'
@@ -359,6 +386,8 @@ if [ -z "$c1" -o -z "$cdlog_dirs" ] ; then
cdlog.init
fi
+# Register completion
+complete -F cdlog.complete cd
# Copyright 2024
# Kaz Kylheku <kaz@kylheku.com>