summaryrefslogtreecommitdiff
path: root/.config/bash/rc.d/emacs.sh
blob: 32ed654c0a1844a3e6a2494a32544a2e17a78497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/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_desired_buffer_name() {
			local base=$PWD suffix=''
			if [[ $base =~ /(src|pkg|doc|pkg-libre|src-libre|trunk|tags|branches)$ ]]; then
				suffix=$BASH_REMATCH
				base=${base%$suffix}
			fi
			base=${base##*/}
			echo "*ansi-term*<${base}${suffix}>"
		}
		PROMPT_COMMAND='_emacs_rename_terminal "$(_emacs_get_desired_buffer_name)"'
		:;;
esac