-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathem.in
executable file
·1371 lines (1193 loc) · 45.7 KB
/
em.in
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# -*- coding: utf-8 -*-
#
# Copyright © 2017-2024 Andrew L. Moore
#
# This script runs emacs(1) and/or emacsclient(1) as appropriate per
# given command-line arguments. For interactive sessions, emacs(1) is
# started in daemon mode as necessary prior to running emacsclient(1).
#
# Invoking this script with multiple FILE arguments by default opens
# FILEs in the top-most frame either sequentially, via the
# `server-edit' (C-x #) command, or randomly, via `switch-to-buffer'
# (C-x b).
#
# In addition to command-line options from emacs(1) and emacsclient(1),
# the following options are recognized:
#
# --buffer [BUF] Visit buffer BUF if given, otherwise `*scratch*'.
# --dired [DIR] Visit directory DIR if given, otherwise `.'.
# --info Display emacs configuration, then exit.
# --many-frames Visit FILEs in parallel, each in its own frame.
# --markdown, -md FILE Visit FILE in mode \`markdown-live-preview-mode'.
# --new-frame, -new Visit FILEs in a new frame.
# --preload-files, -pr Load FILEs in parallel, but visit one at a time.
# --save-kill, -sa Save buffers and kill Emacs and Emacs server.
# --ssh [USER@]HOST[:PATH] Visit PATH on remote HOST via SSH. If PATH not
# given, visit USER's home directory on HOST.
# --ssu [USER@]HOST[:PATH] Visit PATH with SUDO on remote HOST via SSH.
# If PATH not given, visit USER's home directory
# on HOST.
# --sudo [USER] If USER is specified, visit files as USER
# via tramp. Otherwise, visit files as \`root'.
# --trace Trace execution of $pgm(1) script.
# --two-way-merge, -tw FILE1,FILE2[,OUTPUT-FILE]
# Call \`emerge-files' with arguments FILE1 and
# FILE2. Save output to OUTPUT-FILE if given.
# --update-loaddefs[=DIR] Build DIR-loaddefs.el.
# --view, -vi FILE Open FILE in read-only mode.
# --wait Run emacsclient in foreground.
#
# Options may be abbreviated (e.g., `-new' instead of `--new-frame')
# provided they are unambiguous.
#
# If this script is invoked with either no command-line options or
# emacsclient options only, then it runs as a background process and
# switches focus to the Emacs server. Otherwise, Emacs-specific
# command-line options (see `emacs --help') force a new Emacs process
# to be run in the foreground, allowing this script to be invoked,
# e.g., in batch mode within a Makefile.
#
# If the environment variable SSH_TTY is set, and the ssh client and server
# differ, then `--tty' command-line option is enabled by default.
#
shopt -s extglob
: ${AWK_CMD:='@AWK_CMD@'}
: ${BASENAME_CMD:='@BASENAME_CMD@'}
: ${DIRNAME_CMD:='@DIRNAME_CMD@'}
: ${ED_CMD:='@ED_CMD@'}
: ${GREP_CMD:='@GREP_CMD@'}
: ${_EMACS_CMD:='@EMACS_CMD@'}
: ${_EMACSCLIENT_CMD:='@EMACSCLIENT_CMD@'}
: ${FIND_CMD:='@FIND_CMD@'}
: ${FMT_CMD:='@FMT_CMD@'}
: ${HEAD_CMD:='@HEAD_CMD@'}
: ${ID_CMD:='@ID_CMD@'}
: ${KILL_CMD:='@KILL_CMD@'}
: ${MKDIR_CMD:='@MKDIR_CMD@'}
: ${MKTEMP_CMD:='@MKTEMP_CMD@'}
: ${OSASCRIPT_CMD:='@OSASCRIPT_CMD@'}
: ${PKILL_CMD:='@PKILL_CMD@'}
: ${PS_CMD:='@PS_CMD@'}
: ${READLINK_CMD:='@READLINK_CMD@'}
: ${RM_CMD:='@RM_CMD@'}
: ${SED_CMD:='@SED_CMD@'}
: ${SLEEP_CMD:='@SLEEP_CMD@'}
: ${SORT_CMD:='@SORT_CMD@'}
: ${STAT_CMD:='@STAT_CMD@'}
: ${TOUCH_CMD:='@TOUCH_CMD@'}
: ${TTY_CMD:='@TTY_CMD@'}
: ${UNAME_CMD:='@UNAME_CMD@'}
: ${WMCTRL_CMD:='@WMCTRL_CMD@'}
usage ()
{
$ED_CMD -v <<EOF
a
$($_EMACSCLIENT_CMD --help)
.
1s;Usage: [^ ]\{1,\};Usage: $script_name;
# Option \`-f' is shadowed by Emacs option \`--funcall'
/-f SERVER, /s;;;
.,+j
s;\(SERVER\).*\(Set\);\1 \2;
\$-1c
Extended options:
--buffer [BUF] Visit buffer BUF if given, otherwise \`*scratch*'.
--dired [DIR] Visit directory DIR if given, otherwise \`.'.
--many-frames Visit FILEs in parallel, each in its own frame.
--markdown, -md FILE Visit FILE in mode \`markdown-live-preview-mode'.
--new-frame Visit FILEs in a new frame.
--preload-files Load FILEs in parallel, but visit one at a time.
--save-kill, -sa Save buffers and kill Emacs and Emacs server.
--ssh [USER@]HOST[:PATH] Visit PATH on remote HOST via SSH. If PATH not
given, visit USER's home directory on HOST.
--ssu [USER@]HOST[:PATH] Visit PATH with SUDO on remote HOST via SSH.
If PATH not given, visit USER's home directory.
--sudo [USER] If USER is specified, visit files as USER
via tramp. Otherwise, visit files as \`root'.
--trace Trace execution of $script_name(1) script.
--two-way-merge, -tw FILE1,FILE2[,OUTPUT-FILE]
Call \`emerge-files' with arguments FILE1 and FILE2.
Save output to OUTPUT-FILE if given.
--update-loaddefs [DIR] Build DIR-loaddefs.el from DIR/*.el.
--view, -vi FILE Visit FILE in read-only mode.
--wait Run emacsclient in foreground.
In addition to the above, the following Emacs-specific options are
recognized. Most of these cannot be processed by an existing Emacs
server, in which case a separate Emacs process is invoked.
.
# Mark last line of \`emacsclient --help'.
\$kx
\$a
$($_EMACS_CMD --help)
.
# Remove head of \`emacs --help'.
'x+;/options:/-d
s/.*options:\$/Emacs &/
# Remove shadowed options
/-q, --quiet/s;;--quiet ;
?--display?d
/--terminal/s;, -t DEVICE; DEVICE ;
/, -nw/s;; ;
# Remove tail of \`emacs --help'.
\$;?--parent?;\$--d
# Merge last lines of \`emacsclient --help' and \`emacs --help'.
'xm\$--
s;\\.\$;, or alternatively,;
i
.
,p
Q
EOF
}
# Check shell interpreter version.
is-supported-shell ()
{
# $BASH_VERSION prerequisites (want: option `-n')
local -i BASH_MAJOR_PREREQ=4
local -i BASH_MINOR_PREREQ=3
local -i BASH_REVISION_PREREQ=0
if test ."$BASH_VERSION" = .''; then
echo "$script_name: Bash shell required." >&2
return 1
elif (( ${BASH_VERSINFO[0]} < BASH_MAJOR_PREREQ )) ||
(( ${BASH_VERSINFO[0]} == BASH_MAJOR_PREREQ &&
${BASH_VERSINFO[1]} < BASH_MINOR_PREREQ )) ||
(( ${BASH_VERSINFO[0]} == BASH_MAJOR_PREREQ &&
${BASH_VERSINFO[1]} == BASH_MINOR_PREREQ &&
${BASH_VERSINFO[2]} < BASH_REVISION_PREREQ )); then
echo "$script_name: $BASH_VERSION: Newer Bash shell required." >&2
return 1
fi
return 0
}
# OS-agnstoic readlink for existent files/directories.
resolve-existing ()
{
if $READLINK_CMD --version 2>&1 | $GREP_CMD -q 'coreutils'; then
$READLINK_CMD -e "$@"
else
$READLINK_CMD -f N "$@"
fi
}
verify-command-paths ()
{
local -n list=$1
local -i status=0
local cmd
for cmd in "${list[@]}"; do
if eval test .\"\$$cmd\" = .''; then
echo "${BASH_SOURCE[0]}: $cmd: Undefined variable" >&2
(( ++status ))
elif eval test ! -x \"\$$cmd\"; then
eval echo "${BASH_SOURCE[0]}: \$$cmd: No such command" >&2
(( ++status ))
fi
done
}
# Check command paths.
is-valid-environment ()
{
local -i status=0
local -a command_variables=(
$($SED_CMD -nE -e '/^:\s\$\{([A-Z_][A-Z0-9_]*):.*/s//\1/p' "$0")
)
verify-command-paths command_variables
unset -v command_variables
return $status
}
# Check Emacs version.
is-current-emacs ()
{
local -i EMACS_MAJOR_PREREQ=25
local -i status=0
local emacs_version
for editor in _EMACS_CMD _EMACSCLIENT_CMD; do
emacs_version=$(
${!editor} --version |
$HEAD_CMD -1 |
$AWK_CMD '{ print $NF }'
)
if (( ${emacs_version%%.*} < EMACS_MAJOR_PREREQ )); then
echo "$script_name: $emacs_version: Newer ${!editor} required." >&2
status+=1
fi
done
return $status
}
is-running-emacs ()
{
case "$OSTYPE" in
darwin*)
$PKILL_CMD -0 -u "$user_id" -f \
"${_EMACS_CMD##*/}.*daemon=.*${server_name} " >/dev/null 2>&1
;;
*)
$PKILL_CMD -0 -u "$user_id" -f \
"${_EMACS_CMD##*/}.*daemon=.*${server_name}\\b" >/dev/null 2>&1
;;
esac
}
is-running-emacsclient ()
{
case "$OSTYPE" in
darwin*)
$PKILL_CMD -0 -u "$user_id" -f \
"${_EMACSCLIENT_CMD##*/}.*${server_name} " >/dev/null 2>&1
;;
*)
$PKILL_CMD -0 -u "$user_id" -f \
"${_EMACSCLIENT_CMD##*/}.*${server_name}\\b" >/dev/null 2>&1
;;
esac
}
create-frame-or-tty ()
{
if test ."$_EMACSCLIENT_CMD" != ."$_EMACS_CMD"; then
if ! is-running-emacsclient || $create_new_frames; then
echo $frame_or_tty
fi
elif test ."$frame_or_tty" != .'--create-frame'; then
echo $frame_or_tty
fi
}
configure-for-console ()
{
# No frames, by default.
preload_files=false
create_new_frames=false
create_many_new_frames=false
frame_or_tty=-nw
# Provide default Emacs server name. Overriden by option `-s'.
server_name=server_${user_name}_${tty_name##*/}
# Run in foreground.
background_op=''
}
# Set up environment for Emacs, as opposed to emacsclient.
switch-to-emacs ()
{
# Run in foreground.
background_op=''
# Report exit codes (see function `raise-exception' below).
error_diagnostic='Emacs: Exited with error: $error_status'
}
raise-exception ()
{
error_status=$?
echo "Exception raised in: $@" >&3
eval printf \"$script_name: $error_diagnostic\\n\" >&3
if test -s "$stderr_cache"; then
printf "%s\n" "$(< "$stderr_cache" )" >&3
fi
exit $error_status
}
start-emacs-server ()
{
local -i i=0
local first_diag=true
local server_socket_dir
server_socket_dir=$(
$_EMACS_CMD -q -nw --batch \
--eval "(progn (server-start) (prin1 server-socket-dir))"
)
if test ."$server_socket_dir" = .''; then
server_socket_dir=${TMPDIR:-'/tmp'}
fi
echo -n "Launching Server... " >&3
# Removing any left-over socket
if test -d "$server_socket_dir"; then
$FIND_CMD "$server_socket_dir" -type s 2>/dev/null |
while read socket; do
if test ."${socket##*/}" = ."$server_name"; then
$RM_CMD -f "$socket"
fi
done
fi
$_EMACS_CMD --daemon="$server_name" \
>"${emacs_confdir}/${server_name}.log" 2>&1 &
wait $! ||
raise-exception 'server start'
# Verify that server is accessible via emacsclient(1).
$_EMACSCLIENT_CMD -s "$server_name" --no-wait --eval 't' >/dev/null ||
raise-exception 'server access'
# Print Emacs startup errors/warnings to console.
$GREP_CMD -E -iA 7 'warning|error' "${emacs_confdir}/${server_name}.log" |
while read diag; do
if $first_diag; then
echo >&3
first_diag=false
fi
echo "$diag" >&3
done
}
tty-owner-id ()
{
if $STAT_CMD --version | $GREP_CMD -q 'coreutils'; then
# GNU stat...
stat_flag=-c
else
# BSD stat
stat_flag=-f
fi
$STAT_CMD "${stat_flag}" '%u' "$tty_name"
}
# Parse command-line options for requests.
parse-command-line ()
{
local buffer=''
local directory=''
local editor=''
local file=''
local files=''
local file1=''
local file2=''
local file3=''
local username=''
local remotehost=''
local pathname=''
local window_name=''
# Aggregate options
while true; do
case "$1" in
# Ignore alternate editor
-a*|--a*)
# EDITOR given as --alternate-editor=EDITOR
if [[ ."$1" =~ \.[^=]*=(.*) ]]; then
editor=${BASH_REMATCH[1]}
OPTIND+=1
shift
# EDITOR given as second, possibly null, argument.
else
editor=$2
OPTIND+=$(( $# == 1 ? 1 : 2 ))
shift $(( $# == 1 ? 1 : 2 ))
fi
;;
# Extension option `--buffer [BUF]'; open buffer BUF.
--bu*|-bu*)
argv[argc++]='--eval'
# BUFFER given as --buffer=BUFFER
if [[ ."$1" =~ \.[^=]*=(.*) ]]; then
buffer=${BASH_REMATCH[1]}
OPTIND+=1
shift
# BUFFER given as second, possibly null, argument.
else
buffer=$2
OPTIND+=$(( $# == 1 ? 1 : 2 ))
shift $(( $# == 1 ? 1 : 2 ))
fi
argv[argc]='(switch-to-buffer '
argv[argc]+="(if (fboundp 'buffer-name-matching-pattern) "
argv[argc]+='(buffer-name-matching-pattern "'
argv[argc]+=${buffer:-'\\*scratch\\*'}
argv[argc]+='") "'
argv[argc]+=$(printf '%q' "${buffer:-'*scratch*'}")
argv[argc++]+='"))'
window_name=${buffer:-'scratch'}
;;
# Extension option `--dired [DIR]'; open DIR in dired mode.
--dired*|-dired*)
argv[argc++]='--eval'
# DIR given as --dired=DIR
if [[ ."$1" =~ \.[^=]*=(.*) ]]; then
directory=${BASH_REMATCH[1]}
OPTIND+=1
shift
# DIR given as second, possibly null, argument.
else
directory=$2
OPTIND+=$(( $# == 1 ? 1 : 2 ))
shift $(( $# == 1 ? 1 : 2 ))
fi
# If DIR null, default to current directory.
argv[argc]='(dired "'
argv[argc]+=$(printf '%q' "${directory:='.'}")
argv[argc++]+='")'
window_name=$directory
;;
# Extension option `--info'
--inf*)
EMACS_DEV=$($_EMACS_CMD --version | $SED_CMD -ne '/^Dev/p') \
$_EMACS_CMD -q -nw --batch \
--eval '(let ((dev-branch (getenv "EMACS_DEV")))
(princ (concat "GNU Emacs v" emacs-version
"\n \b" dev-branch
"\nconfigure options:\n\t"
system-configuration-options
"\nfeatures:\n\t"
system-configuration-features
"\n")))' |
$FMT_CMD -w 76
exit 0
;;
# Extension option `--many-frames'; visit files in
# parallel, each in its own frame.
--man*|-man*)
preload_files='true'
create_new_frames='true'
create_many_new_frames='true'
frame_or_tty='--create-frame'
OPTIND+=1
shift
;;
# Extension option `--markdown FILE': open FILE in mode
# `markdown-live-preview-mode'.
--mar*|-md)
argv[argc++]='--eval'
# FILE given as --markdown=FILE
if [[ ."$1" =~ \.[^=]*=(.*) ]]; then
# Need eval in case of argument like `~/file'.
file=$(eval resolve-existing "${BASH_REMATCH[1]}")
if test ."$file" = .''; then
echo "${BASH_REMATCH[1]}: No such file or directory"
exit 1
fi
OPTIND+=1
shift
# FILE given as second, possibly null, argument.
else
file=$(resolve-existing "$2")
if test ."$file" = .''; then
echo "$2: No such file or directory"
exit 1
fi
OPTIND+=$(( $# == 1 ? 1 : 2 ))
shift $(( $# == 1 ? 1 : 2 ))
fi
# Open FILE, invoke `markdown-live-preview-mode'.
argv[argc]='(with-current-buffer '
argv[argc]+='(find-file "'
argv[argc]+=$(printf '%q' "${file:='*.md'}")
argv[argc]+='") '
argv[argc]+="(unless (eq major-mode 'markdown-mode)"
argv[argc]+=' (markdown-mode)) '
argv[argc]+='(markdown-live-preview-mode 1) '
argv[argc++]+='(delete-other-windows))'
window_name=$file
;;
# Extension option `--new-frame'; visit file in new frame.
--new*|-new*)
preload_files='false'
create_new_frames='true'
create_many_new_frames='false'
frame_or_tty='--create-frame'
OPTIND+=1
shift
;;
# Extension option `--preload'; load files in parallel.
--pr*|-pr*)
preload_files='true'
OPTIND+=1
shift
;;
# Extension option `--save-kill'.
--sa*|-sa*)
argv[argc++]='--eval'
argv[argc++]='(save-buffers-kill-emacs)'
allow_server_start='false'
emacs_verify_kill='true'
OPTIND+=1
shift
;;
# Extension option `--ssh [USER@]HOST[:PATHNAME]': open
# remote PATHNAME via SSH. If USER is nil, use
# $USER. If PATHNAME is nil, open USER's home
# directory on HOST.
--ssh|-ssh)
argv[argc++]='--eval'
argv[argc]='(find-file "/ssh:'
if [[ ."$1" =~ \.[^=]+=([^@[:blank:]]*@)?([^@:[:blank:]]+:?)(.+)?$ ]]; then
username=${BASH_REMATCH[1]%@}
if test ."$username" != .''; then
argv[argc]+=$(printf '%q@' "$username")
fi
remotehost=${BASH_REMATCH[2]%:}
argv[argc]+=$(printf '%q:' "$remotehost")
pathname=${BASH_REMATCH[3]:-'.'}
if test ."$pathname" != .''; then
argv[argc]+=$(printf '%q' "$pathname")
fi
OPTIND+=1
shift
elif [[ ."$2" =~ \.([^@[:blank:]]*@)?([^@:[:blank:]]+:?)(.+)?$ ]]; then
username=${BASH_REMATCH[1]%@}
if test ."$username" != .''; then
argv[argc]+=$(printf '%q@' "$username")
fi
remotehost=${BASH_REMATCH[2]%:}
argv[argc]+=$(printf '%q:' "$remotehost")
pathname=${BASH_REMATCH[3]:-'.'}
if test ."$pathname" != .''; then
argv[argc]+=$(printf '%q' "$pathname")
fi
OPTIND+=2
shift 2
else
raise-exception "${FUNCNAME[0]}: Invalid SSH argument"
fi
argv[argc++]+='")'
window_name=$pathname
;;
# Extension option `--ssudo [USER@]HOST[:PATHNAME]': open
# remote PATHNAME via SSH then invoke sudo. If USER is nil, use
# $USER. If PATHNAME is nil, open USER's home
# directory on HOST.
--ssu*|-ssu*)
argv[argc++]='--eval'
argv[argc]='(find-file "/ssh:'
if [[ ."$1" =~ \.[^=]+=([^@[:blank:]]*@)?([^@:[:blank:]]+:?)(.+)?$ ]]; then
username=${BASH_REMATCH[1]%@}
if test ."$username" != .''; then
argv[argc]+=$(printf '%q@' "$username")
fi
remotehost=${BASH_REMATCH[2]%:}
argv[argc]+=$(printf '%q|' "$remotehost")
argv[argc]+='sudo:root@'
argv[argc]+=$(printf '%q:' "$remotehost")
pathname=${BASH_REMATCH[3]:-'.'}
if test ."$pathname" != .''; then
argv[argc]+=$(printf '%q' "$pathname")
fi
OPTIND+=1
shift
elif [[ ."$2" =~ \.([^@[:blank:]]*@)?([^@:[:blank:]]+:?)(.+)?$ ]]; then
username=${BASH_REMATCH[1]%@}
if test ."$username" != .''; then
argv[argc]+=$(printf '%q@' "$username")
fi
remotehost=${BASH_REMATCH[2]%:}
argv[argc]+=$(printf '%q|' "$remotehost")
argv[argc]+='sudo:root@'
argv[argc]+=$(printf '%q:' "$remotehost")
pathname=${BASH_REMATCH[3]:-'.'}
if test ."$pathname" != .''; then
argv[argc]+=$(printf '%q' "$pathname")
fi
OPTIND+=2
shift 2
else
raise-exception "${FUNCNAME[0]}: Invalid SSH argument"
fi
argv[argc++]+='")'
window_name=$pathname
;;
# Extension option `--sudo'; with super-user
# privileges via tramp.
--sud*|-sud*)
# USER given as --sudo=USER
if [[ ."$1" =~ \.[^=]*=(.*) ]]; then
sudo_user=${BASH_REMATCH[1]}
else
sudo_user='root'
fi
sudo_user_prefix="/sudo:${sudo_user}@localhost:"
OPTIND+=1
shift
;;
# Extension option `--trace'.
--trac*|-trac*)
OPTIND+=1
shift
# Any prerequisites not met...
if ! is-supported-shell ||
! is-valid-environment ||
! is-current-emacs; then
exit $?
fi
enable_trace='true'
trap ': Line \#"$LINENO"' debug
set -x
;;
# Extension option `--two-way-merge=FILE1,FILE1[,OUTPUT-FILE]':
# open FILEs via Emacs function `emerge-files'.
--tw*|-tw*)
argv[argc++]='--eval'
# FILEs given as --two-way-merge=FILE1,FILE2[,OUTPUT-FILE]
if [[ ."$1" =~ \.[^=]*=([^[:space:]]{1,}) ]]; then
files=${BASH_REMATCH[1]}
# Need eval in case of argument like `~/file'.
file1=$(eval resolve-existing "${files%%,*}")
if test ."$file1" = .''; then
echo "${files%%,*}: No such file or directory"
exit 1
fi
files=${files#*,}
# Need eval in case of argument like `~/file'.
file2=$(eval resolve-existing "${files%%,*}")
if test ."$file2" = .''; then
echo "${files%%,*}: No such file or directory"
exit 1
elif test ."$file2" != ."$files"; then
# Need eval in case of argument like `~/file'.
eval file3=${files#*,}
if [[ ! ."$file3" =~ ^\./ ]]; then
# Since file3 need not exist, resolve-existing
# may not work on it.
file3=$(resolve-existing '.')/${file3}
fi
fi
OPTIND+=1
shift
# FILEs given as second and third, possibly null, arguments.
else
file1=$(resolve-existing "$2")
if test ."$file1" = .''; then
echo "$2: No such file or directory"
exit 1
fi
file2=$(resolve-existing "$3")
if test ."$file2" = .''; then
echo "$3: No such file or directory"
exit 1
fi
OPTIND+=$(( $# > 2 ? 3 : ( $# > 1 ? 2 : 1 ) ))
shift $(( $# > 2 ? 3 : ( $# > 1 ? 2 : 1 ) ))
fi
# Eval `emerge-files' on FILEs.
argv[argc]='(emerge-files '
if test ."$file3" != .''; then
argv[argc]+='nil "'
else
argv[argc]+='1 "'
fi
argv[argc]+=$(printf '%q' "$file1")
argv[argc]+='" "'
argv[argc]+=$(printf '%q' "$file2")
if test ."$file3" != .''; then
argv[argc]+='" "'
argv[argc]+=$(printf '%q' "$file3")
argv[argc]+='" '
else
argv[argc]+='" nil '
fi
argv[argc]+="(and (fboundp 'frame-size-ediff) "
argv[argc]+=" 'frame-size-ediff) "
argv[argc]+="(and (fboundp 'frame-size-default) "
argv[argc++]+=" 'frame-size-default))"
window_name=merge
;;
# Extension option `--update-loaddefs [DIR]'
--up*|-up*)
argv[argc++]='-batch'
argv[argc++]='-q'
argv[argc++]='--eval'
# DIR given as --update-loaddefs=DIR
if [[ ."$1" =~ \.[^=]*=(.*) ]]; then
directory=${BASH_REMATCH[1]}
directory=${directory#\"}
directory=${directory%\"}
OPTIND+=1
shift
# DIR given as second, possibly null, argument.
else
directory=$2
directory=${directory#\"}
directory=${directory%\"}
OPTIND+=$(( $# == 1 ? 1 : 2 ))
shift $(( $# == 1 ? 1 : 2 ))
fi
if test ."$directory" = .''; then
directory='.'
fi
directory=$(resolve-existing "$directory")
# Prefix autoload filename with last element of $directory path.
loaddefs=$($BASENAME_CMD "$directory")
loaddefs+='-loaddefs.el'
# Update directory autoloads to `$(basename DIR)-loaddefs.el'.
argv[argc]='(let ((generated-autoload-file "'
argv[argc]+=$(printf '%q' "${directory}/${loaddefs}")
argv[argc]+='")) '
argv[argc]+='(update-directory-autoloads "'
argv[argc]+=$(printf '%q' "$directory")
argv[argc++]+='"))'
# Emacs-specific argument, so invoke emacs(1).
_EMACSCLIENT_CMD=$_EMACS_CMD
;;
# Extension option `--view'.
--vie*|-vi*)
argv[argc++]='--eval'
argv[argc]='(view-file "'
if [[ ."$1" =~ ^\.[^=]+=(.*) ]]; then
file=${BASH_REMATCH[1]}
file=${file#\"}
file=${file%\"}
argv[argc]+=$(printf '%q' "$file")
OPTIND+=1
shift
# OPTARG given as second, possibly null, argument.
else
file=$2
file=${file#\"}
file=${file%\"}
argv[argc]+=$(printf '%q' "$file")
OPTIND+=$(( $# == 1 ? 1 : 2 ))
shift $(( $# == 1 ? 1 : 2 ))
fi
argv[argc++]+='")'
window_name=$file
;;
# Extension option `--wait'
--wa*|-w*)
background_op=''
OPTIND+=1
shift
;;
# Short version of Emacsclient option `--suppress-output', `-u',
# shadows short version of Emacs option `--user' option.
-u|-supp*|--supp*)
argv[argc++]=$1
OPTIND+=1
shift
;;
# Short version of Emacs option `--funcall', `-f', shadows
# short version of Emacsclient option `--server-file'.
#
# Short version of Emacsclient option `--tramp', `-T',
# shadows short version of Emacs option `--title'.
--bac*|-bac*|-bg|--bg-*|-bg-*| \
--bo*|-bo*|-bd|-bw| \
--ch*|-ch*|--co*|-co*|--cu*|-cu*|-cr| \
--da*|-da*|--di*|-di*|--du*|-du*|-d| \
--fi[nl]*|-fi[nl]*|--fo[nr]*|-fo[nr]*|-fn|-fg| \
--fg-*|-fg-*|-F|--fun*|-fun*|-f| \
--g*|-g*|-ib|--in[ist]*|-in[ist]*| \
-L|--l[oi]*|-l[oi]*|-lsp|-l| \
--mou*|-mou*|-ms|--na*|-na*| \
--pa*|-pa*|--sc*|-sc*|--se*|-se*| \
--t[ei]*|-t[ei]*|-t| \
--u*|-us*|--vi*|-vi*|--xr*|-xr*)
# OPTARG given as --opt=OPTARG.
if [[ ."$1" =~ \.([^=]*)=(.*) ]]; then
argv[argc++]=${BASH_REMATCH[1]}
argv[argc++]=${BASH_REMATCH[2]}
OPTIND+=1
shift
# OPTARG given as second, possibly null, argument.
else
argv[argc++]=$1
argv[argc++]=$2
OPTIND+=$(( $# == 1 ? 1 : 2 ))
shift $(( $# == 1 ? 1 : 2 ))
fi
# Emacs-specific argument, so invoke Emacs.
_EMACSCLIENT_CMD=$_EMACS_CMD
;;
# emacslcient options `-s', `--socket-name',
# `-T', `--tramp'.
#
# As noted above, short version of Emacsclient option
# `--tramp', `-T', shadows by short version of Emacs
# option `--title'.
--so*|-so*|-s|--tr*|-T)
# OPTARG given as --opt=OPTARG.
if [[ ."$1" =~ \.([^=]*)=(.*) ]]; then
argv[argc++]=${BASH_REMATCH[1]}
argv[argc++]=${BASH_REMATCH[2]}
server_name=${BASH_REMATCH[2]}
OPTIND+=1
shift
# OPTARG given as second, possibly null, argument.
else
argv[argc++]=$1
argv[argc++]=$2
server_name=$2
OPTIND+=$(( $# == 1 ? 1 : 2 ))
shift $(( $# == 1 ? 1 : 2 ))
fi
;;
# emacsclient option `--no-wait'.
--no-wa*|-n)
argv[argc++]=$1
OPTIND+=1
shift
;;
# Emacs option `-q' shadows emacsclient short `--quiet' option.
--bas*|-bas*|-D| \
--bat*|-bat*| \
--ful*|-ful*|-fh|-fs|-fw| \
--ic*|-ic*|--max*|-max*|-mm|--mod*|-mod*| \
--no-*|-no-*|-nbc|-nbi|-nl|-nsl|-q| \
--qu*|-qu*|-Q| \
--re*|-r|-rv| \
--ve*|-ve*|-vb|-x)
argv[argc++]=$1
OPTIND+=1
shift
# Emacs-specific argument, so invoke Emacs.
_EMACSCLIENT_CMD=$_EMACS_CMD
;;
# emacsclient option `--create-frame'.
--cr*|-cr*|-c)
preload_files='false'
create_new_frames='true'
create_many_new_frames='false'
frame_or_tty='--create-frame'
OPTIND+=1
shift
;;
# Emacs option `--debug-init'.
--de*|-de*)
argv[argc++]=$1
OPTIND+=1
shift
enable_trace='true'
trap ': Line \#"$LINENO"' debug
set -x
# Emacs-specific argument, so invoke Emacs.
_EMACSCLIENT_CMD=$_EMACS_CMD
;;
# Emacs/emacsclient option `--eval'.
--ex*|-ex*|--ev*|-ev*)
argv[argc++]=$1
argv[argc++]=$2
OPTIND+=$(( $# == 1 ? 1 : 2 ))
shift $(( $# == 1 ? 1 : 2 ))
# Run in foreground.
background_op=''
;;
# Emacs option `--visit FILE'.
--fi*|-fi*|--vi*|-vi*)
argv[argc++]='--eval'
# FILE given as --visit=FILE
if [[ ."$1" =~ \.[^=]*=(.*) ]]; then
file=${BASH_REMATCH[1]}
OPTIND+=1
shift
# FILE given as second, possibly null, argument.
else
file=$2
OPTIND+=$(( $# == 1 ? 1 : 2 ))
shift $(( $# == 1 ? 1 : 2 ))
fi
argv[argc]='(find-file "'
argv[argc]+=$file
argv[argc++]+='")'
;;
# emacsclient option `--frame'
--fr*|-fr*|-F)
# OPTARG given as --opt=OPTARG.
if [[ ."$1" =~ \.([^=]*)=(.*) ]]; then
argv[argc++]=${BASH_REMATCH[1]}
argv[argc++]=${BASH_REMATCH[2]}
OPTIND+=1
shift
# OPTARG given as second, possibly null, argument.
else