27dcca5967082474229d5efafb941e0d5229c760

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

refactor(emacs): simplify emacsclient wrapper code

Diff

 1diff --git a/plugins/emacs/emacsclient.sh b/plugins/emacs/emacsclient.sh
 2index 04a2c2afd518a87281dec087c97a4a1ab1306038..0702d7a33fcea7ae64f4f0cec1b7945b48f4b57d 100755
 3--- a/plugins/emacs/emacsclient.sh
 4+++ b/plugins/emacs/emacsclient.sh
 5@@ -1,29 +1,27 @@
 6 #!/bin/sh
 7 
 8-_emacsfun()
 9-{
10-    # get list of emacs frames.
11-    frameslist=`emacsclient --alternate-editor '' --eval '(frame-list)' 2>/dev/null | egrep -o '(frame)+'`
12+emacsfun() {
13+  local frames="$(emacsclient --alternate-editor "" -n -e "(length (frame-list))" 2>/dev/null)"
14 
15-    if [ "$(echo "$frameslist" | sed -n '$=')" -ge 2 ] ;then
16-        # prevent creating another X frame if there is at least one present.
17-        emacsclient --alternate-editor "" "$@"
18-    else
19-        # Create one if there is no X window yet.
20-        emacsclient --alternate-editor "" --create-frame "$@"
21-    fi
22+  # Only create another X frame if there isn't one present
23+  if [ -z "$frames" -o "$frames" -lt 2 ]; then
24+    emacsclient --alternate-editor "" --create-frame "$@"
25+    return $?
26+  fi
27+
28+  emacsclient --alternate-editor "" "$@"
29 }
30 
31 
32 # adopted from https://github.com/davidshepherd7/emacs-read-stdin/blob/master/emacs-read-stdin.sh
33 # If the second argument is - then write stdin to a tempfile and open the
34 # tempfile. (first argument will be `--no-wait` passed in by the plugin.zsh)
35-if [ "$#" -ge "2" -a "$2" = "-" ]
36-then
37-    tempfile="$(mktemp --tmpdir emacs-stdin-$USERNAME.XXXXXXX 2>/dev/null \
38-                || mktemp -t emacs-stdin-$USERNAME)" # support BSD mktemp
39-    cat - > "$tempfile"
40-    _emacsfun --no-wait $tempfile
41-else
42-    _emacsfun "$@"
43+if [ $# -ge 2 -a "$2" = "-" ]; then
44+  tempfile="$(mktemp --tmpdir emacs-stdin-$USERNAME.XXXXXXX 2>/dev/null \
45+    || mktemp -t emacs-stdin-$USERNAME)" # support BSD mktemp
46+  cat - > "$tempfile"
47+  emacsfun --no-wait "$tempfile"
48+  return $?
49 fi
50+
51+emacsfun "$@"