2009-03-30 23:15:27 -04:00
|
|
|
import mode, method, ispell
|
|
|
|
from mode import Fundamental
|
2007-10-21 20:52:48 -04:00
|
|
|
from lex import Token, Rule, PatternRule, RegionRule, Grammar
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
class WordRule(PatternRule):
|
|
|
|
def __init__(self):
|
|
|
|
PatternRule.__init__(self, 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 lex(self, lexer, parent, m):
|
|
|
|
if m:
|
|
|
|
s = m.group(0)
|
|
|
|
if self._spelled_ok(s):
|
2010-03-01 16:12:27 -05:00
|
|
|
token = Token('text.word', self, lexer.y, lexer.x, s, None, parent, {})
|
2007-07-21 11:40:53 -04:00
|
|
|
else:
|
2020-08-31 20:58:27 -04:00
|
|
|
token = Token('text.misspelled', self, lexer.y, lexer.x, s, None, parent, {})
|
2007-07-21 11:40:53 -04:00
|
|
|
token.color = lexer.get_color(token)
|
|
|
|
lexer.x += len(s)
|
|
|
|
yield token
|
2020-08-31 20:58:27 -04:00
|
|
|
return
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
class ContinuedRule(RegionRule):
|
|
|
|
def __init__(self):
|
|
|
|
RegionRule.__init__(self, r'cont', r'[a-zA-Z0-9_]+- *$', Grammar, r'^ *(?:[^ ]+|$)')
|
|
|
|
|
|
|
|
class TextGrammar(Grammar):
|
|
|
|
rules = [
|
2011-03-07 01:14:23 -05:00
|
|
|
PatternRule(r'text.link', r'[a-z]+://[^ ]*[^- .,;:]'),
|
2007-07-21 11:40:53 -04:00
|
|
|
ContinuedRule(),
|
|
|
|
WordRule(),
|
2010-03-01 16:12:27 -05:00
|
|
|
PatternRule(r'text.punct', r'[^a-zA-Z0-9_]'),
|
|
|
|
PatternRule(r'text.stuff', r'[a-zA-Z0-9_]+'),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
2008-03-17 02:22:09 -04:00
|
|
|
class TextWrapParagraph(method.WrapParagraph):
|
2008-03-05 20:35:06 -05:00
|
|
|
pass
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
class TextInsertSpace(method.Method):
|
2008-03-05 20:35:06 -05:00
|
|
|
wrapper = TextWrapParagraph
|
2007-07-21 11:40:53 -04:00
|
|
|
def execute(self, w, **vargs):
|
|
|
|
cursor = w.logical_cursor()
|
|
|
|
i = cursor.y
|
2008-03-05 20:35:06 -05:00
|
|
|
if len(w.buffer.lines[i]) > self.wrapper.limit:
|
2007-07-21 11:40:53 -04:00
|
|
|
self.wrapper().execute(w)
|
2008-03-05 20:35:06 -05:00
|
|
|
w.insert_string_at_cursor(' ')
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
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
|
2009-03-17 15:24:10 -04:00
|
|
|
for token in w.buffer.highlights[w.mode.name].tokens[cursor.y]:
|
2007-07-21 11:40:53 -04:00
|
|
|
if (token.x <= cursor.x and
|
|
|
|
token.end_x() > cursor.x and
|
2011-03-07 01:14:23 -05:00
|
|
|
token.name == 'text.misspelled'):
|
2007-07-21 11:40:53 -04:00
|
|
|
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()
|
2007-10-19 02:41:33 -04:00
|
|
|
|
2009-03-30 23:15:27 -04:00
|
|
|
class Text(Fundamental):
|
2009-03-17 15:24:10 -04:00
|
|
|
name = 'Text'
|
|
|
|
extensions = ['.txt']
|
|
|
|
grammar = TextGrammar
|
|
|
|
actions = [LearnWord, TextInsertSpace, TextWrapParagraph]
|
|
|
|
config = {
|
2008-11-10 14:58:12 -05:00
|
|
|
'text.margin': 78,
|
|
|
|
}
|
2009-03-30 23:15:27 -04:00
|
|
|
_bindings = {
|
|
|
|
'learn-word': ('C-c l',),
|
|
|
|
'text-insert-space': ('SPACE',),
|
|
|
|
'text-wrap-paragraph': ('M-q',),
|
|
|
|
}
|
2010-03-01 16:12:27 -05:00
|
|
|
colors = {
|
2011-03-07 01:14:23 -05:00
|
|
|
'text.link': ('cyan', 'default'),
|
2010-03-01 16:12:27 -05:00
|
|
|
'text.punct': ('default', 'default'),
|
|
|
|
'text.stuff': ('default', 'default'),
|
|
|
|
'text.word': ('default', 'default'),
|
2010-03-01 17:26:59 -05:00
|
|
|
'text.misspelled': ('red', 'default', 'bold'),
|
2010-03-01 16:12:27 -05:00
|
|
|
}
|
2008-04-18 23:32:08 -04:00
|
|
|
|
2007-10-19 02:41:33 -04:00
|
|
|
install = Text.install
|