diff options
-rw-r--r-- | .config/bash/rc.d/emacs.sh | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/.config/bash/rc.d/emacs.sh b/.config/bash/rc.d/emacs.sh index 838c0a4..32ed654 100644 --- a/.config/bash/rc.d/emacs.sh +++ b/.config/bash/rc.d/emacs.sh @@ -6,17 +6,24 @@ case "$TERM" in EDITOR=$SELECTED_EDITOR VISUAL=$SELECTED_EDITOR export SELECTED_EDITOR EDITOR VISUAL - _set_buffer_name() { - local name="$*" - local name_esc="${name//\\/\\\\}" - name_esc="${name_esc//\"/\\\"}" - emacsclient -e " -(with-current-buffer (window-buffer (selected-window)) - (if (not (string= \"${name}\" (buffer-name))) - (rename-buffer (generate-new-buffer-name \"${name}\")))) -" &>/dev/null + # The following uses the variable _EMACS_BUFFER to store some state + _emacs_quote() { + local str="$*" + str="${str//\\/\\\\}" + str="${str//\"/\\\"}" + str="\"${str}\"" + printf '%s' "$str" } - _create_buffer_name() { + _emacs_rename_terminal() { + local name="$(_emacs_quote "$(_emacs_get_desired_buffer_name)")" + if [[ -n $_EMACS_BUFFER ]]; then + local buffer="(get-buffer $_EMACS_BUFFER)" + else + local buffer='(window-buffer (selected-window))' + fi + _EMACS_BUFFER="$(emacsclient -e "(with-current-buffer ${buffer} (rename-buffer ${name} t)))" 2>/dev/null)" + } + _emacs_get_desired_buffer_name() { local base=$PWD suffix='' if [[ $base =~ /(src|pkg|doc|pkg-libre|src-libre|trunk|tags|branches)$ ]]; then suffix=$BASH_REMATCH @@ -25,6 +32,6 @@ case "$TERM" in base=${base##*/} echo "*ansi-term*<${base}${suffix}>" } - PROMPT_COMMAND='_set_buffer_name "$(_create_buffer_name)"' + PROMPT_COMMAND='_emacs_rename_terminal "$(_emacs_get_desired_buffer_name)"' :;; esac |