which support

--HG--
branch : pmacs2
This commit is contained in:
moculus 2007-06-24 01:41:18 +00:00
parent 2a7fb0d165
commit 734b3ade3c
4 changed files with 65 additions and 75 deletions

View File

@ -10,8 +10,9 @@ from point2 import Point
#import mode, mode_c, mode_mini, mode_python, mode_nasm, mode_perl, mode_search
#import mode_replace, mode_xml, mode_console, mode_sh, mode_text, mode_which
#import mode_mutt, mode_sql, mode_javascript, mode_diff, mode_blame, mode_tt
import mode2, mode_mini, mode_python, mode_perl, mode_search, mode_replace
import mode_xml, mode_life
import mode2
import mode_mini, mode_search, mode_replace, mode_blame, mode_which
import mode_python, mode_perl, mode_xml, mode_life
def run(buffers, jump_to_line=None, init_mode=None):
# save terminal state so we can restore it when the program exits
@ -47,7 +48,7 @@ class Application(object):
self.y, self.x = self.stdscr.getmaxyx()
# initialize some basic stuff
self.margins_visible = True
self.margins_visible = False
#self.margins = [(80, 'blue'), (90, 'red')]
self.margins = [(80, 'blue'), ]
# each highlighted_range contains three things: [window, start_p, end_p]
@ -79,7 +80,7 @@ class Application(object):
# initialize our modes
self.modes = {
# 'blame': mode_blame.Blame,
'blame': mode_blame.Blame,
# 'c': mode_c.C,
# 'console': mode_console.Console,
# 'diff': mode_diff.Diff,
@ -92,7 +93,7 @@ class Application(object):
'search': mode_search.Search,
# 'sh': mode_sh.Sh,
# 'text': mode_text.Text,
# 'which': mode_which.Which,
'which': mode_which.Which,
'xml': mode_xml.XML,
'life': mode_life.Life,
# 'mutt': mode_mutt.Mutt,

View File

@ -162,8 +162,7 @@ class Fundamental(Handler):
def handle_token(self, t):
'''self.handle_token(token): handles input "token"'''
self.window.active_point = None
if DEBUG:
# debug mode is crash prone
try:
act = Handler.handle_token(self, t)
if act is None:
return
@ -171,22 +170,12 @@ class Fundamental(Handler):
self.window.application.clear_error()
act.execute(self.window)
self.window.application.last_action = act.name
else:
# regular mode is hard to get stack traces from
try:
act = Handler.handle_token(self, t)
if act is None:
return
else:
self.window.application.clear_error()
act.execute(self.window)
self.window.application.last_action = act.name
except Exception, e:
if DEBUG:
raise
else:
err = "%s in mode '%s'" % (e, self.name())
self.window.application.set_error(err)
except Exception, e:
if DEBUG:
raise
else:
err = "%s in mode '%s'" % (e, self.name())
self.window.application.set_error(err)
def region_added(self, p, newlines):
if self.tabber is not None:

View File

@ -1,16 +1,38 @@
import color, method, mode, lex, lex_blame, re
import color, mode2
class Blame(mode.Fundamental):
from point2 import Point
from lex2 import Grammar, ConstantRule, PatternRule, RegionRule, DualRegionRule
class MetadataGrammar(Grammar):
rules = [
PatternRule(
name=r'username',
pattern='[a-zA-Z0-9_]+',
),
]
class BlameGrammar(Grammar):
rules = [
RegionRule(
name=r'metadata',
start=r'^[0-9\.]+',
grammar=MetadataGrammar(),
end=r'[0-9]{4}-[0-9]{2}-[0-9]{2}',
),
PatternRule(
name=r'data',
pattern=r'.+$',
),
]
class Blame(mode2.Fundamental):
grammar = BlameGrammar()
def __init__(self, w):
mode.Fundamental.__init__(self, w)
self.grammar = lex_blame.BlameGrammar()
self.lexer = lex.Lexer(self.grammar)
mode2.Fundamental.__init__(self, w)
self.colors = {
'metadata': color.build('red', 'default', 'bold'),
#'data': color.build('green', 'default', 'bold'),
'metadata.start': color.build('blue', 'default', 'bold'),
'metadata.username': color.build('cyan', 'default', 'bold'),
'metadata.end': color.build('green', 'default', 'bold'),
}
def name(self):
return "Blame"

View File

@ -1,54 +1,32 @@
import sets, string
import color, method, mode2
from point2 import Point
import color, method, minibuffer, mode, point
import random
class Which(mode.Fundamental):
'''This is the default mode'''
class Which(mode2.Fundamental):
def __init__(self, w):
mode.Fundamental.__init__(self, w)
mode2.Fundamental.__init__(self, w)
old_mode = w.buffer.method.old_window.mode
self.actions = dict(old_mode.actions)
self.bindings = dict(old_mode.bindings)
for name in self.actions.keys():
old_method = self.actions[name]
new_method = HelpWrapper(old_method)
self.actions[name] = new_method
def name(self):
return "Which"
def handle_token(self, t):
'''self.handle_token(token): handles input "token"'''
self.window.active_point = None
try:
act = mode.Handler.handle_token(self, t)
act = mode2.Handler.handle_token(self, t)
if act is None:
return
else:
self.window.application.clear_error()
act.execute(self.window)
self.window.application.last_action = act.name
s = '%r is %r: %s' % (self.last_sequence, act.name, act.help)
self.window.application.set_error(s)
self._end()
except Exception, e:
self.window.application.set_error('%r is not bound to anything' % self.last_sequence)
_end(self.window)
class HelpWrapper(method.Method):
def __init__(self, m):
self.name = "help-for-%s" % m.name
self.args = []
self.help = 'provide help for the %r command' % m.name
self._m_name = m.name
self._m_help = m.help
def _execute(self, window, **vargs):
seq = window.mode.last_sequence
s = '%r is %r: %s' % (seq, self._m_name, self._m_help)
window.application.set_error(s)
_end(window)
def _end(window):
window.application.close_mini_buffer()
window.buffer.method.old_cursor = None
window.buffer.method.old_window = None
if mode2.DEBUG:
raise
else:
err = "%s in mode '%s'" % (e, self.name())
self.window.application.set_error(err)
self._end()
def name(self):
return "Which"
def _end(self):
self.window.application.close_mini_buffer()
self.window.buffer.method.old_cursor = None
self.window.buffer.method.old_window = None