-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-emacs
executable file
·233 lines (178 loc) · 5.44 KB
/
start-emacs
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#! /bin/bash
# Copyright (c) 2023 Can Aknesil
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
HELP_STR="Usage:
start-emacs -h|--help
Display help message
start-emacs --terminal [FILES...]
New Emacs instance in terminal.
start-emacs [--bind] [FILES...]
New Emacs instance in X.
start-emacs --client --terminal [FILES...]
New Emacs client frame in terminal.
start-emacs --client --new-frame [--bind] [FILES...]
New Emacs client frame in X.
start-emacs --client [--bind] FILES...
Open files in the most recently created frame.
Create new frame if necessary.
start-emacs --client
New Emacs client frame in X, if there are no.
Options:
--bind Wait for Emacs to finish, either by quiting, or via C-#.
If not specified, return immediatelly.
--client Start Emacs as client. Start daemon if not running.
If not specified, start a new Emacs instance.
-h, --help Display help message and exit.
--new-frame Always create a new frame.
--terminal Start new frame in terminal.
If not specified, start new frames in X."
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--client)
OPTARG_CLIENT=YES
shift
;;
# --display)
# OPTARG_DISPLAY="$2"
# shift
# shift
# ;;
--terminal)
OPTARG_TERMINAL=YES
shift
;;
--bind)
OPTARG_BIND=YES
shift
;;
--new-frame)
OPTARG_NEW_FRAME=YES
shift
;;
-h|--help)
OPTARG_HELP=YES
shift
;;
-*|--*)
echo "Unknown option $1"
echo "$HELP_STR"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift
;;
esac
done
FILES=$POSITIONAL_ARGS
if [ "$OPTARG_HELP" = YES ]; then
echo "$HELP_STR"
exit
fi
if [ "$(command -v emacsclientw)" ]; then
ec=emacsclientw # emacsclient as GUI app (on windows)
else
ec=emacsclient
fi
# From Emacs manual: due to limitations of MS-Windows, Emacs cannot
# have both GUI and text-mode frames in the same session. It also
# cannot open text-mode frames on more than a single Command Prompt
# window, because each Windows program can have only one console at
# any given time. For these reasons, if you invoke emacsclient with
# the -c option, and the Emacs server runs in a text-mode session,
# Emacs will always create a new text-mode frame in the same Command
# Prompt window where it was started; a GUI frame will be created only
# if the server runs in a GUI session. Similarly, if you invoke
# emacsclient with the -t option, Emacs will create a GUI frame if the
# server runs in a GUI session, or a text-mode frame when the session
# runs in text mode in a Command Prompt window.
if [ "$OPTARG_CLIENT" = YES ]; then
if [ "$OPTARG_TERMINAL" = YES ]; then
#
# New Emacs client frame in terminal.
#
# e --client --terminal
$ec -a "" -nw $FILES
else
if [ "$OPTARG_NEW_FRAME" = YES ]; then
#
# New Emacs client frame in X.
#
if [ "$OPTARG_BIND" = YES ]; then
# e --client --new-frame --bind
$ec -a "" -c $FILES
else
# e --client --new-frame
$ec -a "" -c -n $FILES
fi
else
if [ "${FILES[*]}" ]; then
#
# Open files in the most recently created frame. Create new frame if necessary.
#
if [ "$OPTARG_BIND" = YES ]; then
# e --client --bind FILES...
# Note: With the command 'ec [-a ""] [-r] FILES...'
# emacsclient generates the following error on
# Windows (Emacs version 29.2):
# Error: Symbol's value as variable is void: font
$ec -a "" -r $FILES
else
# e --client FILES...
# Note: The following command unintentionally
# burries the newly created buffer(s) on
# Windows. (Emacs version 29.2).
$ec -a "" -r -n $FILES
fi
else
#
# New Emacs client frame in X.
#
# Note: Using ec -a "" [-r] [-n] is problematic on
# Windows (Emacs version 29.2). Sometimes this command
# fails to create a frame.
# e --client
$ec -a "" -c -n
fi
fi
fi
else
if [ "$OPTARG_TERMINAL" = YES ]; then
#
# New Emacs instance in terminal.
#
# e --terminal
emacs -nw $FILES
else
#
# New Emacs instance in X.
#
if [ "$OPTARG_BIND" = YES ]; then
# e --bind
emacs $FILES
else
# e
if [ "$(command -v runemacs)" ]; then
runemacs $FILES
else
emacs $FILES 2>&1 >/dev/null & disown
# On Windows, above command leavs the shell currupted.
fi
fi
fi
fi