Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 242
Koz Ross Avatar asked Jul 30 '14 06:07

Koz Ross


2 Answers

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).

like image 157
mzuther Avatar answered Oct 05 '22 07:10

mzuther


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

(modify-all-frames-parameters '((fullscreen . maximized))))
like image 38
juanleon Avatar answered Oct 05 '22 07:10

juanleon