2009-03-30 01:10:25 -04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from mode import Fundamental
|
|
|
|
from method import Method
|
2008-04-01 19:14:58 -04:00
|
|
|
from lex import Grammar, PatternRule, RegionRule
|
2008-04-03 18:09:43 -04:00
|
|
|
from mode.text import TextInsertSpace
|
2008-04-01 19:14:58 -04:00
|
|
|
|
2009-04-08 00:21:29 -04:00
|
|
|
class ArgumentGrammar(Grammar): rules = [PatternRule('data', '[^}]+')]
|
|
|
|
class StringGrammar1(Grammar): rules = [PatternRule('data', "[^']+")]
|
|
|
|
class StringGrammar2(Grammar): rules = [PatternRule('data', "(?:[^']|'(?!'))+")]
|
2008-10-07 23:39:22 -04:00
|
|
|
|
2009-04-03 17:59:05 -04:00
|
|
|
class FalseGrammar(Grammar):
|
|
|
|
rules = [
|
|
|
|
PatternRule('data', r'[^\\]+'),
|
|
|
|
RegionRule('comment', r'\\if', None, r'\\fi'),
|
|
|
|
]
|
2009-04-03 17:52:30 -04:00
|
|
|
|
2008-04-01 19:14:58 -04:00
|
|
|
class LatexGrammar(Grammar):
|
|
|
|
rules = [
|
2009-04-08 00:21:29 -04:00
|
|
|
PatternRule('comment', r'\%.*$'),
|
2009-04-03 17:59:05 -04:00
|
|
|
RegionRule('comment', r'\\iffalse', FalseGrammar, r'\\fi'),
|
2009-04-08 00:21:29 -04:00
|
|
|
PatternRule('latex.wrapper', r'\\(?:begin|end)'),
|
|
|
|
PatternRule('latex.control', r'\\[a-zA-Z]+'),
|
|
|
|
RegionRule('latex.argument', r'{', ArgumentGrammar, r'}'),
|
|
|
|
RegionRule('latex.string', r"``", StringGrammar2, r"''"),
|
|
|
|
RegionRule('latex.string', r"`", StringGrammar1, r"'"),
|
|
|
|
PatternRule('latex.escaped', r'\\.'),
|
|
|
|
PatternRule('latex.special', r'[{}$^_%~#&]'),
|
|
|
|
PatternRule('latex.data', r'[^{}$^_%~#&%\\`]+'),
|
2008-04-01 19:14:58 -04:00
|
|
|
]
|
|
|
|
|
2009-03-30 01:10:25 -04:00
|
|
|
class LatexBuild(Method):
|
2008-04-02 10:43:35 -04:00
|
|
|
'''Insert a pair of LaTeX-style single-quotes into the buffer'''
|
2008-04-16 10:44:24 -04:00
|
|
|
def _getcmd(self, w):
|
|
|
|
return w.application.config.get('latex.buildcmd')
|
2008-04-02 10:43:35 -04:00
|
|
|
def _build(self, w):
|
|
|
|
if w.buffer.changed():
|
|
|
|
return (True, 'Build Cancelled: unsaved buffer')
|
|
|
|
app = w.application
|
2008-04-16 10:44:24 -04:00
|
|
|
buildcmd = self._getcmd(w)
|
|
|
|
cmd = "%s '\\batchmode\\input %s' >/dev/null 2>&1" % (buildcmd,
|
2008-04-02 10:43:35 -04:00
|
|
|
w.buffer.path)
|
|
|
|
status = os.system(cmd)
|
|
|
|
if status == 0:
|
|
|
|
return (True, 'Build OK')
|
|
|
|
else:
|
|
|
|
return (False, 'Build Error')
|
|
|
|
def _modpath(self, w, ext):
|
|
|
|
return os.path.splitext(w.buffer.path)[0] + ext
|
|
|
|
def _readlog(self, w):
|
2008-04-03 18:09:43 -04:00
|
|
|
logpath = self._modpath(w, '.log')
|
2008-04-02 10:43:35 -04:00
|
|
|
f = open(logpath, 'r')
|
|
|
|
output = f.read()
|
|
|
|
f.close()
|
|
|
|
return output
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
(ok, mesg) = self._build(w)
|
|
|
|
w.set_error(mesg)
|
|
|
|
if not ok:
|
|
|
|
output = self._readlog(w)
|
|
|
|
bufname = '*%s*' % self.name
|
|
|
|
w.application.data_buffer(bufname, output, switch_to=not ok)
|
|
|
|
return ok
|
|
|
|
|
|
|
|
class LatexBuildPdf(LatexBuild):
|
|
|
|
'''Insert a pair of LaTeX-style single-quotes into the buffer'''
|
2008-04-16 10:44:24 -04:00
|
|
|
def _getcmd(self, w):
|
|
|
|
return w.application.config.get('latex.pdfbuildcmd')
|
2008-04-02 10:43:35 -04:00
|
|
|
class LatexViewPdf(LatexBuildPdf):
|
|
|
|
'''Insert a pair of LaTeX-style single-quotes into the buffer'''
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
ok = LatexBuildPdf._execute(self, w, **vargs)
|
|
|
|
if ok:
|
2008-04-16 10:44:24 -04:00
|
|
|
viewcmd = w.application.config.get('latex.pdfviewcmd')
|
2008-04-02 10:43:35 -04:00
|
|
|
pid = os.fork()
|
|
|
|
if pid == 0:
|
2008-05-14 09:17:06 -04:00
|
|
|
# redirect stdout/stderr to a log file
|
|
|
|
f = open('.pmacs-latex-pdf.err', 'a')
|
|
|
|
sys.stderr.flush()
|
|
|
|
os.dup2(f.fileno(), sys.stderr.fileno())
|
|
|
|
sys.stdout.flush()
|
|
|
|
os.dup2(f.fileno(), sys.stdout.fileno())
|
|
|
|
# ok, now do the exec
|
2008-04-02 10:43:35 -04:00
|
|
|
pdfpath = self._modpath(w, '.pdf')
|
2008-04-16 10:44:24 -04:00
|
|
|
os.execvp(viewcmd, (viewcmd, pdfpath))
|
2008-04-02 10:43:35 -04:00
|
|
|
|
2009-03-30 01:10:25 -04:00
|
|
|
class LatexInsertSquotes(Method):
|
2008-04-01 19:14:58 -04:00
|
|
|
'''Insert a pair of LaTeX-style single-quotes into the buffer'''
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
w.insert_string_at_cursor("`'")
|
|
|
|
w.backward()
|
2009-03-30 01:10:25 -04:00
|
|
|
class LatexInsertDquotes(Method):
|
2008-04-01 19:14:58 -04:00
|
|
|
'''Insert a pair of LaTeX-style double-quotes into the buffer'''
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
w.insert_string_at_cursor("``''")
|
|
|
|
w.backward()
|
|
|
|
w.backward()
|
2009-03-30 01:10:25 -04:00
|
|
|
class LatexInsertBraces(Method):
|
2008-04-01 19:14:58 -04:00
|
|
|
'''Insert a pair of curly braces into the buffer'''
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
w.insert_string_at_cursor("{}")
|
|
|
|
w.backward()
|
|
|
|
|
2008-04-03 18:09:43 -04:00
|
|
|
class LatexInsertSpace(TextInsertSpace):
|
|
|
|
pass
|
|
|
|
|
2009-03-30 01:10:25 -04:00
|
|
|
class LatexCheckSpelling(Method):
|
2008-04-04 16:12:31 -04:00
|
|
|
"""Check the spelling of the document via ispell -t"""
|
|
|
|
def _execute(self, w, **vargs):
|
2008-04-04 19:16:21 -04:00
|
|
|
# -x no backup file
|
|
|
|
# -M show context menu
|
|
|
|
# -t treat input document as TeX
|
|
|
|
w.application.run_external('ispell', '-x', '-M', '-t', w.buffer.path)
|
|
|
|
if w.buffer.changed_on_disk():
|
|
|
|
w.buffer.reload()
|
2008-04-04 16:12:31 -04:00
|
|
|
|
2009-03-30 01:10:25 -04:00
|
|
|
class Latex(Fundamental):
|
2009-03-17 15:24:10 -04:00
|
|
|
name = 'Latex'
|
2008-04-18 23:32:08 -04:00
|
|
|
extensions = ['.latex', '.tex']
|
2009-02-15 12:06:35 -05:00
|
|
|
commentc = '%'
|
2008-04-18 23:32:08 -04:00
|
|
|
grammar = LatexGrammar
|
|
|
|
colors = {
|
2009-04-08 00:21:29 -04:00
|
|
|
'latex.wrapper': ('magenta', 'default', 'bold'),
|
|
|
|
'latex.control': ('blue', 'default', 'bold'),
|
|
|
|
'latex.argument.null': ('cyan', 'default', 'bold'),
|
|
|
|
'latex.argument.data': ('cyan', 'default', 'bold'),
|
2008-04-18 23:32:08 -04:00
|
|
|
}
|
|
|
|
config = {
|
|
|
|
'latex.buildcmd': 'latex',
|
|
|
|
'latex.pdfbuildcmd': 'pdflatex',
|
|
|
|
'latex.pdfviewcmd': 'evince',
|
|
|
|
}
|
2009-02-15 12:06:35 -05:00
|
|
|
actions = [LatexInsertSquotes,
|
2008-04-18 23:32:08 -04:00
|
|
|
LatexInsertDquotes, LatexInsertBraces, LatexBuild,
|
|
|
|
LatexInsertSpace, LatexBuildPdf, LatexViewPdf,
|
|
|
|
LatexCheckSpelling]
|
2009-02-15 12:06:35 -05:00
|
|
|
_bindings = {
|
|
|
|
'wrap-paragraph': ('M-q',),
|
|
|
|
'latex-insert-squotes': ("M-'",),
|
|
|
|
'latex-insert-dquotes': ('M-"',),
|
|
|
|
'latex-insert-braces': ('M-{',),
|
|
|
|
'latex-build': ("C-c C-c", 'C-c B'),
|
|
|
|
'latex-insert-space': ('SPACE',),
|
|
|
|
'latex-build-pdf': ("C-c C-p",),
|
|
|
|
'latex-view-pdf': ('C-c C-v',),
|
|
|
|
}
|
2008-04-18 23:32:08 -04:00
|
|
|
|
2008-04-01 19:14:58 -04:00
|
|
|
install = Latex.install
|