-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathec
executable file
·50 lines (39 loc) · 1.66 KB
/
ec
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
#!/bin/bash
# Taken from: http://mjwall.com/blog/2013/10/04/how-i-use-emacs/
# This script starts emacs daemon if it is not running, opens whatever file
# you pass in and changes the focus to emacs. Without any arguments, it just
# opens the current buffer or *scratch* if nothing else is open. The following
# example will open ~/.bashrc
# ec ~/.bashrc
# You can also pass it multiple files, it will open them all. Unbury-buffer
# will cycle through those files in order
# The compliment to the script is et, which opens emacs in the terminal
# attached to a daemon
# If you want to execute elisp, pass in -e whatever.
# You may also want to stop the output from returning to the terminal, like
# ec -e "(message \"Hello\")" > /dev/null
# emacsclient options for reference
# -a "" starts emacs daemon and reattaches
# -c creates a new frame
# -n returns control back to the terminal
# -e eval the script
# Number of current visible frames,
# Emacs daemon always has a visible frame called F1
visible_frames() {
emacsclient -a "" -e '(length (visible-frame-list))'
}
change_focus() {
# If Emacs frame is suspended, restore it
emacsclient -n -e "(select-frame-set-input-focus (selected-frame))" > /dev/null
}
# try switching to the frame in case it is just minimized
# will start a server if not running
test "$(visible_frames)" -eq "1" && change_focus
if [ "$(visible_frames)" -lt "2" ]; then # need to create a frame
# -c $@ with no args just opens the scratch buffer
emacsclient -n -c "$@" && change_focus
else # there is already a visible frame besides the daemon, so
change_focus
# -n $@ errors if there are no args
test "$#" -ne "0" && emacsclient -n "$@"
fi