Cute Emacs & Sawfish Hack for the day

So I use emacs (latest from CVS) and gnome sawfish as my window manager.

But I…

  • Hate the *sh languages, they have awful design.
  • I want to reuse emacs sessions, not start up a new one every time.
  • I want the emacs window to jump to the front if I invoke the client.

So emacs has emacsclient.

If you put
in your ~/.emacs
you can…

Start up a normal emacs session…

emacs ~/bin/x

And start editing…

All Good.

Now suppose you back on the command line doing something and you want to
start editing smoothing else…

emacs something.else

Bah! That takes time to load up another emacs. I just want to use the

emacsclient something.else
has the right effect.

But two problems…

  • I always forget about emacsclient. Or if I do remember, I forget I
    had closed the emacs session and must restart.
  • and it leaves the emacs session buried under a pile of
    other windows.

The emacs etc/emacs.bash has a gnarly bit of bash to work around the
first problem, but still leaves the second.

So here is a bit of ruby that does better…

#!/usr/bin/ruby -w

I’m using the latest version of emacs from CVS…

EMACS_DIR = “/usr/local/bin”
EMACS_SERVER_SOCKET = “/tmp/emacs#{Process.uid}/server”

This is the cute bit…

I tell sawfish to find a window with a name starting with “emacs”

and raise it to the top…

def raise_window
system %q{sawfish-client -e ‘(raise-window (get-window-by-name-re

STDOUT.sync = true

any_non_option_arguments = ARGV.any?{|a| a !~ /^-/}

if !any_non_option_arguments

Start up client, is we have server socket

if FileTest.exist? EMACS_SERVER_SOCKET
puts "
Emacs server socket ‘#{EMACS_SERVER_SOCKET}’ exists, invoking
–nowait #{ARGV.join(" ")}

This replaces this process with the emacsclient, which

just tells emacs to do it’s thing then exits…

exec( EMACS_CLIENT, “–no-wait”, *ARGV)

Doesn’t get here


puts "
No emacs server socket ‘#{EMACS_SERVER_SOCKET}’
Invoking ‘#{EMACS}’ directly

Pop an emacs session into the background.

fork do
exec( EMACS, *ARGV)

John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand

Carter’s Clarification of Murphy’s Law.

“Things only ever go right so that they may go more spectacularly wrong

From this principle, all of life and physics may be deduced.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs