108 lines
4.0 KiB
Python
108 lines
4.0 KiB
Python
import commands, os
|
|
import color, method, mode
|
|
from lex import Grammar, PatternRule, RegionRule
|
|
|
|
class LatexGrammar(Grammar):
|
|
rules = [
|
|
PatternRule(r'comment', r'\%.*$'),
|
|
PatternRule(r'latex_control', r'\\[a-zA-Z]+'),
|
|
RegionRule(r'latex_argument', r'{', None, r'}'),
|
|
RegionRule(r'latex_string', r"`", None, r"'"),
|
|
RegionRule(r'latex_string', r"``", None, r"''"),
|
|
PatternRule(r'latex_escaped', r'\\.'),
|
|
PatternRule(r'latex_special', r'[{}$^_%~#&]'),
|
|
]
|
|
|
|
class Latex(mode.Fundamental):
|
|
modename = 'Latex'
|
|
extensions = ['.latex', '.tex']
|
|
grammar = LatexGrammar
|
|
colors = {
|
|
'latex_control': ('blue', 'default', 'bold'),
|
|
'latex_argument.null': ('cyan', 'default', 'bold'),
|
|
'latex_string.start': ('green', 'default', 'bold'),
|
|
'latex_string.null': ('green', 'default', 'bold'),
|
|
'latex_string.end': ('green', 'default', 'bold'),
|
|
'latex_escaped': ('magenta', 'default', 'bold'),
|
|
}
|
|
def __init__(self, w):
|
|
mode.Fundamental.__init__(self, w)
|
|
self.add_bindings('wrap-paragraph', ('M-q',))
|
|
self.add_action_and_bindings(LatexCommentRegion(), ('C-c #', "C-c \%"))
|
|
self.add_action_and_bindings(LatexUncommentRegion(), ('C-u C-c #', "C-u C-c \%"))
|
|
self.add_action_and_bindings(LatexInsertSquotes(), ("M-'",))
|
|
self.add_action_and_bindings(LatexInsertDquotes(), ('M-"',))
|
|
self.add_action_and_bindings(LatexInsertBraces(), ('M-{',))
|
|
self.add_action_and_bindings(LatexBuild(), ("C-c C-c",))
|
|
self.add_action(LatexBuildPdf())
|
|
self.add_action(LatexViewPdf())
|
|
|
|
class LatexBuild(method.Method):
|
|
'''Insert a pair of LaTeX-style single-quotes into the buffer'''
|
|
buildcmd = 'latex'
|
|
def _build(self, w):
|
|
if w.buffer.changed():
|
|
return (True, 'Build Cancelled: unsaved buffer')
|
|
app = w.application
|
|
cmd = "%s '\\batchmode\\input %s' >/dev/null 2>&1" % (self.buildcmd,
|
|
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):
|
|
logpath = self._modpath('.log')
|
|
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'''
|
|
buildcmd = 'pdflatex'
|
|
class LatexViewPdf(LatexBuildPdf):
|
|
'''Insert a pair of LaTeX-style single-quotes into the buffer'''
|
|
viewcmd = 'evince'
|
|
def _execute(self, w, **vargs):
|
|
ok = LatexBuildPdf._execute(self, w, **vargs)
|
|
if ok:
|
|
pid = os.fork()
|
|
if pid == 0:
|
|
pdfpath = self._modpath(w, '.pdf')
|
|
os.execvp(self.viewcmd, (self.viewcmd, pdfpath))
|
|
|
|
class LatexCommentRegion(method.CommentRegion):
|
|
commentc = '%'
|
|
class LatexUncommentRegion(method.UncommentRegion):
|
|
commentc = '%'
|
|
|
|
class LatexInsertSquotes(method.Method):
|
|
'''Insert a pair of LaTeX-style single-quotes into the buffer'''
|
|
def _execute(self, w, **vargs):
|
|
w.insert_string_at_cursor("`'")
|
|
w.backward()
|
|
class LatexInsertDquotes(method.Method):
|
|
'''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()
|
|
class LatexInsertBraces(method.Method):
|
|
'''Insert a pair of curly braces into the buffer'''
|
|
def _execute(self, w, **vargs):
|
|
w.insert_string_at_cursor("{}")
|
|
w.backward()
|
|
|
|
install = Latex.install
|