Change to a more reliable emacs communication method

This commit is contained in:
Alexander Rosenberg 2024-11-03 10:30:31 -08:00
parent 1b290bcf23
commit c58ea7b136
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730

View File

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