8

I'd like to run my Emacs in daemon mode, and then use emacsclient to actually display things. However, if I run emacsclient filename, it shows up in the terminal, which isn't what I want; I instead have to pass it the -c option.

However, that option always creates a new frame, which isn't what I want - I'd rather just have one frame, and have stuff open in a new buffer in the same frame if it already exists; otherwise, it should make me a new frame. However, I'm not sure how to do this.

Additionally, I want that one frame to be maximized, which I usually achieve my starting Emacs with the -mm option; how would I ensure that a frame made by emacsclient is also maximized?

2
  • 2
    I'm not sure about your initial call to emacsclient (ie, before you've opened your first frame), but when there's a graphical frame already open, emacsclient filename visits filename in the existing frame (at least for me). Is it always the case that it opens in terminal for you, or just when you have no existing graphical frame?
    – Dan
    Commented Jul 30, 2014 at 10:29
  • @Dan: Only when I have no existing frame. Is there any way to force it to use a frame by default instead of the terminal? Basically, I would like to have a command which a) opens a frame if there isn't one; and b) if there is a frame, opens the document in the same frame in a new buffer.
    – Koz Ross
    Commented Jul 31, 2014 at 9:03

3 Answers 3

5

The following script does the following:

  • start Emacs server if necessary
  • if there are no open frames, open a new one
  • open the given file(s) in the current frame
#!/bin/bash

# Selected options for "emacsclient"
#
# -c          Create a new frame instead of trying to use the current
#             Emacs frame.
#
# -e          Evaluate the FILE arguments as ELisp expressions.
#
# -n          Don't wait for the server to return.
#
# -t          Open a new Emacs frame on the current terminal.
#
# Note that the "-t" and "-n" options are contradictory: "-t" says to
# take control of the current text terminal to create a new client frame,
# while "-n" says not to take control of the text terminal.  If you
# supply both options, Emacs visits the specified files(s) in an existing
# frame rather than a new client frame, negating the effect of "-t".

# check whether an Emacs server is already running
pgrep -l "^emacs$" > /dev/null

# otherwise, start Emacs server daemon
if [ $? -ne 0 ]; then
    emacs --daemon
fi

# return a list of all frames on $DISPLAY
emacsclient -e "(frames-on-display-list \"$DISPLAY\")" &>/dev/null

# open frames detected, so open files in current frame
if [ $? -eq 0 ]; then
    emacsclient -n -t "$@"
# no open frames detected, so open new frame
else
    emacsclient -n -c "$@"
fi

Edit: fixed expansion of positional arguments (2017-12-31).

2
  • 1
    This works fine for terminal emacs. If you want to use the GUI emacs instead, change the line from emacsclient -n -t $* to emacsclient -n -a emacs $*
    – Jounathaen
    Commented Apr 18, 2017 at 17:43
  • @Jounathaen Both work, so this doesn't change anything for me (Emacs 24.5). But it might be different for other Emacs versions... Commented Apr 24, 2017 at 17:33
2

For having every new frame maximized, you could add this to your .emacs:

(modify-all-frames-parameters '((fullscreen . maximized))))
2
  • Isn't modify-all-frames-parameters a one shot deal for whatever frame(s) is/are open at the moment? Perhaps you were thinking of (add-to-list 'default-frame-alist . . .? Here is a link to some code I use for OSX and Windows to control the precise frame size using pixels -- it may require a more recent or developer build of Emacs though: stackoverflow.com/a/18711628/2112489
    – lawlist
    Commented Jul 30, 2014 at 13:57
  • modify-all-frames-parameters (at least in my emacs version, 24.3) have effect in the frames yet to be created.
    – juanleon
    Commented Jul 31, 2014 at 6:40
1

Is your DISPLAY env set in the terminal where you're running emacsclient? Because the behavior you request should be the default (the behavior of reusing existing frames, I mean).

1
  • When I execute echo $DISPLAY, I get the output :0
    – Koz Ross
    Commented Jul 31, 2014 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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