1

I'm trying to make a script to determine whether emacsclient should be ran with -nc or -t. The script is like this:

#!/bin/sh 
if [ -z "$DISPLAY" ]; then 
    IS_GRAPHICAL=true 
else 
    IS_GRAPHICAL=$(emacs --batch -Q --eval='(if (fboundp '"'"'tool-bar-mode) (message "true") (message "false"))' 2>&1) 
fi 
if $IS_GRAPHICAL; then 
    emacsclient -a "" -nc "$@" 
else 
    emacsclient -a "" -t "$@" 
fi

However the emacs --batch -Q ... line takes as much time as using a bare emacs instance. If I comment out this line, however, I get nothing instead of a clean of screen when this is executed under a tty. Is it possible to make the detection faster?

EDIT: I have already started emacs daemon by a systemd user unit. I want emacsclient to be a window(X11) when launched via dmenu or gmrun, but in terminal while launched in terminal (from shell or git).

EDIT2: I have not expressed myself clearly. I want the script behave like this:

  • stay in the terminal (that is, do not open a GUI window) if called with a file (e.g. called by git-commit) or when graphical environment isn't present (e.g. called while in tty) and don't return until exit (C-x C-c)
  • open a GUI window when called without a file (behave like bare emacs call; when I do this with emacsclient it tells me that input file is required) and return immediately.
  • I don't understand what you're trying to do here. What is this test for tool-bar-mode supposed to accomplish? – Gilles Feb 17 '16 at 1:01
  • @Gilles Uh.. The script is mainly from ArchWiki and I don't know either. – Johnson Steward Feb 17 '16 at 1:46
  • Are you starting emacs --daemon in a console, before X11 init? Do you want to force emacsclient to use an X11 window if possible? "What you are trying to do" requires a description of the use case that is failing for you before one can look at why a solution doesn't do the job. – bsd Feb 17 '16 at 10:00
  • 1
    @bsd Edited above. – Johnson Steward Feb 17 '16 at 10:27
  • emacs is pretty good at understanding its environment, I'm guessing it's only the " in terminal while launched in terminal" part that's needs to be forced, yes? – bsd Feb 17 '16 at 10:36
1

That script from the Arch Wiki

This invocation of emacs that checks (fboundp 'tool-bar-mode) is testing whether your build of Emacs includes GUI support. It's possible to build Emacs without it to save a little bit of RAM and disk space¹, but it's uncommon. Unless you care about compatibility with Emacs builds that lack GUI support, forget about this bit.

You are thus left with

if [ -n "$DISPLAY" ]; then
  emacsclient -a "" -n -c "$@"
else
  emacsclient -a "" "$@"
fi

And even that level of complexity is necessary only if you want to have the call to emacsclient return immediately if it opens a new GUI window. (If it's running in the terminal, then it has to keep running until you finish using that terminal.) It's up to you to decide whether you like this behavior. If you don't then you can simply run

emacsclient -a "" "$@"

And if you start Emacs with your session anyway, e.g. as a systemd unit, then you don't need to tell emacsclient to start it. So we're left with

emacsclient "$@"

which I cannot simplify further.

Your added requirement

  • stay in the terminal (…) if called with a file (…) or when graphical environment isn't present (…)
  • open a GUI window when called without a file (…) and return immediately

For that you need to test whether the script was called with arguments. I'm going to assume that if you pass any emacsclient option (e.g. --eval), that counts as an argument.

if [ $# -ne 0 ]; then
  emacsclient -nw "$@"
elif [ -n "$DISPLAY" ]; then
  emacsclient -c -n
else
  emacsclient -c
fi

¹ No GUI support means you don't need to have the requisite libraries installed. The emacs program can't start if the libraries it eneds aren't present, even if it isn't going to use the GUI features in that particular session.

  • Maybe I have not expressed myself clearly; question edited above; sorry :) – Johnson Steward Feb 18 '16 at 5:10
  • This way I never get a GUI window from emacsclient. Also I can't just get an empty emacs window (e.g. for C-h t) from merely an emccall. – Johnson Steward Feb 18 '16 at 10:09
  • @JohnsonSteward Well I sure couldn't have guessed those requirements, I'm not a mind reader. I've added an implementation of your requirement to my answer. – Gilles Feb 18 '16 at 10:54
  • Uh.. You still ignored something in the question. If I do emc blabla in gmrun, I should get a GUI window. – Johnson Steward Feb 18 '16 at 12:00
  • @JohnsonSteward No, that's not what you asked for. You wrote “stay in the terminal (…) if called with a file” and that's what I implemented. If you want to change your requirements, ask a new question and be sure to state your requirements clearly. – Gilles Feb 18 '16 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.