Add file offset support to emacs-bookmark.zsh

This commit is contained in:
Alexander Rosenberg 2024-09-08 14:47:51 -07:00
parent 79695758a5
commit 6cf3c3c740
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730
2 changed files with 33 additions and 3 deletions

View File

@ -5,11 +5,37 @@ autoload colors && colors
autoload regexp-replace autoload regexp-replace
zmodload -F zsh/stat b:zstat zmodload -F zsh/stat b:zstat
zmodload zsh/datetime zmodload zsh/datetime
zmodload zsh/mapfile
local __bm_bookmark_cache=() local __bm_bookmark_cache=()
local __bm_res=() local __bm_res=()
let __bm_last_read_time=-1 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 { function __bm_find_user_emacs_dir {
[[ -f "${HOME}/.emacs.d/var/bookmark-default.el" ]] && [[ -f "${HOME}/.emacs.d/var/bookmark-default.el" ]] &&
{ printf '%s' "${HOME}/.emacs.d/var/bookmark-default.el"; return 0 } { 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)"}" BM_BOOKMARK_PATH="${BM_BOOKMARK_PATH:-"$(__bm_find_user_emacs_dir)"}"
function __bm_hash_dirs { function __bm_hash_dirs {
# First empty the hash table # First empty the hash table
hash -dr hash -dr
@ -132,8 +157,10 @@ function bm {
if [[ -d "${target}" ]]; then if [[ -d "${target}" ]]; then
cd "${target}" cd "${target}"
(( "${BM_CWD_LS:-0}" )) && ls || true (( "${BM_CWD_LS:-0}" )) && ls || true
elif [[ -e "${target}" ]]; then elif [[ -e "${bm_loc}" ]]; then
${=EDITOR} "${target}" let offset="${__bm_res[2]}"
local rowcol=(${(0)"$(__bm_offset_to_row_col "${bm_loc}" "${offset}")"})
${=EDITOR} "+${rowcol[1]}:${rowcol[2]}" "${bm_loc}"
else else
printf 'Bookmark exists, but trailing path doesn'"'"'t: "%s"\n' \ printf 'Bookmark exists, but trailing path doesn'"'"'t: "%s"\n' \
"${(q)__bm_res[3]}" "${(q)__bm_res[3]}"

View File

@ -88,6 +88,9 @@ else
fi fi
export VISUAL="${EDITOR}" export VISUAL="${EDITOR}"
# Make SBCL *slightly* less frustrating to use on the command line
alias sbcl='rlwrap -pblue -q "\"" sbcl'
# Safer file functions # Safer file functions
alias cp="cp -i" alias cp="cp -i"
alias mv="mv -i" alias mv="mv -i"