diff --git a/emacs-bookmark.zsh b/emacs-bookmark.zsh index a7f9058..cacba9e 100644 --- a/emacs-bookmark.zsh +++ b/emacs-bookmark.zsh @@ -1,14 +1,11 @@ # Emacs-based bookmark system -# Enable color utilities autoload colors && colors -autoload regexp-replace zmodload -F zsh/stat b:zstat zmodload zsh/datetime zmodload zsh/mapfile local __bm_bookmark_cache=() -local __bm_res=() let __bm_last_read_time=-1 function __bm_offset_to_row_col { @@ -67,50 +64,47 @@ function __bm_hash_dirs { } function __bm_update_bookmark_list { - local quoted_output __bm_last_read_time="${EPOCHSECONDS}" + local args + local script case "${BM_MODE}" in 'daemon') - quoted_output=(${(z)${"$(command emacs -Q --batch --eval \ -"(prin1 - (progn - (require 'server) - (server-eval-at \"server\" - '(let ((out)) - (dolist (entry bookmark-alist out) - (let ((path (alist-get 'filename (cdr entry) "")) - (pos (alist-get 'position (cdr entry) 1))) - (setq out (append (list (car entry) path - (expand-file-name path) pos) - out))))))))")":1:-1}}) + script=$(<<'EOF' +(progn + (require 'server) + (dolist (entry (server-eval-at + "server" + '(when (boundp 'bookmark-alist) + bookmark-alist))) + (let ((path (alist-get 'filename (cdr entry) "")) + (pos (alist-get 'position (cdr entry) 1))) + (princ (format "%s\0%s\0%s\0%s\0" (car entry) path + (expand-file-name path) pos))))) +EOF + ) ;; ''|'emacs') - quoted_output=(${(z)${"$(command emacs -Q --batch --eval \ -"(prin1 - (with-temp-buffer - (insert-file-contents \"${BM_BOOKMARK_PATH:gs#\\#\\\\#:gs#\"#\\\"#}\") - (require 'cl-lib) - (let ((out)) - (dolist (entry (read (current-buffer)) out) - (let ((path (alist-get 'filename (cdr entry) "")) - (pos (alist-get 'position (cdr entry) 1))) - (setq out (append (list (car entry) path - (expand-file-name path) pos) - out)))))))))")":1:-1}}) + args="--insert=${BM_BOOKMARK_PATH}" + script=$(<<'EOF' +(progn + (dolist (entry (read (current-buffer))) + (let ((path (alist-get 'filename (cdr entry) "")) + (pos (alist-get 'position (cdr entry) 1))) + (princ (format "%s\0%s\0%s\0%s\0" (car entry) path + (expand-file-name path) pos))))) +EOF + ) ;; *) - printf 'Unknown value for $BM_MODE: "%s"\n' "${BM_MODE}" - return 1 - ;; + printf 'Unknown value for $BM_MODE: "%s"\n' "${BM_MODE}" + return 1 + ;; esac - __bm_bookmark_cache=() - for entry in ${quoted_output}; do - __bm_bookmark_cache+="${(Q)entry}" - done + __bm_bookmark_cache=(${(0)"$(command emacs --batch ${args} --eval "${script}")"}) __bm_hash_dirs } function __bm_bookmark_location { - local parts=(${(s:/:)"${1}"}) + local parts=(${(s:/:)"${2}"}) local bm_name="${parts[1]}" local rest_arr=(${parts:1}) local rest="${(j:/:)rest_arr}" @@ -150,7 +144,8 @@ function bm { __bm_update_bookmark_list || \ { printf 'Updating bookmark list failed!\n'; return 1 } (( ${#} == 0 )) && { __bm_list_bookmarks; return } - __bm_bookmark_location "${1}" + local __bm_res + __bm_bookmark_location __bm_res "${1}" local bm_loc="${__bm_res[1]}" local target="${__bm_res[1]}/${__bm_res[3]}" if (( ${#__bm_res} != 0 )) && [[ -e "${bm_loc}" ]]; then @@ -176,10 +171,11 @@ function _bm { local arg="${(Q)words[${CURRENT}]}" if ! [[ "${arg}" == */* ]]; then for ((i = 1; i < ${#__bm_bookmark_cache}; i+=4)); do - compadd -q -S '/' "${__bm_bookmark_cache[${i}]}" + compadd -q -S '/' -- "${__bm_bookmark_cache[${i}]}" done else - __bm_bookmark_location "${arg}" + local __bm_res + __bm_bookmark_location __bm_res "${arg}" if [[ -d "${__bm_res[1]}" ]]; then local parts=(${(s:/:)__bm_res[3]}) local bm="${${(s:/:)${arg}}[1]}" @@ -223,12 +219,13 @@ function bmadd { name="${1:t}" ;; *) - loc="${1}" - name="${2}" - ;; + loc="${1}" + name="${2}" + ;; esac __bm_update_bookmark_list - if __bm_bookmark_location "${name}" >/dev/null; then + local __bm_res + if __bm_bookmark_location __bm_res "${name}" >/dev/null; then printf 'Bookmark "%s" already exists. Overwrite it? [y/N] ' "${(q)name}" read -q let ans=${?} @@ -265,7 +262,8 @@ function bmrm { return 1 fi __bm_update_bookmark_list - __bm_bookmark_location "${1}" >/dev/null || \ + local __bm_res + __bm_bookmark_location __bm_res "${1}" >/dev/null || \ { printf 'No such bookmark: "%s"\n' "${1}"; return 1 } printf 'Really delete "%s"? [y/N] ' "${(q)1}" if read -q; then @@ -275,10 +273,10 @@ function bmrm { (inhibit-message t)) (bookmark-save) res)")" - [[ "${res}" = 'nil' ]] && printf 'Deleted bookmark "%s"\n' "${(q)1}" \ - || { printf '%s\n' "${res}"; return 1 } + [[ "${res}" = 'nil' ]] && printf 'Deleted bookmark "%s"\n' "${(q)1}" \ + || { printf '%s\n' "${res}"; return 1 } - __bm_update_bookmark_list + __bm_update_bookmark_list else printf '\n' return 1