Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I start emacs server using

emacs --daemon

then open files using

emacsclient filename.ext

but the first file has to be opened using

emacsclient -c filename.ext

in order to create a new frame which can be later used by subsequent files without using -c command line flag for emacsclient.

I want to automate this. "if there is no emacs frame, emacsclient should create a frame else it should use the current frame". How can it be done?

share|improve this question
    
When I do man emacsclient it doesn't tell me about a -c option. Do you know what versions exhibit this behavior? –  dmckee Nov 16 '11 at 16:19
    
@dmckee emacsclient -c is new to the much improved emacsclient in Emacs 23. –  Gilles Nov 17 '11 at 1:08

3 Answers 3

You can first create a frame if there isn't one already, then open the file in a now existing frame. Here's a snippet that creates a frame on the initial display if there isn't any frame now opened on a window display. You may wish to tweak this in a number of ways, such as checking whether there is already a frame on the display with x-display-list. You need (require 'cl) in your `.emacs. This may require some adaptation to work on Windows or Aqua.

emacsclient -e '(unless (find-if (lambda (f)
                                   (let ((p (frame-parameters f)))
                                     (assq '\''window-system p)))
                                 (frame-list))
                  (make-frame-on-display (getenv "DISPLAY")))'
emacsclient filename.ext
share|improve this answer

I think adding Gilles' function to server-switch-hook might do what you want. Unfortunately, I can't seem to get emacs --daemon working to test it.

(add-hook 'server-switch-hook
          (lambda ()
            (unless (find-if (lambda (f)
                               (let ((p (frame-parameters f)))
                                 (assq 'window-system p)))
                             (frame-list))
              (make-frame-on-display (getenv "DISPLAY")))))
share|improve this answer

Here is the emacs-client.sh script I use under Linux to do exactly what you ask for:

#!/bin/sh

xprop -name emacs >/dev/null 2>/dev/null
if [ "$?" = "1" ]; then
    emacsclient -c -n -a "" "$@"
else
    emacsclient -n -a "" "$@"
fi
share|improve this answer
    
This answer needs more upvotes! –  Z0lenDer Apr 17 at 21:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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