SearchEngine.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. '''Define SearchEngine for search dialogs.'''
  2. import re
  3. from Tkinter import StringVar, BooleanVar, TclError
  4. import tkMessageBox
  5. def get(root):
  6. '''Return the singleton SearchEngine instance for the process.
  7. The single SearchEngine saves settings between dialog instances.
  8. If there is not a SearchEngine already, make one.
  9. '''
  10. if not hasattr(root, "_searchengine"):
  11. root._searchengine = SearchEngine(root)
  12. # This creates a cycle that persists until root is deleted.
  13. return root._searchengine
  14. class SearchEngine:
  15. """Handles searching a text widget for Find, Replace, and Grep."""
  16. def __init__(self, root):
  17. '''Initialize Variables that save search state.
  18. The dialogs bind these to the UI elements present in the dialogs.
  19. '''
  20. self.root = root # need for report_error()
  21. self.patvar = StringVar(root, '') # search pattern
  22. self.revar = BooleanVar(root, False) # regular expression?
  23. self.casevar = BooleanVar(root, False) # match case?
  24. self.wordvar = BooleanVar(root, False) # match whole word?
  25. self.wrapvar = BooleanVar(root, True) # wrap around buffer?
  26. self.backvar = BooleanVar(root, False) # search backwards?
  27. # Access methods
  28. def getpat(self):
  29. return self.patvar.get()
  30. def setpat(self, pat):
  31. self.patvar.set(pat)
  32. def isre(self):
  33. return self.revar.get()
  34. def iscase(self):
  35. return self.casevar.get()
  36. def isword(self):
  37. return self.wordvar.get()
  38. def iswrap(self):
  39. return self.wrapvar.get()
  40. def isback(self):
  41. return self.backvar.get()
  42. # Higher level access methods
  43. def setcookedpat(self, pat):
  44. "Set pattern after escaping if re."
  45. # called only in SearchDialog.py: 66
  46. if self.isre():
  47. pat = re.escape(pat)
  48. self.setpat(pat)
  49. def getcookedpat(self):
  50. pat = self.getpat()
  51. if not self.isre(): # if True, see setcookedpat
  52. pat = re.escape(pat)
  53. if self.isword():
  54. pat = r"\b%s\b" % pat
  55. return pat
  56. def getprog(self):
  57. "Return compiled cooked search pattern."
  58. pat = self.getpat()
  59. if not pat:
  60. self.report_error(pat, "Empty regular expression")
  61. return None
  62. pat = self.getcookedpat()
  63. flags = 0
  64. if not self.iscase():
  65. flags = flags | re.IGNORECASE
  66. try:
  67. prog = re.compile(pat, flags)
  68. except re.error as what:
  69. args = what.args
  70. msg = args[0]
  71. col = args[1] if len(args) >= 2 else -1
  72. self.report_error(pat, msg, col)
  73. return None
  74. return prog
  75. def report_error(self, pat, msg, col=-1):
  76. # Derived class could override this with something fancier
  77. msg = "Error: " + str(msg)
  78. if pat:
  79. msg = msg + "\nPattern: " + str(pat)
  80. if col >= 0:
  81. msg = msg + "\nOffset: " + str(col)
  82. tkMessageBox.showerror("Regular expression error",
  83. msg, master=self.root)
  84. def search_text(self, text, prog=None, ok=0):
  85. '''Return (lineno, matchobj) or None for forward/backward search.
  86. This function calls the right function with the right arguments.
  87. It directly return the result of that call.
  88. Text is a text widget. Prog is a precompiled pattern.
  89. The ok parameteris a bit complicated as it has two effects.
  90. If there is a selection, the search begin at either end,
  91. depending on the direction setting and ok, with ok meaning that
  92. the search starts with the selection. Otherwise, search begins
  93. at the insert mark.
  94. To aid progress, the search functions do not return an empty
  95. match at the starting position unless ok is True.
  96. '''
  97. if not prog:
  98. prog = self.getprog()
  99. if not prog:
  100. return None # Compilation failed -- stop
  101. wrap = self.wrapvar.get()
  102. first, last = get_selection(text)
  103. if self.isback():
  104. if ok:
  105. start = last
  106. else:
  107. start = first
  108. line, col = get_line_col(start)
  109. res = self.search_backward(text, prog, line, col, wrap, ok)
  110. else:
  111. if ok:
  112. start = first
  113. else:
  114. start = last
  115. line, col = get_line_col(start)
  116. res = self.search_forward(text, prog, line, col, wrap, ok)
  117. return res
  118. def search_forward(self, text, prog, line, col, wrap, ok=0):
  119. wrapped = 0
  120. startline = line
  121. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  122. while chars:
  123. m = prog.search(chars[:-1], col)
  124. if m:
  125. if ok or m.end() > col:
  126. return line, m
  127. line = line + 1
  128. if wrapped and line > startline:
  129. break
  130. col = 0
  131. ok = 1
  132. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  133. if not chars and wrap:
  134. wrapped = 1
  135. wrap = 0
  136. line = 1
  137. chars = text.get("1.0", "2.0")
  138. return None
  139. def search_backward(self, text, prog, line, col, wrap, ok=0):
  140. wrapped = 0
  141. startline = line
  142. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  143. while 1:
  144. m = search_reverse(prog, chars[:-1], col)
  145. if m:
  146. if ok or m.start() < col:
  147. return line, m
  148. line = line - 1
  149. if wrapped and line < startline:
  150. break
  151. ok = 1
  152. if line <= 0:
  153. if not wrap:
  154. break
  155. wrapped = 1
  156. wrap = 0
  157. pos = text.index("end-1c")
  158. line, col = map(int, pos.split("."))
  159. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  160. col = len(chars) - 1
  161. return None
  162. def search_reverse(prog, chars, col):
  163. '''Search backwards and return an re match object or None.
  164. This is done by searching forwards until there is no match.
  165. Prog: compiled re object with a search method returning a match.
  166. Chars: line of text, without \\n.
  167. Col: stop index for the search; the limit for match.end().
  168. '''
  169. m = prog.search(chars)
  170. if not m:
  171. return None
  172. found = None
  173. i, j = m.span() # m.start(), m.end() == match slice indexes
  174. while i < col and j <= col:
  175. found = m
  176. if i == j:
  177. j = j+1
  178. m = prog.search(chars, j)
  179. if not m:
  180. break
  181. i, j = m.span()
  182. return found
  183. def get_selection(text):
  184. '''Return tuple of 'line.col' indexes from selection or insert mark.
  185. '''
  186. try:
  187. first = text.index("sel.first")
  188. last = text.index("sel.last")
  189. except TclError:
  190. first = last = None
  191. if not first:
  192. first = text.index("insert")
  193. if not last:
  194. last = first
  195. return first, last
  196. def get_line_col(index):
  197. '''Return (line, col) tuple of ints from 'line.col' string.'''
  198. line, col = map(int, index.split(".")) # Fails on invalid index
  199. return line, col
  200. if __name__ == "__main__":
  201. import unittest
  202. unittest.main('idlelib.idle_test.test_searchengine', verbosity=2, exit=False)