From 7f7aa9def40c38f73bfd2a7363c9ca66f9147ca2 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Wed, 21 Aug 2024 01:26:09 -0400 Subject: [PATCH] Update emacs-bookmark.zsh --- emacs-bookmark.zsh | 59 +++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/emacs-bookmark.zsh b/emacs-bookmark.zsh index d058bb4..f8c1365 100644 --- a/emacs-bookmark.zsh +++ b/emacs-bookmark.zsh @@ -5,6 +5,7 @@ autoload colors && colors autoload regexp-replace __bm_bookmark_cache=() +local __bm_res=() function __bm_find_user_emacs_dir { [[ -f "${HOME}/.emacs.d/var/bookmark-default.el" ]] && @@ -74,12 +75,13 @@ function __bm_bookmark_location { local rest="${(j:/:)rest_arr}" for ((i = 1; i < ${#__bm_bookmark_cache}; i+=4)); do if [[ "${bm_name}" = "${__bm_bookmark_cache[${i}]}" ]]; then - printf '%s\n%d\n%s\n' "${__bm_bookmark_cache[${i} + 2]}" \ - "${__bm_bookmark_cache[${i} + 3]}" "${rest}" + __bm_res=("${__bm_bookmark_cache[${i} + 2]}" + "${__bm_bookmark_cache[${i} + 3]}" + "${rest}") return 0 fi done - printf '%s\n' "${bm_name}" + __bm_res=() return 1 } function __bm_list_bookmarks { @@ -98,7 +100,7 @@ function __bm_list_bookmarks { function _bookmarks { for ((i = 1; i < ${#__bm_bookmark_cache}; i+=4)); do - compadd -S '/' "${__bm_bookmark_cache[${i}]}" + compadd -q -S '/' "${__bm_bookmark_cache[${i}]}" done } @@ -107,23 +109,22 @@ function bm { __bm_update_bookmark_list || \ { printf 'Updating bookmark list failed!\n'; return 1 } (( ${#} == 0 )) && { __bm_list_bookmarks; return } - local loc=(${(f)"$(__bm_bookmark_location "${1}")"}) - if [[ -d "${loc[1]}" ]]; then - cd "${loc[1]}" - if ! [[ -z "${loc[3]}" ]]; then - if ! [[ -d "${loc[3]}" ]]; then - cd - >/dev/null - printf 'Bookmark exists, but trailing path doesn''t: "%s"\n' \ - "${loc[3]}" - return 1 - fi - cd "${loc[3]}" + __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}" + [[ "${BM_CWD_LS}" = '1' ]] && ls || true + elif [[ -e "${target}" ]]; then + ${=EDITOR} "${target}" + else + printf 'Bookmark exists, but trailing path doesn'"'"'t: "%s"\n' \ + "${__bm_res[3]}" + return 1 fi - [[ "${BM_CWD_LS}" = '1' ]] && ls || true - elif [[ -e "${loc[1]}" ]]; then - ${=EDITOR} "${loc[1]}" else - printf 'No such bookmark: "%s"\n' "${loc[1]}" + printf 'No such bookmark: "%s"\n' "${__bm_res[1]}" return 1 fi } @@ -132,9 +133,9 @@ function _bm { if ! [[ "${arg}" == */* ]]; then _arguments '1::bookmark:_bookmarks' else - local loc=(${(f)"$(__bm_bookmark_location "${arg}")"}) - if [[ -d "${loc[1]}" ]]; then - local parts=(${(s:/:)loc[3]}) + __bm_bookmark_location "${arg}" + if [[ -d "${__bm_res[1]}" ]]; then + local parts=(${(s:/:)__bm_res[3]}) local bm="${${(s:/:)${arg}}[1]}" local subdir="${(j:/:)parts[1,${#parts}-1]}" local search="${parts[${#parts}]}" @@ -143,11 +144,15 @@ function _bm { subdir="${subdir#/}" search="" fi - for file in "${loc[1]}/${subdir}/${search}"*(/); do - local clean_file="${bm}/${file#"${loc[1]}"}" - regexp-replace clean_file '/+' '/' - compadd -U -S '/' "${clean_file}" - done + local pre_path="${__bm_res[1]}/${subdir}/" + local prefix="${bm}/${subdir}" + 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 fi fi }