2022-08-28 02:52:37 -07:00
|
|
|
# A simple bookmark system
|
|
|
|
# $ZSH_BOOKMARK_DIR must be defined
|
|
|
|
|
|
|
|
# Create bookmark directory if it does not exist
|
|
|
|
[ ! -d "${ZSH_BOOKMARK_DIR}" ] && mkdir -p "${ZSH_BOOKMARK_DIR}"
|
|
|
|
function __zsh_list_bookmarks {
|
|
|
|
command ls "${ZSH_BOOKMARK_DIR}"
|
|
|
|
}
|
2022-11-08 22:15:32 -08:00
|
|
|
function __zsh_check_bookmark_name {
|
|
|
|
! [[ "${1}" == *'/'* ]]
|
|
|
|
}
|
2022-08-28 02:52:37 -07:00
|
|
|
|
|
|
|
# bm <bookmark>
|
|
|
|
function bm {
|
|
|
|
# With no args, list all bookmarks
|
|
|
|
[ "${#}" -eq 0 ] && { __zsh_list_bookmarks; return }
|
|
|
|
# Otherwise, goto the bookmark in argv[1]
|
2022-11-08 22:15:32 -08:00
|
|
|
__zsh_check_bookmark_name "${1}" || { echo "No such bookmark: '${1}'!"; return 1 }
|
2022-11-10 20:43:33 -08:00
|
|
|
[ -L "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "No such bookmark: '${1}'!"; return 1 }
|
|
|
|
[ -e "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "The bookmark '${1}' refers to a non-existant file!"; return 1 }
|
2022-08-28 02:52:37 -07:00
|
|
|
cd -P "${ZSH_BOOKMARK_DIR}/${1}"
|
|
|
|
[[ -v ZSH_BOOKMARK_LS ]] && "${ZSH_BOOKMARK_LS}" && ls
|
|
|
|
}
|
|
|
|
function _bm {
|
|
|
|
for file in "${ZSH_BOOKMARK_DIR}/"*; do
|
|
|
|
compadd "$(basename "${file}")"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
compdef _bm bm
|
|
|
|
|
|
|
|
# bmadd [name] [dir]
|
|
|
|
function bmadd {
|
|
|
|
# With no args, add pwd
|
|
|
|
[ "${#}" -eq 0 ] && { bmadd "$(basename "${PWD}")" "${PWD}"; return }
|
|
|
|
# With one arg, add pwd as argv[1]
|
|
|
|
[ "${#}" -eq 1 ] && { bmadd "${1}" "${PWD}"; return }
|
|
|
|
local real_path="$(realpath "${2}")"
|
|
|
|
local bm_name="${1}"
|
2022-11-08 22:15:32 -08:00
|
|
|
__zsh_check_bookmark_name "${bm_name}" || { echo "Invalid bookmark: '${1}'!"; return 1 }
|
2022-11-10 20:43:33 -08:00
|
|
|
if [ -L "${ZSH_BOOKMARK_DIR}/${bm_name}" ]; then
|
2022-08-28 02:52:37 -07:00
|
|
|
echo "A bookmark with the name '${bm_name}' already exists!"
|
|
|
|
local reply
|
|
|
|
read -q 'reply?Overwrite? [y/N] '
|
|
|
|
printf '\n'
|
2022-08-28 12:27:24 -07:00
|
|
|
[[ "${reply}" =~ '[yY]' ]] || return 1
|
2022-08-28 02:52:37 -07:00
|
|
|
fi
|
|
|
|
ln -nfs "${real_path}" "${ZSH_BOOKMARK_DIR}/${bm_name}"
|
|
|
|
echo "Created bookmark '${bm_name}'"
|
|
|
|
}
|
|
|
|
|
|
|
|
# bmrm <name>
|
|
|
|
function bmrm {
|
|
|
|
[ "${#}" -eq 0 ] && { echo "usage: bmrm <name>"; return 1 }
|
2022-11-08 22:15:32 -08:00
|
|
|
__zsh_check_bookmark_name "${1}" || { echo "No bookmark with the name '${1}' exists!"; return 1 }
|
2022-11-10 20:43:33 -08:00
|
|
|
[ -L "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "No bookmark with the name '${1}' exists!"; return 1 }
|
2022-08-28 02:52:37 -07:00
|
|
|
local reply
|
|
|
|
read -q 'reply?Really delete? [y/N] '
|
|
|
|
printf '\n'
|
2022-08-28 12:27:24 -07:00
|
|
|
[[ "${reply}" =~ '[yY]' ]] || return 1
|
2022-10-06 12:00:13 -07:00
|
|
|
command rm "${ZSH_BOOKMARK_DIR}/${1}"
|
2022-08-28 02:52:37 -07:00
|
|
|
echo "Deleted bookmark '${1}'"
|
|
|
|
}
|
|
|
|
function _bmrm {
|
|
|
|
_bm
|
|
|
|
}
|
|
|
|
compdef _bmrm bmrm
|