!/usr/bin/python

Copyright Greg Detre (2007) -

greg@gregdetre.co.uk. Released under the GPL.

”” An attempt to wrestle emacsclient into behavior consistent with a standard emacs call.

If you call this:

If you feed in any arguments, it’ll pass them on to either emacsclient or emacs. If you don’t, then it’ll just eval make-frame-on-display to display a new emacs frame (just as if you’d typed ‘emacs’ on its own).

Whenever it calls emacsclient, it uses the -n flag, which means that the shell doesn’t display ‘waiting for emacs’ and you can kill the buffer without it telling you there are still clients connected. ”“”

import os, sys, string

args = sys.argv[:] display = os.getenv(‘DISPLAY’)

this means the user fed in an argument

if len(sys.argv)>1: # throw away the first arg, which is just calling this # very python script, and then join all the other # arguments together with spaces args = string.join( \ # put each argument in quotes (to deal with # filenames that contain spaces) [‘”%s”’ % x for x in sys.argv[1:]], \ ‘ ‘) # because there are some arguments (presumably # filenames), we can just feed these into emacsclient # and it will open them cmd = “emacsclient -n %s” % args

else: # the user didn’t feed in any arguments args = ” # so we need to call emacsclient with # make-frame-on-display, because you can’t call it # directly with no arguments # # we use make-frame-on-display rather than make-frame # because now we can deal with the general case, # including when the current display is not the same as # the host running emacs (i.e. you’re ssh’d into the # machine running the emacs server) cmd = “”“emacsclient -n -e ‘(make-frame-on-display “%s”)’ %s”“” \ % (display, args)

xxx - it might be nice to suppress the ‘can’t find socket’ error

message

retcode = os.system( cmd )

if non-zero, it probably means that there’s no running

emacs server, so start a new instance of emacs instead

if retcode != 0: os.system( ‘emacs %s &’ % args)



Home - Blog - Startups - Software - Research - Writing

Email me - updated 31 Dec 2012