diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2024-02-22 16:38:14 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2024-02-22 16:38:14 -0800 |
commit | efcc4b981d2a10ac3b10522c596f8bf9ddff6ff9 (patch) | |
tree | fa931c217f3a619e8d8c45279f501ab10f110e79 | |
parent | f34b25a9bdb1ef8d61d0d34fe04c40c1aec24270 (diff) | |
download | cdlog-efcc4b981d2a10ac3b10522c596f8bf9ddff6ff9.tar.gz cdlog-efcc4b981d2a10ac3b10522c596f8bf9ddff6ff9.tar.bz2 cdlog-efcc4b981d2a10ac3b10522c596f8bf9ddff6ff9.zip |
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.
-rw-r--r-- | README.md | 10 | ||||
-rw-r--r-- | cdlog.sh | 19 |
2 files changed, 29 insertions, 0 deletions
@@ -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 <n>` command. + ## How is this better? * It's not hard-coded C code inside the shell; it consists of a small amount of @@ -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 |