pmacs3/mode/search.py

148 lines
5.3 KiB
Python
Raw Permalink Normal View History

2008-11-08 10:30:04 -05:00
import re, string
import method, minibuffer, searchutil
from method import Method
from mode import Fundamental
2007-07-21 11:40:53 -04:00
2008-04-18 23:32:08 -04:00
selected_color = 'magenta'
2007-07-21 11:40:53 -04:00
unselected_color = 'yellow'
def _make_regex(w, s):
try:
if w.buffer.method.is_literal:
2007-07-21 18:58:17 -04:00
s = searchutil.escape_literal(s)
if s.islower():
return re.compile(s, re.IGNORECASE)
else:
return re.compile(s)
2007-07-21 11:40:53 -04:00
else:
return re.compile(s)
except:
raise searchutil.IllegalPatternError("failed to compile: %r" % s)
2007-07-21 11:40:53 -04:00
class SearchNext(Method):
'In a search command, move to the next occurance'
2007-07-21 11:40:53 -04:00
def execute(self, w, **vargs):
w.buffer.method.direction = 'next'
s = w.buffer.make_string()
if s:
try:
r = _make_regex(w, s)
2007-07-21 18:58:17 -04:00
searchutil.find_next(r, w.buffer.method.old_window, move=True)
except searchutil.IllegalPatternError:
w.application.clear_highlighted_ranges('search')
2007-07-21 11:40:53 -04:00
else:
action = InsertSearchString(w.application.last_search)
action.execute(w)
2007-07-21 11:40:53 -04:00
class SearchPrevious(Method):
'In a search command, move to the previous occurance'
2007-07-21 11:40:53 -04:00
def execute(self, w, **vargs):
w.buffer.method.direction = 'previous'
if not w.buffer.make_string():
return
else:
s = w.buffer.make_string()
w2 = w.buffer.method.old_window
try:
r = _make_regex(w, s)
2007-07-21 18:58:17 -04:00
searchutil.find_previous(r, w2, move=True)
except searchutil.IllegalPatternError:
w.application.clear_highlighted_ranges('search')
2007-07-21 11:40:53 -04:00
class EndSearch(Method):
'End the current search command, leaving the cursor in place'
2007-07-21 11:40:53 -04:00
def execute(self, w, **vargs):
old_w = w.buffer.method.old_window
old_c = w.buffer.method.old_cursor
_end(w)
old_w.set_mark_point(old_c)
w.set_error("Mark set to search start")
class CancelSearch(Method):
'End the current search command, restoring the cursor to the search start'
2007-07-21 11:40:53 -04:00
def execute(self, w, **vargs):
w.buffer.method.old_window.goto(w.buffer.method.old_cursor)
_end(w)
w.set_error("Search cancelled")
class SearchDeleteLeft(Method):
2007-07-21 11:40:53 -04:00
def execute(self, w, **vargs):
w.left_delete()
_post_delete(w)
class SearchDeleteLeftWord(Method):
2007-07-21 11:40:53 -04:00
def execute(self, w, **vargs):
w.delete_left_word()
2007-07-21 11:40:53 -04:00
_post_delete(w)
def _post_delete(w):
old_cursor = w.buffer.method.old_cursor
old_w = w.buffer.method.old_window
old_w.goto(old_cursor)
if not w.buffer.make_string():
w.application.clear_highlighted_ranges('search')
2007-07-21 11:40:53 -04:00
return
s = w.buffer.make_string()
w2 = w.buffer.method.old_window
try:
r = _make_regex(w, s)
if w.buffer.method.direction == 'next':
2007-07-21 18:58:17 -04:00
searchutil.find_next(r, w2, move=False)
2007-07-21 11:40:53 -04:00
else:
2007-07-21 18:58:17 -04:00
searchutil.find_previous(r, w2, move=False)
except searchutil.IllegalPatternError:
w.application.clear_highlighted_ranges('search')
2007-07-21 11:40:53 -04:00
class InsertSearchString(Method):
2007-07-21 11:40:53 -04:00
def __init__(self, s):
self.name = 'insert-search-string-%s' % (s)
self.string = s
self.args = []
self.help = ''
2007-07-21 11:40:53 -04:00
def execute(self, w, **vargs):
w.insert_string_at_cursor(self.string)
s = w.buffer.make_string()
if not s:
w.application.clear_highlighted_ranges('search')
2007-07-21 11:40:53 -04:00
return
else:
try:
r = _make_regex(w, s)
w2 = w.buffer.method.old_window
if w.buffer.method.direction == 'next':
2007-07-21 18:58:17 -04:00
searchutil.find_next(r, w2, move=False)
2007-07-21 11:40:53 -04:00
else:
2007-07-21 18:58:17 -04:00
searchutil.find_previous(r, w2, move=False)
except searchutil.IllegalPatternError:
w.application.clear_highlighted_ranges('search')
2007-07-21 11:40:53 -04:00
def _end(w):
w.application.close_mini_buffer()
w.application.clear_highlighted_ranges('search')
2007-07-21 11:40:53 -04:00
w.application.last_search = w.buffer.make_string()
2007-10-19 02:41:33 -04:00
class Search(Fundamental):
name = 'Search'
2008-04-18 23:32:08 -04:00
actions = [SearchNext, SearchPrevious, EndSearch, CancelSearch,
SearchDeleteLeft, SearchDeleteLeftWord]
def __init__(self, w):
Fundamental.__init__(self, w)
2008-04-18 23:32:08 -04:00
# clear out all the defaults that we don't want/need, and add ours
self.actions = {}
self.bindings = {}
self.add_bindings('search-next', ('C-s',))
self.add_bindings('search-previous', ('C-r',))
2009-01-15 19:07:41 -05:00
self.add_bindings('end-search', ('RETURN', 'C-n', 'D_ARROW', 'C-p', 'U_ARROW',
'C-a', 'HOME', 'C-e', 'END', 'C-f',
'R_ARROW', 'C-b', 'L_ARROW'))
2008-06-10 14:29:19 -04:00
self.add_bindings('cancel-search', ('C-]', 'C-g',))
2008-04-18 23:32:08 -04:00
self.add_bindings('search-delete-left', ('DELETE', 'BACKSPACE',))
self.add_bindings('search-delete-left-word', ('M-DELETE', 'M-BACKSPACE',))
# create all the insert actions for the character ranges we like
for collection in (string.ascii_letters, string.digits, string.punctuation):
2008-04-18 23:32:08 -04:00
for c in collection:
self.add_action_and_bindings(InsertSearchString(c), (c,))
self.add_action_and_bindings(InsertSearchString(' '), ('SPACE',))
2007-10-19 02:41:33 -04:00
install = Search.install