diff options
Diffstat (limited to '.config/bash/rc.d/90_emacs.sh')
-rw-r--r-- | .config/bash/rc.d/90_emacs.sh | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/.config/bash/rc.d/90_emacs.sh b/.config/bash/rc.d/90_emacs.sh new file mode 100644 index 0000000..79d5e61 --- /dev/null +++ b/.config/bash/rc.d/90_emacs.sh @@ -0,0 +1,67 @@ +#!/hint/bash + +if [[ $TERM == eterm* ]]; then + ## Primatives for interacting with Emacs ############################### + + # _emacs_run LISP + _emacs_run() { + emacsclient -a false -e "$*" 2>/dev/null + } + # _emacs_quote UNQUOTED_STRING + _emacs_quote() { + local str="$*" + str="${str//\\/\\\\}" # \ -> \\ + str="${str//\"/\\\"}" # " -> \" + str="\"${str}\"" # wrap it in quotes + printf '%s' "$str" + } + # _emacs_unquote QUOTED_STRING + _emacs_unquote() { + local str="$*" + if [[ $str =~ ^\"(.*)\"$ ]]; then + str=${BASH_REMATCH[1]} # un-quote it + str="${str//\\\\/\\}" # \\ -> \ + str="${str//\\\"/\"}" # \" -> " + fi + printf '%s' "$str" + } + + ## High-level tasks #################################################### + + # Set the TRAMP directory for remote hosts (shell -> emacs) + _emacs_set_remote_dir() { + # Because (term-handle-ansi-terminal-messages) is run + # after each item is set, the user should be set + # before the hostname is set, otherwise it will set + # (default-directory) to an invalid TRAMP string. + # + # Because the hostname is compared to (system-name) to + # check if it is localhost, "$(hostname -f)" may need + # to be used instead of $HOSTNAME, if + # $HOSTNAME/$(hostname) doesn't return a qualified + # domain. + local hostname=$HOSTNAME + [[ $hostname = *.* ]] || hostname="$(hostname -f)" + printf '\eAnSiT%s %s\n' \ + u "$USER" \ + c "$PWD" \ + h "$hostname" + } + # Set the shell's X11 display (emacs -> shell) + _emacs_set_shell_DISPLAY() { + export DISPLAY=$(_emacs_unquote "$(_emacs_run "(cdr (assoc 'display (frame-parameters)))")") + [[ $DISPLAY != nil ]] || unset DISPLAY + } + + ## Install the hooks ################################################### + + export SELECTED_EDITOR='emacsclient' + export EDITOR=$SELECTED_EDITOR + export VISUAL=$SELECTED_EDITOR + export PAGER=cat + if _emacs_run '()' >/dev/null; then + PROMPT_COMMAND+='_emacs_set_shell_DISPLAY;' + fi + # Remember, the $() strips the trailing newline, so add it back in + PS1_EXTRA+='\[$(_emacs_set_remote_dir)'$'\n\\]' +fi |