Merge branch 'update-bashmarks'

This commit is contained in:
Koichi Murase 2023-10-12 19:12:29 +09:00
commit c597bb3c20
2 changed files with 167 additions and 105 deletions

View File

@ -1,26 +1,53 @@
# Bashmarks plugin # Bashmarks plugin
The Bashmarks plugin allows you to create and use bookmarks for directories on your filesystems. The Bashmarks plugin allows you to create and use bookmarks for directories on
your filesystems. This plugin is derived from the project
https://github.com/huing/bashmarks but contains improvements. This plugin is
licensed under the 3-Clause BSD license.
## Quickstart ## Quickstart
Create a new bookmark using the *bm -a* command: Create a new bookmark using the *bm -a* command:
`$ bm -a mydir` ```bash
$ bm -a mydir
```
The command above creates a bookmark for the current directory with the name *mydir*. The command above creates a bookmark for the current directory with the name
*mydir*.
You can navigate to the location of a bookmark using the *bm -g* command: You can navigate to the location of a bookmark using the *bm -g* command:
`$ bm -g mydir` ```bash
$ bm -g mydir
```
You can also supply just any bookmark name and the *-g* option will be assumed: You can also supply just any bookmark name and the *-g* option will be assumed:
`$ bm mydir` ```bash
$ bm mydir
```
Tab completion is available when you need to enter a bookmark name in a command, such as when using *bm -g* Tab completion is available when you need to enter a bookmark name in a
command, such as when using *bm -g*
Easily list all bookmarks you have setup using the *bm -l* command. Easily list all bookmarks you have set up using the *bm -l* command:
```bash
$ bm -l
```
## Configuration
- **`BASHMARKS_SDIR`**: This variable contains the path to the file that stores
the information of bookmarks of directory names. The default path is
`~/.sdirs`. The old interface `SDIRS` is now deprecated.
## Configuration
- `BASHMARKS_SDIR`: This variable contains the path to the file that stores the
information of bookmarks of directory names. The default path is `~/.sdirs`.
The old interface `SDIRS` is now deprecated.
## Commands ## Commands
@ -38,4 +65,6 @@ Easily list all bookmarks you have setup using the *bm -l* command.
## Valid bookmark names ## Valid bookmark names
A bookmark name can contain lower and upper case characters (A-Z), numbers and underscore characters. No periods, semicolons, spaces or other characters are allowed in a bookmark name. A bookmark name can contain lower and upper case characters (A-Z), numbers and
underscore characters. No periods, semicolons, spaces or other characters are
allowed in a bookmark name.

View File

@ -1,27 +1,39 @@
#! bash oh-my-bash.module #! bash oh-my-bash.module
# Copyright (c) 2015, Toan Nguyen - https://nntoan.github.io # Copyright (c) 2010, Huy Nguyen, https://everyhue.me
# Copyright (c) 2015, Toan Nguyen, https://nntoan.github.io
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without modification, are permitted provided # This plugin is derived from the project https://github.com/huing/bashmarks.
# that the following conditions are met: # This version is based on the following commit in the upstream project:
# #
# * Redistributions of source code must retain the above copyright notice, this list of conditions # https://github.com/huyng/bashmarks/commit/264952f2225691b5f99a498e4834e2c69bf4f5f5
# and the following disclaimer. #
# * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the # This plugin is licensed under the BSD-3 License.
# following disclaimer in the documentation and/or other materials provided with the distribution. #------------------------------------------------------------------------------
# * Neither the name of Toan Nguyen Ngoc (aka NNToan) nor the names of contributors # Redistribution and use in source and binary forms, with or without
# may be used to endorse or promote products derived from this software without # modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Huy Nguyen nor the names of contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission. # specific prior written permission.
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
#------------------------------------------------------------------------------
# USAGE: # USAGE:
# bm -a bookmarkname - saves the curr dir as bookmarkname # bm -a bookmarkname - saves the curr dir as bookmarkname
@ -33,60 +45,78 @@
# bm -d [TAB] - tab completion is available # bm -d [TAB] - tab completion is available
# bm -l - list all bookmarks # bm -l - list all bookmarks
# setup file to store bookmarks # Default configurations
if [ ! -n "$SDIRS" ]; then if [[ ! ${BASHMARKS_SDIRS-} ]]; then
SDIRS=~/.sdirs BASHMARKS_SDIRS=${SDIRS:-$HOME/.sdirs}
fi
# Deprecated interfaces (2023-10-12)
_omb_deprecate_declare 20000 SDIRS BASHMARKS_SDIRS sync
_omb_deprecate_function 20000 _echo_usage _bashmarks_usage
_omb_deprecate_function 20000 _save_bookmark _bashmarks_save
_omb_deprecate_function 20000 _delete_bookmark _bashmarks_delete
_omb_deprecate_function 20000 _goto_bookmark _bashmarks_goto
_omb_deprecate_function 20000 _list_bookmark _bashmarks_list
_omb_deprecate_function 20000 _print_bookmark _bashmarks_print
_omb_deprecate_function 20000 _l _bashmarks_list_names
_omb_deprecate_function 20000 _bookmark_name_valid _bashmarks_is_valid_bookmark_name
_omb_deprecate_function 20000 _comp _bashmarks_comp_cmd_bm
_omb_deprecate_function 20000 _compzsh _bashmarks_compzsh_cmd_bm
_omb_deprecate_function 20000 _purge_line _bashmarks_purge_line
# setup file to store bookmarks
if [[ ! -e $BASHMARKS_SDIRS ]]; then
touch "$BASHMARKS_SDIRS"
fi fi
touch "$SDIRS"
# main function # main function
function bm { function bm {
option="${1}" local option=$1
case ${option} in case $option in
# save current directory to bookmarks [ bm -a BOOKMARK_NAME ] # save current directory to bookmarks [ bm -a BOOKMARK_NAME ]
-a) -a)
_save_bookmark "$2" _bashmarks_save "$2"
;; ;;
# delete bookmark [ bm -d BOOKMARK_NAME ] # delete bookmark [ bm -d BOOKMARK_NAME ]
-d) -d)
_delete_bookmark "$2" _bashmarks_delete "$2"
;; ;;
# jump to bookmark [ bm -g BOOKMARK_NAME ] # jump to bookmark [ bm -g BOOKMARK_NAME ]
-g) -g)
_goto_bookmark "$2" _bashmarks_goto "$2"
;; ;;
# print bookmark [ bm -p BOOKMARK_NAME ] # print bookmark [ bm -p BOOKMARK_NAME ]
-p) -p)
_print_bookmark "$2" _bashmarks_print "$2"
;; ;;
# show bookmark list [ bm -l ] # show bookmark list [ bm -l ]
-l) -l)
_list_bookmark _bashmarks_list
;; ;;
# help [ bm -h ] # help [ bm -h ]
-h) -h)
_echo_usage _bashmarks_usage
;; ;;
*) *)
if [[ $1 == -* ]]; then if [[ $1 == -* ]]; then
# unrecognized option. echo error message and usage [ bm -X ] # unrecognized option. echo error message and usage [ bm -X ]
echo "Unknown option '$1'" echo "Unknown option '$1'"
_echo_usage _bashmarks_usage
kill -SIGINT $$ kill -SIGINT $$
exit 1 exit 1
elif [[ $1 == "" ]]; then elif [[ $1 == "" ]]; then
# no args supplied - echo usage [ bm ] # no args supplied - echo usage [ bm ]
_echo_usage _bashmarks_usage
else else
# non-option supplied as first arg. assume goto [ bm BOOKMARK_NAME ] # non-option supplied as first arg. assume goto [ bm BOOKMARK_NAME ]
_goto_bookmark "$1" _bashmarks_goto "$1"
fi fi
;; ;;
esac esac
} }
# print usage information # print usage information
function _echo_usage { function _bashmarks_usage {
echo 'USAGE:' echo 'USAGE:'
echo "bm -h - Prints this usage info" echo "bm -h - Prints this usage info"
echo 'bm -a <bookmark_name> - Saves the current directory as "bookmark_name"' echo 'bm -a <bookmark_name> - Saves the current directory as "bookmark_name"'
@ -97,87 +127,90 @@ function _echo_usage {
} }
# save current directory to bookmarks # save current directory to bookmarks
function _save_bookmark { function _bashmarks_save {
_bookmark_name_valid "$@" if _bashmarks_is_valid_bookmark_name "$@"; then
if [ -z "$exit_message" ]; then _bashmarks_purge_line "$BASHMARKS_SDIRS" "export DIR_$1="
_purge_line "$SDIRS" "export DIR_$1="
CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g") CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g")
echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS echo "export DIR_$1=\"$CURDIR\"" >> "$BASHMARKS_SDIRS"
fi fi
} }
# delete bookmark # delete bookmark
function _delete_bookmark { function _bashmarks_delete {
_bookmark_name_valid "$@" if _bashmarks_is_valid_bookmark_name "$@"; then
if [ -z "$exit_message" ]; then _bashmarks_purge_line "$BASHMARKS_SDIRS" "export DIR_$1="
_purge_line "$SDIRS" "export DIR_$1="
unset "DIR_$1" unset "DIR_$1"
fi fi
} }
# jump to bookmark # jump to bookmark
function _goto_bookmark { function _bashmarks_goto {
source $SDIRS source "$BASHMARKS_SDIRS"
target="$(eval $(echo echo $(echo \$DIR_$1)))" local target_varname=DIR_$1
if [ -d "$target" ]; then local target=${!target_varname-}
cd "$target" if [[ -d $target ]]; then
elif [ ! -n "$target" ]; then cd "$target"
printf '%s\n' "${_omb_term_brown}WARNING: '${1}' bashmark does not exist${_omb_term_reset}" elif [[ ! $target ]]; then
else printf '%s\n' "${_omb_term_brown}WARNING: '${1}' bashmark does not exist${_omb_term_reset}"
printf '%s\n' "${_omb_term_brown}WARNING: '${target}' does not exist${_omb_term_reset}" else
fi printf '%s\n' "${_omb_term_brown}WARNING: '${target}' does not exist${_omb_term_reset}"
fi
} }
# list bookmarks with dirname # list bookmarks with dirname
function _list_bookmark { function _bashmarks_list {
source $SDIRS source "$BASHMARKS_SDIRS"
# if color output is not working for you, comment out the line below '\033[1;32m' == "red" # if color output is not working for you, comment out the line below '\033[1;32m' == "red"
env | sort | awk '/DIR_.+/{split(substr($0,5),parts,"="); printf("\033[0;33m%-20s\033[0m %s\n", parts[1], parts[2]);}' env | sort | awk '/^DIR_.+/{split(substr($0,5),parts,"="); printf("\033[0;33m%-20s\033[0m %s\n", parts[1], parts[2]);}'
# uncomment this line if color output is not working with the line above # uncomment this line if color output is not working with the line above
# env | grep "^DIR_" | cut -c5- | sort |grep "^.*=" # env | grep "^DIR_" | cut -c5- | sort |grep "^.*="
} }
# print bookmark # print bookmark
function _print_bookmark { function _bashmarks_print {
source $SDIRS source "$BASHMARKS_SDIRS"
echo "$(eval $(echo echo $(echo \$DIR_$1)))" echo "$(eval $(echo echo $(echo \$DIR_$1)))"
} }
# list bookmarks without dirname # list bookmarks without dirname
function _l { function _bashmarks_list_names {
source $SDIRS source "$BASHMARKS_SDIRS"
env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "=" env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "="
} }
# validate bookmark name # validate bookmark name
function _bookmark_name_valid { # @var[out] exit_message
exit_message="" function _bashmarks_is_valid_bookmark_name {
if [ -z $1 ]; then local exit_message=""
exit_message="bookmark name required" if [[ ! $1 ]]; then
echo $exit_message exit_message="bookmark name required"
elif [ "$1" != "$(echo $1 | sed 's/[^A-Za-z0-9_]//g')" ]; then echo "$exit_message" >&2
exit_message="bookmark name is not valid" return 1
echo $exit_message elif [[ $1 == *[!A-Za-z0-9_]* ]]; then
fi exit_message="bookmark name is not valid"
echo "$exit_message" >&2
return 1
fi
} }
# completion command # completion command
function _comp { function _bashmarks_comp_cmd_bm {
local curw COMPREPLY=()
COMPREPLY=() if ((COMP_CWORD >= 2)) && [[ ${COMP_WORDS[1]} == -[gpd] ]]; then
curw=${COMP_WORDS[COMP_CWORD]} local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W '`_l`' -- $curw)) COMPREPLY=($(compgen -W '$(_bashmarks_list_names)' -- "$cur"))
return 0 fi
return 0
} }
# ZSH completion command # ZSH completion command
function _compzsh { function _bashmarks_compzsh_cmd_bm {
reply=($(_l)) reply=($(_bashmarks_list_names))
} }
# safe delete line from sdirs # safe delete line from sdirs
function _purge_line { function _bashmarks_purge_line {
if [ -s "$1" ]; then if [[ -s $1 ]]; then
# safely create a temp file # safely create a temp file
t=$(mktemp -t bashmarks.XXXXXX) || exit 1 t=$(mktemp -t bashmarks.XXXXXX) || exit 1
trap "/bin/rm -f -- '$t'" EXIT trap "/bin/rm -f -- '$t'" EXIT
@ -192,22 +225,22 @@ function _purge_line {
fi fi
} }
# bind completion command for g,p,d to _comp # bind completion command for g,p,d to _bashmarks_comp_cmd_bm
if [ $ZSH_VERSION ]; then if [[ ${ZSH_VERSION-} ]]; then
compctl -K _compzsh bm -g compctl -K _bashmarks_compzsh_cmd_bm bm -g
compctl -K _compzsh bm -p compctl -K _bashmarks_compzsh_cmd_bm bm -p
compctl -K _compzsh bm -d compctl -K _bashmarks_compzsh_cmd_bm bm -d
compctl -K _compzsh g compctl -K _bashmarks_compzsh_cmd_bm g
compctl -K _compzsh p compctl -K _bashmarks_compzsh_cmd_bm p
compctl -K _compzsh d compctl -K _bashmarks_compzsh_cmd_bm d
else else
shopt -s progcomp shopt -s progcomp
complete -F _comp bm -g complete -F _bashmarks_comp_cmd_bm bm
complete -F _comp bm -p complete -F _bashmarks_comp_cmd_bm bm
complete -F _comp bm -d complete -F _bashmarks_comp_cmd_bm bm
complete -F _comp g complete -F _bashmarks_comp_cmd_bm g
complete -F _comp p complete -F _bashmarks_comp_cmd_bm p
complete -F _comp d complete -F _bashmarks_comp_cmd_bm d
fi fi
alias s='bm -a' # Save a bookmark [bookmark_name] alias s='bm -a' # Save a bookmark [bookmark_name]