diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2024-01-19 08:11:12 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2024-01-19 08:11:12 -0800 |
commit | 79133a2f01c4ab988ea48d032ac07eeb8116ad54 (patch) | |
tree | 10d9eb9089e36c852918ce25a53817715252182d | |
parent | 5f07a27b7e0f44c2f1fec70165a13eb6f0c72408 (diff) | |
download | cdlog-79133a2f01c4ab988ea48d032ac07eeb8116ad54.tar.gz cdlog-79133a2f01c4ab988ea48d032ac07eeb8116ad54.tar.bz2 cdlog-79133a2f01c4ab988ea48d032ac07eeb8116ad54.zip |
cs takes argument; swapping behavior removed from cd.
* cdlog.sh (cdlog.chdir): No specifial behavior when $PWD
equals $c1.
(cdlog.swap): Swapping behavior implemented here now,
with optional numeric argument to swap with entries
other than first.
-rw-r--r-- | README.md | 21 | ||||
-rw-r--r-- | cdlog.sh | 92 |
2 files changed, 79 insertions, 34 deletions
@@ -19,9 +19,11 @@ is erased. The entries are stored in the variables `c1` through `c9`. The first four entries are also copied into the variables `x`, `y`, `z` and `w` for shorter access. -* `cs` (cd swap) is an alias for a command which changes back to the most -recent directory in the log as if by using `cd "$c1"` or `cd "$x"`. -This triggers a special swapping behavior in `cdlog.chdir`; see below. +* `cs` (cd swap) is an alias for a command which exchanges +the current directory with a selected `cslog` entry selected +by a numeric argument, which defaults to 1. The current directory +overwrites that entry in the log, and then the shell changes to the directory +which was previously in that entry. * `pd` (pop dir) is an alias for a command which changes to the most recent directory in the log, and removes that entry from the log. The second @@ -37,15 +39,6 @@ from a variable name will expand that variable into the command line. The directory changing commands do nothing if you attempt to change into the same directory you are already in. There is no effect on the FIFO. -The internal function `cdlog.chdir` has a special behavior. -If you change to the directory that is currently in `c1`, -a swap is performed. Rather than pushing a new entry into the `cdlog` FIFO, -the most recent entry is replaced with the previous directory -that you are coming from. For instance if the current directory -is `/etc` and `c1` holds `/var`, then `cdlog.chdir /var` -will change to `/var` and just replace `c1` with `/etc`, without rotating -through the FIFO entries `c2` through `c9`. - ## How is this better? * It's not hard-coded C code inside the shell; it consists of a small amount of @@ -70,10 +63,6 @@ tabs engage further completion. * Bash's `direxpand` option does not work for `~-n` notation. Completion works, but the notation remains unexpanded. -* `cdlog`'s data, consisting purely of variables, can be passed to a child -process so that the log is available in a subshell. Just execute `export c1 c2 -c3 c4 c5 c6 c7 c8 c9 x y z w`. - * Most of `cdlog` is portable to other shells. All that is nonportable in the code is the use of `local`, and the `shopt -o direxpand` command. @@ -30,28 +30,84 @@ cdlog.chdir() # only if we successfully change to a different # directory do the following - if [ "$PWD" == "$c1" ] ; then - # If we changed to the directory that is most - # recent in the cdlog, then we just replace that - # entry by the one we are coming from. - c1=$cur - else - # Otherwise rotate through all - c9=$c8 - c8=$c7 - c7=$c6 - c6=$c5 - c5=$c4 - c4=$c3 - c3=$c2 - c2=$c1 - c1=$cur - fi + c9=$c8 + c8=$c7 + c7=$c6 + c6=$c5 + c5=$c4 + c4=$c3 + c3=$c2 + c2=$c1 + c1=$cur cdlog.nicks fi } +# Swap current directory with specified directory +cdlog.swap() +{ + local cur=$PWD + local n=1 + + if [ $# -eq 1 ]; then + n=$1 + fi + + case $n in + ( 1 | x ) + if command cd "$c1" ; then + c1=$cur + fi + ;; + ( 2 | y ) + if command cd "$c2" ; then + c2=$cur + fi + ;; + ( 3 | z ) + if command cd "$c3" ; then + c3=$cur + fi + ;; + ( 4 | w ) + if command cd "$c4" ; then + c4=$cur + fi + ;; + ( 5 ) + if command cd "$c5" ; then + c5=$cur + fi + ;; + ( 6 ) + if command cd "$c6" ; then + c6=$cur + fi + ;; + ( 7 ) + if command cd "$c7" ; then + c7=$cur + fi + ;; + ( 8 ) + if command cd "$c8" ; then + c8=$cur + fi + ;; + ( 9 ) + if command cd "$c9" ; then + c9=$cur + fi + ;; + ( * ) + printf "cdlog.swap: bad argument: %s\n" "$n" + ;; + esac + + cdlog.nicks +} + # Change to most recent diretory in cdlog and remove it # from the log. cdlog.pop() @@ -82,7 +138,7 @@ cdlog() } # Aliases. -alias cs='cdlog.chdir "$c1"' +alias cs='cdlog.swap' alias cd='cdlog.chdir -P' alias pd='cdlog.pop' |