aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md15
-rw-r--r--cdlog.sh35
2 files changed, 48 insertions, 2 deletions
diff --git a/README.md b/README.md
index 7c7bd3f..64214e2 100644
--- a/README.md
+++ b/README.md
@@ -50,6 +50,8 @@ presents you with a numbered list of these sessions. You can recover to one of
the sessions, use a `c` prefix on the number to clone the session into
the current session, or press Enter to do nothing.
+* The `cdalias` command is used for defining "cd aliases"; see below.
+
In addition, the `cdlog.sh` script sets the bash `direxpand` option.
With the `direxpand` option, Tab completion on a directory coming
from a variable name will expand that variable into the command line.
@@ -57,6 +59,19 @@ 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.
+## Aliases
+
+`cdlog` provides its own "cd alias" feature. If the argument to
+`cd` contains no slashes, then it is used as a key to look up an
+alias, which replaces it. The replacement is not considered to
+be an alias even if it looks like one. Aliases are defined using
+the `cdalias` command which is an alias. It takes two arguments.
+
+Implementation notes: aliases are stored in the `cdlog_alias`
+array. The `cdlog.chdir` function recognizes an alias only if
+it is invoked as `cdlog.chdir -p <alias>`, which is the way
+`cd` invokes it.
+
## Persistence
Whenever the nine-element FIFO changes, the current directory and the
diff --git a/cdlog.sh b/cdlog.sh
index 017a9f0..e1a6181 100644
--- a/cdlog.sh
+++ b/cdlog.sh
@@ -1,7 +1,9 @@
# License at bottom.
-# Configuration variables
+# Globals
+declare -A cdlog_alias # cd aliases
+# Configuration variables
cdlog_autocs=${cdlog_autocs-y} # cd behaves like cs if match
# Update after FIFO change.
@@ -146,7 +148,21 @@ cdlog.recover()
cdlog.chdir()
{
local cur=$PWD
- local i=10
+ local i
+ local def
+
+ if [ $# -eq 2 -a "$1" = -P ] ; then
+ case $2 in
+ ( */* )
+ ;;
+ ( * )
+ def=${cdlog_alias[$2]}
+ if [ -n "$def" ] ; then
+ set -- -P "$def"
+ fi
+ ;;
+ esac
+ fi
if command cd "$@" && [ "$PWD" != "$cur" ]; then
# only if we successfully change to a different
@@ -303,6 +319,20 @@ cdlog.mcd()
return $res
}
+cdlog.alias()
+{
+ local ali=
+ local def=
+
+ if [ $# -ne 2 ] ; then
+ printf "cdlog.alias: two arguments required\n"
+ return 1
+ fi
+
+ cdlog_alias[$1]=$2
+ return 0
+}
+
# Aliases.
alias cd='cdlog.chdir -P'
alias pd='cdlog.pop'
@@ -312,6 +342,7 @@ alias cll='cdlog -l'
alias mcd='cdlog.mcd'
alias mcs='cdlog.mcd -s'
alias cdr='cdlog.recover'
+alias cdalias='cdlog.alias'
# Better completion for $x[Tab]
shopt -s direxpand