blob: 0e7fba231ab55cf3aa91aea2c8ab1a53ab10a78b [file] [log] [blame]
Guido van Rossum504b0bf1999-01-02 21:28:54 +00001from Tkinter import *
2from EditorWindow import EditorWindow
3import re
4import tkMessageBox
Martin v. Löwis75ea1e12002-08-10 12:22:12 +00005import IOBinding
Guido van Rossum504b0bf1999-01-02 21:28:54 +00006
7class OutputWindow(EditorWindow):
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.
13 """
14
15 def __init__(self, *args):
16 apply(EditorWindow.__init__, (self,) + args)
17 self.text.bind("<<goto-file-line>>", self.goto_file_line)
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
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
35 # Act as output file
36
37 def write(self, s, tags=(), mark="insert"):
Martin v. Löwis75ea1e12002-08-10 12:22:12 +000038 # 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
Martin v. Löwisdc135172002-02-23 22:39:37 +000046 self.text.insert(mark, s, tags)
Guido van Rossum504b0bf1999-01-02 21:28:54 +000047 self.text.see(mark)
48 self.text.update()
49
50 def writelines(self, l):
51 map(self.write, l)
52
Guido van Rossum3d0ff001999-04-22 20:50:33 +000053 def flush(self):
54 pass
55
Guido van Rossum504b0bf1999-01-02 21:28:54 +000056 # Our own right-button menu
57
58 rmenu_specs = [
59 ("Go to file/line", "<<goto-file-line>>"),
60 ]
61
62 file_line_pats = [
63 r'file "([^"]*)", line (\d+)',
64 r'([^\s]+)\((\d+)\)',
65 r'([^\s]+):\s*(\d+):',
66 ]
67
68 file_line_progs = None
69
70 def goto_file_line(self, event=None):
71 if self.file_line_progs is None:
72 l = []
73 for pat in self.file_line_pats:
74 l.append(re.compile(pat, re.IGNORECASE))
75 self.file_line_progs = l
76 # x, y = self.event.x, self.event.y
77 # self.text.mark_set("insert", "@%d,%d" % (x, y))
78 line = self.text.get("insert linestart", "insert lineend")
Guido van Rossumba23bed2000-03-07 16:25:11 +000079 result = self._file_line_helper(line)
80 if not result:
81 # Try the previous line. This is handy e.g. in tracebacks,
82 # where you tend to right-click on the displayed source line
83 line = self.text.get("insert -1line linestart",
84 "insert -1line lineend")
85 result = self._file_line_helper(line)
86 if not result:
87 tkMessageBox.showerror(
88 "No special line",
89 "The line you point at doesn't look like "
90 "a valid file name followed by a line number.",
91 master=self.text)
92 return
93 filename, lineno = result
94 edit = self.flist.open(filename)
95 edit.gotoline(lineno)
96
97 def _file_line_helper(self, line):
Guido van Rossum504b0bf1999-01-02 21:28:54 +000098 for prog in self.file_line_progs:
99 m = prog.search(line)
100 if m:
101 break
102 else:
Guido van Rossumba23bed2000-03-07 16:25:11 +0000103 return None
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000104 filename, lineno = m.group(1, 2)
105 try:
106 f = open(filename, "r")
107 f.close()
Guido van Rossumba23bed2000-03-07 16:25:11 +0000108 except IOError:
109 return None
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000110 try:
Guido van Rossumba23bed2000-03-07 16:25:11 +0000111 return filename, int(lineno)
112 except TypeError:
113 return None
Guido van Rossum70f6d991999-04-22 22:28:42 +0000114
115# These classes are currently not used but might come in handy
116
117class OnDemandOutputWindow:
118
119 tagdefs = {
120 # XXX Should use IdlePrefs.ColorPrefs
121 "stdout": {"foreground": "blue"},
122 "stderr": {"foreground": "#007700"},
Tim Peters70c43782001-01-17 08:48:39 +0000123 }
124
Guido van Rossum70f6d991999-04-22 22:28:42 +0000125 def __init__(self, flist):
126 self.flist = flist
127 self.owin = None
Tim Peters70c43782001-01-17 08:48:39 +0000128
Guido van Rossum70f6d991999-04-22 22:28:42 +0000129 def write(self, s, tags, mark):
130 if not self.owin:
131 self.setup()
132 self.owin.write(s, tags, mark)
Tim Peters70c43782001-01-17 08:48:39 +0000133
Guido van Rossum70f6d991999-04-22 22:28:42 +0000134 def setup(self):
135 self.owin = owin = OutputWindow(self.flist)
136 text = owin.text
137 for tag, cnf in self.tagdefs.items():
138 if cnf:
139 apply(text.tag_configure, (tag,), cnf)
140 text.tag_raise('sel')
141 self.write = self.owin.write
142
143class PseudoFile:
144
145 def __init__(self, owin, tags, mark="end"):
146 self.owin = owin
147 self.tags = tags
148 self.mark = mark
149
150 def write(self, s):
151 self.owin.write(s, self.tags, self.mark)
152
153 def writelines(self, l):
154 map(self.write, l)
155
156 def flush(self):
157 pass