diff options
author | Guido van Rossum <guido@python.org> | 1998-10-10 18:48:31 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-10-10 18:48:31 (GMT) |
commit | 3b4ca0ddad8d1e224f71e89f4c7fbc8de5c6edc4 (patch) | |
tree | 1a66ed7c7eec87f31d61a2a083096e5cad89a39c /Tools/idle/SearchBinding.py | |
parent | dc1adabcb86ee0813c9bae2d5cc59be5cad1ff31 (diff) | |
download | cpython-3b4ca0ddad8d1e224f71e89f4c7fbc8de5c6edc4.zip cpython-3b4ca0ddad8d1e224f71e89f4c7fbc8de5c6edc4.tar.gz cpython-3b4ca0ddad8d1e224f71e89f4c7fbc8de5c6edc4.tar.bz2 |
Initial checking of Tk-based Python IDE.
Features: text editor with syntax coloring and undo;
subclassed into interactive Python shell which adds history.
Diffstat (limited to 'Tools/idle/SearchBinding.py')
-rw-r--r-- | Tools/idle/SearchBinding.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/Tools/idle/SearchBinding.py b/Tools/idle/SearchBinding.py new file mode 100644 index 0000000..6abe5e0 --- /dev/null +++ b/Tools/idle/SearchBinding.py @@ -0,0 +1,84 @@ +import re +import tkSimpleDialog +import tkMessageBox + +class SearchBinding: + + def __init__(self, text): + self.text = text + self.pat = "" + self.prog = None + self.text.bind("<<find>>", self.find_event) + self.text.bind("<<find-next>>", self.find_next_event) + self.text.bind("<<find-same>>", self.find_same_event) + self.text.bind("<<goto-line>>", self.goto_line_event) + + def find_event(self, event): + default = self.text.get("self.first", "sel.last") or self.pat + new = tkSimpleDialog.askstring("Find", + "Regular Expression:", + initialvalue=default, + parent=self.text) + if not new: + return "break" + self.pat = new + try: + self.prog = re.compile(self.pat) + except re.error, msg: + tkMessageBox.showerror("RE error", str(msg), + master=self.text) + return "break" + return self.find_next_event(event) + + def find_same_event(self, event): + pat = self.text.get("sel.first", "sel.last") + if not pat: + return self.find_event(event) + self.pat = re.escape(pat) + self.prog = None + try: + self.prog = re.compile(self.pat) + except re.error, msg: + tkMessageBox.showerror("RE error", str(message), + master=self.text) + return "break" + self.text.mark_set("insert", "sel.last") + return self.find_next_event(event) + + def find_next_event(self, event): + if not self.pat: + return self.find_event(event) + if not self.prog: + self.text.bell() + ##print "No program" + return "break" + self.text.mark_set("find", "insert") + while 1: + chars = self.text.get("find", "find lineend +1c") + ##print "Searching", `chars` + if not chars: + self.text.bell() + ##print "end of buffer" + break + m = self.prog.search(chars) + if m: + i, j = m.span() + self.text.mark_set("insert", "find +%dc" % j) + self.text.mark_set("find", "find +%dc" % i) + self.text.tag_remove("sel", "1.0", "end") + self.text.tag_add("sel", "find", "insert") + self.text.see("insert") + break + self.text.mark_set("find", "find lineend +1c") + return "break" + + def goto_line_event(self, event): + lineno = tkSimpleDialog.askinteger("Goto", + "Go to line number:") + if lineno is None: + return "break" + if lineno <= 0: + self.text.bell() + return "break" + self.text.mark_set("insert", "%d.0" % lineno) + self.text.see("insert") |