From 12fc4e20184060d3344b72b047dc31dc44065610 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 23 Feb 2024 12:39:10 -0800 Subject: 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. --- README.md | 6 ++++++ cdlog.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) 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 -- cgit v1.2.3