transient-mark mode == higlight_mark

--HG--
branch : pmacs2
This commit is contained in:
moculus 2009-03-19 04:23:09 +00:00
parent f4382381cb
commit 4ed41352f5
4 changed files with 129 additions and 43 deletions

View File

@ -19,6 +19,7 @@ class Application(object):
# initialize some basic stuff # initialize some basic stuff
self.config = {} self.config = {}
self.highlighted_ranges = [] self.highlighted_ranges = []
self.highlight_mark = False
self.mini_active = False self.mini_active = False
self.mini_buffer = None self.mini_buffer = None
self.mini_prompt = "" self.mini_prompt = ""
@ -100,6 +101,8 @@ class Application(object):
exec("import %s" % name) exec("import %s" % name)
mod = eval(name) mod = eval(name)
for mname in dir(mod): for mname in dir(mod):
if mname.startswith('_'):
continue
cls = eval("%s.%s" % (name, mname)) cls = eval("%s.%s" % (name, mname))
if hasattr(cls, '_is_method') and cls._is_method: if hasattr(cls, '_is_method') and cls._is_method:
self.methods[cls._name()] = cls() self.methods[cls._name()] = cls()
@ -580,13 +583,21 @@ class Application(object):
if os.path.exists(path): if os.path.exists(path):
try: try:
f = open(path, 'r') f = open(path, 'r')
#execfile(path)
exec(f) exec(f)
f.close() f.close()
except Exception, e: except Exception, e:
s = traceback.format_exc() s = traceback.format_exc()
self.rcerror = 'There was an error during startup:\n\n%s' % s self.rcerror = 'There was an error during startup:\n\n%s' % s
# after actions get handled, do some stuff
def post_action_hook(self, act):
self.last_action = act.name
if self.highlight_mark:
if act.name == 'set-mark' or act.metadata.get('is_move'):
pass
else:
self.highlight_mark = False
# the mighty run-loop! # the mighty run-loop!
def run(self): def run(self):
self.done = False self.done = False
@ -619,7 +630,7 @@ class Application(object):
self.draw() self.draw()
self.need_draw = False self.need_draw = False
# gjeiwgjeiw # clean up, clean up
for b in self.bufferlist.buffers: for b in self.bufferlist.buffers:
b.close() b.close()
@ -780,6 +791,71 @@ class Application(object):
x += slot.width x += slot.width
count += 1 count += 1
def highlight_simple_range(self, slot, y1, x1, x2, fg, bg):
count = slot.window.mode.header
tx1, tx2 = slot.window.mode.lmargin, slot.width - 1
x, y = slot.window.first.xy()
while count < slot.height:
if y1 == y and x1 < slot.width + x:
sy = slot.y_offset + count
sx1 = x1 - x + slot.window.mode.lmargin
sx2 = x2 - x + slot.window.mode.lmargin
if x1 <= x:
if x2 < slot.width + x:
self.highlight_chars(sy, tx1, sx2, fg, bg)
break
else:
self.highlight_chars(sy, tx1, tx2, fg, bg)
else:
if x2 < slot.width + x:
self.highlight_chars(sy, sx1, sx2, fg, bg)
break
else:
self.highlight_chars(sy, sx1, tx2, fg, bg)
if x + slot.width > len(slot.window.buffer.lines[y]):
x = 0
y += 1
else:
x += slot.width - 1
count += 1
def highlight_complex_range(self, slot, p1, p2, fg, bg):
count = slot.window.mode.header
tx1, tx2 = slot.window.mode.lmargin, slot.width - 1
x, y = slot.window.first.xy()
while count < slot.height:
if p1.y <= y:
sy = slot.y_offset + count
if p1.y == y:
if p1.x > slot.width + x:
pass
elif p1.x > x:
self.highlight_chars(sy, p1.x - x + tx1, tx2, fg, bg)
else:
self.highlight_chars(sy, tx1, tx2, fg, bg)
elif p2.y > y:
self.highlight_chars(sy, tx1, tx2, fg, bg)
elif p2.y == y and p2.x >= x and p2.x < slot.width + x:
if slot.width > p2.x - x:
self.highlight_chars(sy, tx1, p2.x - x + tx1, fg, bg)
break
else:
self.highlight_chars(sy, tx1, tx2, fg, bg)
if x + slot.width > len(slot.window.buffer.lines[y]):
x = 0
y += 1
else:
x += slot.width - 1
count += 1
def highlight_range(self, slot, p1, p2, fg, bg):
if p1.y == p2.y:
return self.highlight_simple_range(slot, p1.y, p1.x, p2.x, fg, bg)
else:
return self.highlight_complex_range(slot, p1, p2, fg, bg)
def draw_slot(self, i): def draw_slot(self, i):
assert self.active_slot < len(self.bufferlist.slots), \ assert self.active_slot < len(self.bufferlist.slots), \
"strange: %d < %d" % (self.active_slot, len(self.bufferlist.slots)) "strange: %d < %d" % (self.active_slot, len(self.bufferlist.slots))
@ -807,24 +883,27 @@ class Application(object):
for hr in self.highlighted_ranges: for hr in self.highlighted_ranges:
(high_w, p1, p2, fg, bg) = hr (high_w, p1, p2, fg, bg) = hr
if w is high_w and p2 >= w.first and p1 <= w.last: if w is high_w and p2 >= w.first and p1 <= w.last:
count = w.mode.header #raise Exception(repr((slot, p1, p2, fg, bg)))
x, y = w.first.xy() self.highlight_range(slot, p1, p2, fg, bg)
px = p1.x
while count < slot.height: #self.highlight_range(slot, Point(13, 8), Point(29, 8), 'black', 'magenta')
if p1.y == y and px >= x and px - x < slot.width: if not self.highlighted_ranges and self.highlight_mark:
sy, sx = slot.y_offset + count, px - x + w.mode.lmargin fg, bg = 'black', 'cyan'
if slot.width > p2.x - x: cursor = w.logical_cursor()
self.highlight_chars(sy, sx, p2.x-x, fg, bg) mark = w.mark
break if mark is None:
return
if mark <= cursor:
p1, p2 = mark, cursor
else: else:
self.highlight_chars(sy, sx, slot.width - 1, fg, bg) p1, p2 = cursor, mark
px += slot.width - px + x - 1
if x + slot.width > len(w.buffer.lines[y]): if p1 < w.first: p1 = w.first
x = 0 if p2 > w.last: p2 = w.last
y += 1 #raise Exception("blargh")
else: self.highlight_range(slot, p1, p2, fg, bg)
x += slot.width - 1 #raise Exception("p1=%r p2=%r fg=%r bg=%r" % (p1, p2, fg, bg))
count += 1
if w.margins_visible: if w.margins_visible:
shade = util.get_margin_color(w, 'blue') shade = util.get_margin_color(w, 'blue')
@ -956,10 +1035,7 @@ def run_app(stdscr, buffers, **kwargs):
a = Application(stdscr, buffers, **kwargs) a = Application(stdscr, buffers, **kwargs)
metax = a.methods['meta-x'] metax = a.methods['meta-x']
for cmd in kwargs.get('init_cmds', []): for cmd in kwargs.get('init_cmds', []):
#act = a.methods[name]
#act.execute(a.active_window())
metax.execute(a.active_window(), method=cmd) metax.execute(a.active_window(), method=cmd)
#a.last_action = act.name
a.run() a.run()
return 0 return 0

View File

@ -64,6 +64,7 @@ class Method(object):
_is_method = True _is_method = True
args = [] args = []
help = "" help = ""
metadata = {}
def __init__(self): def __init__(self):
self.name = self._name() self.name = self._name()
if self.__doc__: if self.__doc__:
@ -497,6 +498,10 @@ class CenterView(Method):
class SetMark(Method): class SetMark(Method):
'''Set the mark to the current cursor location''' '''Set the mark to the current cursor location'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
if w.application.last_action == self.name:
w.application.highlight_mark = True
w.set_error("Highlighting enabled: %r" % w.application.highlight_mark)
else:
w.set_mark() w.set_mark()
class SwitchMark(Method): class SwitchMark(Method):
'''Switch the mark and the cursor locations''' '''Switch the mark and the cursor locations'''

View File

@ -6,63 +6,67 @@ from point import Point
from method import Method, Argument from method import Method, Argument
class StartOfLine(Method): class _Move(Method):
'''Abstract base move action'''
metadata = {'is_move': True}
class StartOfLine(_Move):
'''Move the cursor to the start of the current line''' '''Move the cursor to the start of the current line'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.start_of_line() w.start_of_line()
class EndOfLine(Method): class EndOfLine(_Move):
'''Move the cursor to the end of the current line''' '''Move the cursor to the end of the current line'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.end_of_line() w.end_of_line()
class Forward(Method): class Forward(_Move):
'''Move the cursor right one character''' '''Move the cursor right one character'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.forward() w.forward()
class Backward(Method): class Backward(_Move):
'''Move the cursor left one character''' '''Move the cursor left one character'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.backward() w.backward()
class NextLine(Method): class NextLine(_Move):
'''Move the cursor down one line''' '''Move the cursor down one line'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.next_line() w.next_line()
class PreviousLine(Method): class PreviousLine(_Move):
'''Move the cursor up one line''' '''Move the cursor up one line'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.previous_line() w.previous_line()
class PageUp(Method): class PageUp(_Move):
'''Move the cursor up one page''' '''Move the cursor up one page'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.page_up() w.page_up()
class PageDown(Method): class PageDown(_Move):
'''Move the cursor down one page''' '''Move the cursor down one page'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.page_down() w.page_down()
class GotoBeginning(Method): class GotoBeginning(_Move):
'''Move the cursor to the beginning of the buffer''' '''Move the cursor to the beginning of the buffer'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.set_mark() w.set_mark()
w.goto_beginning() w.goto_beginning()
class GotoEnd(Method): class GotoEnd(_Move):
'''Move the cursor to the end of the buffer''' '''Move the cursor to the end of the buffer'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.set_mark() w.set_mark()
w.goto_end() w.goto_end()
class RightWord(Method): class RightWord(_Move):
'''Move the cursor to the start of the word to the right''' '''Move the cursor to the start of the word to the right'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.right_word() w.right_word()
class LeftWord(Method): class LeftWord(_Move):
'''Move the cursor to the start of the word to the left''' '''Move the cursor to the start of the word to the left'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.left_word() w.left_word()
class NextSection(Method): class NextSection(_Move):
'''Move the cursor to the next section''' '''Move the cursor to the next section'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
cursor = w.logical_cursor() cursor = w.logical_cursor()
@ -74,7 +78,7 @@ class NextSection(Method):
break break
seen_null_line = regex.whitespace.match(w.buffer.lines[i]) seen_null_line = regex.whitespace.match(w.buffer.lines[i])
i += 1 i += 1
class PreviousSection(Method): class PreviousSection(_Move):
'''Move the cursor to the previous section''' '''Move the cursor to the previous section'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
cursor = w.logical_cursor() cursor = w.logical_cursor()
@ -87,19 +91,19 @@ class PreviousSection(Method):
seen_null_line = regex.whitespace.match(w.buffer.lines[i]) seen_null_line = regex.whitespace.match(w.buffer.lines[i])
i -= 1 i -= 1
class GotoChar(Method): class GotoChar(_Move):
'''Jump to the specified character''' '''Jump to the specified character'''
args = [Argument("charno", type=type(0), prompt="Goto char: ")] args = [Argument("charno", type=type(0), prompt="Goto char: ")]
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.goto_char(vargs["charno"]) w.goto_char(vargs["charno"])
class ForwardChars(Method): class ForwardChars(_Move):
'''Move forward the specified number of characters''' '''Move forward the specified number of characters'''
args = [Argument("charno", type=type(0), prompt="Forward chars: ")] args = [Argument("charno", type=type(0), prompt="Forward chars: ")]
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.forward_chars(vargs["charno"]) w.forward_chars(vargs["charno"])
class GotoLine(Method): class GotoLine(_Move):
'''Jump to the specified line number''' '''Jump to the specified line number'''
args = [Argument("lineno", type=type(0), prompt="Goto line: ")] args = [Argument("lineno", type=type(0), prompt="Goto line: ")]
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
@ -112,7 +116,7 @@ class GotoLine(Method):
n = 1 n = 1
w.goto_line(n) w.goto_line(n)
class ForwardLines(Method): class ForwardLines(_Move):
'''Move forward the specified number of lines''' '''Move forward the specified number of lines'''
args = [Argument("lineno", type=type(0), prompt="Forward lines: ")] args = [Argument("lineno", type=type(0), prompt="Forward lines: ")]
def _execute(self, w, **vargs): def _execute(self, w, **vargs):

View File

@ -449,7 +449,8 @@ class Fundamental(Handler):
return return
else: else:
act.execute(self.window) act.execute(self.window)
self.window.application.last_action = act.name self.window.application.post_action_hook(act)
#self.window.application.last_action = act.name
except ActionError, e: except ActionError, e:
#XYZ: HACK--should fix #XYZ: HACK--should fix
if t in ('C-]', 'C-g'): if t in ('C-]', 'C-g'):