From d4ebbaf5c97183220254a4cb3d9355dff6d4614c Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Sun, 12 Jul 2009 01:23:13 -0400 Subject: [PATCH] mode detection now uses regexes; modes updated --HG-- branch : pmacs2 --- mode/perl.py | 3 +- mode/python.py | 3 +- mode/sh.py | 5 ++- mode/xml.py | 95 +++++++++++++++++++++++--------------------------- window.py | 8 ++--- 5 files changed, 55 insertions(+), 59 deletions(-) diff --git a/mode/perl.py b/mode/perl.py index 2923bb0..a22f253 100644 --- a/mode/perl.py +++ b/mode/perl.py @@ -698,7 +698,8 @@ class PerlContext(context.Context): class Perl(Fundamental): name = 'Perl' extensions = ['.pl', '.pm', '.pod'] - detection = ['perl'] + #detection = ['perl'] + detection = [re.compile('^#!(?:.+/)?perl')] tabbercls = PerlTabber2 grammar = PerlGrammar commentc = '#' diff --git a/mode/python.py b/mode/python.py index f053119..47ee471 100644 --- a/mode/python.py +++ b/mode/python.py @@ -560,7 +560,8 @@ class Python(mode.Fundamental): ''' name = 'Python' extensions = ['.py'] - detection = ['python'] + #detection = ['python'] + detection = [re.compile('^#!(?:.+/)python')] tabbercls = PythonTabber grammar = PythonGrammar opentokens = ('delimiter',) diff --git a/mode/sh.py b/mode/sh.py index fcc0f4e..18e0a6f 100644 --- a/mode/sh.py +++ b/mode/sh.py @@ -1,3 +1,5 @@ +import re + import commands from tab import StackTabber from mode import Fundamental @@ -190,7 +192,8 @@ class Sh(Fundamental): paths = ['/etc/profile'] basenames = ['.bashrc', '.bash_profile', '.profile'] extensions = ['.bash', '.sh'] - detection = ['sh', 'bash'] + #detection = ['sh', 'bash'] + detection = [re.compile('^#!(?:.+/)sh'), re.compile('^#!(?:.+/)bash')] grammar = ShGrammar tabbercls = ShTabber opentokens = ('delimiter', 'sh_reserved', 'case.start') diff --git a/mode/xml.py b/mode/xml.py index 7b69903..d4b7a48 100644 --- a/mode/xml.py +++ b/mode/xml.py @@ -1,56 +1,55 @@ +import re + import color, method, mode from lex import Grammar, Rule, PatternRule, RegionRule, PatternMatchRule class StringGrammar1(Grammar): rules = [ - PatternRule(r'data', r'[^"&]+'), - PatternRule(r'escaped', r'&[a-z]+;'), + PatternRule('data', '[^"&]+'), + PatternRule('escaped', '&[a-z]+;'), ] class StringGrammar2(Grammar): rules = [ - PatternRule(r'data', r"[^'&]+"), - PatternRule(r'escaped', r'&[a-z]+;'), + PatternRule('data', r"[^'&]+"), + PatternRule('escaped', '&[a-z]+;'), ] class CDataGrammar(Grammar): - rules = [PatternRule(r'data', r'(?:[^\]]|\](?!\])|\]\](?!>))+')] + rules = [PatternRule('data', r'(?:[^\]]|\](?!\])|\]\](?!>))+')] class CommentGrammar(Grammar): - rules = [PatternRule(r'data', r'(?:[^-]|-(?!-)|--(?!>))+')] -class MetadataGrammar(Grammar): - rules = [PatternRule(r'data', r'(?:[^?]|\?(?!>))+')] -class DoctypeGrammar(Grammar): - rules = [PatternRule(r'data', r'[^>]+')] - + rules = [PatternRule('data', '(?:[^-]|-(?!-)|--(?!>))+')] class TagGrammar(Grammar): rules = [ - PatternRule(r'attrname', r'[a-zA-Z_][a-zA-Z0-9_]+(?==)'), - PatternRule(r'namespace', r'[a-zA-Z_]+(?=:)'), - PatternRule(r'meta', r'\?xml'), - PatternRule(r'doctype', r'!DOCTYPE'), - PatternRule(r'name', r'[a-zA-Z_][a-zA-Z0-9_]*'), - PatternRule(r'delimiter', r'[:/=]'), - RegionRule(r'string', r'"', StringGrammar1, r'"'), - RegionRule(r'string', r"'", StringGrammar2, r"'"), - PatternRule(r'spaces', r' +'), - PatternRule(r'eol', r'\n'), + PatternRule('attrname', '[a-zA-Z_][a-zA-Z0-9_]+(?==)'), + PatternRule('namespace', '[a-zA-Z_]+(?=:)'), + PatternRule('name', '[a-zA-Z_][a-zA-Z0-9_]*'), + PatternRule('delimiter', '[:/=]'), + RegionRule('string', '"', StringGrammar1, '"'), + RegionRule('string', "'", StringGrammar2, "'"), + PatternRule('spaces', ' +'), + PatternRule('eol', r'\n'), ] +class MetadataGrammar(Grammar): + rules = [PatternRule('meta', r'\?(?:xml)?')] + TagGrammar.rules +class DoctypeGrammar(Grammar): + rules = [PatternRule('doctype', '!DOCTYPE')] + TagGrammar.rules class XMLGrammar(Grammar): rules = [ # TODO: how does cdata work again? - PatternRule(r'data', r'[^<& \n]+'), - PatternRule(r'spaces', r' +'), - PatternRule(r'xml.entity', r'&[a-z]+;'), - PatternRule(r'eol', r'\n'), - PatternMatchRule('x', r'(<)(/)([a-zA-Z_][a-zA-Z0-9_]*)(>)', + PatternRule('data', r'[^<& \n]+'), + PatternRule('spaces', ' +'), + PatternRule('xml.entity', '&[a-z]+;'), + PatternRule('eol', r'\n'), + PatternMatchRule('x', '(<)(/)([a-zA-Z_][a-zA-Z0-9_]*)(>)', 'xml.tag.start', 'xml.tag.delimiter', 'xml.tag.name', 'xml.tag.end'), - #RegionRule(r'xml.tag', r'<(?![\?!])', TagGrammar, r'/?>'), - RegionRule(r'xml.tag', r'<', TagGrammar, r'/?>'), - RegionRule(r'comment', r''), - #RegionRule(r'xml.metadata', r'<\?', MetadataGrammar, r'\?>'), - #RegionRule(r'xml.doctype', ''), - RegionRule(r'xml.cdata', r''), + RegionRule('xml.tag', r'<(?![\?!])', TagGrammar, '/?>'), + RegionRule('xml.tag', r'<(?![\?!])', TagGrammar, '/?>'), + RegionRule('comment', ''), + RegionRule('xml.tag', r'<(?=\?)', MetadataGrammar, r'\?>'), + RegionRule('xml.tag', '<(?!!)', DoctypeGrammar, '>'), + RegionRule('xml.cdata', r''), ] class XmlValidate(method.shell.Exec): @@ -90,28 +89,20 @@ class XmlCreateCdata(method.Method): class XML(mode.Fundamental): name = 'XML' extensions = ['.xml', '.xml.in', '.xsl', '.xsd'] - detection = ['?xml'] + detection = [re.compile(r'^<\?xml')] grammar = XMLGrammar colors = { - #'xml.metadata.start': ('magenta', 'default', 'bold'), - #'xml.metadata.data': ('magenta', 'default', 'bold'), - #'xml.metadata.end': ('magenta', 'default', 'bold'), - #'xml.doctype.start': ('magenta', 'default', 'bold'), - #'xml.doctype.data': ('magenta', 'default', 'bold'), - #'xml.doctype.end': ('magenta', 'default', 'bold'), - - 'xml.tag.meta': ('magenta', 'default', 'bold'), - 'xml.tag.doctype': ('magenta', 'default', 'bold'), - - 'xml.tag.start': ('default', 'default', 'bold'), - 'xml.tag.namespace': ('magenta', 'default', 'bold'), - 'xml.tag.name': ('blue', 'default', 'bold'), - 'xml.tag.attrname': ('cyan', 'default', 'bold'), - 'xml.tag.end': ('default', 'default', 'bold'), - 'xml.entity': ('magenta', 'default', 'bold'), - 'xml.cdata.start': ('magenta', 'default', 'bold'), - 'xml.cdata.data': ('green', 'default', 'bold'), - 'xml.cdata.end': ('magenta', 'default', 'bold'), + 'xml.tag.meta': ('magenta', 'default', 'bold'), + 'xml.tag.doctype': ('magenta', 'default', 'bold'), + 'xml.tag.start': ('default', 'default', 'bold'), + 'xml.tag.namespace': ('magenta', 'default', 'bold'), + 'xml.tag.name': ('blue', 'default', 'bold'), + 'xml.tag.attrname': ('cyan', 'default', 'bold'), + 'xml.tag.end': ('default', 'default', 'bold'), + 'xml.entity': ('magenta', 'default', 'bold'), + 'xml.cdata.start': ('magenta', 'default', 'bold'), + 'xml.cdata.data': ('green', 'default', 'bold'), + 'xml.cdata.end': ('magenta', 'default', 'bold'), } actions = [XmlValidate, XmlCreateTag, XmlCreateComment, XmlCreateCdata] _bindings = { diff --git a/window.py b/window.py index 8c6515b..afed9f5 100644 --- a/window.py +++ b/window.py @@ -65,10 +65,10 @@ class Window(object): elif regex.auto_mode_vi.search(firstline): mode_name = regex.auto_mode_vi.search(firstline).group(1) else: - line = b.lines[0] - for word in a.mode_detection: - if word in line: - mode_name = a.mode_detection[word] + for (r, name) in a.mode_detection.items(): + if r.match(b.lines[0]): + mode_name = name + break cls = a.modes.get(mode_name, a.modes['fundamental']) self.set_mode(cls(self))