pmacs3/mode_text.py

65 lines
2.1 KiB
Python
Raw Normal View History

2007-03-06 10:05:38 -05:00
import sets, sys
import color, mode, lex, lex_text, method, ispell
class Text(mode.Fundamental):
def __init__(self, w):
mode.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.grammar = lex_text.TextGrammar()
self.lexer = lex.Lexer(self.grammar)
self.default_color = color.build('default', 'default')
self.colors = {
'misspelled word': color.build('red', 'default', 'bold'),
'misspelled continued word': color.build('red', 'default', 'bold'),
}
#self.highlighter.lex_buffer()
#self.get_regions()
def name(self):
return "Text"
class TextInsertSpace(method.Method):
limit = 80
#wrapper = method.WrapLine
wrapper = method.WrapParagraph
def execute(self, window, **vargs):
window.insert_string(' ')
cursor = window.logical_cursor()
i = cursor.y
if len(window.buffer.lines[i]) > self.limit:
self.wrapper().execute(window)
class LearnWord(method.Method):
def execute(self, window, **vargs):
if window.mode.highlighter.tokens is None:
window.mode.highlighter.lex_buffer()
cursor = window.logical_cursor()
cursor_offset = window.get_cursor_offset()
tok = None
for t in window.mode.highlighter.tokens:
if t.start <= cursor_offset and cursor_offset < t.end:
tok = t
break
if tok:
word = tok.string
if tok.name.startswith('all-caps'):
s = "%r is all-caps" % (word)
elif tok.name.startswith('misspelled'):
ispell.get_speller().learn(word)
window.mode.highlighter.invalidate_tokens()
s = "Added %r to personal dictionary" % (word)
else:
s = "%r is already in the dictionary" % (word)
else:
s = "No word to learn found"
window.application.set_error(s)