2008-12-25

opening emacsclient windows on the current desktop

Using emacsclient deserves its own entry, but let's assume you've already set that up. Basically, you'll put (server-start) in your .emacs, and emacs will run in server mode, which means you can open files in an existing emacs by using the 'emacsclient' command.

One small annoyance with this is that when using virtual desktop / workspaces, running emacsclient will open the file on a different virtual desktop. As with just about anything, there are some tricks to move your existing emacs to the current desktop; add the following to your .emacs:

;; move to current desktop 
(add-hook 'server-switch-hook
  (lambda ()
    (call-process
      "wmctrl" nil nil nil "-i" "-R"
      (frame-parameter (or frame (selected-frame)) 'outer-window-id))))
This works when using X (GNOME, KDE,...), and requires the wmctrl program. An alternative solution is to use a new window ('frame', remember emacs terminology) with the new buffer. I am not using that, because I prefer to use one single window. However, if you prefer to use seperate windows for each buffer opened with emacsclient, you can use this trick (courtesy of EmacsWiki) instead:
;; open a new frame
(add-hook 'server-switch-hook
   (lambda nil
     (let ((server-buf (current-buffer)))
       (bury-buffer)
       (switch-to-buffer-other-frame server-buf))))
Overall, emacsclient is a nice tool; as with many things about emacs, it does take a bit of work to make it work they way you want.

2 comments:

Speredenn said...

Hi,

I'm trying to use wmctrl with Emacs 23 in my Gnome 2.28 environment to have all my emacs buffers in a single frame. I've used your code in my .emacs configuration file but it doesn't behave as expected (I still have a frame per buffer per file opened from Nautilus).

Could it be linked with the use of Compiz? I've disabled the visual effects but it doesn't seem to change anything to the Emacs behavior.

djcb said...

@Speredenn: hmmm... not sure. I would try to experiment a bit with wmctrl, and see what works (and what doesn't) with compiz.