From c53d9e45848d07b1bd07747298b35868231f8ae9 Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Fri, 5 Apr 2013 23:00:22 -0400 Subject: [PATCH] add twine mode --HG-- branch : pmacs2 --- application.py | 2 +- mode/twine.py | 175 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 mode/twine.py diff --git a/application.py b/application.py index 4fdebfb..9a9a58b 100755 --- a/application.py +++ b/application.py @@ -177,7 +177,7 @@ class Application(object): 'haskell', 'erlang', 'iperl', 'iperlmini', 'ipython', 'ipythonmini', 'awk', 'shell', 'shellmini', 'fstab', 'yacc', 'pipe', 'mbox', 'error', 'lua', 'lily', 'forth', 'ebnf', 'colortest', 'go', - 'inform6', 'scala', 'markdown', 'roy', + 'inform6', 'scala', 'markdown', 'roy', 'twine', ) for name in names: exec("import mode.%s; mode.%s.install(self)" % (name, name)) diff --git a/mode/twine.py b/mode/twine.py new file mode 100644 index 0000000..3bbb40b --- /dev/null +++ b/mode/twine.py @@ -0,0 +1,175 @@ +from lex import Grammar, PatternRule, RegionRule, OverrideRegionRule, PatternMatchRule +from mode import Fundamental +from method import Method, WrapParagraph +from mode.text import TextInsertSpace + +from mode.c import CGrammar +from mode.html import HTMLGrammar +from mode.java import JavaGrammar +from mode.javascript import JavascriptGrammar +from mode.python import PythonGrammar +from mode.scala import ScalaGrammar +from mode.sql import SqlGrammar +from mode.xml import XMLGrammar + +class LineGrammar(Grammar): pass +LineGrammar.rules = [ + PatternRule('line', r'.+'), + PatternRule('eol', r'\n'), +] + +class WordGrammar(Grammar): pass +WordGrammar.rules = [ + PatternRule('word', r'[A-Za-z0-9_]+'), +] + +class CmdGrammar(Grammar): pass +CmdGrammar.rules = [ + PatternRule('tw.var', r'\$[a-zA-Z0-9_]+'), + PatternRule('tw.keyword', '(?<=<<)[a-zA-Z0-9_]+'), + PatternRule('tw.op', r'(?:eq|neq|gte|gt|lte|lt|and|or|not)'), + PatternRule('arg', r'"(?:[^\\"]|\.)*"'), + PatternRule('arg', r"'(?:[^\\']|\.)*'"), + PatternRule('spaces', r' +'), +] + +class TwineGrammar(Grammar): pass +TwineGrammar.rules = [ + #PatternRule('md.head1', r'^#.+'), + #PatternRule('md.head2', r'^##.+'), + #PatternRule('md.head3', r'^###.+'), + #PatternRule('md.head4', r'^####.+'), + #PatternRule('md.head5', r'^#####.+'), + #PatternRule('md.head6', r'^######.+'), + # + #RegionRule('md.tag', r'^<(?P[a-zA-Z_][a-zA-Z0-9_]*)>$', HTMLGrammar, r'^$'), + # + #PatternRule('md.bullet', r'^\s*[\*\-\*] '), + # + #PatternMatchRule('', r'(\[.+?\])(\(.+?\))', 'md.linkname', 'md.linkurl'), + # + #PatternRule('md.block3', r'^(?:>\s*>\s*>\s*)*>\s*>\s*>(?:[^>].*)?\n$'), + #PatternRule('md.block2', r'^(?:>\s*>\s*>\s*)*>\s*>(?:[^>].*)?\n$'), + #PatternRule('md.block1', r'^(?:>\s*>\s*>\s*)*>(?:[^>].*)?\n$'), + # + #PatternRule('md.code', r'^(?: |\t).*$'), + # + #PatternRule('md.inlinelink', r'http://[^`*\t(){}\[\] ]+'), + # + #RegionRule('md.lang', r'^```c$', CGrammar, r'^```'), + #RegionRule('md.lang', r'^```html$', HTMLGrammar, r'^```'), + #RegionRule('md.lang', r'^```(?:javascript|js)$', JavascriptGrammar, r'^```'), + #RegionRule('md.lang', r'^```java$', JavaGrammar, r'^```'), + #RegionRule('md.lang', r'^```python$', PythonGrammar, r'^```'), + #RegionRule('md.lang', r'^```scala$', ScalaGrammar, r'^```'), + #RegionRule('md.lang', r'^```sql$', SqlGrammar, r'^```'), + #RegionRule('md.lang', r'^```xml$', XMLGrammar, r'^```'), + # + #RegionRule('md.code', r'^```.+$', LineGrammar, r'^```'), + # + #RegionRule('md.bold', r'\*\*', TwineGrammar, r'\*\*'), + #RegionRule('md.tt', r'``', TwineGrammar, r'``'), + # + #RegionRule('md.bold', r'\*', TwineGrammar, r'\*'), + #RegionRule('md.tt', r'`', WordGrammar, r'`'), + + #PatternRule('md.escaped', r'\\.'), + + PatternRule('tw.passage', r'::.+'), + + PatternMatchRule('', '(\[\[)([^\[\]|]*)(\|)([^\[\]|]*)(\]\])', + 'tw.passage', 'tw.link', 'tw.passage', 'tw.passage', 'tw.passage'), + PatternMatchRule('', r'(\[\[)([^\[\]|]*)(\]\])', + 'tw.passage', 'tw.link', 'tw.passage'), + + PatternRule('tw.keyword', '(?<=<<)[a-zA-Z0-9_]+'), + + #RegionRule('tw.cmd', r'<<', None, r'>>'), + RegionRule('tw.cmd', r'<<', CmdGrammar, r'>>'), + + #PatternMatchRule('', r'(<>)', + # 'tw.choice', 'tw.passage', 'tw.choice', 'tw.link', 'tw.choice'), + #PatternMatchRule('', r'(<>)', + # 'tw.choice', 'tw.link', 'tw.choice'), + + PatternRule('tw.var', r'\$[a-zA-Z0-9_]+'), + RegionRule('tw.html', r'', Grammar, r''), + + #PatternRule('tw.link', r'[[a-zA-Z\'"\-]+'), + PatternRule('tw.word', r'[a-zA-Z\'"\-]+'), + #RegionRule('tw.html', r'', HTMLGrammar, r''), + + PatternRule('spaces', '\s+'), + PatternRule('eol', r'\n'), + PatternRule('null', r'.'), +] + + +class TwWrapParagraph(WrapParagraph): + limit = 75 +#class TwInsertSpace(TextInsertSpace): +# limit = 75 +# wrapper = TwWrapParagraph + +# white is for delimiters, operators, numbers +default = ('default', 'default') + +# magenta is for keywords/builtins +lo_magenta = ('magenta202', 'default') +hi_magenta = ('magenta414', 'default') + +# red is for comments +lo_red = ('red300', 'default') +hi_red = ('red511', 'default') + +# orange is for macro definitions, headers and constants +hi_orange = ('yellow531', 'default') +lo_orange = ('yellow520', 'default') + +# yellow is for parts of macros +hi_yellow = ('yellow551', 'default') +lo_yellow = ('yellow330', 'default') + +# green is for strings and characters +lo_green = ('green030', 'default') +hi_green = ('green050', 'default') + +# cyan is for types +lo_cyan = ('cyan033', 'default') +hi_cyan = ('cyan155', 'default') + +# blue is definitions, functions and some macros +lo_blue = ('blue113', 'default') +hi_blue = ('blue225', 'default') + +class Twine(Fundamental): + name = 'Twine' + extensions = ['.tw'] + grammar = TwineGrammar + colors = { + 'tw.passage': hi_magenta, + 'tw.delim': hi_magenta, + 'tw.link': hi_cyan, + 'tw.var': hi_yellow, + #'tw.choice': hi_orange, + 'tw.cmd.start': hi_orange, + 'tw.keyword': hi_orange, + 'tw.op': hi_magenta, + 'tw.cmd.arg': hi_yellow, + 'tw.cmd.end': hi_orange, + + 'tw.html.start': hi_green, + 'tw.html.null': hi_green, + 'tw.html.end': hi_green, + + 'tw.word': default, + } + actions = [TwWrapParagraph] + config = { + 'tw.margin': 75, + } + _bindings = { + 'tw-wrap-paragraph': ('M-q',), + } + +install = Twine.install