# Cute Emacs & Sawfish Hack for the day

**URL:** <https://www.ruby-forum.com/t/cute-emacs-sawfish-hack-for-the-day/101287>\
**Category:** Ruby\
**Created:** [August 4, 2006, 2:28am UTC](https://www.ruby-forum.com/t/cute-emacs-sawfish-hack-for-the-day/101287 "2006-08-04T02:28:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [August 4, 2006, 2:28am UTC](https://www.ruby-forum.com/t/cute-emacs-sawfish-hack-for-the-day/101287/1 "2006-08-04T02:28:50Z")

</div>

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  
(start-server)  
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  
same…

So…  
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\_SUFFIX = “-cvs”  
EMACS\_CLIENT = “#{EMACS\_DIR}/emacsclient#{EMACS\_SUFFIX}”  
EMACS = “#{EMACS\_DIR}/emacs#{EMACS\_SUFFIX}”  
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  
“^emacs”))’}  
end

STDOUT.sync = true

any\_non\_option\_arguments = ARGV.any?{|a| a !~ /^-/}

if !any\_non\_option\_arguments  
raise\_window  
exit  
end

# Start up client, is we have server socket

if FileTest.exist? EMACS\_SERVER\_SOCKET  
puts "  
Emacs server socket ‘#{EMACS\_SERVER\_SOCKET}’ exists, invoking  
#{EMACS\_CLIENT}  
–nowait #{ARGV.join(" ")}  
"  
raise\_window

# 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

end

puts "  
No emacs server socket ‘#{EMACS\_SERVER\_SOCKET}’  
Invoking ‘#{EMACS}’ directly  
"

# Pop an emacs session into the background.

fork do  
exec( EMACS, \*ARGV)  
end

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

Carter’s Clarification of Murphy’s Law.

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

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