summaryrefslogtreecommitdiff
path: root/.config/bash
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2014-04-16 03:06:21 -0400
committerLuke Shumaker <LukeShu@sbcglobal.net>2014-04-21 13:27:02 -0400
commitc7b1d83a36ff7451f0759dd30faa4242e71468e3 (patch)
tree62b433517085317a829dedd6c879d33eb5dc026e /.config/bash
parent7000ec189d6aecca3cbee5c68fe877f11cf0784e (diff)
improve emacs/bash integration
Diffstat (limited to '.config/bash')
-rw-r--r--.config/bash/rc.d/emacs.sh135
1 files changed, 96 insertions, 39 deletions
diff --git a/.config/bash/rc.d/emacs.sh b/.config/bash/rc.d/emacs.sh
index 9c1bf4d..165d71e 100644
--- a/.config/bash/rc.d/emacs.sh
+++ b/.config/bash/rc.d/emacs.sh
@@ -1,41 +1,98 @@
#!/bin/bash
-case "$TERM" in
- eterm*)
- SELECTED_EDITOR='emacsclient'
- EDITOR=$SELECTED_EDITOR
- VISUAL=$SELECTED_EDITOR
- export SELECTED_EDITOR EDITOR VISUAL
- # The following uses the variable _EMACS_BUFFER to store some state
- _emacs_quote() {
- local str="$*"
- str="${str//\\/\\\\}"
- str="${str//\"/\\\"}"
- str="\"${str}\""
- printf '%s' "$str"
- }
- _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_short_cwd() {
- local base=$1
- local suffix=''
- if [[ $base =~ (/(src|pkg|doc|pkg-libre|src-libre|trunk|tags|branches))*$ ]]; then
- suffix=$BASH_REMATCH
- base=${base%$suffix}
- fi
- base=${base##*/}
- echo ${base}${suffix}
- }
- _emacs_get_desired_buffer_name() {
- echo "*ansi-term*<$(_emacs_get_short_cwd "$PWD")>"
- }
- PROMPT_COMMAND='_emacs_rename_terminal "$(_emacs_get_desired_buffer_name)"'
- :;;
-esac
+if [[ $TERM == eterm* ]]; then
+ SELECTED_EDITOR='emacsclient'
+ EDITOR=$SELECTED_EDITOR
+ VISUAL=$SELECTED_EDITOR
+ export SELECTED_EDITOR EDITOR VISUAL
+
+ ## Primatives for interacting with Emacs ###############################
+
+ # _emacs_run LISP
+ _emacs_run() {
+ emacsclient -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"
+ }
+
+ ## Deal with renaming the terminal #####################################
+
+ # _emacs_rename_terminal NEW_BUFFER_NAME
+ # This function uses the variable _EMACS_BUFFER to store some state
+ _emacs_rename_terminal() {
+ local name=$(_emacs_quote "$*")
+ if [[ -n $_EMACS_BUFFER ]]; then
+ local buffer="(get-buffer $_EMACS_BUFFER)"
+ else
+ local buffer='(window-buffer (selected-window))'
+ fi
+ _EMACS_BUFFER=$(_emacs_run "(with-current-buffer ${buffer} (rename-buffer ${name} t))")
+ }
+ # _emacs_get_short_cwd
+ _emacs_get_short_cwd() {
+ local base=$PWD
+ local suffix=''
+ # The regex here is a list of directory names
+ # that aren't really helpful, and that the
+ # parent directory should be included also.
+ if [[ $base =~ (/(src|pkg|doc|pkg-libre|src-libre|trunk|tags|branches))*$ ]]; then
+ suffix=$BASH_REMATCH
+ base=${base%$suffix}
+ fi
+ base=${base##*/}
+ echo ${base}${suffix}
+ }
+ # _emacs_get_desired_buffer_name
+ _emacs_get_desired_buffer_name() {
+ echo "*ansi-term*<$(_emacs_get_short_cwd)>"
+ }
+
+ ## High-level tasks ####################################################
+
+ # Like uniquify on the buffer name (shell -> emacs)
+ _emacs_set_buffer_name() {
+ # This doesn't work correctly on remote hosts.
+ # The "correct" solution is probably to hook into
+ # default-directory being set in term.el
+ _emacs_rename_terminal "$(_emacs_get_desired_buffer_name)"
+ }
+ # Set the TRAMP directory for remote hosts (shell -> emacs)
+ _emacs_set_remote_dir() {
+ if [[ -n $SSH_CONNECTION ]]; then
+ printf '\eAnSiT%s %s\n' \
+ u "$USER" \
+ c "$PWD" \
+ h "$HOSTNAME"
+ fi
+ }
+ # Set the shell's X11 display (emacs -> shell)
+ _emacs_set_shell_DISPLAY() {
+ export DISPLAY=$(_emacs_unquote "$(_emacs_run "(cdr (assoc 'display (frame-parameters)))")")
+ }
+
+ ## Do those things #####################################################
+
+ _emacs_PROMPT_COMMAND() {
+ _emacs_set_buffer_name
+ _emacs_set_remote_dir
+ _emacs_set_shell_DISPLAY
+ }
+ PROMPT_COMMAND=_emacs_PROMPT_COMMAND
+fi