Changeset 391 for python/trunk/Lib/idlelib/SearchEngine.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/idlelib/SearchEngine.py
r2 r391 1 '''Define SearchEngine for search dialogs.''' 1 2 import re 2 from Tkinter import *3 from Tkinter import StringVar, BooleanVar, TclError 3 4 import tkMessageBox 4 5 5 6 def get(root): 7 '''Return the singleton SearchEngine instance for the process. 8 9 The single SearchEngine saves settings between dialog instances. 10 If there is not a SearchEngine already, make one. 11 ''' 6 12 if not hasattr(root, "_searchengine"): 7 13 root._searchengine = SearchEngine(root) 8 # XXX This will never garbage-collect -- who cares14 # This creates a cycle that persists until root is deleted. 9 15 return root._searchengine 10 16 11 17 class SearchEngine: 18 """Handles searching a text widget for Find, Replace, and Grep.""" 12 19 13 20 def __init__(self, root): 14 self.root = root 15 # State shared by search, replace, and grep; 16 # the search dialogs bind these to UI elements. 17 self.patvar = StringVar(root) # search pattern 18 self.revar = BooleanVar(root) # regular expression? 19 self.casevar = BooleanVar(root) # match case? 20 self.wordvar = BooleanVar(root) # match whole word? 21 self.wrapvar = BooleanVar(root) # wrap around buffer? 22 self.wrapvar.set(1) # (on by default) 23 self.backvar = BooleanVar(root) # search backwards? 21 '''Initialize Variables that save search state. 22 23 The dialogs bind these to the UI elements present in the dialogs. 24 ''' 25 self.root = root # need for report_error() 26 self.patvar = StringVar(root, '') # search pattern 27 self.revar = BooleanVar(root, False) # regular expression? 28 self.casevar = BooleanVar(root, False) # match case? 29 self.wordvar = BooleanVar(root, False) # match whole word? 30 self.wrapvar = BooleanVar(root, True) # wrap around buffer? 31 self.backvar = BooleanVar(root, False) # search backwards? 24 32 25 33 # Access methods … … 48 56 # Higher level access methods 49 57 58 def setcookedpat(self, pat): 59 "Set pattern after escaping if re." 60 # called only in SearchDialog.py: 66 61 if self.isre(): 62 pat = re.escape(pat) 63 self.setpat(pat) 64 50 65 def getcookedpat(self): 51 66 pat = self.getpat() 52 if not self.isre(): 67 if not self.isre(): # if True, see setcookedpat 53 68 pat = re.escape(pat) 54 69 if self.isword(): … … 57 72 58 73 def getprog(self): 74 "Return compiled cooked search pattern." 59 75 pat = self.getpat() 60 76 if not pat: … … 67 83 try: 68 84 prog = re.compile(pat, flags) 69 except re.error ,what:85 except re.error as what: 70 86 try: 71 87 msg, col = what … … 78 94 79 95 def report_error(self, pat, msg, col=-1): 80 # Derived class could overrid this with something fancier96 # Derived class could override this with something fancier 81 97 msg = "Error: " + str(msg) 82 98 if pat: 83 msg = msg + "\n p\Pattern: " + str(pat)99 msg = msg + "\nPattern: " + str(pat) 84 100 if col >= 0: 85 101 msg = msg + "\nOffset: " + str(col) … … 87 103 msg, master=self.root) 88 104 89 def setcookedpat(self, pat):90 if self.isre():91 pat = re.escape(pat)92 self.setpat(pat)93 94 105 def search_text(self, text, prog=None, ok=0): 95 """Search a text widget for the pattern. 96 97 If prog is given, it should be the precompiled pattern. 98 Return a tuple (lineno, matchobj); None if not found. 99 100 This obeys the wrap and direction (back) settings. 101 102 The search starts at the selection (if there is one) or 103 at the insert mark (otherwise). If the search is forward, 104 it starts at the right of the selection; for a backward 105 search, it starts at the left end. An empty match exactly 106 at either end of the selection (or at the insert mark if 107 there is no selection) is ignored unless the ok flag is true 108 -- this is done to guarantee progress. 109 110 If the search is allowed to wrap around, it will return the 111 original selection if (and only if) it is the only match. 112 113 """ 106 '''Return (lineno, matchobj) or None for forward/backward search. 107 108 This function calls the right function with the right arguments. 109 It directly return the result of that call. 110 111 Text is a text widget. Prog is a precompiled pattern. 112 The ok parameteris a bit complicated as it has two effects. 113 114 If there is a selection, the search begin at either end, 115 depending on the direction setting and ok, with ok meaning that 116 the search starts with the selection. Otherwise, search begins 117 at the insert mark. 118 119 To aid progress, the search functions do not return an empty 120 match at the starting position unless ok is True. 121 ''' 122 114 123 if not prog: 115 124 prog = self.getprog() … … 180 189 return None 181 190 182 # Helper to search backwards in a string.183 # (Optimized for the case where the pattern isn't found.)184 185 191 def search_reverse(prog, chars, col): 192 '''Search backwards and return an re match object or None. 193 194 This is done by searching forwards until there is no match. 195 Prog: compiled re object with a search method returning a match. 196 Chars: line of text, without \n. 197 Col: stop index for the search; the limit for match.end(). 198 ''' 186 199 m = prog.search(chars) 187 200 if not m: 188 201 return None 189 202 found = None 190 i, j = m.span() 203 i, j = m.span() # m.start(), m.end() == match slice indexes 191 204 while i < col and j <= col: 192 205 found = m … … 199 212 return found 200 213 201 # Helper to get selection end points, defaulting to insert mark.202 # Return a tuple of indices ("line.col" strings).203 204 214 def get_selection(text): 215 '''Return tuple of 'line.col' indexes from selection or insert mark. 216 ''' 205 217 try: 206 218 first = text.index("sel.first") … … 214 226 return first, last 215 227 216 # Helper to parse a text index into a (line, col) tuple.217 218 228 def get_line_col(index): 229 '''Return (line, col) tuple of ints from 'line.col' string.''' 219 230 line, col = map(int, index.split(".")) # Fails on invalid index 220 231 return line, col 232 233 if __name__ == "__main__": 234 from test import support; support.use_resources = ['gui'] 235 import unittest 236 unittest.main('idlelib.idle_test.test_searchengine', verbosity=2, exit=False)
Note:
See TracChangeset
for help on using the changeset viewer.