On  1.07.04, G. Milde wrote:
> 
> I wrote a simple shell script that seems to do the trick. 

There is an error when the filename is relative and  
pwd != LyX's working dir.

The attached python version solves this. 
 
 Günter

-- 
G.Milde at web.de
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

"""Open a file in a running LyX or start LyX with the file

   Needs the LyX-Server running (Set Edit>Preferences>Paths>Server-pipe)
   Test the LyX-Server with lyx -dbg 4096.
"""

# TODO: a function for nonblocking opening of a file/pipe
#       a function for reading a nonblocking pipe with select
#       a lyxclient object (see /usr/share/pybliographer/Pyblio/LyX.py)


import sys, os

# --- Customizable values -------------------------------------------
# the server pipe path (".in" and ".out" will be added)
lyxpipe = os.environ.get("LYXPIPE", "~/.lyx/lyxpipe")

# the lyx-function we want to use (see lfuns in the LyX-Wiki)
lfun = "file-open"

# --- Preparation ---------------------------------------------------

# normalize the path and convert ~-constructs
pipename = os.path.abspath(os.path.expanduser(lyxpipe))

inpipename = pipename+".in"
outpipename = pipename+".out"

# --- open the server pipe -------------------------------------------

# the built in file() function doesnot allow nonblocking mode
# inpipe  = file(lyxpipe+'.in', 'w', 0)

# --> use os.open
# instead of advance tests we catch exceptions (no pipe, lefover pipe)
try:
    inpipe_fd = os.open(inpipename, os.O_WRONLY|os.O_NONBLOCK)
except OSError, exception:
    # print "\nerror opening " + inpipename
    if exception.errno == 2:
	print "serverpipe " + inpipename + " doesnot exist:"
	print "wrong path or no running LyX"
    elif exception.errno == 6:
	print "pipe exists but not open for reading" 
	print "assume leftover from a crashed lyx -> deleting it"
	os.remove(inpipename)
	os.remove(outpipename)
    else:
	raise
    print "starting a new lyx session"
    os.execvp("lyx", sys.argv)

# convert to built in file object
inpipe = os.fdopen(inpipe_fd, 'w', 0)

# Send a LYXCMD for every filename argument
for arg in sys.argv[1:]:
    arg = os.path.abspath(arg)
    # close the LYXCMD line with a '\n' or it will be ignored!
    inpipe.write("LYXCMD:lyx-remote:%s:%s\n"%(lfun,arg))
    # response = outpipe.read()
    # print response

inpipe.close()	     

Reply via email to