2022-01-15 19:00:35 +09:00
|
|
|
#! bash oh-my-bash.module
|
2023-10-12 15:20:52 +09:00
|
|
|
# Copyright (c) 2010, Huy Nguyen, https://everyhue.me
|
|
|
|
# Copyright (c) 2015, Toan Nguyen, https://nntoan.github.io
|
2017-03-19 15:40:25 +07:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
2023-10-12 15:20:52 +09:00
|
|
|
# This plugin is derived from the project https://github.com/huing/bashmarks.
|
|
|
|
# This version is based on the following commit in the upstream project:
|
2017-03-19 15:40:25 +07:00
|
|
|
#
|
2023-10-12 15:26:27 +09:00
|
|
|
# https://github.com/huyng/bashmarks/commit/264952f2225691b5f99a498e4834e2c69bf4f5f5
|
2023-10-12 15:20:52 +09:00
|
|
|
#
|
|
|
|
# This plugin is licensed under the BSD-3 License.
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
# Redistribution and use in source and binary forms, with or 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
|
2017-03-19 15:40:25 +07:00
|
|
|
# specific prior written permission.
|
|
|
|
#
|
2023-10-12 15:20:52 +09:00
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# 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
|
2017-03-19 15:40:25 +07:00
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
2023-10-12 15:20:52 +09:00
|
|
|
#------------------------------------------------------------------------------
|
2017-03-19 15:40:25 +07:00
|
|
|
|
|
|
|
# USAGE:
|
|
|
|
# bm -a bookmarkname - saves the curr dir as bookmarkname
|
|
|
|
# bm -g bookmarkname - jumps to the that bookmark
|
|
|
|
# bm -g b[TAB] - tab completion is available
|
|
|
|
# bm -p bookmarkname - prints the bookmark
|
|
|
|
# bm -p b[TAB] - tab completion is available
|
|
|
|
# bm -d bookmarkname - deletes the bookmark
|
|
|
|
# bm -d [TAB] - tab completion is available
|
|
|
|
# bm -l - list all bookmarks
|
|
|
|
|
2023-10-12 15:54:14 +09:00
|
|
|
# Default configurations
|
|
|
|
if [[ ! ${BASHMARKS_SDIRS-} ]]; then
|
|
|
|
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
|
|
|
|
|
2017-03-19 15:40:25 +07:00
|
|
|
# setup file to store bookmarks
|
2023-10-12 15:54:14 +09:00
|
|
|
if [[ ! -e $BASHMARKS_SDIRS ]]; then
|
|
|
|
touch "$BASHMARKS_SDIRS"
|
2017-03-19 15:40:25 +07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# main function
|
|
|
|
function bm {
|
2023-10-12 15:35:23 +09:00
|
|
|
local option=$1
|
|
|
|
case $option in
|
2017-03-20 19:51:00 +07:00
|
|
|
# save current directory to bookmarks [ bm -a BOOKMARK_NAME ]
|
|
|
|
-a)
|
2023-10-12 15:54:14 +09:00
|
|
|
_bashmarks_save "$2"
|
2017-03-20 19:51:00 +07:00
|
|
|
;;
|
|
|
|
# delete bookmark [ bm -d BOOKMARK_NAME ]
|
|
|
|
-d)
|
2023-10-12 15:54:14 +09:00
|
|
|
_bashmarks_delete "$2"
|
2017-03-20 19:51:00 +07:00
|
|
|
;;
|
|
|
|
# jump to bookmark [ bm -g BOOKMARK_NAME ]
|
|
|
|
-g)
|
2023-10-12 15:54:14 +09:00
|
|
|
_bashmarks_goto "$2"
|
2017-03-20 19:51:00 +07:00
|
|
|
;;
|
|
|
|
# print bookmark [ bm -p BOOKMARK_NAME ]
|
|
|
|
-p)
|
2023-10-12 15:54:14 +09:00
|
|
|
_bashmarks_print "$2"
|
2017-03-20 19:51:00 +07:00
|
|
|
;;
|
|
|
|
# show bookmark list [ bm -l ]
|
|
|
|
-l)
|
2023-10-12 15:54:14 +09:00
|
|
|
_bashmarks_list
|
2017-03-20 19:51:00 +07:00
|
|
|
;;
|
|
|
|
# help [ bm -h ]
|
2019-07-26 00:24:54 -07:00
|
|
|
-h)
|
2023-10-12 15:54:14 +09:00
|
|
|
_bashmarks_usage
|
2019-07-26 00:24:54 -07:00
|
|
|
;;
|
2017-03-20 19:51:00 +07:00
|
|
|
*)
|
2019-07-26 00:24:54 -07:00
|
|
|
if [[ $1 == -* ]]; then
|
|
|
|
# unrecognized option. echo error message and usage [ bm -X ]
|
|
|
|
echo "Unknown option '$1'"
|
2023-10-12 15:54:14 +09:00
|
|
|
_bashmarks_usage
|
2019-07-26 00:24:54 -07:00
|
|
|
kill -SIGINT $$
|
|
|
|
exit 1
|
|
|
|
elif [[ $1 == "" ]]; then
|
|
|
|
# no args supplied - echo usage [ bm ]
|
2023-10-12 15:54:14 +09:00
|
|
|
_bashmarks_usage
|
2019-07-26 00:24:54 -07:00
|
|
|
else
|
|
|
|
# non-option supplied as first arg. assume goto [ bm BOOKMARK_NAME ]
|
2023-10-12 15:54:14 +09:00
|
|
|
_bashmarks_goto "$1"
|
2019-07-26 00:24:54 -07:00
|
|
|
fi
|
2017-03-20 19:51:00 +07:00
|
|
|
;;
|
|
|
|
esac
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
2019-07-26 00:24:54 -07:00
|
|
|
# print usage information
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_usage {
|
2019-07-26 00:24:54 -07:00
|
|
|
echo 'USAGE:'
|
|
|
|
echo "bm -h - Prints this usage info"
|
|
|
|
echo 'bm -a <bookmark_name> - Saves the current directory as "bookmark_name"'
|
|
|
|
echo 'bm [-g] <bookmark_name> - Goes (cd) to the directory associated with "bookmark_name"'
|
|
|
|
echo 'bm -p <bookmark_name> - Prints the directory associated with "bookmark_name"'
|
|
|
|
echo 'bm -d <bookmark_name> - Deletes the bookmark'
|
|
|
|
echo 'bm -l - Lists all available bookmarks'
|
|
|
|
}
|
|
|
|
|
2017-03-19 15:40:25 +07:00
|
|
|
# save current directory to bookmarks
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_save {
|
|
|
|
if _bashmarks_is_valid_bookmark_name "$@"; then
|
|
|
|
_bashmarks_purge_line "$BASHMARKS_SDIRS" "export DIR_$1="
|
2017-03-20 19:51:00 +07:00
|
|
|
CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g")
|
2023-10-12 15:54:14 +09:00
|
|
|
echo "export DIR_$1=\"$CURDIR\"" >> "$BASHMARKS_SDIRS"
|
2017-03-20 19:51:00 +07:00
|
|
|
fi
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
# delete bookmark
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_delete {
|
|
|
|
if _bashmarks_is_valid_bookmark_name "$@"; then
|
|
|
|
_bashmarks_purge_line "$BASHMARKS_SDIRS" "export DIR_$1="
|
2017-03-20 19:51:00 +07:00
|
|
|
unset "DIR_$1"
|
|
|
|
fi
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
# jump to bookmark
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_goto {
|
|
|
|
source "$BASHMARKS_SDIRS"
|
2023-10-12 15:35:23 +09:00
|
|
|
local target_varname=DIR_$1
|
|
|
|
local target=${!target_varname-}
|
|
|
|
if [[ -d $target ]]; then
|
2023-10-12 15:21:51 +09:00
|
|
|
cd "$target"
|
2023-10-12 15:35:23 +09:00
|
|
|
elif [[ ! $target ]]; then
|
2023-10-12 15:21:51 +09:00
|
|
|
printf '%s\n' "${_omb_term_brown}WARNING: '${1}' bashmark does not exist${_omb_term_reset}"
|
|
|
|
else
|
|
|
|
printf '%s\n' "${_omb_term_brown}WARNING: '${target}' does not exist${_omb_term_reset}"
|
|
|
|
fi
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
# list bookmarks with dirname
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_list {
|
|
|
|
source "$BASHMARKS_SDIRS"
|
2023-10-12 15:21:51 +09:00
|
|
|
# if color output is not working for you, comment out the line below '\033[1;32m' == "red"
|
2023-10-12 15:26:27 +09:00
|
|
|
env | sort | awk '/^DIR_.+/{split(substr($0,5),parts,"="); printf("\033[0;33m%-20s\033[0m %s\n", parts[1], parts[2]);}'
|
2023-10-12 15:21:51 +09:00
|
|
|
# uncomment this line if color output is not working with the line above
|
|
|
|
# env | grep "^DIR_" | cut -c5- | sort |grep "^.*="
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
# print bookmark
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_print {
|
|
|
|
source "$BASHMARKS_SDIRS"
|
2023-10-12 15:21:51 +09:00
|
|
|
echo "$(eval $(echo echo $(echo \$DIR_$1)))"
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
# list bookmarks without dirname
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_list_names {
|
|
|
|
source "$BASHMARKS_SDIRS"
|
2023-10-12 15:21:51 +09:00
|
|
|
env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "="
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
# validate bookmark name
|
2023-10-12 15:35:23 +09:00
|
|
|
# @var[out] exit_message
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_is_valid_bookmark_name {
|
|
|
|
local exit_message=""
|
2023-10-12 15:35:23 +09:00
|
|
|
if [[ ! $1 ]]; then
|
2023-10-12 15:21:51 +09:00
|
|
|
exit_message="bookmark name required"
|
2023-10-12 15:54:14 +09:00
|
|
|
echo "$exit_message" >&2
|
|
|
|
return 1
|
2023-10-12 15:35:23 +09:00
|
|
|
elif [[ $1 == *[!A-Za-z0-9_]* ]]; then
|
2023-10-12 15:21:51 +09:00
|
|
|
exit_message="bookmark name is not valid"
|
2023-10-12 15:54:14 +09:00
|
|
|
echo "$exit_message" >&2
|
|
|
|
return 1
|
2023-10-12 15:21:51 +09:00
|
|
|
fi
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
# completion command
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_comp_cmd_bm {
|
2023-10-12 15:21:51 +09:00
|
|
|
COMPREPLY=()
|
2023-10-12 15:46:28 +09:00
|
|
|
if ((COMP_CWORD >= 2)) && [[ ${COMP_WORDS[1]} == -[gpd] ]]; then
|
|
|
|
local cur=${COMP_WORDS[COMP_CWORD]}
|
2023-10-12 15:54:14 +09:00
|
|
|
COMPREPLY=($(compgen -W '$(_bashmarks_list_names)' -- "$cur"))
|
2023-10-12 15:46:28 +09:00
|
|
|
fi
|
2023-10-12 15:21:51 +09:00
|
|
|
return 0
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
# ZSH completion command
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_compzsh_cmd_bm {
|
|
|
|
reply=($(_bashmarks_list_names))
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
# safe delete line from sdirs
|
2023-10-12 15:54:14 +09:00
|
|
|
function _bashmarks_purge_line {
|
2023-10-12 15:35:23 +09:00
|
|
|
if [[ -s $1 ]]; then
|
2017-03-20 19:51:00 +07:00
|
|
|
# safely create a temp file
|
|
|
|
t=$(mktemp -t bashmarks.XXXXXX) || exit 1
|
|
|
|
trap "/bin/rm -f -- '$t'" EXIT
|
|
|
|
|
|
|
|
# purge line
|
2020-02-12 06:46:00 +01:00
|
|
|
sed "/$2/d" "$1" >| "$t"
|
2017-03-20 19:51:00 +07:00
|
|
|
/bin/mv "$t" "$1"
|
|
|
|
|
|
|
|
# cleanup temp file
|
|
|
|
/bin/rm -f -- "$t"
|
|
|
|
trap - EXIT
|
|
|
|
fi
|
2017-03-19 15:40:25 +07:00
|
|
|
}
|
|
|
|
|
2023-10-12 15:54:14 +09:00
|
|
|
# bind completion command for g,p,d to _bashmarks_comp_cmd_bm
|
2023-10-12 15:35:23 +09:00
|
|
|
if [[ ${ZSH_VERSION-} ]]; then
|
2023-10-12 15:54:14 +09:00
|
|
|
compctl -K _bashmarks_compzsh_cmd_bm bm -g
|
|
|
|
compctl -K _bashmarks_compzsh_cmd_bm bm -p
|
|
|
|
compctl -K _bashmarks_compzsh_cmd_bm bm -d
|
|
|
|
compctl -K _bashmarks_compzsh_cmd_bm g
|
|
|
|
compctl -K _bashmarks_compzsh_cmd_bm p
|
|
|
|
compctl -K _bashmarks_compzsh_cmd_bm d
|
2017-03-19 15:40:25 +07:00
|
|
|
else
|
2017-03-20 19:51:00 +07:00
|
|
|
shopt -s progcomp
|
2023-10-12 15:54:14 +09:00
|
|
|
complete -F _bashmarks_comp_cmd_bm bm
|
|
|
|
complete -F _bashmarks_comp_cmd_bm bm
|
|
|
|
complete -F _bashmarks_comp_cmd_bm bm
|
|
|
|
complete -F _bashmarks_comp_cmd_bm g
|
|
|
|
complete -F _bashmarks_comp_cmd_bm p
|
|
|
|
complete -F _bashmarks_comp_cmd_bm d
|
2017-03-19 15:40:25 +07:00
|
|
|
fi
|
|
|
|
|
|
|
|
alias s='bm -a' # Save a bookmark [bookmark_name]
|
|
|
|
alias g='bm -g' # Go to bookmark [bookmark_name]
|
|
|
|
alias p='bm -p' # Print bookmark of a path [path]
|
|
|
|
alias d='bm -d' # Delete a bookmark [bookmark_name]
|