Cute Emacs & Sawfish Hack for the day....

Discussion in 'Ruby' started by John Carter, Aug 4, 2006.

  1. John Carter

    John Carter Guest

    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 Carter Phone : (64)(3) 358 6639
    Tait Electronics Fax : (64)(3) 359 4632
    PO Box 1645 Christchurch Email :
    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.
     
    John Carter, Aug 4, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Talimore
    Replies:
    4
    Views:
    572
    T. Audry Glamour
    Jul 18, 2004
  2. Charles Banas

    multidimensional arrays and cute tricks

    Charles Banas, Jan 21, 2004, in forum: C Programming
    Replies:
    9
    Views:
    402
    Dave Thompson
    Feb 2, 2004
  3. Heiko =?ISO-8859-15?Q?K=F6hler?=

    ANN: scintilla based, scriptable code editor CUTE 0.1.6 released

    Heiko =?ISO-8859-15?Q?K=F6hler?=, Jul 27, 2003, in forum: Python
    Replies:
    0
    Views:
    377
    Heiko =?ISO-8859-15?Q?K=F6hler?=
    Jul 27, 2003
  4. Heiko =?ISO-8859-15?Q?K=F6hler?=

    CUTE 0.2 released

    Heiko =?ISO-8859-15?Q?K=F6hler?=, Sep 12, 2003, in forum: Python
    Replies:
    0
    Views:
    328
    Heiko =?ISO-8859-15?Q?K=F6hler?=
    Sep 12, 2003
  5. Andy
    Replies:
    1
    Views:
    766
Loading...

Share This Page