2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
8 # author Jürgen Spitzmüller
9 # Full author contact details are available in file CREDITS
11 # This script searches the home directory for a PDF or PS
12 # file with a name containing year and author. If found,
13 # it opens the file in a viewer.
15 import getopt, os, sys, subprocess
17 pdf_viewers = ('pdfview', 'kpdf', 'okular', 'qpdfview --unique',
18 'evince', 'xreader', 'kghostview', 'xpdf', 'SumatraPDF',
19 'acrobat', 'acroread', 'mupdf',
20 'gv', 'ghostview', 'AcroRd32', 'gsview64', 'gsview32')
22 ps_viewers = ("kghostview", "okular", "qpdfview --unique",
23 "evince", "xreader", "gv", "ghostview -swap",
24 "gsview64", "gsview32")
27 sys.stderr.write("lyxpaperview: %s\n" % message)
30 sys.stderr.write("lyxpaperview error: %s\n" % message)
34 msg = "Usage: %s [-v pdfviewer] [-w psviewer] titletoken-1 [titletoken-2] ... [titletoken-n]\n" \
35 " Each title token must occur in the filename (at an arbitrary position).\n" \
36 " You might use quotes to enter multi-word tokens"
37 return msg % prog_name
39 # Copied from lyxpreview_tools.py
40 # PATH and PATHEXT environment variables
41 path = os.environ["PATH"].split(os.pathsep)
43 if "PATHEXT" in os.environ:
44 extlist += os.environ["PATHEXT"].split(os.pathsep)
47 def find_exe(candidates):
50 for command in candidates:
51 prog = command.split()[0]
52 for directory in path:
54 full_path = os.path.join(directory, prog + ext)
55 if os.access(full_path, os.X_OK):
56 # The thing is in the PATH already (or we wouldn't
57 # have found it). Return just the basename to avoid
58 # problems when the path to the executable contains
60 if full_path.lower().endswith('.py'):
61 return command.replace(prog, '"%s" "%s"'
62 % (sys.executable, full_path))
68 def find_exe_or_terminate(candidates):
69 exe = find_exe(candidates)
71 error("Unable to find executable from '%s'" % " ".join(candidates))
77 # use locate if possible (faster)
78 if find_exe(['locate']):
79 p1 = subprocess.Popen(['locate', '-i', args[0].lower()], stdout=subprocess.PIPE)
80 px = subprocess.Popen(['grep', '-Ei', '\.pdf$|\.ps$'], stdin=p1.stdout, stdout=subprocess.PIPE)
85 px = subprocess.Popen(['grep', '-i', arg], stdin=px.stdout, stdout=subprocess.PIPE)
86 p4 = subprocess.Popen(['head', '-n 2'], stdin=px.stdout, stdout=subprocess.PIPE)
88 output = p4.communicate()
89 return output[0].decode("utf8")[:-1]# strip trailing '\n'
90 # FIXME add something for windows as well?
91 # Maybe dir /s /b %WINDIR%\*author* | findstr .*year.*\."ps pdf"
93 for root, dirs, files in os.walk(path):
95 lfname = fname.lower()
96 if lfname.endswith(('.pdf', '.ps')):
99 if lfname.find(arg.lower()) == -1:
103 return os.path.join(root, fname)
109 opts, args = getopt.getopt(sys.argv[1:], "v:w:")
119 error(usage(progname))
121 result = find(args, path = os.environ["HOME"])
123 message("no document found!")
126 message("found document %s" % result)
129 if result.lower().endswith('.ps'):
131 viewer = find_exe_or_terminate(ps_viewers)
136 viewer = find_exe_or_terminate(pdf_viewers)
140 subprocess.call([viewer, result])
144 if __name__ == "__main__":