2acae3797b713db2520bb27e76f25d3de6cee48e

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

Message

fix(emacs): assess if there are open frames of the expected type

This change looks at the frame type of the open frames ('framep) and
looks if they're of the type requested based on the arguments passed
to emacsclient (-nw/-t/--tty require tty frames, otherwise we need
graphical frames).

NOTE: this code considers anything different than t as graphical
terminals, including MS-DOS types (pc). I don't have such a setup
to test if this is correct.

Diff

 1diff --git a/plugins/emacs/emacsclient.sh b/plugins/emacs/emacsclient.sh
 2index 0702d7a33fcea7ae64f4f0cec1b7945b48f4b57d..53a3a428a7537f88b96998a793a7226103d87af3 100755
 3--- a/plugins/emacs/emacsclient.sh
 4+++ b/plugins/emacs/emacsclient.sh
 5@@ -1,10 +1,20 @@
 6 #!/bin/sh
 7 
 8 emacsfun() {
 9-  local frames="$(emacsclient --alternate-editor "" -n -e "(length (frame-list))" 2>/dev/null)"
10+  local cmd frames
11+
12+  # Build the Emacs Lisp command to check for suitable frames
13+  # See https://www.gnu.org/software/emacs/manual/html_node/elisp/Frames.html#index-framep
14+  case "$*" in
15+  *-t*|*--tty*|*-nw*) cmd="(memq 't (mapcar 'framep (frame-list)))" ;; # if != nil, there are tty frames
16+  *) cmd="(delete 't (mapcar 'framep (frame-list)))" ;; # if != nil, there are graphical terminals (x, w32, ns)
17+  esac
18+
19+  # Check if there are suitable frames
20+  frames="$(emacsclient -a '' -n -e "$cmd" 2>/dev/null)"
21 
22   # Only create another X frame if there isn't one present
23-  if [ -z "$frames" -o "$frames" -lt 2 ]; then
24+  if [ -z "$frames" -o "$frames" = nil ]; then
25     emacsclient --alternate-editor "" --create-frame "$@"
26     return $?
27   fi