David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | from Tkinter import * |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 2 | from .EditorWindow import EditorWindow |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 3 | import re |
| 4 | import tkMessageBox |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 5 | from . 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"): |
Martin v. Löwis | 0f98d8f | 2007-08-13 06:26:48 +0000 | [diff] [blame] | 38 | if isinstance(s, (bytes, str8)): |
| 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() |
| 43 | |
Kurt B. Kaiser | 66aaf74 | 2007-08-09 18:00:23 +0000 | [diff] [blame] | 44 | def writelines(self, lines): |
| 45 | for line in lines: |
| 46 | self.write(line) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 47 | |
| 48 | def flush(self): |
| 49 | pass |
| 50 | |
| 51 | # Our own right-button menu |
| 52 | |
| 53 | rmenu_specs = [ |
| 54 | ("Go to file/line", "<<goto-file-line>>"), |
| 55 | ] |
| 56 | |
| 57 | file_line_pats = [ |
| 58 | r'file "([^"]*)", line (\d+)', |
| 59 | r'([^\s]+)\((\d+)\)', |
| 60 | r'([^\s]+):\s*(\d+):', |
| 61 | ] |
| 62 | |
| 63 | file_line_progs = None |
| 64 | |
| 65 | def goto_file_line(self, event=None): |
| 66 | if self.file_line_progs is None: |
| 67 | l = [] |
| 68 | for pat in self.file_line_pats: |
| 69 | l.append(re.compile(pat, re.IGNORECASE)) |
| 70 | self.file_line_progs = l |
| 71 | # x, y = self.event.x, self.event.y |
| 72 | # self.text.mark_set("insert", "@%d,%d" % (x, y)) |
| 73 | line = self.text.get("insert linestart", "insert lineend") |
| 74 | result = self._file_line_helper(line) |
| 75 | if not result: |
| 76 | # Try the previous line. This is handy e.g. in tracebacks, |
| 77 | # where you tend to right-click on the displayed source line |
| 78 | line = self.text.get("insert -1line linestart", |
| 79 | "insert -1line lineend") |
| 80 | result = self._file_line_helper(line) |
| 81 | if not result: |
| 82 | tkMessageBox.showerror( |
| 83 | "No special line", |
| 84 | "The line you point at doesn't look like " |
| 85 | "a valid file name followed by a line number.", |
| 86 | master=self.text) |
| 87 | return |
| 88 | filename, lineno = result |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 89 | edit = self.flist.open(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 90 | edit.gotoline(lineno) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 91 | |
| 92 | def _file_line_helper(self, line): |
| 93 | for prog in self.file_line_progs: |
| 94 | m = prog.search(line) |
| 95 | if m: |
| 96 | break |
| 97 | else: |
| 98 | return None |
| 99 | filename, lineno = m.group(1, 2) |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 100 | try: |
| 101 | f = open(filename, "r") |
| 102 | f.close() |
| 103 | except IOError: |
| 104 | return None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 105 | try: |
| 106 | return filename, int(lineno) |
| 107 | except TypeError: |
| 108 | return None |
| 109 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 110 | # These classes are currently not used but might come in handy |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 111 | |
| 112 | class OnDemandOutputWindow: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 113 | |
| 114 | tagdefs = { |
| 115 | # XXX Should use IdlePrefs.ColorPrefs |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 116 | "stdout": {"foreground": "blue"}, |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 117 | "stderr": {"foreground": "#007700"}, |
| 118 | } |
| 119 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 120 | def __init__(self, flist): |
| 121 | self.flist = flist |
| 122 | self.owin = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 123 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 124 | def write(self, s, tags, mark): |
| 125 | if not self.owin: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 126 | self.setup() |
| 127 | self.owin.write(s, tags, mark) |
| 128 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 129 | def setup(self): |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 130 | self.owin = owin = OutputWindow(self.flist) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 131 | text = owin.text |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 132 | for tag, cnf in self.tagdefs.items(): |
| 133 | if cnf: |
Raymond Hettinger | 931237e | 2003-07-09 18:48:24 +0000 | [diff] [blame] | 134 | text.tag_configure(tag, **cnf) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 135 | text.tag_raise('sel') |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 136 | self.write = self.owin.write |
| 137 | |
| 138 | #class PseudoFile: |
| 139 | # |
| 140 | # def __init__(self, owin, tags, mark="end"): |
| 141 | # self.owin = owin |
| 142 | # self.tags = tags |
| 143 | # self.mark = mark |
| 144 | |
| 145 | # def write(self, s): |
| 146 | # self.owin.write(s, self.tags, self.mark) |
| 147 | |
| 148 | # def writelines(self, l): |
| 149 | # map(self.write, l) |
| 150 | |
| 151 | # def flush(self): |
| 152 | # pass |