-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtpo-invoke-emacs.sh
executable file
·214 lines (202 loc) · 7.83 KB
/
tpo-invoke-emacs.sh
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#! /bin/bash
#
# Invokes Emacs with current directory unless files specified on command
# line and using ~/.emacs.tpo instead of ~/.emacs (for when using shared
# hosts). If the X-Windows DISPLAY environment is not set, then
# the console version of Emacs is forced (via --no-windows).
#
# Notes:
# - Background support is placed here rather than in Bash aliases or
# functions, so that the Emacs processes don't show up via Bash jobs
# command (as it tends to clutter the display).
# - Basis for em alias under Linux.
# - See win32-emacs.sh for similar script for use under Cygwin.
# - disables following shellcheck warnings:
# SC2090: [Quotes/backslashes in this variable will not be respected.]
#
#-------------------------------------------------------------------------------
# Note: Based on following alias/functions definition (see tohara-aliases.bash)
#
# emacs_options="--geometry 80x50"
# if [ -e "~/.emacs.tpo" ]; then
# alias emacs-tpo='emacs -l ~/.emacs.tpo'
# if [ "$DISPLAY" = "" ]; then
# alias emacs-tpo='emacs -l ~/.emacs.tpo --no-windows $emacs_options'
# function em () { if [ "$1" = "" ]; then emacs-tpo .; else emacs-tpo "$@"; fi; }
# else
# function em () { if [ "$1" = "" ]; then emacs-tpo .; else emacs-tpo "$@"; fi & }
# fi
# else
# function em () { if [ "$1" = "" ]; then emacs $emacs_options .; else emacs $emacs_options "$@"; fi & }
# alias emacs-tpo=em
# fi
#
# Uncomment following line(s) for tracing:
# - xtrace shows arg expansion (and often is sufficient)
# - verbose shows source commands as is (but usually is superfluous w/ xtrace)
#
## echo "$@"
## set -o xtrace
## DEBUG: set -o verbose
# Show usage statement
#
if [ "$1" = "--help" ]; then
script=$(basename "$0")
echo ""
echo "usage: $script [--trace] [--[skip-]nohup] [--options token-or-string] [--foreground] [--quick] [--emacs program] [--] [emacs-arguments]"
echo ""
echo "ex: $0 -- --geometry 80x50"
echo ""
echo "Notes:"
echo "- Put any emacs arguments after the -- spec."
echo "- You can also use EMACS_PROGRAM environment variable to override program."
echo ""
exit
fi
# Setup Emacs options (50 rows, 80 columns, and use background process)
# Note: Emacs sets EMACS env. var in terminal session, so EMACS_PROGRAM used instead
# Aside: in general longer names preferred to minimize conflicts as with DEBUG vs. DEBUG_LEVEL.
## OLD: emacs_options="--geometry 80x50"
use_nohup="0"
## TODO: emacs_options="${EMACS_OPTIONS:-}"
## OLD: emacs_options=""
emacs_options=()
in_background="1"
## OLD: emacs="emacs"
## BAD: emacs="${EMACS:-emacs}"
emacs="${EMACS_PROGRAM:-emacs}"
if [ "${EMACS_FONT:-""}" != "" ]; then
## OLD: emacs_options="$emacs_options -fn '$EMACS_FONT'"
emacs_options+=("-fn" "$EMACS_FONT")
fi
## TEST
## # Use nohup if under Mac OS
## if [[ "$OSTYPE" =~ darwin.* ]]; then
## native_m1_emacs="/Applications/Emacs.app/Contents/MacOS/Emacs-arm64-11"
## if [ -e "$native_m1_emacs" ]; then
## emacs="$native_m1_emacs"
## else
## use_nohup="1"
## fi
## fi
# Use nohup if under Mac OS
if [[ "$OSTYPE" =~ darwin.* ]]; then
use_nohup="1"
fi
# Parse command-line options
#
moreoptions=0; case "$1" in -*) moreoptions=1 ;; esac
quick=0
emacs_args=0
under_mac="0"
if [[ "$OSTYPE" =~ darwin.* ]]; then
under_mac="1"
fi
while [ "$moreoptions" = "1" ]; do
if [ "$1" = "--trace" ]; then
set -o xtrace;
elif [ "$1" = "--emacs" ]; then
emacs="$2"
shift
elif [ "$1" = "--foreground" ]; then
in_background="0"
elif [ "$1" = "--mac" ]; then
under_mac="1"
elif [ "$1" = "--nohup" ]; then
use_nohup="1"
elif [ "$1" = "--skip-nohup" ]; then
use_nohup="0"
elif [[ ("$1" = "-q") || ("$1" = "--quick") ]]; then
## OLD: emacs_options="$emacs_options -q";
emacs_options+=("-q")
quick=1
elif [ "$1" = "--options" ]; then
## OLD: emacs_options="$emacs_options $2";
emacs_options+=("$2");
shift;
elif [ "$1" = "--" ]; then
shift;
emacs_args=1
break;
else
echo "ERROR: Unknown option: $1";
exit;
fi
shift 1;
moreoptions=0; case "$1" in -*) moreoptions=1 ;; esac
done
# note: remainder of "$@" used below
# Force console model if DISPLAY environment not set
if [ "$DISPLAY" = "" ]; then
## DEBUG:
echo "no DISPLAY setting, so adding --no-windows and setting in_background"
## OLD: emacs_options="$emacs_options --no-windows"
emacs_options+=("--no-windows")
in_background="0"
fi
# Use ~/.emacs.tpo (in place of ~/.emacs) if available
if [[ ($quick = "0") && (-e "$HOME/.emacs.tpo") ]]; then
## OLD: emacs_options="$emacs_options -l $HOME/.emacs.tpo"
emacs_options+=("-l" "$HOME/.emacs.tpo")
fi
# resolve-path(filename) => absolute filename
function resolve-path() {
local filename="$1"
local new_filename
new_filename="$(realpath "$filename")"
## DEBUG: echo "resolving '$filename' => '$new_filename'" 1>&2
echo "$new_filename"
}
# Reset bash flags
## OLD: export BASHRC_PROCESSED=0 PROFILE_PROCESSED=0
export BASHRC_PROCESSED=0 PROFILE_PROCESSED=0 SCRIPT_PID=""
## TODO: resolve <space> in emacs options
# EX: "-DAMA-Ubuntu<space>Mono-normal-normal-normal-*-24-*-*-*-m-0-iso10646-1" => "-DAMA-Ubuntu Mono-normal-normal-normal-*-24-*-*-*-m-0-iso10646-1"
# NOTE: works around problem with space in font name in EMACS_OPTIONS env. var
## emacs_options="${emacs_options//<space>/ /}"
# Invoke emacs, adding current directory if no args (so dired invoked)
## DEBUG: echo "FYI: which '$emacs' => '$(which "$emacs")'"
# note: disables shellcheck SC2046 [Quote this to prevent word splitting], SC2048 [Use $... (with quotes) to prevent whitespace problems], and SC2086 [Double quote to prevent globbing]
# shellcheck disable=SC2046,SC2048,SC2086
if [ "$in_background" = "1" ]; then
# note: eval not used with variable for "&" in case spaces in filenames
# TODO: rework so that no-op option added if empty (to avoid SC2086 disabled)
# ex: emacs "${emacs_options:- --eval 1}" "$@" &
args=("$@");
if [ "$under_mac" = "1" ]; then
args=()
if [ $# == 0 ]; then args+=("$(resolve-path .)"); fi
# note: resolve fullpath for non-option filename due to quirk under macos
for filename in "$@"; do
## DEBUG: echo "filename=$filename"
if [[ ("$emacs_args" = "0") && ($filename =~ [^-]*) ]]; then
filename=$(resolve-path "$filename")
fi
args+=("$filename")
done
fi
#
if [ "$use_nohup" = "1" ]; then
## BAD: nohup emacs $emacs_options "$@" >> $TEMP/nohup.log 2>&1 &
## OLD: nohup emacs $emacs_options $(realpath "$@" 2> /dev/null) >> $TEMP/nohup.log 2>&1 &
# note: adds current directory so emacs starts in it rather than home (note< stupid nohup quirk
# TODO: simplify awkward Bash constructions to similate csh 'unshift .'!
## OLD: nohup emacs $emacs_options $(realpath "${args[*]}") >> $TEMP/nohup.log 2>&1 &
## OLD2: $nohup emacs $emacs_options $(realpath "${args[*]}") >> $TEMP/nohup.log 2>&1 &
## DEBUG: echo "background w/ nohup"
## DEBUG: echo "issuing: $emacs "${emacs_options[@]}" '$(realpath ${args[*]})' \>\> $TEMP/nohup.log 2>&1 &"
## OLD: nohup "$emacs" $emacs_options $(realpath "${args[*]}") >> $TEMP/nohup.log 2>&1 &
# shellcheck disable=SC2090
nohup "$emacs" "${emacs_options[@]}" "${args[*]}" >> $TEMP/nohup.log 2>&1 &
else
## DEBUG: echo "regular background (i.e., non-nohup)"
## DEBUG: echo "issuing: $emacs "${emacs_options[@]}" ${args[*]} &"
## DEBUG: set -o xtrace
## DEBUG: echo "${args[@]}"
"$emacs" "${emacs_options[@]}" "${args[@]}" &
fi
else
## DEBUG: echo "in foreground"
## DEBUG: echo "issuing: $emacs "${emacs_options[@]}" ${args[*]}"
"$emacs" "${emacs_options[@]}" "$@"
fi