pmacs3/mode_text.py

88 lines
3.1 KiB
Python

import color, mode2, method, ispell
from lex2 import Token, Rule, PatternRule, RegionRule, Grammar
class WordRule(PatternRule):
def __init__(self):
PatternRule.__init__(self, name=r'word',
pattern=r"[a-zA-Z][a-zA-Z-\']*[a-zA-Z](?=$|[^a-zA-Z0-9-_])")
def _spelled_ok(self, word):
if ispell.can_spell():
speller = ispell.get_speller()
return speller.check(word, caps=False, title=False)
else:
return True
def _match(self, lexer, parent, m):
s = m.group(0)
if self._spelled_ok(s):
token = Token('word', self, lexer.y, lexer.x, s, parent, {})
else:
token = Token('misspelled', self, lexer.y, lexer.x, s, parent, {})
lexer.add_token(token)
lexer.x += len(s)
class ContinuedRule(RegionRule):
def __init__(self):
RegionRule.__init__(self, name=r'cont', start=r'[a-zA-Z0-9_]+- *$',
grammar=Grammar(), end=r'^ *(?:[^ ]+|$)')
class TextGrammar(Grammar):
rules = [
ContinuedRule(),
WordRule(),
PatternRule(name=r'punct', pattern=r'[^a-zA-Z0-9_]'),
PatternRule(name=r'stuff', pattern=r'[a-zA-Z0-9_]+'),
]
class Text(mode2.Fundamental):
grammar = TextGrammar()
def __init__(self, w):
mode2.Fundamental.__init__(self, w)
self.add_action_and_bindings(LearnWord(), ('C-c l',))
self.add_action_and_bindings(TextInsertSpace(), ('SPACE',))
self.add_action_and_bindings(method.WrapParagraph(), ('M-q',))
self.colors = {
'misspelled': color.build('red', 'default'),
'cont.start': color.build('default', 'default'),
'cont.end': color.build('default', 'default'),
'word': color.build('default', 'default'),
'punct': color.build('default', 'default'),
'stuff': color.build('default', 'default'),
}
def name(self):
return "Text"
class TextInsertSpace(method.Method):
limit = 80
wrapper = method.WrapParagraph
def execute(self, w, **vargs):
w.insert_string_at_cursor(' ')
cursor = w.logical_cursor()
i = cursor.y
if len(w.buffer.lines[i]) > self.limit:
self.wrapper().execute(w)
class LearnWord(method.Method):
def execute(self, w, **vargs):
if not ispell.can_spell():
w.application.set_error('Spelling support is unavailable')
return
cursor = w.logical_cursor()
word = None
for token in w.buffer.highlights[w.mode.name()].tokens[cursor.y]:
if (token.x <= cursor.x and
token.end_x() > cursor.x and
token.name == 'misspelled'):
word = token.string
if word is None:
w.application.set_error('No misspelled word was found')
return
speller = ispell.get_speller()
speller.learn(word)
w.application.set_error("Added %r to personal dictionary" % (word))
# cheap way to relex just this word; there should really be a function
w.insert_string_at_cursor(' ')
w.left_delete()