PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib-python/2.7/idlelib/SearchEngine.py

https://bitbucket.org/varialus/jyjy
Python | 220 lines | 165 code | 25 blank | 30 comment | 44 complexity | 8c476b3165d6029881f958a8d8972cae MD5 | raw file
  1. import re
  2. from Tkinter import *
  3. import tkMessageBox
  4. def get(root):
  5. if not hasattr(root, "_searchengine"):
  6. root._searchengine = SearchEngine(root)
  7. # XXX This will never garbage-collect -- who cares
  8. return root._searchengine
  9. class SearchEngine:
  10. def __init__(self, root):
  11. self.root = root
  12. # State shared by search, replace, and grep;
  13. # the search dialogs bind these to UI elements.
  14. self.patvar = StringVar(root) # search pattern
  15. self.revar = BooleanVar(root) # regular expression?
  16. self.casevar = BooleanVar(root) # match case?
  17. self.wordvar = BooleanVar(root) # match whole word?
  18. self.wrapvar = BooleanVar(root) # wrap around buffer?
  19. self.wrapvar.set(1) # (on by default)
  20. self.backvar = BooleanVar(root) # search backwards?
  21. # Access methods
  22. def getpat(self):
  23. return self.patvar.get()
  24. def setpat(self, pat):
  25. self.patvar.set(pat)
  26. def isre(self):
  27. return self.revar.get()
  28. def iscase(self):
  29. return self.casevar.get()
  30. def isword(self):
  31. return self.wordvar.get()
  32. def iswrap(self):
  33. return self.wrapvar.get()
  34. def isback(self):
  35. return self.backvar.get()
  36. # Higher level access methods
  37. def getcookedpat(self):
  38. pat = self.getpat()
  39. if not self.isre():
  40. pat = re.escape(pat)
  41. if self.isword():
  42. pat = r"\b%s\b" % pat
  43. return pat
  44. def getprog(self):
  45. pat = self.getpat()
  46. if not pat:
  47. self.report_error(pat, "Empty regular expression")
  48. return None
  49. pat = self.getcookedpat()
  50. flags = 0
  51. if not self.iscase():
  52. flags = flags | re.IGNORECASE
  53. try:
  54. prog = re.compile(pat, flags)
  55. except re.error, what:
  56. try:
  57. msg, col = what
  58. except:
  59. msg = str(what)
  60. col = -1
  61. self.report_error(pat, msg, col)
  62. return None
  63. return prog
  64. def report_error(self, pat, msg, col=-1):
  65. # Derived class could overrid this with something fancier
  66. msg = "Error: " + str(msg)
  67. if pat:
  68. msg = msg + "\np\Pattern: " + str(pat)
  69. if col >= 0:
  70. msg = msg + "\nOffset: " + str(col)
  71. tkMessageBox.showerror("Regular expression error",
  72. msg, master=self.root)
  73. def setcookedpat(self, pat):
  74. if self.isre():
  75. pat = re.escape(pat)
  76. self.setpat(pat)
  77. def search_text(self, text, prog=None, ok=0):
  78. """Search a text widget for the pattern.
  79. If prog is given, it should be the precompiled pattern.
  80. Return a tuple (lineno, matchobj); None if not found.
  81. This obeys the wrap and direction (back) settings.
  82. The search starts at the selection (if there is one) or
  83. at the insert mark (otherwise). If the search is forward,
  84. it starts at the right of the selection; for a backward
  85. search, it starts at the left end. An empty match exactly
  86. at either end of the selection (or at the insert mark if
  87. there is no selection) is ignored unless the ok flag is true
  88. -- this is done to guarantee progress.
  89. If the search is allowed to wrap around, it will return the
  90. original selection if (and only if) it is the only match.
  91. """
  92. if not prog:
  93. prog = self.getprog()
  94. if not prog:
  95. return None # Compilation failed -- stop
  96. wrap = self.wrapvar.get()
  97. first, last = get_selection(text)
  98. if self.isback():
  99. if ok:
  100. start = last
  101. else:
  102. start = first
  103. line, col = get_line_col(start)
  104. res = self.search_backward(text, prog, line, col, wrap, ok)
  105. else:
  106. if ok:
  107. start = first
  108. else:
  109. start = last
  110. line, col = get_line_col(start)
  111. res = self.search_forward(text, prog, line, col, wrap, ok)
  112. return res
  113. def search_forward(self, text, prog, line, col, wrap, ok=0):
  114. wrapped = 0
  115. startline = line
  116. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  117. while chars:
  118. m = prog.search(chars[:-1], col)
  119. if m:
  120. if ok or m.end() > col:
  121. return line, m
  122. line = line + 1
  123. if wrapped and line > startline:
  124. break
  125. col = 0
  126. ok = 1
  127. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  128. if not chars and wrap:
  129. wrapped = 1
  130. wrap = 0
  131. line = 1
  132. chars = text.get("1.0", "2.0")
  133. return None
  134. def search_backward(self, text, prog, line, col, wrap, ok=0):
  135. wrapped = 0
  136. startline = line
  137. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  138. while 1:
  139. m = search_reverse(prog, chars[:-1], col)
  140. if m:
  141. if ok or m.start() < col:
  142. return line, m
  143. line = line - 1
  144. if wrapped and line < startline:
  145. break
  146. ok = 1
  147. if line <= 0:
  148. if not wrap:
  149. break
  150. wrapped = 1
  151. wrap = 0
  152. pos = text.index("end-1c")
  153. line, col = map(int, pos.split("."))
  154. chars = text.get("%d.0" % line, "%d.0" % (line+1))
  155. col = len(chars) - 1
  156. return None
  157. # Helper to search backwards in a string.
  158. # (Optimized for the case where the pattern isn't found.)
  159. def search_reverse(prog, chars, col):
  160. m = prog.search(chars)
  161. if not m:
  162. return None
  163. found = None
  164. i, j = m.span()
  165. while i < col and j <= col:
  166. found = m
  167. if i == j:
  168. j = j+1
  169. m = prog.search(chars, j)
  170. if not m:
  171. break
  172. i, j = m.span()
  173. return found
  174. # Helper to get selection end points, defaulting to insert mark.
  175. # Return a tuple of indices ("line.col" strings).
  176. def get_selection(text):
  177. try:
  178. first = text.index("sel.first")
  179. last = text.index("sel.last")
  180. except TclError:
  181. first = last = None
  182. if not first:
  183. first = text.index("insert")
  184. if not last:
  185. last = first
  186. return first, last
  187. # Helper to parse a text index into a (line, col) tuple.
  188. def get_line_col(index):
  189. line, col = map(int, index.split(".")) # Fails on invalid index
  190. return line, col