From efcc4b981d2a10ac3b10522c596f8bf9ddff6ff9 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 22 Feb 2024 16:38:14 -0800 Subject: Implement autocs mode. * cdlog.sh (cdlog_autocs): New variable. (cdlog.chdir): Implement autocs logic, if enabled. (cdlog.mcd): Use Bash dynamic scoping to override the cdlog_autocs variable false; we don't want mcd to have swapping semantics regardless of autocs mode; the user must use the distinct command mcs. * README.md: Documented. --- README.md | 10 ++++++++++ cdlog.sh | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index c6bf5d8..7c7bd3f 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,16 @@ When one or more persisted session are present, `cdlog`'s initialization mentions this, suggesting that the `cdr` command may be used to recover to one of the sessions. +## `autocs` mode + +The variable `cdlog_autocs`, whose default value is `y` (enabled) +controls `autocs` mode. In `autocs` mode, whenever the +`cd`/`cdlog.chdir` command successfully changes to a new directory, +and that directory already occurs in the FIFO, then the previous +current directory is placed into the FIFO, overwriting that entry. +Effectively, the original current directory is swapped with the +FIFO entry as if using the `cs ` command. + ## 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 59368f9..017a9f0 100644 --- a/cdlog.sh +++ b/cdlog.sh @@ -1,5 +1,9 @@ # License at bottom. +# Configuration variables + +cdlog_autocs=${cdlog_autocs-y} # cd behaves like cs if match + # Update after FIFO change. cdlog.update() { @@ -142,11 +146,25 @@ cdlog.recover() cdlog.chdir() { local cur=$PWD + local i=10 if command cd "$@" && [ "$PWD" != "$cur" ]; then # only if we successfully change to a different # directory do the following + if [ $cdlog_autocs ] ; then + local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9") + unset d[0] + + for ((i = 1; i <= 9; i++)); do + if [ "${d[$i]}" = "$PWD" ] ; then + d[$i]=$cur + cdlog.args "${d[@]}" + return 0 + fi + done + fi + c9=$c8; c8=$c7; c7=$c6 c6=$c5; c5=$c4; c4=$c3 c3=$c2; c2=$c1 @@ -253,6 +271,7 @@ cdlog.mcd() local res=1 local i local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9") + local cdlog_autocs= # override to off; mcd doesn't do autocs if [ $# -gt 0 ] && [ "$1" = -s ] ; then swap=y -- cgit v1.2.3