Add auto-updating of bookmarks

This commit is contained in:
Alexander Rosenberg 2024-08-27 11:28:29 -07:00
parent 06c33df30d
commit 181dd2d533
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730
2 changed files with 18 additions and 6 deletions

View File

@ -3,9 +3,12 @@
# Enable color utilities
autoload colors && colors
autoload regexp-replace
zmodload -F zsh/stat b:zstat
zmodload zsh/datetime
__bm_bookmark_cache=()
local __bm_bookmark_cache=()
local __bm_res=()
let __bm_last_read_time=-1
function __bm_find_user_emacs_dir {
[[ -f "${HOME}/.emacs.d/var/bookmark-default.el" ]] &&
@ -24,8 +27,11 @@ function __bm_find_user_emacs_dir {
"daemon" or define $BM_BOOKMARK_PATH!\n'
return 1
}
BM_BOOKMARK_PATH="${BM_BOOKMARK_PATH:-"$(__bm_find_user_emacs_dir)"}"
function __bm_update_bookmark_list {
local quoted_output
__bm_last_read_time="${EPOCHSECONDS}"
case "${BM_MODE}" in
'daemon')
quoted_output=(${(z)${"$(command emacs -Q --batch --eval \
@ -42,9 +48,6 @@ function __bm_update_bookmark_list {
out))))))))")":1:-1}})
;;
''|'emacs')
if ! [[ -v BM_BOOKMARK_PATH ]]; then
local BM_BOOKMARK_PATH="$(__bm_find_user_emacs_dir)"
fi
quoted_output=(${(z)${"$(command emacs -Q --batch --eval \
"(prin1
(with-temp-buffer
@ -115,7 +118,7 @@ function bm {
if (( ${#__bm_res} != 0 )) && [[ -e "${bm_loc}" ]]; then
if [[ -d "${target}" ]]; then
cd "${target}"
[[ "${BM_CWD_LS}" = '1' ]] && ls || true
(( "${BM_CWD_LS:-0}" )) && ls || true
elif [[ -e "${target}" ]]; then
${=EDITOR} "${target}"
else
@ -241,4 +244,12 @@ function _bmrm {
}
compdef _bmrm bmrm
function __bm_auto_reload_hook {
if (( "${BM_AUTO_RELOAD:-0}" )) &&
(( ${__bm_last_read_time} < "$(zstat +mtime "${BM_BOOKMARK_PATH}")" )); then
__bm_update_bookmark_list
fi
}
(( ${precmd_functions[(I)__bm_auto_reload_hook]} )) || precmd_functions+=(__bm_auto_reload_hook)
__bm_update_bookmark_list

View File

@ -241,8 +241,9 @@ if cmd_exists direnv; then
fi
# Bookmarks
[[ -v BM_MODE ]] || BM_CWD_LS=1
[[ -v BM_CWD_LS ]] || BM_CWD_LS=1
[[ -v BM_MODE ]] || BM_MODE=daemon
[[ -v BM_AUTO_RELOAD ]] || BM_AUTO_RELOAD=1
source "${ZSH_CONFIG_DIR}/emacs-bookmark.zsh"
# Platform specific stuff