diff options
author | Marc Cornellà <hello@mcornella.com> | 2021-09-28 10:10:43 +0200 |
---|---|---|
committer | Marc Cornellà <hello@mcornella.com> | 2021-09-29 16:50:12 +0200 |
commit | 27dcca5967082474229d5efafb941e0d5229c760 (patch) | |
tree | 753c5f743635f55f71b18bbaf0b44eb7447aaee3 /plugins/emacs/emacsclient.sh | |
parent | b37114b4d5804f8125d3951efde6a1b9d39610d1 (diff) | |
download | zsh-27dcca5967082474229d5efafb941e0d5229c760.tar.gz zsh-27dcca5967082474229d5efafb941e0d5229c760.tar.bz2 zsh-27dcca5967082474229d5efafb941e0d5229c760.zip |
refactor(emacs): simplify emacsclient wrapper code
Diffstat (limited to 'plugins/emacs/emacsclient.sh')
-rwxr-xr-x | plugins/emacs/emacsclient.sh | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/plugins/emacs/emacsclient.sh b/plugins/emacs/emacsclient.sh index 04a2c2afd..0702d7a33 100755 --- a/plugins/emacs/emacsclient.sh +++ b/plugins/emacs/emacsclient.sh @@ -1,29 +1,27 @@ #!/bin/sh -_emacsfun() -{ - # get list of emacs frames. - frameslist=`emacsclient --alternate-editor '' --eval '(frame-list)' 2>/dev/null | egrep -o '(frame)+'` +emacsfun() { + local frames="$(emacsclient --alternate-editor "" -n -e "(length (frame-list))" 2>/dev/null)" - if [ "$(echo "$frameslist" | sed -n '$=')" -ge 2 ] ;then - # prevent creating another X frame if there is at least one present. - emacsclient --alternate-editor "" "$@" - else - # Create one if there is no X window yet. - emacsclient --alternate-editor "" --create-frame "$@" - fi + # Only create another X frame if there isn't one present + if [ -z "$frames" -o "$frames" -lt 2 ]; then + emacsclient --alternate-editor "" --create-frame "$@" + return $? + fi + + emacsclient --alternate-editor "" "$@" } # adopted from https://github.com/davidshepherd7/emacs-read-stdin/blob/master/emacs-read-stdin.sh # If the second argument is - then write stdin to a tempfile and open the # tempfile. (first argument will be `--no-wait` passed in by the plugin.zsh) -if [ "$#" -ge "2" -a "$2" = "-" ] -then - tempfile="$(mktemp --tmpdir emacs-stdin-$USERNAME.XXXXXXX 2>/dev/null \ - || mktemp -t emacs-stdin-$USERNAME)" # support BSD mktemp - cat - > "$tempfile" - _emacsfun --no-wait $tempfile -else - _emacsfun "$@" +if [ $# -ge 2 -a "$2" = "-" ]; then + tempfile="$(mktemp --tmpdir emacs-stdin-$USERNAME.XXXXXXX 2>/dev/null \ + || mktemp -t emacs-stdin-$USERNAME)" # support BSD mktemp + cat - > "$tempfile" + emacsfun --no-wait "$tempfile" + return $? fi + +emacsfun "$@" |