2024-03-10 05:54:07 -07:00
|
|
|
# Emacs-based bookmark system
|
|
|
|
|
|
|
|
# Enable color utilities
|
|
|
|
autoload colors && colors
|
2024-08-19 21:37:48 -07:00
|
|
|
autoload regexp-replace
|
2024-08-27 11:28:29 -07:00
|
|
|
zmodload -F zsh/stat b:zstat
|
|
|
|
zmodload zsh/datetime
|
2024-03-10 05:54:07 -07:00
|
|
|
|
2024-08-27 11:28:29 -07:00
|
|
|
local __bm_bookmark_cache=()
|
2024-08-20 22:26:09 -07:00
|
|
|
local __bm_res=()
|
2024-08-27 11:28:29 -07:00
|
|
|
let __bm_last_read_time=-1
|
2024-03-10 05:54:07 -07:00
|
|
|
|
|
|
|
function __bm_find_user_emacs_dir {
|
|
|
|
[[ -f "${HOME}/.emacs.d/var/bookmark-default.el" ]] &&
|
|
|
|
{ printf '%s' "${HOME}/.emacs.d/var/bookmark-default.el"; return 0 }
|
|
|
|
[[ -f "${HOME}/.emacs.d/bookmark-default.el" ]] &&
|
|
|
|
{ printf '%s' "${HOME}/.emacs.d/bookmark-default.el"; return 0 }
|
|
|
|
[[ -f "${HOME}/.config/emacs/var/bookmark-default.el" ]] &&
|
|
|
|
{ printf '%s' "${HOME}/.config/emacs/var/bookmark-default.el"; return 0 }
|
|
|
|
[[ -f "${HOME}/.config/emacs/bookmark-default.el" ]] &&
|
|
|
|
{ printf '%s' "${HOME}/.config/emacs/bookmark-default.el"; return 0 }
|
|
|
|
[[ -f "${XDG_CONFIG_HOME}/emacs/var/bookmark-default.el" ]] &&
|
|
|
|
{ printf '%s' "${XDG_CONFIG_HOME}/emacs/var/bookmark-default.el"; return 0 }
|
|
|
|
[[ -f "${XDG_CONFIG_HOME}/emacs/bookmark-default.el" ]] &&
|
|
|
|
{ printf '%s' "${XDG_CONFIG_HOME}/emacs/bookmark-default.el"; return 0 }
|
|
|
|
printf 'Could not discover Emacs bookmark file! Please set $BM_MODE to
|
|
|
|
"daemon" or define $BM_BOOKMARK_PATH!\n'
|
|
|
|
return 1
|
|
|
|
}
|
2024-08-27 11:28:29 -07:00
|
|
|
BM_BOOKMARK_PATH="${BM_BOOKMARK_PATH:-"$(__bm_find_user_emacs_dir)"}"
|
|
|
|
|
2024-03-10 05:54:07 -07:00
|
|
|
function __bm_update_bookmark_list {
|
|
|
|
local quoted_output
|
2024-08-27 11:28:29 -07:00
|
|
|
__bm_last_read_time="${EPOCHSECONDS}"
|
2024-03-10 05:54:07 -07:00
|
|
|
case "${BM_MODE}" in
|
|
|
|
'daemon')
|
2024-03-10 18:11:29 -07:00
|
|
|
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}})
|
2024-03-10 05:54:07 -07:00
|
|
|
;;
|
|
|
|
''|'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}})
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
function __bm_bookmark_location {
|
2024-08-18 11:42:48 -07:00
|
|
|
local parts=(${(s:/:)"${1}"})
|
|
|
|
local bm_name="${parts[1]}"
|
|
|
|
local rest_arr=(${parts:1})
|
|
|
|
local rest="${(j:/:)rest_arr}"
|
2024-03-10 05:54:07 -07:00
|
|
|
for ((i = 1; i < ${#__bm_bookmark_cache}; i+=4)); do
|
2024-08-18 11:42:48 -07:00
|
|
|
if [[ "${bm_name}" = "${__bm_bookmark_cache[${i}]}" ]]; then
|
2024-08-20 22:26:09 -07:00
|
|
|
__bm_res=("${__bm_bookmark_cache[${i} + 2]}"
|
|
|
|
"${__bm_bookmark_cache[${i} + 3]}"
|
|
|
|
"${rest}")
|
2024-03-10 05:54:07 -07:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
2024-08-20 22:26:09 -07:00
|
|
|
__bm_res=()
|
2024-03-10 05:54:07 -07:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
function __bm_list_bookmarks {
|
|
|
|
for ((i = 1; i < ${#__bm_bookmark_cache}; i+=4)); do
|
|
|
|
local name="${__bm_bookmark_cache[${i}]}"
|
|
|
|
local pretty_path="${__bm_bookmark_cache[${i} + 1]}"
|
|
|
|
local abs_path="${__bm_bookmark_cache[${i} + 2]}"
|
|
|
|
local print_color=""
|
|
|
|
if [[ -d "${abs_path}" ]]; then
|
|
|
|
print_color="${bold_color}${fg[blue]}"
|
|
|
|
fi
|
|
|
|
printf "${print_color}%s${reset_color} => %s\n" \
|
|
|
|
"${name}" "${pretty_path}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-03-10 06:12:51 -07:00
|
|
|
function _bookmarks {
|
|
|
|
for ((i = 1; i < ${#__bm_bookmark_cache}; i+=4)); do
|
2024-08-20 22:26:09 -07:00
|
|
|
compadd -q -S '/' "${__bm_bookmark_cache[${i}]}"
|
2024-03-10 06:12:51 -07:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-03-10 05:54:07 -07:00
|
|
|
alias lsbm="__bm_update_bookmark_list && __bm_list_bookmarks"
|
|
|
|
function bm {
|
|
|
|
__bm_update_bookmark_list || \
|
|
|
|
{ printf 'Updating bookmark list failed!\n'; return 1 }
|
|
|
|
(( ${#} == 0 )) && { __bm_list_bookmarks; return }
|
2024-08-20 22:26:09 -07:00
|
|
|
__bm_bookmark_location "${1}"
|
|
|
|
local bm_loc="${__bm_res[1]}"
|
|
|
|
local target="${__bm_res[1]}/${__bm_res[3]}"
|
|
|
|
if (( ${#__bm_res} != 0 )) && [[ -e "${bm_loc}" ]]; then
|
|
|
|
if [[ -d "${target}" ]]; then
|
|
|
|
cd "${target}"
|
2024-08-27 11:28:29 -07:00
|
|
|
(( "${BM_CWD_LS:-0}" )) && ls || true
|
2024-08-20 22:26:09 -07:00
|
|
|
elif [[ -e "${target}" ]]; then
|
|
|
|
${=EDITOR} "${target}"
|
|
|
|
else
|
|
|
|
printf 'Bookmark exists, but trailing path doesn'"'"'t: "%s"\n' \
|
2024-08-26 10:34:32 -07:00
|
|
|
"${(q)__bm_res[3]}"
|
2024-08-20 22:26:09 -07:00
|
|
|
return 1
|
2024-08-18 11:42:48 -07:00
|
|
|
fi
|
2024-03-10 05:54:07 -07:00
|
|
|
else
|
2024-08-26 10:34:32 -07:00
|
|
|
printf 'No such bookmark: "%s"\n' "${(q)1}"
|
2024-03-10 05:54:07 -07:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2024-03-10 06:12:51 -07:00
|
|
|
function _bm {
|
2024-08-26 10:34:32 -07:00
|
|
|
local arg="${(Q)words[${CURRENT}]}"
|
2024-08-19 21:37:48 -07:00
|
|
|
if ! [[ "${arg}" == */* ]]; then
|
|
|
|
_arguments '1::bookmark:_bookmarks'
|
|
|
|
else
|
2024-08-20 22:26:09 -07:00
|
|
|
__bm_bookmark_location "${arg}"
|
|
|
|
if [[ -d "${__bm_res[1]}" ]]; then
|
|
|
|
local parts=(${(s:/:)__bm_res[3]})
|
2024-08-19 21:37:48 -07:00
|
|
|
local bm="${${(s:/:)${arg}}[1]}"
|
2024-08-26 10:34:32 -07:00
|
|
|
local subdir="${(j:/:)parts[1,-2]}"
|
2024-08-19 21:37:48 -07:00
|
|
|
local search="${parts[${#parts}]}"
|
|
|
|
if ((${#parts} > 0)) && [[ "${arg}" == */ ]]; then
|
|
|
|
subdir="${subdir}/${search}"
|
|
|
|
subdir="${subdir#/}"
|
|
|
|
search=""
|
|
|
|
fi
|
2024-08-20 22:26:09 -07:00
|
|
|
local pre_path="${__bm_res[1]}/${subdir}/"
|
2024-08-26 10:34:32 -07:00
|
|
|
local raw_arg="${words[${CURRENT}]}"
|
|
|
|
local prefix="${${(s:/:)${raw_arg}}[1]}/${subdir}"
|
2024-08-20 22:26:09 -07:00
|
|
|
if ! [[ -z "${subdir}" ]]; then
|
|
|
|
prefix+='/'
|
|
|
|
fi
|
|
|
|
compset -P "${(b)prefix}"
|
|
|
|
for file in "${pre_path}${search}"*; do
|
|
|
|
compadd -W "${pre_path}" -f "${file:t}"
|
|
|
|
done
|
2024-08-19 21:37:48 -07:00
|
|
|
fi
|
|
|
|
fi
|
2024-03-10 06:12:51 -07:00
|
|
|
}
|
|
|
|
compdef _bm bm
|
2024-03-10 05:54:07 -07:00
|
|
|
|
|
|
|
function bmadd {
|
|
|
|
if [[ "${1}" = '-h' ]]; then
|
|
|
|
printf 'usage: bmadd [PATH] [NAME]\n'
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
[[ "${1}" = '--' ]] && shift 1
|
|
|
|
local name
|
|
|
|
local loc
|
|
|
|
case "${#}" in
|
|
|
|
0)
|
|
|
|
loc="${PWD}"
|
|
|
|
name="${PWD:t}"
|
|
|
|
;;
|
|
|
|
1)
|
|
|
|
loc="${1}"
|
|
|
|
name="${1:t}"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
loc="${1}"
|
|
|
|
name="${2}"
|
|
|
|
;;
|
|
|
|
esac
|
2024-03-10 06:32:19 -07:00
|
|
|
__bm_update_bookmark_list
|
|
|
|
if __bm_bookmark_location "${name}" >/dev/null; then
|
2024-08-26 10:34:32 -07:00
|
|
|
printf 'Bookmark "%s" already exists. Overwrite it? [y/N] ' "${(q)name}"
|
2024-03-10 06:32:19 -07:00
|
|
|
read -q
|
|
|
|
let ans=${?}
|
|
|
|
printf '\n'
|
|
|
|
(( ${ans} != 0 )) && return 1
|
|
|
|
fi
|
2024-03-10 05:54:07 -07:00
|
|
|
local res="$(emacsclient --eval \
|
|
|
|
"(let* ((loc \"${loc:gs#\\#\\\\#:gs#\"#\\\"#}\")
|
|
|
|
(name \"${name:gs#\\#\\\\#:gs#\"#\\\"#}\")
|
|
|
|
(res (with-temp-buffer
|
|
|
|
(set-visited-file-name loc t nil)
|
|
|
|
(bookmark-set name)
|
|
|
|
(set-buffer-modified-p nil)))
|
|
|
|
(inhibit-message t))
|
|
|
|
(bookmark-save)
|
|
|
|
res)")"
|
2024-08-26 10:34:32 -07:00
|
|
|
[[ "${res}" = 'nil' ]] && printf 'Added bookmark "%s"\n' "${(q)name}" \
|
2024-03-10 05:54:07 -07:00
|
|
|
|| { printf '%s\n' "${res}"; return 1 }
|
2024-08-24 14:01:23 -07:00
|
|
|
__bm_update_bookmark_list
|
2024-03-10 05:54:07 -07:00
|
|
|
}
|
2024-03-10 06:12:51 -07:00
|
|
|
function _bmadd {
|
|
|
|
_arguments ':file:_files' ':name'
|
|
|
|
}
|
|
|
|
compdef _bmadd bmadd
|
2024-03-10 05:54:07 -07:00
|
|
|
|
|
|
|
function bmrm {
|
|
|
|
if [[ "${1}" = '-h' ]]; then
|
|
|
|
printf 'usage: bmrm [NAME]\n'
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
[[ "${1}" = '--' ]] && shift 1
|
|
|
|
if (( ${#} < 1 )); then
|
|
|
|
printf 'usage: bmrm [NAME]\n'
|
|
|
|
return 1
|
|
|
|
fi
|
2024-03-10 06:32:19 -07:00
|
|
|
__bm_update_bookmark_list
|
|
|
|
__bm_bookmark_location "${1}" >/dev/null || \
|
|
|
|
{ printf 'No such bookmark: "%s"\n' "${1}"; return 1 }
|
2024-08-26 10:34:32 -07:00
|
|
|
printf 'Really delete "%s"? [y/N] ' "${(q)1}"
|
2024-03-10 05:54:07 -07:00
|
|
|
if read -q; then
|
|
|
|
printf '\n'
|
|
|
|
local res="$(emacsclient --eval \
|
|
|
|
"(let* ((res (bookmark-delete \"${1:gs#\\#\\\\#:gs#\"#\\\"#}\"))
|
|
|
|
(inhibit-message t))
|
|
|
|
(bookmark-save)
|
|
|
|
res)")"
|
2024-08-26 10:34:32 -07:00
|
|
|
[[ "${res}" = 'nil' ]] && printf 'Deleted bookmark "%s"\n' "${(q)1}" \
|
2024-03-10 05:54:07 -07:00
|
|
|
|| { printf '%s\n' "${res}"; return 1 }
|
2024-08-27 11:34:24 -07:00
|
|
|
|
|
|
|
__bm_update_bookmark_list
|
2024-03-10 05:54:07 -07:00
|
|
|
else
|
|
|
|
printf '\n'
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2024-03-10 06:12:51 -07:00
|
|
|
function _bmrm {
|
|
|
|
_arguments ':bookmark:_bookmarks'
|
|
|
|
}
|
|
|
|
compdef _bmrm bmrm
|
|
|
|
|
2024-08-27 11:28:29 -07:00
|
|
|
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)
|
|
|
|
|
2024-03-10 06:12:51 -07:00
|
|
|
__bm_update_bookmark_list
|