aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-04-10 16:29:56 -0700
committerKaz Kylheku <kaz@kylheku.com>2024-04-10 16:29:56 -0700
commitc068e59e179c92b2a7b27e5dbf737ef767e340b9 (patch)
tree6fa951415dafb9030bcdce9c747a59758388bd5c
parent5977af54dc0806445445a8732905c8d7ba572e41 (diff)
downloadcdlog-c068e59e179c92b2a7b27e5dbf737ef767e340b9.tar.gz
cdlog-c068e59e179c92b2a7b27e5dbf737ef767e340b9.tar.bz2
cdlog-c068e59e179c92b2a7b27e5dbf737ef767e340b9.zip
Allow cdalias by number or @ search.
* cdlog.sh (cdlog.alias): Remove unused local variable ali. Check for a digit or argument starting with @ and expand accordingly, checking for failure. * README.md: Documented.
-rw-r--r--README.md8
-rw-r--r--cdlog.sh24
2 files changed, 30 insertions, 2 deletions
diff --git a/README.md b/README.md
index 8af4d6c..2a0fdc3 100644
--- a/README.md
+++ b/README.md
@@ -82,6 +82,14 @@ 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.
+If the replacement (second argument) of `cdalias` is a digit
+from 1 to 9, then it refers to that entry in the `cdlog`.
+The path at that index will be retrieved and used as the alias value.
+
+If the replacement begins with `@` then it is a search. If
+the search succeeds, then the result will be used as the alias
+value.
+
## Searches
Search are arguments which begin with `@`. The rest
diff --git a/cdlog.sh b/cdlog.sh
index 25118ff..869f708 100644
--- a/cdlog.sh
+++ b/cdlog.sh
@@ -402,7 +402,6 @@ cdlog.mcd()
cdlog.alias()
{
- local ali=
local def=
if [ $# -ne 2 ] ; then
@@ -415,7 +414,28 @@ cdlog.alias()
return 1
fi
- cdlog_alias[$1]=$2
+ def=$2
+
+ case "$def" in
+ ( @* )
+ if ! def=$(cdlog.at2path "$2"); then
+ printf "cdlog.alias: no match for %s\n" "$2"
+ return 1
+ fi
+ ;;
+ ( [1-9] )
+ def=${cdlog_hist[$2]}
+ if [ -z "$def" ] ; then
+ printf "cdlog.alias: %s position empty\n" "$2"
+ return 1
+ fi
+ ;;
+ ( * )
+ def=$2
+ ;;
+ esac
+
+ cdlog_alias[$1]=$def
return 0
}