diff --git a/mode_search.py b/mode_search.py index 211de6a..dcba11a 100644 --- a/mode_search.py +++ b/mode_search.py @@ -8,23 +8,17 @@ class Search(mode2.Fundamental): def __init__(self, w): mode2.Fundamental.__init__(self, w) + # clear out all the defaults that we don't want/need self.actions = {} self.bindings = {} - default_actions = ( - (SearchNext(), ('C-s',)), - (SearchPrevious(), ('C-r',)), - (EndSearch(), ('RETURN', 'C-n', 'C-p', 'C-a', 'C-e', 'C-f', 'C-b')), - (CancelSearch(), ('C-]',)), - (DeleteLeft(), ('DELETE', 'BACKSPACE',)), - (DeleteLeftWord(), ('M-DELETE', 'M-BACKSPACE',)), - ) - - # add the search actions - for pair in default_actions: - (action, sequences) = pair - assert type(sequences) == type(()), repr(pair) - self.add_action_and_bindings(action, sequences) + # add some useful bindings + self.add_action_and_bindings(SearchNext(), ('C-s',)) + self.add_action_and_bindings(SearchPrevious(), ('C-r',)) + self.add_action_and_bindings(EndSearch(), ('RETURN', 'C-n', 'C-p', 'C-a', 'C-e', 'C-f', 'C-b')) + self.add_action_and_bindings(CancelSearch(), ('C-]',)) + self.add_action_and_bindings(SearchDeleteLeft(), ('DELETE', 'BACKSPACE',)) + self.add_action_and_bindings(SearchDeleteLeftWord(), ('M-DELETE', 'M-BACKSPACE',)) # create all the insert actions for the character ranges we like for collection in (string.letters, string.digits, string.punctuation): @@ -38,10 +32,8 @@ class Search(mode2.Fundamental): class SearchNext(method.Method): def execute(self, w, **vargs): w.buffer.method.direction = 'next' - s = w.buffer.make_string() - if not s: - s = w.application.last_search - w.buffer.set_data(s) + if not w.buffer.make_string(): + w.buffer.set_data(w.application.last_search) else: old_w = w.buffer.method.old_window _find_next(old_w, w, move=True) @@ -49,8 +41,7 @@ class SearchNext(method.Method): class SearchPrevious(method.Method): def execute(self, w, **vargs): w.buffer.method.direction = 'previous' - s = w.buffer.make_string() - if not s: + if not w.buffer.make_string(): return else: old_w = w.buffer.method.old_window @@ -58,34 +49,28 @@ class SearchPrevious(method.Method): class EndSearch(method.Method): def execute(self, w, **vargs): - old_w = w.buffer.method.old_window - old_cursor = w.buffer.method.old_cursor _end(w) - old_w.set_mark_point(old_cursor) + w.buffer.method.old_window.set_mark_point(w.buffer.method.old_cursor) w.set_error("Mark set to search start") class CancelSearch(method.Method): def execute(self, w, **vargs): - old_w = w.buffer.method.old_window - old_cursor = w.buffer.method.old_cursor - old_w.goto(old_cursor) + w.buffer.method.old_window.goto(w.buffer.method.old_cursor) _end(w) w.set_error("Search cancelled") -class DeleteLeft(method.Method): +class SearchDeleteLeft(method.Method): def execute(self, w, **vargs): w.left_delete() old_cursor = w.buffer.method.old_cursor old_w = w.buffer.method.old_window old_w.goto(old_cursor) - _end(w) - return if w.buffer.method.direction == 'next': _find_next(old_w, w, move=False) else: _find_previous(old_w, w, move=False) -class DeleteLeftWord(method.Method): +class SearchDeleteLeftWord(method.Method): def execute(self, w, **vargs): w.kill_left_word() old_cursor = w.buffer.method.old_cursor @@ -115,11 +100,9 @@ class InsertSearchString(method.Method): _find_previous(old_w, w, move=False) def _end(w): - s = w.buffer.make_string() - w.application.last_search = s w.application.close_mini_buffer() - #w.application.highlighted_range = [] w.application.clear_highlighted_ranges() + w.application.last_search = w.buffer.make_string() w.buffer.method.old_cursor = None w.buffer.method.old_window = None @@ -135,12 +118,11 @@ def _find_previous(old_w, new_w, move=False): while y >= 0: if s in l: # success - x = l.index(s) - old_w.goto(Point(x, y)) + x = l.rindex(s) + (p1, p2) = (Point(x,y), Point(x + len(s), y)) + old_w.goto(p1) old_w.application.clear_highlighted_ranges() - new_w.application.add_highlighted_range(old_w, - Point(x,y), - Point(x + len(s), y)) + new_w.application.add_highlighted_range(old_w, p1, p2) break elif y >= len(old_b.lines) - 1: # failure @@ -164,11 +146,10 @@ def _find_next(old_w, new_w, move=False): if s in l: # success x = l.index(s) + x - old_w.goto(Point(x, y)) + (p1, p2) = (Point(x,y), Point(x + len(s), y)) + old_w.goto(p1) old_w.application.clear_highlighted_ranges() - new_w.application.add_highlighted_range(old_w, - Point(x,y), - Point(x + len(s), y)) + new_w.application.add_highlighted_range(old_w, p1, p2) break elif y >= len(old_b.lines) - 1: # failure