From 181dd2d533eaeedc2b6570ddb93771b404cc5ea9 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Tue, 27 Aug 2024 11:28:29 -0700 Subject: [PATCH] Add auto-updating of bookmarks --- emacs-bookmark.zsh | 21 ++++++++++++++++----- init.zsh | 3 ++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/emacs-bookmark.zsh b/emacs-bookmark.zsh index c095fff..b232062 100644 --- a/emacs-bookmark.zsh +++ b/emacs-bookmark.zsh @@ -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 diff --git a/init.zsh b/init.zsh index b136788..593bf0f 100644 --- a/init.zsh +++ b/init.zsh @@ -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