“”” An attempt to wrestle emacsclient into behavior consistent with a standard emacs call.
If you call this:
when there’s no running emacs server, it’ll start a new instance of emacs (in the background)
when there is a running emacs server, it’ll use emacsclient
it’ll use the DISPLAY environment variable where possible, so that it will show up on your running display, even if you’re ssh’d into another computer when you call it. if you’re just running it on your local computer, it’ll show up there
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’)
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)
retcode = os.system( cmd )
if retcode != 0: os.system( ‘emacs %s &’ % args)
Home - Blog - Startups - Software - Research - Writing
Email me - updated 31 Dec 2012