You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.1 KiB

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