Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 1 | from tkinter import * |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 2 | from idlelib.EditorWindow import EditorWindow |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 3 | import re |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 4 | import tkinter.messagebox as tkMessageBox |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 5 | from idlelib import IOBinding |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 6 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 7 | class OutputWindow(EditorWindow): |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 8 | |
| 9 | """An editor window that can serve as an output file. |
| 10 | |
| 11 | Also the future base class for the Python shell window. |
| 12 | This class has no input facilities. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 13 | """ |
| 14 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 15 | def __init__(self, *args): |
Raymond Hettinger | 931237e | 2003-07-09 18:48:24 +0000 | [diff] [blame] | 16 | EditorWindow.__init__(self, *args) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 17 | self.text.bind("<<goto-file-line>>", self.goto_file_line) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 18 | |
| 19 | # Customize EditorWindow |
| 20 | |
| 21 | def ispythonsource(self, filename): |
| 22 | # No colorization needed |
| 23 | return 0 |
| 24 | |
| 25 | def short_title(self): |
| 26 | return "Output" |
| 27 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 28 | def maybesave(self): |
| 29 | # Override base class method -- don't ask any questions |
| 30 | if self.get_saved(): |
| 31 | return "yes" |
| 32 | else: |
| 33 | return "no" |
| 34 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 35 | # Act as output file |
| 36 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 37 | def write(self, s, tags=(), mark="insert"): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 38 | if isinstance(s, (bytes, bytes)): |
Martin v. Löwis | 0f98d8f | 2007-08-13 06:26:48 +0000 | [diff] [blame] | 39 | s = s.decode(IOBinding.encoding, "replace") |
Kurt B. Kaiser | d011636 | 2002-09-02 21:29:40 +0000 | [diff] [blame] | 40 | self.text.insert(mark, s, tags) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 41 | self.text.see(mark) |
| 42 | self.text.update() |
Martin v. Löwis | 30d5e6c | 2012-07-25 11:32:26 +0200 | [diff] [blame^] | 43 | return len(s) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 44 | |
Kurt B. Kaiser | 66aaf74 | 2007-08-09 18:00:23 +0000 | [diff] [blame] | 45 | def writelines(self, lines): |
| 46 | for line in lines: |
| 47 | self.write(line) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 48 | |
| 49 | def flush(self): |
| 50 | pass |
| 51 | |
| 52 | # Our own right-button menu |
| 53 | |
| 54 | rmenu_specs = [ |
| 55 | ("Go to file/line", "<<goto-file-line>>"), |
| 56 | ] |
| 57 | |
| 58 | file_line_pats = [ |
Kurt B. Kaiser | cef4b81 | 2009-05-06 03:23:37 +0000 | [diff] [blame] | 59 | # order of patterns matters |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 60 | r'file "([^"]*)", line (\d+)', |
| 61 | r'([^\s]+)\((\d+)\)', |
Kurt B. Kaiser | cef4b81 | 2009-05-06 03:23:37 +0000 | [diff] [blame] | 62 | r'^(\s*\S.*?):\s*(\d+):', # Win filename, maybe starting with spaces |
| 63 | r'([^\s]+):\s*(\d+):', # filename or path, ltrim |
| 64 | r'^\s*(\S.*?):\s*(\d+):', # Win abs path with embedded spaces, ltrim |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 65 | ] |
| 66 | |
| 67 | file_line_progs = None |
| 68 | |
| 69 | def goto_file_line(self, event=None): |
| 70 | if self.file_line_progs is None: |
| 71 | l = [] |
| 72 | for pat in self.file_line_pats: |
| 73 | l.append(re.compile(pat, re.IGNORECASE)) |
| 74 | self.file_line_progs = l |
| 75 | # x, y = self.event.x, self.event.y |
| 76 | # self.text.mark_set("insert", "@%d,%d" % (x, y)) |
| 77 | line = self.text.get("insert linestart", "insert lineend") |
| 78 | result = self._file_line_helper(line) |
| 79 | if not result: |
| 80 | # Try the previous line. This is handy e.g. in tracebacks, |
| 81 | # where you tend to right-click on the displayed source line |
| 82 | line = self.text.get("insert -1line linestart", |
| 83 | "insert -1line lineend") |
| 84 | result = self._file_line_helper(line) |
| 85 | if not result: |
| 86 | tkMessageBox.showerror( |
| 87 | "No special line", |
| 88 | "The line you point at doesn't look like " |
| 89 | "a valid file name followed by a line number.", |
| 90 | master=self.text) |
| 91 | return |
| 92 | filename, lineno = result |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 93 | edit = self.flist.open(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 94 | edit.gotoline(lineno) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 95 | |
| 96 | def _file_line_helper(self, line): |
| 97 | for prog in self.file_line_progs: |
Georg Brandl | 7b2669b | 2009-04-27 16:58:05 +0000 | [diff] [blame] | 98 | match = prog.search(line) |
| 99 | if match: |
| 100 | filename, lineno = match.group(1, 2) |
| 101 | try: |
| 102 | f = open(filename, "r") |
| 103 | f.close() |
| 104 | break |
| 105 | except IOError: |
| 106 | continue |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 107 | else: |
| 108 | return None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 109 | try: |
| 110 | return filename, int(lineno) |
| 111 | except TypeError: |
| 112 | return None |
| 113 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 114 | # These classes are currently not used but might come in handy |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 115 | |
| 116 | class OnDemandOutputWindow: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 117 | |
| 118 | tagdefs = { |
| 119 | # XXX Should use IdlePrefs.ColorPrefs |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 120 | "stdout": {"foreground": "blue"}, |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 121 | "stderr": {"foreground": "#007700"}, |
| 122 | } |
| 123 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 124 | def __init__(self, flist): |
| 125 | self.flist = flist |
| 126 | self.owin = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 127 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 128 | def write(self, s, tags, mark): |
| 129 | if not self.owin: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 130 | self.setup() |
| 131 | self.owin.write(s, tags, mark) |
| 132 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 133 | def setup(self): |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 134 | self.owin = owin = OutputWindow(self.flist) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 135 | text = owin.text |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 136 | for tag, cnf in self.tagdefs.items(): |
| 137 | if cnf: |
Raymond Hettinger | 931237e | 2003-07-09 18:48:24 +0000 | [diff] [blame] | 138 | text.tag_configure(tag, **cnf) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 139 | text.tag_raise('sel') |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 140 | self.write = self.owin.write |