Georg Brandl | 6634bf2 | 2008-05-20 07:13:37 +0000 | [diff] [blame] | 1 | from Tkinter import * |
Florent Xicluna | d630c04 | 2010-04-02 07:24:52 +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 | 6634bf2 | 2008-05-20 07:13:37 +0000 | [diff] [blame] | 4 | import tkMessageBox |
Florent Xicluna | d630c04 | 2010-04-02 07:24:52 +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"): |
Kurt B. Kaiser | 7de3772 | 2003-06-15 17:49:59 +0000 | [diff] [blame] | 38 | # Tk assumes that byte strings are Latin-1; |
| 39 | # we assume that they are in the locale's encoding |
| 40 | if isinstance(s, str): |
| 41 | try: |
| 42 | s = unicode(s, IOBinding.encoding) |
| 43 | except UnicodeError: |
| 44 | # some other encoding; let Tcl deal with it |
| 45 | pass |
Kurt B. Kaiser | d011636 | 2002-09-02 21:29:40 +0000 | [diff] [blame] | 46 | self.text.insert(mark, s, tags) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 47 | self.text.see(mark) |
| 48 | self.text.update() |
| 49 | |
Florent Xicluna | d630c04 | 2010-04-02 07:24:52 +0000 | [diff] [blame] | 50 | def writelines(self, lines): |
| 51 | for line in lines: |
| 52 | self.write(line) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 53 | |
| 54 | def flush(self): |
| 55 | pass |
| 56 | |
| 57 | # Our own right-button menu |
| 58 | |
| 59 | rmenu_specs = [ |
Andrew Svetlov | 5018db7 | 2012-11-01 22:39:14 +0200 | [diff] [blame^] | 60 | ("Cut", "<<cut>>", "rmenu_check_cut"), |
| 61 | ("Copy", "<<copy>>", "rmenu_check_copy"), |
| 62 | ("Paste", "<<paste>>", "rmenu_check_paste"), |
| 63 | (None, None, None), |
| 64 | ("Go to file/line", "<<goto-file-line>>", None), |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 65 | ] |
| 66 | |
| 67 | file_line_pats = [ |
Kurt B. Kaiser | 62e9a65 | 2009-05-03 02:05:22 +0000 | [diff] [blame] | 68 | # order of patterns matters |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 69 | r'file "([^"]*)", line (\d+)', |
| 70 | r'([^\s]+)\((\d+)\)', |
Kurt B. Kaiser | 62e9a65 | 2009-05-03 02:05:22 +0000 | [diff] [blame] | 71 | r'^(\s*\S.*?):\s*(\d+):', # Win filename, maybe starting with spaces |
| 72 | r'([^\s]+):\s*(\d+):', # filename or path, ltrim |
| 73 | r'^\s*(\S.*?):\s*(\d+):', # Win abs path with embedded spaces, ltrim |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 74 | ] |
| 75 | |
| 76 | file_line_progs = None |
| 77 | |
| 78 | def goto_file_line(self, event=None): |
| 79 | if self.file_line_progs is None: |
| 80 | l = [] |
| 81 | for pat in self.file_line_pats: |
| 82 | l.append(re.compile(pat, re.IGNORECASE)) |
| 83 | self.file_line_progs = l |
| 84 | # x, y = self.event.x, self.event.y |
| 85 | # self.text.mark_set("insert", "@%d,%d" % (x, y)) |
| 86 | line = self.text.get("insert linestart", "insert lineend") |
| 87 | result = self._file_line_helper(line) |
| 88 | if not result: |
| 89 | # Try the previous line. This is handy e.g. in tracebacks, |
| 90 | # where you tend to right-click on the displayed source line |
| 91 | line = self.text.get("insert -1line linestart", |
| 92 | "insert -1line lineend") |
| 93 | result = self._file_line_helper(line) |
| 94 | if not result: |
| 95 | tkMessageBox.showerror( |
| 96 | "No special line", |
| 97 | "The line you point at doesn't look like " |
| 98 | "a valid file name followed by a line number.", |
| 99 | master=self.text) |
| 100 | return |
| 101 | filename, lineno = result |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 102 | edit = self.flist.open(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 103 | edit.gotoline(lineno) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 104 | |
| 105 | def _file_line_helper(self, line): |
| 106 | for prog in self.file_line_progs: |
Kurt B. Kaiser | 94290d3 | 2009-04-26 23:22:11 +0000 | [diff] [blame] | 107 | match = prog.search(line) |
| 108 | if match: |
| 109 | filename, lineno = match.group(1, 2) |
| 110 | try: |
| 111 | f = open(filename, "r") |
| 112 | f.close() |
| 113 | break |
| 114 | except IOError: |
| 115 | continue |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 116 | else: |
| 117 | return None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 118 | try: |
| 119 | return filename, int(lineno) |
| 120 | except TypeError: |
| 121 | return None |
| 122 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 123 | # These classes are currently not used but might come in handy |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 124 | |
| 125 | class OnDemandOutputWindow: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 126 | |
| 127 | tagdefs = { |
| 128 | # XXX Should use IdlePrefs.ColorPrefs |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 129 | "stdout": {"foreground": "blue"}, |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 130 | "stderr": {"foreground": "#007700"}, |
| 131 | } |
| 132 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 133 | def __init__(self, flist): |
| 134 | self.flist = flist |
| 135 | self.owin = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 136 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 137 | def write(self, s, tags, mark): |
| 138 | if not self.owin: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 139 | self.setup() |
| 140 | self.owin.write(s, tags, mark) |
| 141 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 142 | def setup(self): |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 143 | self.owin = owin = OutputWindow(self.flist) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 144 | text = owin.text |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 145 | for tag, cnf in self.tagdefs.items(): |
| 146 | if cnf: |
Raymond Hettinger | 931237e | 2003-07-09 18:48:24 +0000 | [diff] [blame] | 147 | text.tag_configure(tag, **cnf) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 148 | text.tag_raise('sel') |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 149 | self.write = self.owin.write |