From c8768bf7cb0ba35bd9f0dc7f3e037b6cabd535fe Mon Sep 17 00:00:00 2001 From: moculus Date: Tue, 1 Apr 2008 23:14:58 +0000 Subject: [PATCH] latex mode --HG-- branch : pmacs2 --- application.py | 2 +- buffer.py | 2 +- lex.py | 9 +++++-- method/__init__.py | 6 +++-- mode/dir.py | 2 +- mode/latex.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 mode/latex.py diff --git a/application.py b/application.py index c66905e..b9e37d7 100755 --- a/application.py +++ b/application.py @@ -113,7 +113,7 @@ class Application(object): 'dir', 'elisp', 'hex', 'html', 'java', 'javascript', 'lisp', 'make', 'mini', 'mutt', 'nasm', 'ocaml', 'perl', 'python', 'replace', 'rst', 'scheme', 'search', 'sh', 'sql', 'tt', - 'text', 'text2', 'which', 'xml', 'cheetah', 'colortext') + 'text', 'text2', 'which', 'xml', 'cheetah', 'colortext', 'latex') for name in names: exec("import mode.%s; mode.%s.install(self)" % (name, name)) diff --git a/buffer.py b/buffer.py index 92854f9..669eb9e 100644 --- a/buffer.py +++ b/buffer.py @@ -611,7 +611,7 @@ class PathListBuffer(DirBuffer): btype = 'pathlist' def __init__(self, name, paths, nl='\n'): Buffer.__init__(self, nl) - self.paths = paths + self.paths = list(paths) self.path = os.getcwd() self._name = name def path_exists(self): diff --git a/lex.py b/lex.py index b97eb59..6c67857 100755 --- a/lex.py +++ b/lex.py @@ -181,7 +181,7 @@ class RegionRule(Rule): while len(args) > 1: grammar = args.pop(0) pattern = args.pop(0) - assert hasattr(grammar, 'rules'), repr(grammar) + #assert hasattr(grammar, 'rules'), repr(grammar) assert type(pattern) == type(''), repr(pattern) self.pairs.append((grammar, pattern)) if len(args) == 1: @@ -238,7 +238,12 @@ class RegionRule(Rule): del lexer.mode.gstack[fqname] else: mode = lexer.mode - grammar = self.pairs[i][0] + # XYZ + #grammar = self.pairs[i][0] + if self.pairs[i][0] is not None: + grammar = self.pairs[i][0] + else: + grammar = lexer.grammar lexer.mstack.append(mode) if self.pairs[i][1]: diff --git a/method/__init__.py b/method/__init__.py index 3e2102f..2137212 100644 --- a/method/__init__.py +++ b/method/__init__.py @@ -453,6 +453,7 @@ class GetIndentionLevel(Method): # commenting class CommentRegion(Method): '''Prepend a comment to every line in the current buffer''' + commentc = '#' def _execute(self, w, **vargs): cursor = w.logical_cursor() if cursor < w.mark: @@ -465,9 +466,10 @@ class CommentRegion(Method): w.input_line = "Empty kill region" return for y in range(p1.y, p2.y): - w.buffer.insert_string(Point(0, y), "#") + w.buffer.insert_string(Point(0, y), self.commentc) class UncommentRegion(Method): '''Remove a comment from every line in the current buffer''' + commentc = '#' def _execute(self, w, **vargs): cursor = w.logical_cursor() if cursor < w.mark: @@ -480,7 +482,7 @@ class UncommentRegion(Method): w.input_line = "Empty kill region" return for y in range(p1.y, p2.y): - if w.buffer.lines[y].startswith("#"): + if w.buffer.lines[y].startswith(self.commentc): w.buffer.delete(Point(0, y), Point(1, y)) # wrapping/justifying/etc diff --git a/mode/dir.py b/mode/dir.py index ac947d3..d4eab89 100644 --- a/mode/dir.py +++ b/mode/dir.py @@ -95,7 +95,7 @@ class DirGrep(Method): def _execute(self, w, **vargs): cmd = 'grep -rEl %r %r' % (vargs['pattern'], w.buffer.path) (status, output) = commands.getstatusoutput(cmd) - paths = output.split('\n') + paths = [x for x in output.split('\n') if x] bufname = '*%s*' % self.name.title() b = buffer.PathListBuffer(bufname, paths) b.modename = 'dir' diff --git a/mode/latex.py b/mode/latex.py new file mode 100644 index 0000000..12b5372 --- /dev/null +++ b/mode/latex.py @@ -0,0 +1,60 @@ +import color, mode +from lex import Grammar, PatternRule, RegionRule +from method import Method, CommentRegion, UncommentRegion + +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"''"), + 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-{',)) + +class LatexCommentRegion(CommentRegion): + commentc = '%' +class LatexUncommentRegion(UncommentRegion): + commentc = '%' + +class LatexInsertSquotes(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): + '''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): + '''Insert a pair of curly braces into the buffer''' + def _execute(self, w, **vargs): + w.insert_string_at_cursor("{}") + w.backward() + +install = Latex.install