On Sun, Dec 20, 2009 at 03:02:30AM +0100, Enrico Forestieri wrote:

> The script works on both Windows and Linux but doesn't work
> on Cygwin :( I don't know why.

For some reason, COMMAND has to be quoted on cygwin. The attached now works
on everything I have access to. Bennet, does it work for you on Mac?
If not, please launch python in a terminal and report the output of the
following command: "import sys; print sys.platform"

-- 
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.

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[1:]
            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 == "macosx":
    LYXSYSDIRS = ["/Applications/LyX.app/Contents/Resources"]
    LYXBASEDIR = "LyX"
    old_dir = os.getcwd()
    os.chdir(HOME + "/Library/Application Support")
elif OSTYPE == "win32":
    LYXSYSDIRS = [r"..\Resources"]
    LYXBASEDIR = "lyx"
    old_dir = os.getcwd()
    os.chdir(os.environ["APPDATA"])  
else:
    LYXSYSDIRS = ["/usr/share/lyx", "/usr/local/share/lyx", "/opt/share/lyx"]
    LYXBASEDIR = ".lyx"
    old_dir = os.getcwd()
    os.chdir(HOME)

LYXPIPE = ""

print "We're running " + OSTYPE
print "Looking in " + os.getcwd()
for LYXDIR in glob.glob(LYXBASEDIR + "*"):
    PREFERENCES = LYXDIR + "/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

CAT = "cat"
QUOTE = "\""
file = sys.argv[1]
if OSTYPE == "macosx":
    file = string.replace(file, "/private", "")
elif OSTYPE == "cygwin":
    cygpath = os.popen("cygpath -a \"" + file + "\"")
    file = cygpath.read().strip()
    cygpath.close()
elif OSTYPE == "win32":
    CAT = "type"
    QUOTE = ""
    
print "Using the lyxpipe " + LYXPIPE
COMMAND = "LYXCMD:revdvi:server-goto-file-row:" + file + " " + sys.argv[2]
print COMMAND

LYXPIPEIN = "\"" + LYXPIPE + ".in\""
LYXPIPEOUT = "\"" + LYXPIPE + ".out\""

handle = os.popen("echo " + QUOTE + COMMAND + QUOTE + ">" + LYXPIPEIN, 'r')
cmd_stdout = handle.read().strip()
cmd_status = handle.close()
print cmd_stdout
if not cmd_status:
    handle = os.popen(CAT + " " + LYXPIPEOUT, 'r')
    cmd_stdout = handle.read().strip()
    cmd_status = handle.close()
    print "Response: " + cmd_stdout

Reply via email to