summaryrefslogtreecommitdiff
path: root/emacsterm.sh
blob: e83270defe7a6490a58a822a2fd7a56f2b90cd2f (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash

mimic=rxvt

usage() {
	echo "Usage $0 [OPTIONS]"
	echo
	echo 'Options:'
	echo '  -r        Use rxvt-style parsing of CMD for -e (execve)'
	echo '  -x        Use xterm-style parsing of CMD for -e (system)'
	echo '  -e CMD    Execute CMD instead of ${SHELL:-/bin/sh}'
	echo '  -h        Show this message'
}

cmd=${SHELL:-/bin/sh}
usage=false
error=false
while getopts 'rxe:h' flag; do
	case "$flag" in
		r) mimic=rxvt;;
		x) mimic=xterm;;
		e)
			if [[ $cmd == "${SHELL:-/bin/sh}" ]]; then
				cmd="$(mktemp -t "${0##*/}.XXXXXXXXXX")"
				trap "rm -f '$cmd'" EXIT
				{
					echo '#!/bin/bash'
					case "$mimic" in
						xterm)
							printf "sh -c %q\n" "$OPTARG"
							;;
						rxvt)
							shift $(($OPTIND - 1))
							printf '%q ' "$OPTARG" "$@"
							echo
							;;
					esac
				} > "$cmd"
				chmod 755 "$cmd"
			else
				echo "$0: option -e may only be given once" >>/dev/stderr
				error=true
			fi
			;;
		h) usage=true;;
		*) error=true;;
	esac
done
shift $(($OPTIND - 1))
if [[ $# != 0 ]]; then
	echo "$0: extra arguments: $*" >>/dev/stderr
	error=true
fi
if $error; then
	usage >>/dev/stderr
	exit 1
fi
if $usage; then
	usage
	exit 0
fi

emacsclient -a "" -c --eval "(ansi-term \"${cmd}\")" '(set-window-dedicated-p (selected-window) t)'