On Sat, Dec 19, 2009 at 06:58:29AM -0500, Ben M. wrote: > Okay, here's a very rough draft. This is the first thing I've written > in Python, so some things are very ugly. It works for me under Win32, > but I haven't tested under anything else.
I also tested it on Linux and Cygwin. See comments below. > One other thing that's on my mind: can we automatically fix the > version_suffix upon make? There is this code > lyxrc.dist.in:\serverpipe "~/....@version_suffix@/lyxpipe" > which makes me think I can do > version_suffix = "@version_suffix@" > lyxeditor.py. There's a better way to do that (the way the shell script uses and you seemingly missed in your conversion to python), i.e. using the same script whatever the LyX version. > Finally, can we change the name? Perhaps to gotorow.py? :) I have no strong feelings, but lyxeditor was historically used and there already are other programs doing the same thing and having lyxeditor in their name... > # TODO: Insert dependence on version_suffix This is not needed when using globbing (see the revised script attached here). Essentially, we look for ${LYXBASEDIR}* and thus catch all possible version suffixes. > if line.startswith("~/"): > line = HOME + line[2:] Here you forgot the separator: HOME + "/" + line[2:] > if OSTYPE == "macos": > LYXSYSDIRS = ["/Applications/LyX.app/Contents/Resources"] > LYXBASEDIR = ["LyX" + version_suffix] LYXBASEDIR = "LyX" as we will be using globbing. Same below > old_dir = os.getcwd() > os.chdir(HOME + "/Library/Application Support") > elif OSTYPE == "win32": > LYXSYSDIRS = [r"..\Resources"] > LYXBASEDIR = [r"lyx" + version_suffix] > old_dir = os.getcwd() > os.chdir(os.environ["APPDATA"]) > else: > LYXSYSDIRS = ["/usr/share/lyx", "/usr/local/share/lyx", "/opt/share/lyx"] > LYXBASEDIR = [".lyx" + version_suffix] > old_dir = os.getcwd() > os.chdir(HOME) > > LYXPIPE = "" > > print os.getcwd() > for LYXDIR in LYXBASEDIR: for LYXDIR in glob.glob(LYXBASEDIR + "*"): > PREFERENCES = LYXDIR + "/preferences" > print "Looking in " + PREFERENCES > if not os.path.isfile(PREFERENCES): > continue > # See if preferences file contains a \serverpipe entry > LYXPIPE = parse_serverpipe(PREFERENCES) > # If it does and $LYXPIPE.in exists, break out of the loop > if (not LYXPIPE == "") and os.path.isfile(LYXPIPE + ".in"): if (not LYXPIPE == "") and os.path.exists(LYXPIPE + ".in"): Because a pipe is not a regular file on *nix and the check would fail. > break > LYXPIPE="" > > os.chdir(old_dir) > > if LYXPIPE == "": > # The preferences file does not set lyxpipe, so check lyxrc.dist > for SUBDIR in LYXSYSDIRS: > for LYXSYSDIR in os.listdir(SUBDIR): for LYXSYSDIR in glob.glob(SUBDIR + "*"): > LYXRC_DIST = LYXSYSDIR + "/lyxrc.dist" > if not os.path.isfile(LYXRC_DIST): > continue > # See if lyxrc.dist contains a \serverpipe entry > LYXPIPE = parse_serverpipe(LYXRC_DIST) > # If it does and $LYXPIPE.in exists, break out of the loop > if (not LYXPIPE == "") and os.path.isfile(LYXPIPE + ".in"): if (not LYXPIPE == "") and os.path.exists(LYXPIPE + ".in"): > break > LYXPIPE="" > if (not LYXPIPE == ""): > break > > if LYXPIPE == "": > print "Unable to find the lyxpipe!" > sys.exit(1) > > # Let's do the job > > file = sys.argv[1] > if OSTYPE == "macosx": > print "We're running macosx" > file = string.replace(file, "/private", "") > elif OSTYPE == "cygwin": > print "We're running cygwin" > file = subprocess.Popen("cygpath -a \"" + file + "\"", stdout = > subprocess.PIPE).communicate()[0] I couldn't make this to work and replaced it with cygpath = os.popen("cygpath -a \"" + file + "\"") file = cygpath.read().strip() cygpath.close() These were the most important things. For other marginal details see the attached. The script works on both Windows and Linux but doesn't work on Cygwin :( I don't know why. -- Enrico
#! /usr/bin/env python # -*- coding: utf-8 -*- # file lyxeditor.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING. # author Ronald Florence # author Angus Leeming # author Bennett Helm # author Enrico Forestieri # author Ben M. # Full author contact details are available in file CREDITS # This script passes filename and line number of a latex file to the lyxpipe # of a running instance of LyX. If the filename is an absolute path pointing # to an already existing latex file in the temp dir (produced by a preview, # for example), LyX will jump to the corresponding line in the .lyx file. # It may also be invoked by a viewer for performing a reverse DVI/PDF search. # TODO: Invoke with pythonw instead of python. Pythonw should be added to AltInstaller (26kb) import os, re, string, sys, glob OSTYPE = sys.platform if OSTYPE == "darwin": OSTYPE="macosx" if OSTYPE == "win32": HOME = os.environ["USERPROFILE"] else: HOME = os.environ["HOME"] def parse_serverpipe(PREFERENCES): print "Parsing " + PREFERENCES fh = open(PREFERENCES, "r") lines = fh.readlines() serverpipe = r"\serverpipe " # Want to find lines starting with \serverpipe for line in lines: # Remove extra whitespace line = line.strip() # Does line start with \serverpipe? if line.startswith(serverpipe): # Remove \serverpipe line = line[len(serverpipe):].strip() # Get rid of any quotation marks if line[0] == line[-1] == '"': line = line[1:-1].strip() # Substitute $HOME for ~/ if line.startswith("~/"): line = HOME + "/" + line[2:] if not line == "": return line # Nothing found return "" if not len(sys.argv) == 3: print "Usage: " + sys.argv[0] + " <latexfile> <lineno>" sys.exit(1) if OSTYPE == "macos": LYXSYSDIRS = ["/Applications/LyX.app/Contents/Resources"] LYXBASEDIR = "LyX" CAT = "cat" old_dir = os.getcwd() os.chdir(HOME + "/Library/Application Support") elif OSTYPE == "win32": LYXSYSDIRS = [r"..\Resources"] LYXBASEDIR = "lyx" CAT = "type" old_dir = os.getcwd() os.chdir(os.environ["APPDATA"]) else: LYXSYSDIRS = ["/usr/share/lyx", "/usr/local/share/lyx", "/opt/share/lyx"] LYXBASEDIR = ".lyx" CAT = "cat" old_dir = os.getcwd() os.chdir(HOME) LYXPIPE = "" print os.getcwd() for LYXDIR in glob.glob(LYXBASEDIR + "*"): PREFERENCES = LYXDIR + "/preferences" print "Looking in " + PREFERENCES if not os.path.isfile(PREFERENCES): continue # See if preferences file contains a \serverpipe entry LYXPIPE = parse_serverpipe(PREFERENCES) # If it does and $LYXPIPE.in exists, break out of the loop if (not LYXPIPE == "") and os.path.exists(LYXPIPE + ".in"): break LYXPIPE="" os.chdir(old_dir) if LYXPIPE == "": # The preferences file does not set lyxpipe, so check lyxrc.dist for SUBDIR in LYXSYSDIRS: for LYXSYSDIR in glob.glob(SUBDIR + "*"): LYXRC_DIST = LYXSYSDIR + "/lyxrc.dist" if not os.path.isfile(LYXRC_DIST): continue # See if lyxrc.dist contains a \serverpipe entry LYXPIPE = parse_serverpipe(LYXRC_DIST) # If it does and $LYXPIPE.in exists, break out of the loop if (not LYXPIPE == "") and os.path.exists(LYXPIPE + ".in"): break LYXPIPE="" if (not LYXPIPE == ""): break if LYXPIPE == "": print "Unable to find the lyxpipe!" sys.exit(1) # Let's do the job file = sys.argv[1] if OSTYPE == "macosx": print "We're running macosx" file = string.replace(file, "/private", "") elif OSTYPE == "cygwin": print "We're running cygwin" cygpath = os.popen("cygpath -a \"" + file + "\"") file = cygpath.read().strip() cygpath.close() elif OSTYPE == "win32": print "We're running win32" print "Using the lyxpipe " + LYXPIPE COMMAND = "LYXCMD:revdvi:server-goto-file-row:" + file + " " + sys.argv[2] print COMMAND handle = os.popen("echo " + COMMAND + "> " + LYXPIPE + ".in", 'r') cmd_stdout = handle.read().strip() cmd_status = handle.close() print cmd_stdout if not cmd_status: handle = os.popen(CAT + " " + LYXPIPE + ".out", 'r') cmd_stdout = handle.read() cmd_status = handle.close() print "Response: " + cmd_stdout print "Success." # Failed alternatives to os.popen #print cmd_stdout #print cmd_status #pipe = open(LYXPIPE + ".in", "w") #pipe.write(COMMAND) #pipe.close() #import win32pipe #pipe = win32pipe.popen(LYXPIPE + ".in", "w") #pipe.write(COMMAND)