searchengine.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. '''Define SearchEngine for search dialogs.'''
  2. import re
  3. from tkinter import StringVar, BooleanVar, TclError
  4. from tkinter import messagebox
  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 search.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 e:
  69. self.report_error(pat, e.msg, e.pos)
  70. return None
  71. return prog
  72. def report_error(self, pat, msg, col=None):
  73. # Derived class could override this with something fancier
  74. msg = "Error: " + str(msg)
  75. if pat:
  76. msg = msg + "\nPattern: " + str(pat)
  77. if col is not None:
  78. msg = msg + "\nOffset: " + str(col)
  79. messagebox.showerror("Regular expression error",
  80. msg, master=self.root)
  81. def search_text(self, text, prog=None, ok=0):
  82. '''Return (lineno, matchobj) or None for forward/backward search.
  83. This function calls the right function with the right arguments.
  84. It directly return the result of that call.
  85. Text is a text widget. Prog is a precompiled pattern.
  86. The ok parameter is a bit complicated as it has two effects.
  87. If there is a selection, the search begin at either end,
  88. depending on the direction setting and ok, with ok meaning that
  89. the search starts with the selection. Otherwise, search begins
  90. at the insert mark.
  91. To aid progress, the search functions do not return an empty
  92. match at the starting position unless ok is True.
  93. '''
  94. if not prog:
  95. prog = self.getprog()
  96. if not prog:
  97. return None # Compilation failed -- stop
  98. wrap = self.wrapvar.get()
  99. first, last = get_selection(text)
  100. if self.isback():
  101. if ok:
  102. start = last
  103. else:
  104. start = first
  105. line, col = get_line_col(start)
  106. res = self.search_backward(text, prog, line, col, wrap, ok)
  107. else:
  108. if ok:
  109. start = first
  110. else:
  111. start = last
  112. line, col = get_line_col(start)
  113. res = self.search_forward(text, prog, line, col, wrap, ok)
  114. return res
  115. def search_forward(self, text, prog, line, col, wrap, ok=0):
  116. wrapped = 0
  117. startline = line
  118. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  119. while chars:
  120. m = prog.search(chars[:-1], col)
  121. if m:
  122. if ok or m.end() > col:
  123. return line, m
  124. line = line + 1
  125. if wrapped and line > startline:
  126. break
  127. col = 0
  128. ok = 1
  129. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  130. if not chars and wrap:
  131. wrapped = 1
  132. wrap = 0
  133. line = 1
  134. chars = text.get("1.0", "2.0")
  135. return None
  136. def search_backward(self, text, prog, line, col, wrap, ok=0):
  137. wrapped = 0
  138. startline = line
  139. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  140. while True:
  141. m = search_reverse(prog, chars[:-1], col)
  142. if m:
  143. if ok or m.start() < col:
  144. return line, m
  145. line = line - 1
  146. if wrapped and line < startline:
  147. break
  148. ok = 1
  149. if line <= 0:
  150. if not wrap:
  151. break
  152. wrapped = 1
  153. wrap = 0
  154. pos = text.index("end-1c")
  155. line, col = map(int, pos.split("."))
  156. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  157. col = len(chars) - 1
  158. return None
  159. def search_reverse(prog, chars, col):
  160. '''Search backwards and return an re match object or None.
  161. This is done by searching forwards until there is no match.
  162. Prog: compiled re object with a search method returning a match.
  163. Chars: line of text, without \\n.
  164. Col: stop index for the search; the limit for match.end().
  165. '''
  166. m = prog.search(chars)
  167. if not m:
  168. return None
  169. found = None
  170. i, j = m.span() # m.start(), m.end() == match slice indexes
  171. while i < col and j <= col:
  172. found = m
  173. if i == j:
  174. j = j+1
  175. m = prog.search(chars, j)
  176. if not m:
  177. break
  178. i, j = m.span()
  179. return found
  180. def get_selection(text):
  181. '''Return tuple of 'line.col' indexes from selection or insert mark.
  182. '''
  183. try:
  184. first = text.index("sel.first")
  185. last = text.index("sel.last")
  186. except TclError:
  187. first = last = None
  188. if not first:
  189. first = text.index("insert")
  190. if not last:
  191. last = first
  192. return first, last
  193. def get_line_col(index):
  194. '''Return (line, col) tuple of ints from 'line.col' string.'''
  195. line, col = map(int, index.split(".")) # Fails on invalid index
  196. return line, col
  197. if __name__ == "__main__":
  198. from unittest import main
  199. main('idlelib.idle_test.test_searchengine', verbosity=2)