From 6cf3c3c740aaacfec997a2d3430f82c8a556288b Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Sun, 8 Sep 2024 14:47:51 -0700 Subject: [PATCH] Add file offset support to emacs-bookmark.zsh --- emacs-bookmark.zsh | 33 ++++++++++++++++++++++++++++++--- init.zsh | 3 +++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/emacs-bookmark.zsh b/emacs-bookmark.zsh index f1a04b9..5e6e60d 100644 --- a/emacs-bookmark.zsh +++ b/emacs-bookmark.zsh @@ -5,11 +5,37 @@ 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 { + let off="${2}" + let row=1 + let col=0 + for line in "${(@f)mapfile[${1}]}"; do + let len="${#line}" + if (( off > len )); then + off='off - (len + 1)' + if (( off <= 0 )); then + (( len == 0 )) && col=0 || col='len - 1' + break + fi + row+=1 + else + col='off' + off=0 + break; + fi + done <"${1}" + if (( off > 0 )); then + row+=-1 + fi + printf '%d\0%d' "${row}" "${col}" +} + 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 } @@ -29,7 +55,6 @@ function __bm_find_user_emacs_dir { } BM_BOOKMARK_PATH="${BM_BOOKMARK_PATH:-"$(__bm_find_user_emacs_dir)"}" - function __bm_hash_dirs { # First empty the hash table hash -dr @@ -132,8 +157,10 @@ function bm { if [[ -d "${target}" ]]; then cd "${target}" (( "${BM_CWD_LS:-0}" )) && ls || true - elif [[ -e "${target}" ]]; then - ${=EDITOR} "${target}" + elif [[ -e "${bm_loc}" ]]; then + let offset="${__bm_res[2]}" + local rowcol=(${(0)"$(__bm_offset_to_row_col "${bm_loc}" "${offset}")"}) + ${=EDITOR} "+${rowcol[1]}:${rowcol[2]}" "${bm_loc}" else printf 'Bookmark exists, but trailing path doesn'"'"'t: "%s"\n' \ "${(q)__bm_res[3]}" diff --git a/init.zsh b/init.zsh index 593bf0f..e0ed498 100644 --- a/init.zsh +++ b/init.zsh @@ -88,6 +88,9 @@ else fi export VISUAL="${EDITOR}" +# Make SBCL *slightly* less frustrating to use on the command line +alias sbcl='rlwrap -pblue -q "\"" sbcl' + # Safer file functions alias cp="cp -i" alias mv="mv -i"