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