blob: 38c59bdf9b4e667334be86f04b74beda3e672f34 [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
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04009from idlelib import iomenu
David Scherer7aced172000-08-15 01:13:23 +000010
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040011
Cheryl Sabella998f4962017-08-27 18:06:00 -040012file_line_pats = [
13 # order of patterns matters
14 r'file "([^"]*)", line (\d+)',
15 r'([^\s]+)\((\d+)\)',
16 r'^(\s*\S.*?):\s*(\d+):', # Win filename, maybe starting with spaces
17 r'([^\s]+):\s*(\d+):', # filename or path, ltrim
18 r'^\s*(\S.*?):\s*(\d+):', # Win abs path with embedded spaces, ltrim
19]
Kurt B. Kaiser969de452002-06-12 03:28:57 +000020
Cheryl Sabella998f4962017-08-27 18:06:00 -040021file_line_progs = None
22
23
24def compile_progs():
25 "Compile the patterns for matching to file name and line number."
26 global file_line_progs
27 file_line_progs = [re.compile(pat, re.IGNORECASE)
28 for pat in file_line_pats]
29
30
31def file_line_helper(line):
32 """Extract file name and line number from line of text.
33
34 Check if line of text contains one of the file/line patterns.
35 If it does and if the file and line are valid, return
36 a tuple of the file name and line number. If it doesn't match
37 or if the file or line is invalid, return None.
38 """
39 if not file_line_progs:
40 compile_progs()
41 for prog in file_line_progs:
42 match = prog.search(line)
43 if match:
44 filename, lineno = match.group(1, 2)
45 try:
46 f = open(filename, "r")
47 f.close()
48 break
49 except OSError:
50 continue
51 else:
52 return None
53 try:
54 return filename, int(lineno)
55 except TypeError:
56 return None
57
58
59class OutputWindow(EditorWindow):
Kurt B. Kaiser969de452002-06-12 03:28:57 +000060 """An editor window that can serve as an output file.
61
62 Also the future base class for the Python shell window.
63 This class has no input facilities.
Cheryl Sabella998f4962017-08-27 18:06:00 -040064
65 Adds binding to open a file at a line to the text widget.
David Scherer7aced172000-08-15 01:13:23 +000066 """
67
David Scherer7aced172000-08-15 01:13:23 +000068 # Our own right-button menu
David Scherer7aced172000-08-15 01:13:23 +000069 rmenu_specs = [
Andrew Svetlovd1837672012-11-01 22:41:19 +020070 ("Cut", "<<cut>>", "rmenu_check_cut"),
71 ("Copy", "<<copy>>", "rmenu_check_copy"),
72 ("Paste", "<<paste>>", "rmenu_check_paste"),
73 (None, None, None),
74 ("Go to file/line", "<<goto-file-line>>", None),
David Scherer7aced172000-08-15 01:13:23 +000075 ]
76
Tal Einat7036e1d2019-07-17 11:15:53 +030077 allow_codecontext = False
78
Cheryl Sabella998f4962017-08-27 18:06:00 -040079 def __init__(self, *args):
80 EditorWindow.__init__(self, *args)
81 self.text.bind("<<goto-file-line>>", self.goto_file_line)
wohlganger58fc71c2017-09-10 16:19:47 -050082 self.text.unbind("<<toggle-code-context>>")
Cheryl Sabella804a5d92018-12-28 13:15:30 -050083 self.update_menu_state('options', '*Code Context', 'disabled')
David Scherer7aced172000-08-15 01:13:23 +000084
Cheryl Sabella998f4962017-08-27 18:06:00 -040085 # Customize EditorWindow
86 def ispythonsource(self, filename):
87 "Python source is only part of output: do not colorize."
88 return False
89
90 def short_title(self):
91 "Customize EditorWindow title."
92 return "Output"
93
94 def maybesave(self):
95 "Customize EditorWindow to not display save file messagebox."
96 return 'yes' if self.get_saved() else 'no'
97
98 # Act as output file
99 def write(self, s, tags=(), mark="insert"):
100 """Write text to text widget.
101
102 The text is inserted at the given index with the provided
103 tags. The text widget is then scrolled to make it visible
104 and updated to display it, giving the effect of seeing each
105 line as it is added.
106
107 Args:
108 s: Text to insert into text widget.
109 tags: Tuple of tag strings to apply on the insert.
110 mark: Index for the insert.
111
112 Return:
113 Length of text inserted.
114 """
Tal Einat9ebe8792018-12-07 08:32:21 +0200115 if isinstance(s, bytes):
Cheryl Sabella998f4962017-08-27 18:06:00 -0400116 s = s.decode(iomenu.encoding, "replace")
117 self.text.insert(mark, s, tags)
118 self.text.see(mark)
119 self.text.update()
120 return len(s)
121
122 def writelines(self, lines):
123 "Write each item in lines iterable."
124 for line in lines:
125 self.write(line)
126
127 def flush(self):
128 "No flushing needed as write() directly writes to widget."
129 pass
130
131 def showerror(self, *args, **kwargs):
132 messagebox.showerror(*args, **kwargs)
David Scherer7aced172000-08-15 01:13:23 +0000133
134 def goto_file_line(self, event=None):
Cheryl Sabella998f4962017-08-27 18:06:00 -0400135 """Handle request to open file/line.
136
137 If the selected or previous line in the output window
138 contains a file name and line number, then open that file
139 name in a new window and position on the line number.
140
141 Otherwise, display an error messagebox.
142 """
David Scherer7aced172000-08-15 01:13:23 +0000143 line = self.text.get("insert linestart", "insert lineend")
Cheryl Sabella998f4962017-08-27 18:06:00 -0400144 result = file_line_helper(line)
David Scherer7aced172000-08-15 01:13:23 +0000145 if not result:
146 # Try the previous line. This is handy e.g. in tracebacks,
147 # where you tend to right-click on the displayed source line
148 line = self.text.get("insert -1line linestart",
149 "insert -1line lineend")
Cheryl Sabella998f4962017-08-27 18:06:00 -0400150 result = file_line_helper(line)
David Scherer7aced172000-08-15 01:13:23 +0000151 if not result:
Cheryl Sabella998f4962017-08-27 18:06:00 -0400152 self.showerror(
David Scherer7aced172000-08-15 01:13:23 +0000153 "No special line",
154 "The line you point at doesn't look like "
155 "a valid file name followed by a line number.",
Terry Jan Reedy3be2e542015-09-25 22:22:55 -0400156 parent=self.text)
David Scherer7aced172000-08-15 01:13:23 +0000157 return
158 filename, lineno = result
Cheryl Sabella998f4962017-08-27 18:06:00 -0400159 self.flist.gotofileline(filename, lineno)
David Scherer7aced172000-08-15 01:13:23 +0000160
David Scherer7aced172000-08-15 01:13:23 +0000161
Kurt B. Kaiser969de452002-06-12 03:28:57 +0000162# These classes are currently not used but might come in handy
David Scherer7aced172000-08-15 01:13:23 +0000163class OnDemandOutputWindow:
David Scherer7aced172000-08-15 01:13:23 +0000164
165 tagdefs = {
166 # XXX Should use IdlePrefs.ColorPrefs
David Scherer7aced172000-08-15 01:13:23 +0000167 "stdout": {"foreground": "blue"},
Kurt B. Kaiser969de452002-06-12 03:28:57 +0000168 "stderr": {"foreground": "#007700"},
169 }
170
David Scherer7aced172000-08-15 01:13:23 +0000171 def __init__(self, flist):
172 self.flist = flist
173 self.owin = None
David Scherer7aced172000-08-15 01:13:23 +0000174
Kurt B. Kaiser969de452002-06-12 03:28:57 +0000175 def write(self, s, tags, mark):
176 if not self.owin:
David Scherer7aced172000-08-15 01:13:23 +0000177 self.setup()
178 self.owin.write(s, tags, mark)
179
David Scherer7aced172000-08-15 01:13:23 +0000180 def setup(self):
Kurt B. Kaiser969de452002-06-12 03:28:57 +0000181 self.owin = owin = OutputWindow(self.flist)
David Scherer7aced172000-08-15 01:13:23 +0000182 text = owin.text
David Scherer7aced172000-08-15 01:13:23 +0000183 for tag, cnf in self.tagdefs.items():
184 if cnf:
Raymond Hettinger931237e2003-07-09 18:48:24 +0000185 text.tag_configure(tag, **cnf)
David Scherer7aced172000-08-15 01:13:23 +0000186 text.tag_raise('sel')
Kurt B. Kaiser969de452002-06-12 03:28:57 +0000187 self.write = self.owin.write
Cheryl Sabella998f4962017-08-27 18:06:00 -0400188
189if __name__ == '__main__':
Terry Jan Reedy4d921582018-06-19 19:12:52 -0400190 from unittest import main
191 main('idlelib.idle_test.test_outwin', verbosity=2, exit=False)