blob: 5ab08bbaf4bc9553e05d4c525cde1576da654a33 [file] [log] [blame]
Cheryl Sabella998f4962017-08-27 18:06:00 -04001"""Editor window that can serve as an output file.
2"""
3
David Scherer7aced172000-08-15 01:13:23 +00004import re
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -04005
Cheryl Sabella998f4962017-08-27 18:06:00 -04006from tkinter import messagebox
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -04007
8from idlelib.editor import EditorWindow
David Scherer7aced172000-08-15 01:13:23 +00009
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040010
Cheryl Sabella998f4962017-08-27 18:06:00 -040011file_line_pats = [
12 # order of patterns matters
13 r'file "([^"]*)", line (\d+)',
14 r'([^\s]+)\((\d+)\)',
15 r'^(\s*\S.*?):\s*(\d+):', # Win filename, maybe starting with spaces
16 r'([^\s]+):\s*(\d+):', # filename or path, ltrim
17 r'^\s*(\S.*?):\s*(\d+):', # Win abs path with embedded spaces, ltrim
18]
Kurt B. Kaiser969de452002-06-12 03:28:57 +000019
Cheryl Sabella998f4962017-08-27 18:06:00 -040020file_line_progs = None
21
22
23def compile_progs():
24 "Compile the patterns for matching to file name and line number."
25 global file_line_progs
26 file_line_progs = [re.compile(pat, re.IGNORECASE)
27 for pat in file_line_pats]
28
29
30def file_line_helper(line):
31 """Extract file name and line number from line of text.
32
33 Check if line of text contains one of the file/line patterns.
34 If it does and if the file and line are valid, return
35 a tuple of the file name and line number. If it doesn't match
36 or if the file or line is invalid, return None.
37 """
38 if not file_line_progs:
39 compile_progs()
40 for prog in file_line_progs:
41 match = prog.search(line)
42 if match:
43 filename, lineno = match.group(1, 2)
44 try:
45 f = open(filename, "r")
46 f.close()
47 break
48 except OSError:
49 continue
50 else:
51 return None
52 try:
53 return filename, int(lineno)
54 except TypeError:
55 return None
56
57
58class OutputWindow(EditorWindow):
Kurt B. Kaiser969de452002-06-12 03:28:57 +000059 """An editor window that can serve as an output file.
60
61 Also the future base class for the Python shell window.
62 This class has no input facilities.
Cheryl Sabella998f4962017-08-27 18:06:00 -040063
64 Adds binding to open a file at a line to the text widget.
David Scherer7aced172000-08-15 01:13:23 +000065 """
66
David Scherer7aced172000-08-15 01:13:23 +000067 # Our own right-button menu
David Scherer7aced172000-08-15 01:13:23 +000068 rmenu_specs = [
Andrew Svetlovd1837672012-11-01 22:41:19 +020069 ("Cut", "<<cut>>", "rmenu_check_cut"),
70 ("Copy", "<<copy>>", "rmenu_check_copy"),
71 ("Paste", "<<paste>>", "rmenu_check_paste"),
72 (None, None, None),
73 ("Go to file/line", "<<goto-file-line>>", None),
David Scherer7aced172000-08-15 01:13:23 +000074 ]
75
Tal Einat7123ea02019-07-23 15:22:11 +030076 allow_code_context = False
Tal Einat7036e1d2019-07-17 11:15:53 +030077
Cheryl Sabella998f4962017-08-27 18:06:00 -040078 def __init__(self, *args):
79 EditorWindow.__init__(self, *args)
80 self.text.bind("<<goto-file-line>>", self.goto_file_line)
David Scherer7aced172000-08-15 01:13:23 +000081
Cheryl Sabella998f4962017-08-27 18:06:00 -040082 # Customize EditorWindow
83 def ispythonsource(self, filename):
84 "Python source is only part of output: do not colorize."
85 return False
86
87 def short_title(self):
88 "Customize EditorWindow title."
89 return "Output"
90
91 def maybesave(self):
92 "Customize EditorWindow to not display save file messagebox."
93 return 'yes' if self.get_saved() else 'no'
94
95 # Act as output file
96 def write(self, s, tags=(), mark="insert"):
97 """Write text to text widget.
98
99 The text is inserted at the given index with the provided
100 tags. The text widget is then scrolled to make it visible
101 and updated to display it, giving the effect of seeing each
102 line as it is added.
103
104 Args:
105 s: Text to insert into text widget.
106 tags: Tuple of tag strings to apply on the insert.
107 mark: Index for the insert.
108
109 Return:
110 Length of text inserted.
111 """
Serhiy Storchaka2515a282020-06-30 03:18:22 +0300112 assert isinstance(s, str)
Cheryl Sabella998f4962017-08-27 18:06:00 -0400113 self.text.insert(mark, s, tags)
114 self.text.see(mark)
115 self.text.update()
116 return len(s)
117
118 def writelines(self, lines):
119 "Write each item in lines iterable."
120 for line in lines:
121 self.write(line)
122
123 def flush(self):
124 "No flushing needed as write() directly writes to widget."
125 pass
126
127 def showerror(self, *args, **kwargs):
128 messagebox.showerror(*args, **kwargs)
David Scherer7aced172000-08-15 01:13:23 +0000129
130 def goto_file_line(self, event=None):
Cheryl Sabella998f4962017-08-27 18:06:00 -0400131 """Handle request to open file/line.
132
133 If the selected or previous line in the output window
134 contains a file name and line number, then open that file
135 name in a new window and position on the line number.
136
137 Otherwise, display an error messagebox.
138 """
David Scherer7aced172000-08-15 01:13:23 +0000139 line = self.text.get("insert linestart", "insert lineend")
Cheryl Sabella998f4962017-08-27 18:06:00 -0400140 result = file_line_helper(line)
David Scherer7aced172000-08-15 01:13:23 +0000141 if not result:
142 # Try the previous line. This is handy e.g. in tracebacks,
143 # where you tend to right-click on the displayed source line
144 line = self.text.get("insert -1line linestart",
145 "insert -1line lineend")
Cheryl Sabella998f4962017-08-27 18:06:00 -0400146 result = file_line_helper(line)
David Scherer7aced172000-08-15 01:13:23 +0000147 if not result:
Cheryl Sabella998f4962017-08-27 18:06:00 -0400148 self.showerror(
David Scherer7aced172000-08-15 01:13:23 +0000149 "No special line",
150 "The line you point at doesn't look like "
151 "a valid file name followed by a line number.",
Terry Jan Reedy3be2e542015-09-25 22:22:55 -0400152 parent=self.text)
David Scherer7aced172000-08-15 01:13:23 +0000153 return
154 filename, lineno = result
Cheryl Sabella998f4962017-08-27 18:06:00 -0400155 self.flist.gotofileline(filename, lineno)
David Scherer7aced172000-08-15 01:13:23 +0000156
David Scherer7aced172000-08-15 01:13:23 +0000157
Kurt B. Kaiser969de452002-06-12 03:28:57 +0000158# These classes are currently not used but might come in handy
David Scherer7aced172000-08-15 01:13:23 +0000159class OnDemandOutputWindow:
David Scherer7aced172000-08-15 01:13:23 +0000160
161 tagdefs = {
162 # XXX Should use IdlePrefs.ColorPrefs
David Scherer7aced172000-08-15 01:13:23 +0000163 "stdout": {"foreground": "blue"},
Kurt B. Kaiser969de452002-06-12 03:28:57 +0000164 "stderr": {"foreground": "#007700"},
165 }
166
David Scherer7aced172000-08-15 01:13:23 +0000167 def __init__(self, flist):
168 self.flist = flist
169 self.owin = None
David Scherer7aced172000-08-15 01:13:23 +0000170
Kurt B. Kaiser969de452002-06-12 03:28:57 +0000171 def write(self, s, tags, mark):
172 if not self.owin:
David Scherer7aced172000-08-15 01:13:23 +0000173 self.setup()
174 self.owin.write(s, tags, mark)
175
David Scherer7aced172000-08-15 01:13:23 +0000176 def setup(self):
Kurt B. Kaiser969de452002-06-12 03:28:57 +0000177 self.owin = owin = OutputWindow(self.flist)
David Scherer7aced172000-08-15 01:13:23 +0000178 text = owin.text
David Scherer7aced172000-08-15 01:13:23 +0000179 for tag, cnf in self.tagdefs.items():
180 if cnf:
Raymond Hettinger931237e2003-07-09 18:48:24 +0000181 text.tag_configure(tag, **cnf)
David Scherer7aced172000-08-15 01:13:23 +0000182 text.tag_raise('sel')
Kurt B. Kaiser969de452002-06-12 03:28:57 +0000183 self.write = self.owin.write
Cheryl Sabella998f4962017-08-27 18:06:00 -0400184
185if __name__ == '__main__':
Terry Jan Reedy4d921582018-06-19 19:12:52 -0400186 from unittest import main
187 main('idlelib.idle_test.test_outwin', verbosity=2, exit=False)