Using Emacs as a server and opening only one window exactly, which should be maximized
Asked Answered
C

3

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?

Capercaillie answered 30/7, 2014 at 6:13 Comment(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?Hetzel
@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.Capercaillie
A
2

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

(modify-all-frames-parameters '((fullscreen . maximized))))
Autography answered 30/7, 2014 at 6:50 Comment(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: https://mcmap.net/q/599657/-maximize-emacs-on-start-up-not-the-fullscreenCoray
modify-all-frames-parameters (at least in my emacs version, 24.3) have effect in the frames yet to be created.Autography
K
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).

Kensell answered 18/1, 2015 at 22:19 Comment(2)
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 $*Equilibrium
@Equilibrium Both work, so this doesn't change anything for me (Emacs 24.5). But it might be different for other Emacs versions...Kensell
A
2

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

(modify-all-frames-parameters '((fullscreen . maximized))))
Autography answered 30/7, 2014 at 6:50 Comment(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: https://mcmap.net/q/599657/-maximize-emacs-on-start-up-not-the-fullscreenCoray
modify-all-frames-parameters (at least in my emacs version, 24.3) have effect in the frames yet to be created.Autography
A
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).

Astrid answered 30/7, 2014 at 13:11 Comment(1)
When I execute echo $DISPLAY, I get the output :0Capercaillie

© 2022 - 2024 — McMap. All rights reserved.