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