-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathem.sh
106 lines (93 loc) · 2.66 KB
/
em.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
#!/bin/sh
#
# Script to start Emacs with emacsclient if a client is available.
# Author: Thorsten Dworzak
# Description:
# Wrapper script for Emacs and emacsclient.
# Starts either Emacs (on current host) or emacsclient, depending on whether there is
# already an Emacs process; this is determined by looking for the process or a tag file
# in user's home directory.
# With -n option, starts a new Emacs process, skipping the emacsclient functionality.
# With -d commands are only printed, not executed
#
# Usage: em.sh [-n] [-d] [emacs-option(s)] [file(s)]
#
# Setup
emacs="emacs"
emacs_options=""
client="emacsclient"
emacsclient_options="-a $emacs"
ehfile=~/.emacshost
# create a title/color for new XEmacs processes
title=""
# https://en.wikipedia.org/wiki/X11_color_names
colour="OldLace"
while [ $# -gt 0 ]; do
case "$1" in
-n) new_emacs=true; shift;;
-d) debug=true; shift;;
*) break;;
esac
done
# Start a new emacs process and exit
if [ "$new_emacs" == true ]; then
echo starting new Emacs process requested by -n option...
colour="DeepSkyBlue"
cmd="${emacs} -bg $colour ${emacs_options} $title $*"
if [ "$debug" == true ]; then
echo $cmd
else
(nohup $cmd > /dev/null 2>&1 &) > /dev/null
fi
exit 0
fi
# search running emacs process
if pgrep -U $(id -u) emacsclient > /dev/null; then
running=true;
fi
hostname=`uname -n`
# Usually emacs handles interrupts, but sometimes they end up here
trap "{ \mv ${ehfile} ${ehfile}.last ; exit 255; }" HUP INT
if [[ "$running" != true && ! -f $ehfile ]]; then
# create new tagfile
echo $hostname > ${ehfile}
# start new emacs process and wait until it's finished
echo no running Emacs found, starting new process...
cmd="${emacs} -bg $colour ${emacs_options} $title $*"
if [ "$debug" == true ]; then
echo $cmd
else
eval $cmd
wait
# remove tagfile after application quits
\mv ${ehfile} ${ehfile}.last
fi
else
if [[ "$running" == true && ! -f $ehfile && "$debug" != true ]]; then
# if emacs is running but no tagfile, create tagfile
echo $hostname > ${ehfile}
fi
cmd="$client $emacsclient_options $*"
other_host=$hostname
# get the server host
if [ -f $ehfile ]; then
other_host=`cat $ehfile`
fi
# is this host the server host?
if [ $other_host != $hostname ]; then
cmd="ssh -Y $other_host -f $cmd"
else
cmd="nohup $cmd > /dev/null 2>&1 &"
fi
# make it so
if [ "$debug" == true ]; then
echo $cmd
else
eval $cmd
if [ "$?" -ne 0 ]; then
echo "ERROR occurred."
echo "Note: DISPLAY is currently set to $DISPLAY"
fi
fi
fi
# end