editor.py 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. import importlib.abc
  2. import importlib.util
  3. import os
  4. import platform
  5. import re
  6. import string
  7. import sys
  8. import tokenize
  9. import traceback
  10. import webbrowser
  11. from tkinter import *
  12. from tkinter.font import Font
  13. from tkinter.ttk import Scrollbar
  14. from tkinter import simpledialog
  15. from tkinter import messagebox
  16. from idlelib.config import idleConf
  17. from idlelib import configdialog
  18. from idlelib import grep
  19. from idlelib import help
  20. from idlelib import help_about
  21. from idlelib import macosx
  22. from idlelib.multicall import MultiCallCreator
  23. from idlelib import pyparse
  24. from idlelib import query
  25. from idlelib import replace
  26. from idlelib import search
  27. from idlelib.tree import wheel_event
  28. from idlelib.util import py_extensions
  29. from idlelib import window
  30. # The default tab setting for a Text widget, in average-width characters.
  31. TK_TABWIDTH_DEFAULT = 8
  32. _py_version = ' (%s)' % platform.python_version()
  33. darwin = sys.platform == 'darwin'
  34. def _sphinx_version():
  35. "Format sys.version_info to produce the Sphinx version string used to install the chm docs"
  36. major, minor, micro, level, serial = sys.version_info
  37. # TODO remove unneeded function since .chm no longer installed
  38. release = f'{major}{minor}'
  39. release += f'{micro}'
  40. if level == 'candidate':
  41. release += f'rc{serial}'
  42. elif level != 'final':
  43. release += f'{level[0]}{serial}'
  44. return release
  45. class EditorWindow:
  46. from idlelib.percolator import Percolator
  47. from idlelib.colorizer import ColorDelegator, color_config
  48. from idlelib.undo import UndoDelegator
  49. from idlelib.iomenu import IOBinding, encoding
  50. from idlelib import mainmenu
  51. from idlelib.statusbar import MultiStatusBar
  52. from idlelib.autocomplete import AutoComplete
  53. from idlelib.autoexpand import AutoExpand
  54. from idlelib.calltip import Calltip
  55. from idlelib.codecontext import CodeContext
  56. from idlelib.sidebar import LineNumbers
  57. from idlelib.format import FormatParagraph, FormatRegion, Indents, Rstrip
  58. from idlelib.parenmatch import ParenMatch
  59. from idlelib.zoomheight import ZoomHeight
  60. filesystemencoding = sys.getfilesystemencoding() # for file names
  61. help_url = None
  62. allow_code_context = True
  63. allow_line_numbers = True
  64. user_input_insert_tags = None
  65. def __init__(self, flist=None, filename=None, key=None, root=None):
  66. # Delay import: runscript imports pyshell imports EditorWindow.
  67. from idlelib.runscript import ScriptBinding
  68. if EditorWindow.help_url is None:
  69. dochome = os.path.join(sys.base_prefix, 'Doc', 'index.html')
  70. if sys.platform.count('linux'):
  71. # look for html docs in a couple of standard places
  72. pyver = 'python-docs-' + '%s.%s.%s' % sys.version_info[:3]
  73. if os.path.isdir('/var/www/html/python/'): # "python2" rpm
  74. dochome = '/var/www/html/python/index.html'
  75. else:
  76. basepath = '/usr/share/doc/' # standard location
  77. dochome = os.path.join(basepath, pyver,
  78. 'Doc', 'index.html')
  79. elif sys.platform[:3] == 'win':
  80. import winreg # Windows only, block only executed once.
  81. docfile = ''
  82. KEY = (rf"Software\Python\PythonCore\{sys.winver}"
  83. r"\Help\Main Python Documentation")
  84. try:
  85. docfile = winreg.QueryValue(winreg.HKEY_CURRENT_USER, KEY)
  86. except FileNotFoundError:
  87. try:
  88. docfile = winreg.QueryValue(winreg.HKEY_LOCAL_MACHINE,
  89. KEY)
  90. except FileNotFoundError:
  91. pass
  92. if os.path.isfile(docfile):
  93. dochome = docfile
  94. elif sys.platform == 'darwin':
  95. # documentation may be stored inside a python framework
  96. dochome = os.path.join(sys.base_prefix,
  97. 'Resources/English.lproj/Documentation/index.html')
  98. dochome = os.path.normpath(dochome)
  99. if os.path.isfile(dochome):
  100. EditorWindow.help_url = dochome
  101. if sys.platform == 'darwin':
  102. # Safari requires real file:-URLs
  103. EditorWindow.help_url = 'file://' + EditorWindow.help_url
  104. else:
  105. EditorWindow.help_url = ("https://docs.python.org/%d.%d/"
  106. % sys.version_info[:2])
  107. self.flist = flist
  108. root = root or flist.root
  109. self.root = root
  110. self.menubar = Menu(root)
  111. self.top = top = window.ListedToplevel(root, menu=self.menubar)
  112. if flist:
  113. self.tkinter_vars = flist.vars
  114. #self.top.instance_dict makes flist.inversedict available to
  115. #configdialog.py so it can access all EditorWindow instances
  116. self.top.instance_dict = flist.inversedict
  117. else:
  118. self.tkinter_vars = {} # keys: Tkinter event names
  119. # values: Tkinter variable instances
  120. self.top.instance_dict = {}
  121. self.recent_files_path = idleConf.userdir and os.path.join(
  122. idleConf.userdir, 'recent-files.lst')
  123. self.prompt_last_line = '' # Override in PyShell
  124. self.text_frame = text_frame = Frame(top)
  125. self.vbar = vbar = Scrollbar(text_frame, name='vbar')
  126. width = idleConf.GetOption('main', 'EditorWindow', 'width', type='int')
  127. text_options = {
  128. 'name': 'text',
  129. 'padx': 5,
  130. 'wrap': 'none',
  131. 'highlightthickness': 0,
  132. 'width': width,
  133. 'tabstyle': 'wordprocessor', # new in 8.5
  134. 'height': idleConf.GetOption(
  135. 'main', 'EditorWindow', 'height', type='int'),
  136. }
  137. self.text = text = MultiCallCreator(Text)(text_frame, **text_options)
  138. self.top.focused_widget = self.text
  139. self.createmenubar()
  140. self.apply_bindings()
  141. self.top.protocol("WM_DELETE_WINDOW", self.close)
  142. self.top.bind("<<close-window>>", self.close_event)
  143. if macosx.isAquaTk():
  144. # Command-W on editor windows doesn't work without this.
  145. text.bind('<<close-window>>', self.close_event)
  146. # Some OS X systems have only one mouse button, so use
  147. # control-click for popup context menus there. For two
  148. # buttons, AquaTk defines <2> as the right button, not <3>.
  149. text.bind("<Control-Button-1>",self.right_menu_event)
  150. text.bind("<2>", self.right_menu_event)
  151. else:
  152. # Elsewhere, use right-click for popup menus.
  153. text.bind("<3>",self.right_menu_event)
  154. text.bind('<MouseWheel>', wheel_event)
  155. text.bind('<Button-4>', wheel_event)
  156. text.bind('<Button-5>', wheel_event)
  157. text.bind('<Configure>', self.handle_winconfig)
  158. text.bind("<<cut>>", self.cut)
  159. text.bind("<<copy>>", self.copy)
  160. text.bind("<<paste>>", self.paste)
  161. text.bind("<<center-insert>>", self.center_insert_event)
  162. text.bind("<<help>>", self.help_dialog)
  163. text.bind("<<python-docs>>", self.python_docs)
  164. text.bind("<<about-idle>>", self.about_dialog)
  165. text.bind("<<open-config-dialog>>", self.config_dialog)
  166. text.bind("<<open-module>>", self.open_module_event)
  167. text.bind("<<do-nothing>>", lambda event: "break")
  168. text.bind("<<select-all>>", self.select_all)
  169. text.bind("<<remove-selection>>", self.remove_selection)
  170. text.bind("<<find>>", self.find_event)
  171. text.bind("<<find-again>>", self.find_again_event)
  172. text.bind("<<find-in-files>>", self.find_in_files_event)
  173. text.bind("<<find-selection>>", self.find_selection_event)
  174. text.bind("<<replace>>", self.replace_event)
  175. text.bind("<<goto-line>>", self.goto_line_event)
  176. text.bind("<<smart-backspace>>",self.smart_backspace_event)
  177. text.bind("<<newline-and-indent>>",self.newline_and_indent_event)
  178. text.bind("<<smart-indent>>",self.smart_indent_event)
  179. self.fregion = fregion = self.FormatRegion(self)
  180. # self.fregion used in smart_indent_event to access indent_region.
  181. text.bind("<<indent-region>>", fregion.indent_region_event)
  182. text.bind("<<dedent-region>>", fregion.dedent_region_event)
  183. text.bind("<<comment-region>>", fregion.comment_region_event)
  184. text.bind("<<uncomment-region>>", fregion.uncomment_region_event)
  185. text.bind("<<tabify-region>>", fregion.tabify_region_event)
  186. text.bind("<<untabify-region>>", fregion.untabify_region_event)
  187. indents = self.Indents(self)
  188. text.bind("<<toggle-tabs>>", indents.toggle_tabs_event)
  189. text.bind("<<change-indentwidth>>", indents.change_indentwidth_event)
  190. text.bind("<Left>", self.move_at_edge_if_selection(0))
  191. text.bind("<Right>", self.move_at_edge_if_selection(1))
  192. text.bind("<<del-word-left>>", self.del_word_left)
  193. text.bind("<<del-word-right>>", self.del_word_right)
  194. text.bind("<<beginning-of-line>>", self.home_callback)
  195. if flist:
  196. flist.inversedict[self] = key
  197. if key:
  198. flist.dict[key] = self
  199. text.bind("<<open-new-window>>", self.new_callback)
  200. text.bind("<<close-all-windows>>", self.flist.close_all_callback)
  201. text.bind("<<open-class-browser>>", self.open_module_browser)
  202. text.bind("<<open-path-browser>>", self.open_path_browser)
  203. text.bind("<<open-turtle-demo>>", self.open_turtle_demo)
  204. self.set_status_bar()
  205. text_frame.pack(side=LEFT, fill=BOTH, expand=1)
  206. text_frame.rowconfigure(1, weight=1)
  207. text_frame.columnconfigure(1, weight=1)
  208. vbar['command'] = self.handle_yview
  209. vbar.grid(row=1, column=2, sticky=NSEW)
  210. text['yscrollcommand'] = vbar.set
  211. text['font'] = idleConf.GetFont(self.root, 'main', 'EditorWindow')
  212. text.grid(row=1, column=1, sticky=NSEW)
  213. text.focus_set()
  214. self.set_width()
  215. # usetabs true -> literal tab characters are used by indent and
  216. # dedent cmds, possibly mixed with spaces if
  217. # indentwidth is not a multiple of tabwidth,
  218. # which will cause Tabnanny to nag!
  219. # false -> tab characters are converted to spaces by indent
  220. # and dedent cmds, and ditto TAB keystrokes
  221. # Although use-spaces=0 can be configured manually in config-main.def,
  222. # configuration of tabs v. spaces is not supported in the configuration
  223. # dialog. IDLE promotes the preferred Python indentation: use spaces!
  224. usespaces = idleConf.GetOption('main', 'Indent',
  225. 'use-spaces', type='bool')
  226. self.usetabs = not usespaces
  227. # tabwidth is the display width of a literal tab character.
  228. # CAUTION: telling Tk to use anything other than its default
  229. # tab setting causes it to use an entirely different tabbing algorithm,
  230. # treating tab stops as fixed distances from the left margin.
  231. # Nobody expects this, so for now tabwidth should never be changed.
  232. self.tabwidth = 8 # must remain 8 until Tk is fixed.
  233. # indentwidth is the number of screen characters per indent level.
  234. # The recommended Python indentation is four spaces.
  235. self.indentwidth = self.tabwidth
  236. self.set_notabs_indentwidth()
  237. # Store the current value of the insertofftime now so we can restore
  238. # it if needed.
  239. if not hasattr(idleConf, 'blink_off_time'):
  240. idleConf.blink_off_time = self.text['insertofftime']
  241. self.update_cursor_blink()
  242. # When searching backwards for a reliable place to begin parsing,
  243. # first start num_context_lines[0] lines back, then
  244. # num_context_lines[1] lines back if that didn't work, and so on.
  245. # The last value should be huge (larger than the # of lines in a
  246. # conceivable file).
  247. # Making the initial values larger slows things down more often.
  248. self.num_context_lines = 50, 500, 5000000
  249. self.per = per = self.Percolator(text)
  250. self.undo = undo = self.UndoDelegator()
  251. per.insertfilter(undo)
  252. text.undo_block_start = undo.undo_block_start
  253. text.undo_block_stop = undo.undo_block_stop
  254. undo.set_saved_change_hook(self.saved_change_hook)
  255. # IOBinding implements file I/O and printing functionality
  256. self.io = io = self.IOBinding(self)
  257. io.set_filename_change_hook(self.filename_change_hook)
  258. self.good_load = False
  259. self.set_indentation_params(False)
  260. self.color = None # initialized below in self.ResetColorizer
  261. self.code_context = None # optionally initialized later below
  262. self.line_numbers = None # optionally initialized later below
  263. if filename:
  264. if os.path.exists(filename) and not os.path.isdir(filename):
  265. if io.loadfile(filename):
  266. self.good_load = True
  267. is_py_src = self.ispythonsource(filename)
  268. self.set_indentation_params(is_py_src)
  269. else:
  270. io.set_filename(filename)
  271. self.good_load = True
  272. self.ResetColorizer()
  273. self.saved_change_hook()
  274. self.update_recent_files_list()
  275. self.load_extensions()
  276. menu = self.menudict.get('window')
  277. if menu:
  278. end = menu.index("end")
  279. if end is None:
  280. end = -1
  281. if end >= 0:
  282. menu.add_separator()
  283. end = end + 1
  284. self.wmenu_end = end
  285. window.register_callback(self.postwindowsmenu)
  286. # Some abstractions so IDLE extensions are cross-IDE
  287. self.askinteger = simpledialog.askinteger
  288. self.askyesno = messagebox.askyesno
  289. self.showerror = messagebox.showerror
  290. # Add pseudoevents for former extension fixed keys.
  291. # (This probably needs to be done once in the process.)
  292. text.event_add('<<autocomplete>>', '<Key-Tab>')
  293. text.event_add('<<try-open-completions>>', '<KeyRelease-period>',
  294. '<KeyRelease-slash>', '<KeyRelease-backslash>')
  295. text.event_add('<<try-open-calltip>>', '<KeyRelease-parenleft>')
  296. text.event_add('<<refresh-calltip>>', '<KeyRelease-parenright>')
  297. text.event_add('<<paren-closed>>', '<KeyRelease-parenright>',
  298. '<KeyRelease-bracketright>', '<KeyRelease-braceright>')
  299. # Former extension bindings depends on frame.text being packed
  300. # (called from self.ResetColorizer()).
  301. autocomplete = self.AutoComplete(self, self.user_input_insert_tags)
  302. text.bind("<<autocomplete>>", autocomplete.autocomplete_event)
  303. text.bind("<<try-open-completions>>",
  304. autocomplete.try_open_completions_event)
  305. text.bind("<<force-open-completions>>",
  306. autocomplete.force_open_completions_event)
  307. text.bind("<<expand-word>>", self.AutoExpand(self).expand_word_event)
  308. text.bind("<<format-paragraph>>",
  309. self.FormatParagraph(self).format_paragraph_event)
  310. parenmatch = self.ParenMatch(self)
  311. text.bind("<<flash-paren>>", parenmatch.flash_paren_event)
  312. text.bind("<<paren-closed>>", parenmatch.paren_closed_event)
  313. scriptbinding = ScriptBinding(self)
  314. text.bind("<<check-module>>", scriptbinding.check_module_event)
  315. text.bind("<<run-module>>", scriptbinding.run_module_event)
  316. text.bind("<<run-custom>>", scriptbinding.run_custom_event)
  317. text.bind("<<do-rstrip>>", self.Rstrip(self).do_rstrip)
  318. self.ctip = ctip = self.Calltip(self)
  319. text.bind("<<try-open-calltip>>", ctip.try_open_calltip_event)
  320. #refresh-calltip must come after paren-closed to work right
  321. text.bind("<<refresh-calltip>>", ctip.refresh_calltip_event)
  322. text.bind("<<force-open-calltip>>", ctip.force_open_calltip_event)
  323. text.bind("<<zoom-height>>", self.ZoomHeight(self).zoom_height_event)
  324. if self.allow_code_context:
  325. self.code_context = self.CodeContext(self)
  326. text.bind("<<toggle-code-context>>",
  327. self.code_context.toggle_code_context_event)
  328. else:
  329. self.update_menu_state('options', '*ode*ontext', 'disabled')
  330. if self.allow_line_numbers:
  331. self.line_numbers = self.LineNumbers(self)
  332. if idleConf.GetOption('main', 'EditorWindow',
  333. 'line-numbers-default', type='bool'):
  334. self.toggle_line_numbers_event()
  335. text.bind("<<toggle-line-numbers>>", self.toggle_line_numbers_event)
  336. else:
  337. self.update_menu_state('options', '*ine*umbers', 'disabled')
  338. def handle_winconfig(self, event=None):
  339. self.set_width()
  340. def set_width(self):
  341. text = self.text
  342. inner_padding = sum(map(text.tk.getint, [text.cget('border'),
  343. text.cget('padx')]))
  344. pixel_width = text.winfo_width() - 2 * inner_padding
  345. # Divide the width of the Text widget by the font width,
  346. # which is taken to be the width of '0' (zero).
  347. # http://www.tcl.tk/man/tcl8.6/TkCmd/text.htm#M21
  348. zero_char_width = \
  349. Font(text, font=text.cget('font')).measure('0')
  350. self.width = pixel_width // zero_char_width
  351. def new_callback(self, event):
  352. dirname, basename = self.io.defaultfilename()
  353. self.flist.new(dirname)
  354. return "break"
  355. def home_callback(self, event):
  356. if (event.state & 4) != 0 and event.keysym == "Home":
  357. # state&4==Control. If <Control-Home>, use the Tk binding.
  358. return None
  359. if self.text.index("iomark") and \
  360. self.text.compare("iomark", "<=", "insert lineend") and \
  361. self.text.compare("insert linestart", "<=", "iomark"):
  362. # In Shell on input line, go to just after prompt
  363. insertpt = int(self.text.index("iomark").split(".")[1])
  364. else:
  365. line = self.text.get("insert linestart", "insert lineend")
  366. for insertpt in range(len(line)):
  367. if line[insertpt] not in (' ','\t'):
  368. break
  369. else:
  370. insertpt=len(line)
  371. lineat = int(self.text.index("insert").split('.')[1])
  372. if insertpt == lineat:
  373. insertpt = 0
  374. dest = "insert linestart+"+str(insertpt)+"c"
  375. if (event.state&1) == 0:
  376. # shift was not pressed
  377. self.text.tag_remove("sel", "1.0", "end")
  378. else:
  379. if not self.text.index("sel.first"):
  380. # there was no previous selection
  381. self.text.mark_set("my_anchor", "insert")
  382. else:
  383. if self.text.compare(self.text.index("sel.first"), "<",
  384. self.text.index("insert")):
  385. self.text.mark_set("my_anchor", "sel.first") # extend back
  386. else:
  387. self.text.mark_set("my_anchor", "sel.last") # extend forward
  388. first = self.text.index(dest)
  389. last = self.text.index("my_anchor")
  390. if self.text.compare(first,">",last):
  391. first,last = last,first
  392. self.text.tag_remove("sel", "1.0", "end")
  393. self.text.tag_add("sel", first, last)
  394. self.text.mark_set("insert", dest)
  395. self.text.see("insert")
  396. return "break"
  397. def set_status_bar(self):
  398. self.status_bar = self.MultiStatusBar(self.top)
  399. sep = Frame(self.top, height=1, borderwidth=1, background='grey75')
  400. if sys.platform == "darwin":
  401. # Insert some padding to avoid obscuring some of the statusbar
  402. # by the resize widget.
  403. self.status_bar.set_label('_padding1', ' ', side=RIGHT)
  404. self.status_bar.set_label('column', 'Col: ?', side=RIGHT)
  405. self.status_bar.set_label('line', 'Ln: ?', side=RIGHT)
  406. self.status_bar.pack(side=BOTTOM, fill=X)
  407. sep.pack(side=BOTTOM, fill=X)
  408. self.text.bind("<<set-line-and-column>>", self.set_line_and_column)
  409. self.text.event_add("<<set-line-and-column>>",
  410. "<KeyRelease>", "<ButtonRelease>")
  411. self.text.after_idle(self.set_line_and_column)
  412. def set_line_and_column(self, event=None):
  413. line, column = self.text.index(INSERT).split('.')
  414. self.status_bar.set_label('column', 'Col: %s' % column)
  415. self.status_bar.set_label('line', 'Ln: %s' % line)
  416. """ Menu definitions and functions.
  417. * self.menubar - the always visible horizontal menu bar.
  418. * mainmenu.menudefs - a list of tuples, one for each menubar item.
  419. Each tuple pairs a lower-case name and list of dropdown items.
  420. Each item is a name, virtual event pair or None for separator.
  421. * mainmenu.default_keydefs - maps events to keys.
  422. * text.keydefs - same.
  423. * cls.menu_specs - menubar name, titlecase display form pairs
  424. with Alt-hotkey indicator. A subset of menudefs items.
  425. * self.menudict - map menu name to dropdown menu.
  426. * self.recent_files_menu - 2nd level cascade in the file cascade.
  427. * self.wmenu_end - set in __init__ (purpose unclear).
  428. createmenubar, postwindowsmenu, update_menu_label, update_menu_state,
  429. ApplyKeybings (2nd part), reset_help_menu_entries,
  430. _extra_help_callback, update_recent_files_list,
  431. apply_bindings, fill_menus, (other functions?)
  432. """
  433. menu_specs = [
  434. ("file", "_File"),
  435. ("edit", "_Edit"),
  436. ("format", "F_ormat"),
  437. ("run", "_Run"),
  438. ("options", "_Options"),
  439. ("window", "_Window"),
  440. ("help", "_Help"),
  441. ]
  442. def createmenubar(self):
  443. """Populate the menu bar widget for the editor window.
  444. Each option on the menubar is itself a cascade-type Menu widget
  445. with the menubar as the parent. The names, labels, and menu
  446. shortcuts for the menubar items are stored in menu_specs. Each
  447. submenu is subsequently populated in fill_menus(), except for
  448. 'Recent Files' which is added to the File menu here.
  449. Instance variables:
  450. menubar: Menu widget containing first level menu items.
  451. menudict: Dictionary of {menuname: Menu instance} items. The keys
  452. represent the valid menu items for this window and may be a
  453. subset of all the menudefs available.
  454. recent_files_menu: Menu widget contained within the 'file' menudict.
  455. """
  456. mbar = self.menubar
  457. self.menudict = menudict = {}
  458. for name, label in self.menu_specs:
  459. underline, label = prepstr(label)
  460. postcommand = getattr(self, f'{name}_menu_postcommand', None)
  461. menudict[name] = menu = Menu(mbar, name=name, tearoff=0,
  462. postcommand=postcommand)
  463. mbar.add_cascade(label=label, menu=menu, underline=underline)
  464. if macosx.isCarbonTk():
  465. # Insert the application menu
  466. menudict['application'] = menu = Menu(mbar, name='apple',
  467. tearoff=0)
  468. mbar.add_cascade(label='IDLE', menu=menu)
  469. self.fill_menus()
  470. self.recent_files_menu = Menu(self.menubar, tearoff=0)
  471. self.menudict['file'].insert_cascade(3, label='Recent Files',
  472. underline=0,
  473. menu=self.recent_files_menu)
  474. self.base_helpmenu_length = self.menudict['help'].index(END)
  475. self.reset_help_menu_entries()
  476. def postwindowsmenu(self):
  477. """Callback to register window.
  478. Only called when Window menu exists.
  479. """
  480. menu = self.menudict['window']
  481. end = menu.index("end")
  482. if end is None:
  483. end = -1
  484. if end > self.wmenu_end:
  485. menu.delete(self.wmenu_end+1, end)
  486. window.add_windows_to_menu(menu)
  487. def update_menu_label(self, menu, index, label):
  488. "Update label for menu item at index."
  489. menuitem = self.menudict[menu]
  490. menuitem.entryconfig(index, label=label)
  491. def update_menu_state(self, menu, index, state):
  492. "Update state for menu item at index."
  493. menuitem = self.menudict[menu]
  494. menuitem.entryconfig(index, state=state)
  495. def handle_yview(self, event, *args):
  496. "Handle scrollbar."
  497. if event == 'moveto':
  498. fraction = float(args[0])
  499. lines = (round(self.getlineno('end') * fraction) -
  500. self.getlineno('@0,0'))
  501. event = 'scroll'
  502. args = (lines, 'units')
  503. self.text.yview(event, *args)
  504. return 'break'
  505. rmenu = None
  506. def right_menu_event(self, event):
  507. text = self.text
  508. newdex = text.index(f'@{event.x},{event.y}')
  509. try:
  510. in_selection = (text.compare('sel.first', '<=', newdex) and
  511. text.compare(newdex, '<=', 'sel.last'))
  512. except TclError:
  513. in_selection = False
  514. if not in_selection:
  515. text.tag_remove("sel", "1.0", "end")
  516. text.mark_set("insert", newdex)
  517. if not self.rmenu:
  518. self.make_rmenu()
  519. rmenu = self.rmenu
  520. self.event = event
  521. iswin = sys.platform[:3] == 'win'
  522. if iswin:
  523. text.config(cursor="arrow")
  524. for item in self.rmenu_specs:
  525. try:
  526. label, eventname, verify_state = item
  527. except ValueError: # see issue1207589
  528. continue
  529. if verify_state is None:
  530. continue
  531. state = getattr(self, verify_state)()
  532. rmenu.entryconfigure(label, state=state)
  533. rmenu.tk_popup(event.x_root, event.y_root)
  534. if iswin:
  535. self.text.config(cursor="ibeam")
  536. return "break"
  537. rmenu_specs = [
  538. # ("Label", "<<virtual-event>>", "statefuncname"), ...
  539. ("Close", "<<close-window>>", None), # Example
  540. ]
  541. def make_rmenu(self):
  542. rmenu = Menu(self.text, tearoff=0)
  543. for item in self.rmenu_specs:
  544. label, eventname = item[0], item[1]
  545. if label is not None:
  546. def command(text=self.text, eventname=eventname):
  547. text.event_generate(eventname)
  548. rmenu.add_command(label=label, command=command)
  549. else:
  550. rmenu.add_separator()
  551. self.rmenu = rmenu
  552. def rmenu_check_cut(self):
  553. return self.rmenu_check_copy()
  554. def rmenu_check_copy(self):
  555. try:
  556. indx = self.text.index('sel.first')
  557. except TclError:
  558. return 'disabled'
  559. else:
  560. return 'normal' if indx else 'disabled'
  561. def rmenu_check_paste(self):
  562. try:
  563. self.text.tk.call('tk::GetSelection', self.text, 'CLIPBOARD')
  564. except TclError:
  565. return 'disabled'
  566. else:
  567. return 'normal'
  568. def about_dialog(self, event=None):
  569. "Handle Help 'About IDLE' event."
  570. # Synchronize with macosx.overrideRootMenu.about_dialog.
  571. help_about.AboutDialog(self.top)
  572. return "break"
  573. def config_dialog(self, event=None):
  574. "Handle Options 'Configure IDLE' event."
  575. # Synchronize with macosx.overrideRootMenu.config_dialog.
  576. configdialog.ConfigDialog(self.top,'Settings')
  577. return "break"
  578. def help_dialog(self, event=None):
  579. "Handle Help 'IDLE Help' event."
  580. # Synchronize with macosx.overrideRootMenu.help_dialog.
  581. if self.root:
  582. parent = self.root
  583. else:
  584. parent = self.top
  585. help.show_idlehelp(parent)
  586. return "break"
  587. def python_docs(self, event=None):
  588. if sys.platform[:3] == 'win':
  589. try:
  590. os.startfile(self.help_url)
  591. except OSError as why:
  592. messagebox.showerror(title='Document Start Failure',
  593. message=str(why), parent=self.text)
  594. else:
  595. webbrowser.open(self.help_url)
  596. return "break"
  597. def cut(self,event):
  598. self.text.event_generate("<<Cut>>")
  599. return "break"
  600. def copy(self,event):
  601. if not self.text.tag_ranges("sel"):
  602. # There is no selection, so do nothing and maybe interrupt.
  603. return None
  604. self.text.event_generate("<<Copy>>")
  605. return "break"
  606. def paste(self,event):
  607. self.text.event_generate("<<Paste>>")
  608. self.text.see("insert")
  609. return "break"
  610. def select_all(self, event=None):
  611. self.text.tag_add("sel", "1.0", "end-1c")
  612. self.text.mark_set("insert", "1.0")
  613. self.text.see("insert")
  614. return "break"
  615. def remove_selection(self, event=None):
  616. self.text.tag_remove("sel", "1.0", "end")
  617. self.text.see("insert")
  618. return "break"
  619. def move_at_edge_if_selection(self, edge_index):
  620. """Cursor move begins at start or end of selection
  621. When a left/right cursor key is pressed create and return to Tkinter a
  622. function which causes a cursor move from the associated edge of the
  623. selection.
  624. """
  625. self_text_index = self.text.index
  626. self_text_mark_set = self.text.mark_set
  627. edges_table = ("sel.first+1c", "sel.last-1c")
  628. def move_at_edge(event):
  629. if (event.state & 5) == 0: # no shift(==1) or control(==4) pressed
  630. try:
  631. self_text_index("sel.first")
  632. self_text_mark_set("insert", edges_table[edge_index])
  633. except TclError:
  634. pass
  635. return move_at_edge
  636. def del_word_left(self, event):
  637. self.text.event_generate('<Meta-Delete>')
  638. return "break"
  639. def del_word_right(self, event):
  640. self.text.event_generate('<Meta-d>')
  641. return "break"
  642. def find_event(self, event):
  643. search.find(self.text)
  644. return "break"
  645. def find_again_event(self, event):
  646. search.find_again(self.text)
  647. return "break"
  648. def find_selection_event(self, event):
  649. search.find_selection(self.text)
  650. return "break"
  651. def find_in_files_event(self, event):
  652. grep.grep(self.text, self.io, self.flist)
  653. return "break"
  654. def replace_event(self, event):
  655. replace.replace(self.text)
  656. return "break"
  657. def goto_line_event(self, event):
  658. text = self.text
  659. lineno = query.Goto(
  660. text, "Go To Line",
  661. "Enter a positive integer\n"
  662. "('big' = end of file):"
  663. ).result
  664. if lineno is not None:
  665. text.tag_remove("sel", "1.0", "end")
  666. text.mark_set("insert", f'{lineno}.0')
  667. text.see("insert")
  668. self.set_line_and_column()
  669. return "break"
  670. def open_module(self):
  671. """Get module name from user and open it.
  672. Return module path or None for calls by open_module_browser
  673. when latter is not invoked in named editor window.
  674. """
  675. # XXX This, open_module_browser, and open_path_browser
  676. # would fit better in iomenu.IOBinding.
  677. try:
  678. name = self.text.get("sel.first", "sel.last").strip()
  679. except TclError:
  680. name = ''
  681. file_path = query.ModuleName(
  682. self.text, "Open Module",
  683. "Enter the name of a Python module\n"
  684. "to search on sys.path and open:",
  685. name).result
  686. if file_path is not None:
  687. if self.flist:
  688. self.flist.open(file_path)
  689. else:
  690. self.io.loadfile(file_path)
  691. return file_path
  692. def open_module_event(self, event):
  693. self.open_module()
  694. return "break"
  695. def open_module_browser(self, event=None):
  696. filename = self.io.filename
  697. if not (self.__class__.__name__ == 'PyShellEditorWindow'
  698. and filename):
  699. filename = self.open_module()
  700. if filename is None:
  701. return "break"
  702. from idlelib import browser
  703. browser.ModuleBrowser(self.root, filename)
  704. return "break"
  705. def open_path_browser(self, event=None):
  706. from idlelib import pathbrowser
  707. pathbrowser.PathBrowser(self.root)
  708. return "break"
  709. def open_turtle_demo(self, event = None):
  710. import subprocess
  711. cmd = [sys.executable,
  712. '-c',
  713. 'from turtledemo.__main__ import main; main()']
  714. subprocess.Popen(cmd, shell=False)
  715. return "break"
  716. def gotoline(self, lineno):
  717. if lineno is not None and lineno > 0:
  718. self.text.mark_set("insert", "%d.0" % lineno)
  719. self.text.tag_remove("sel", "1.0", "end")
  720. self.text.tag_add("sel", "insert", "insert +1l")
  721. self.center()
  722. def ispythonsource(self, filename):
  723. if not filename or os.path.isdir(filename):
  724. return True
  725. base, ext = os.path.splitext(os.path.basename(filename))
  726. if os.path.normcase(ext) in py_extensions:
  727. return True
  728. line = self.text.get('1.0', '1.0 lineend')
  729. return line.startswith('#!') and 'python' in line
  730. def close_hook(self):
  731. if self.flist:
  732. self.flist.unregister_maybe_terminate(self)
  733. self.flist = None
  734. def set_close_hook(self, close_hook):
  735. self.close_hook = close_hook
  736. def filename_change_hook(self):
  737. if self.flist:
  738. self.flist.filename_changed_edit(self)
  739. self.saved_change_hook()
  740. self.top.update_windowlist_registry(self)
  741. self.ResetColorizer()
  742. def _addcolorizer(self):
  743. if self.color:
  744. return
  745. if self.ispythonsource(self.io.filename):
  746. self.color = self.ColorDelegator()
  747. # can add more colorizers here...
  748. if self.color:
  749. self.per.insertfilterafter(filter=self.color, after=self.undo)
  750. def _rmcolorizer(self):
  751. if not self.color:
  752. return
  753. self.color.removecolors()
  754. self.per.removefilter(self.color)
  755. self.color = None
  756. def ResetColorizer(self):
  757. "Update the color theme"
  758. # Called from self.filename_change_hook and from configdialog.py
  759. self._rmcolorizer()
  760. self._addcolorizer()
  761. EditorWindow.color_config(self.text)
  762. if self.code_context is not None:
  763. self.code_context.update_highlight_colors()
  764. if self.line_numbers is not None:
  765. self.line_numbers.update_colors()
  766. IDENTCHARS = string.ascii_letters + string.digits + "_"
  767. def colorize_syntax_error(self, text, pos):
  768. text.tag_add("ERROR", pos)
  769. char = text.get(pos)
  770. if char and char in self.IDENTCHARS:
  771. text.tag_add("ERROR", pos + " wordstart", pos)
  772. if '\n' == text.get(pos): # error at line end
  773. text.mark_set("insert", pos)
  774. else:
  775. text.mark_set("insert", pos + "+1c")
  776. text.see(pos)
  777. def update_cursor_blink(self):
  778. "Update the cursor blink configuration."
  779. cursorblink = idleConf.GetOption(
  780. 'main', 'EditorWindow', 'cursor-blink', type='bool')
  781. if not cursorblink:
  782. self.text['insertofftime'] = 0
  783. else:
  784. # Restore the original value
  785. self.text['insertofftime'] = idleConf.blink_off_time
  786. def ResetFont(self):
  787. "Update the text widgets' font if it is changed"
  788. # Called from configdialog.py
  789. # Update the code context widget first, since its height affects
  790. # the height of the text widget. This avoids double re-rendering.
  791. if self.code_context is not None:
  792. self.code_context.update_font()
  793. # Next, update the line numbers widget, since its width affects
  794. # the width of the text widget.
  795. if self.line_numbers is not None:
  796. self.line_numbers.update_font()
  797. # Finally, update the main text widget.
  798. new_font = idleConf.GetFont(self.root, 'main', 'EditorWindow')
  799. self.text['font'] = new_font
  800. self.set_width()
  801. def RemoveKeybindings(self):
  802. """Remove the virtual, configurable keybindings.
  803. Leaves the default Tk Text keybindings.
  804. """
  805. # Called from configdialog.deactivate_current_config.
  806. self.mainmenu.default_keydefs = keydefs = idleConf.GetCurrentKeySet()
  807. for event, keylist in keydefs.items():
  808. self.text.event_delete(event, *keylist)
  809. for extensionName in self.get_standard_extension_names():
  810. xkeydefs = idleConf.GetExtensionBindings(extensionName)
  811. if xkeydefs:
  812. for event, keylist in xkeydefs.items():
  813. self.text.event_delete(event, *keylist)
  814. def ApplyKeybindings(self):
  815. """Apply the virtual, configurable keybindings.
  816. Alse update hotkeys to current keyset.
  817. """
  818. # Called from configdialog.activate_config_changes.
  819. self.mainmenu.default_keydefs = keydefs = idleConf.GetCurrentKeySet()
  820. self.apply_bindings()
  821. for extensionName in self.get_standard_extension_names():
  822. xkeydefs = idleConf.GetExtensionBindings(extensionName)
  823. if xkeydefs:
  824. self.apply_bindings(xkeydefs)
  825. # Update menu accelerators.
  826. menuEventDict = {}
  827. for menu in self.mainmenu.menudefs:
  828. menuEventDict[menu[0]] = {}
  829. for item in menu[1]:
  830. if item:
  831. menuEventDict[menu[0]][prepstr(item[0])[1]] = item[1]
  832. for menubarItem in self.menudict:
  833. menu = self.menudict[menubarItem]
  834. end = menu.index(END)
  835. if end is None:
  836. # Skip empty menus
  837. continue
  838. end += 1
  839. for index in range(0, end):
  840. if menu.type(index) == 'command':
  841. accel = menu.entrycget(index, 'accelerator')
  842. if accel:
  843. itemName = menu.entrycget(index, 'label')
  844. event = ''
  845. if menubarItem in menuEventDict:
  846. if itemName in menuEventDict[menubarItem]:
  847. event = menuEventDict[menubarItem][itemName]
  848. if event:
  849. accel = get_accelerator(keydefs, event)
  850. menu.entryconfig(index, accelerator=accel)
  851. def set_notabs_indentwidth(self):
  852. "Update the indentwidth if changed and not using tabs in this window"
  853. # Called from configdialog.py
  854. if not self.usetabs:
  855. self.indentwidth = idleConf.GetOption('main', 'Indent','num-spaces',
  856. type='int')
  857. def reset_help_menu_entries(self):
  858. """Update the additional help entries on the Help menu."""
  859. help_list = idleConf.GetAllExtraHelpSourcesList()
  860. helpmenu = self.menudict['help']
  861. # First delete the extra help entries, if any.
  862. helpmenu_length = helpmenu.index(END)
  863. if helpmenu_length > self.base_helpmenu_length:
  864. helpmenu.delete((self.base_helpmenu_length + 1), helpmenu_length)
  865. # Then rebuild them.
  866. if help_list:
  867. helpmenu.add_separator()
  868. for entry in help_list:
  869. cmd = self._extra_help_callback(entry[1])
  870. helpmenu.add_command(label=entry[0], command=cmd)
  871. # And update the menu dictionary.
  872. self.menudict['help'] = helpmenu
  873. def _extra_help_callback(self, resource):
  874. """Return a callback that loads resource (file or web page)."""
  875. def display_extra_help(helpfile=resource):
  876. if not helpfile.startswith(('www', 'http')):
  877. helpfile = os.path.normpath(helpfile)
  878. if sys.platform[:3] == 'win':
  879. try:
  880. os.startfile(helpfile)
  881. except OSError as why:
  882. messagebox.showerror(title='Document Start Failure',
  883. message=str(why), parent=self.text)
  884. else:
  885. webbrowser.open(helpfile)
  886. return display_extra_help
  887. def update_recent_files_list(self, new_file=None):
  888. "Load and update the recent files list and menus"
  889. # TODO: move to iomenu.
  890. rf_list = []
  891. file_path = self.recent_files_path
  892. if file_path and os.path.exists(file_path):
  893. with open(file_path,
  894. encoding='utf_8', errors='replace') as rf_list_file:
  895. rf_list = rf_list_file.readlines()
  896. if new_file:
  897. new_file = os.path.abspath(new_file) + '\n'
  898. if new_file in rf_list:
  899. rf_list.remove(new_file) # move to top
  900. rf_list.insert(0, new_file)
  901. # clean and save the recent files list
  902. bad_paths = []
  903. for path in rf_list:
  904. if '\0' in path or not os.path.exists(path[0:-1]):
  905. bad_paths.append(path)
  906. rf_list = [path for path in rf_list if path not in bad_paths]
  907. ulchars = "1234567890ABCDEFGHIJK"
  908. rf_list = rf_list[0:len(ulchars)]
  909. if file_path:
  910. try:
  911. with open(file_path, 'w',
  912. encoding='utf_8', errors='replace') as rf_file:
  913. rf_file.writelines(rf_list)
  914. except OSError as err:
  915. if not getattr(self.root, "recentfiles_message", False):
  916. self.root.recentfiles_message = True
  917. messagebox.showwarning(title='IDLE Warning',
  918. message="Cannot save Recent Files list to disk.\n"
  919. f" {err}\n"
  920. "Select OK to continue.",
  921. parent=self.text)
  922. # for each edit window instance, construct the recent files menu
  923. for instance in self.top.instance_dict:
  924. menu = instance.recent_files_menu
  925. menu.delete(0, END) # clear, and rebuild:
  926. for i, file_name in enumerate(rf_list):
  927. file_name = file_name.rstrip() # zap \n
  928. callback = instance.__recent_file_callback(file_name)
  929. menu.add_command(label=ulchars[i] + " " + file_name,
  930. command=callback,
  931. underline=0)
  932. def __recent_file_callback(self, file_name):
  933. def open_recent_file(fn_closure=file_name):
  934. self.io.open(editFile=fn_closure)
  935. return open_recent_file
  936. def saved_change_hook(self):
  937. short = self.short_title()
  938. long = self.long_title()
  939. if short and long:
  940. title = short + " - " + long + _py_version
  941. elif short:
  942. title = short
  943. elif long:
  944. title = long
  945. else:
  946. title = "untitled"
  947. icon = short or long or title
  948. if not self.get_saved():
  949. title = "*%s*" % title
  950. icon = "*%s" % icon
  951. self.top.wm_title(title)
  952. self.top.wm_iconname(icon)
  953. def get_saved(self):
  954. return self.undo.get_saved()
  955. def set_saved(self, flag):
  956. self.undo.set_saved(flag)
  957. def reset_undo(self):
  958. self.undo.reset_undo()
  959. def short_title(self):
  960. filename = self.io.filename
  961. return os.path.basename(filename) if filename else "untitled"
  962. def long_title(self):
  963. return self.io.filename or ""
  964. def center_insert_event(self, event):
  965. self.center()
  966. return "break"
  967. def center(self, mark="insert"):
  968. text = self.text
  969. top, bot = self.getwindowlines()
  970. lineno = self.getlineno(mark)
  971. height = bot - top
  972. newtop = max(1, lineno - height//2)
  973. text.yview(float(newtop))
  974. def getwindowlines(self):
  975. text = self.text
  976. top = self.getlineno("@0,0")
  977. bot = self.getlineno("@0,65535")
  978. if top == bot and text.winfo_height() == 1:
  979. # Geometry manager hasn't run yet
  980. height = int(text['height'])
  981. bot = top + height - 1
  982. return top, bot
  983. def getlineno(self, mark="insert"):
  984. text = self.text
  985. return int(float(text.index(mark)))
  986. def get_geometry(self):
  987. "Return (width, height, x, y)"
  988. geom = self.top.wm_geometry()
  989. m = re.match(r"(\d+)x(\d+)\+(-?\d+)\+(-?\d+)", geom)
  990. return list(map(int, m.groups()))
  991. def close_event(self, event):
  992. self.close()
  993. return "break"
  994. def maybesave(self):
  995. if self.io:
  996. if not self.get_saved():
  997. if self.top.state()!='normal':
  998. self.top.deiconify()
  999. self.top.lower()
  1000. self.top.lift()
  1001. return self.io.maybesave()
  1002. def close(self):
  1003. try:
  1004. reply = self.maybesave()
  1005. if str(reply) != "cancel":
  1006. self._close()
  1007. return reply
  1008. except AttributeError: # bpo-35379: close called twice
  1009. pass
  1010. def _close(self):
  1011. if self.io.filename:
  1012. self.update_recent_files_list(new_file=self.io.filename)
  1013. window.unregister_callback(self.postwindowsmenu)
  1014. self.unload_extensions()
  1015. self.io.close()
  1016. self.io = None
  1017. self.undo = None
  1018. if self.color:
  1019. self.color.close()
  1020. self.color = None
  1021. self.text = None
  1022. self.tkinter_vars = None
  1023. self.per.close()
  1024. self.per = None
  1025. self.top.destroy()
  1026. if self.close_hook:
  1027. # unless override: unregister from flist, terminate if last window
  1028. self.close_hook()
  1029. def load_extensions(self):
  1030. self.extensions = {}
  1031. self.load_standard_extensions()
  1032. def unload_extensions(self):
  1033. for ins in list(self.extensions.values()):
  1034. if hasattr(ins, "close"):
  1035. ins.close()
  1036. self.extensions = {}
  1037. def load_standard_extensions(self):
  1038. for name in self.get_standard_extension_names():
  1039. try:
  1040. self.load_extension(name)
  1041. except:
  1042. print("Failed to load extension", repr(name))
  1043. traceback.print_exc()
  1044. def get_standard_extension_names(self):
  1045. return idleConf.GetExtensions(editor_only=True)
  1046. extfiles = { # Map built-in config-extension section names to file names.
  1047. 'ZzDummy': 'zzdummy',
  1048. }
  1049. def load_extension(self, name):
  1050. fname = self.extfiles.get(name, name)
  1051. try:
  1052. try:
  1053. mod = importlib.import_module('.' + fname, package=__package__)
  1054. except (ImportError, TypeError):
  1055. mod = importlib.import_module(fname)
  1056. except ImportError:
  1057. print("\nFailed to import extension: ", name)
  1058. raise
  1059. cls = getattr(mod, name)
  1060. keydefs = idleConf.GetExtensionBindings(name)
  1061. if hasattr(cls, "menudefs"):
  1062. self.fill_menus(cls.menudefs, keydefs)
  1063. ins = cls(self)
  1064. self.extensions[name] = ins
  1065. if keydefs:
  1066. self.apply_bindings(keydefs)
  1067. for vevent in keydefs:
  1068. methodname = vevent.replace("-", "_")
  1069. while methodname[:1] == '<':
  1070. methodname = methodname[1:]
  1071. while methodname[-1:] == '>':
  1072. methodname = methodname[:-1]
  1073. methodname = methodname + "_event"
  1074. if hasattr(ins, methodname):
  1075. self.text.bind(vevent, getattr(ins, methodname))
  1076. def apply_bindings(self, keydefs=None):
  1077. """Add events with keys to self.text."""
  1078. if keydefs is None:
  1079. keydefs = self.mainmenu.default_keydefs
  1080. text = self.text
  1081. text.keydefs = keydefs
  1082. for event, keylist in keydefs.items():
  1083. if keylist:
  1084. text.event_add(event, *keylist)
  1085. def fill_menus(self, menudefs=None, keydefs=None):
  1086. """Fill in dropdown menus used by this window.
  1087. Items whose name begins with '!' become checkbuttons.
  1088. Other names indicate commands. None becomes a separator.
  1089. """
  1090. if menudefs is None:
  1091. menudefs = self.mainmenu.menudefs
  1092. if keydefs is None:
  1093. keydefs = self.mainmenu.default_keydefs
  1094. menudict = self.menudict
  1095. text = self.text
  1096. for mname, entrylist in menudefs:
  1097. menu = menudict.get(mname)
  1098. if not menu:
  1099. continue
  1100. for entry in entrylist:
  1101. if entry is None:
  1102. menu.add_separator()
  1103. else:
  1104. label, eventname = entry
  1105. checkbutton = (label[:1] == '!')
  1106. if checkbutton:
  1107. label = label[1:]
  1108. underline, label = prepstr(label)
  1109. accelerator = get_accelerator(keydefs, eventname)
  1110. def command(text=text, eventname=eventname):
  1111. text.event_generate(eventname)
  1112. if checkbutton:
  1113. var = self.get_var_obj(eventname, BooleanVar)
  1114. menu.add_checkbutton(label=label, underline=underline,
  1115. command=command, accelerator=accelerator,
  1116. variable=var)
  1117. else:
  1118. menu.add_command(label=label, underline=underline,
  1119. command=command,
  1120. accelerator=accelerator)
  1121. def getvar(self, name):
  1122. var = self.get_var_obj(name)
  1123. if var:
  1124. value = var.get()
  1125. return value
  1126. else:
  1127. raise NameError(name)
  1128. def setvar(self, name, value, vartype=None):
  1129. var = self.get_var_obj(name, vartype)
  1130. if var:
  1131. var.set(value)
  1132. else:
  1133. raise NameError(name)
  1134. def get_var_obj(self, eventname, vartype=None):
  1135. """Return a tkinter variable instance for the event.
  1136. """
  1137. var = self.tkinter_vars.get(eventname)
  1138. if not var and vartype:
  1139. # Create a Tkinter variable object.
  1140. self.tkinter_vars[eventname] = var = vartype(self.text)
  1141. return var
  1142. # Tk implementations of "virtual text methods" -- each platform
  1143. # reusing IDLE's support code needs to define these for its GUI's
  1144. # flavor of widget.
  1145. # Is character at text_index in a Python string? Return 0 for
  1146. # "guaranteed no", true for anything else. This info is expensive
  1147. # to compute ab initio, but is probably already known by the
  1148. # platform's colorizer.
  1149. def is_char_in_string(self, text_index):
  1150. if self.color:
  1151. # Return true iff colorizer hasn't (re)gotten this far
  1152. # yet, or the character is tagged as being in a string
  1153. return self.text.tag_prevrange("TODO", text_index) or \
  1154. "STRING" in self.text.tag_names(text_index)
  1155. else:
  1156. # The colorizer is missing: assume the worst
  1157. return 1
  1158. # If a selection is defined in the text widget, return (start,
  1159. # end) as Tkinter text indices, otherwise return (None, None)
  1160. def get_selection_indices(self):
  1161. try:
  1162. first = self.text.index("sel.first")
  1163. last = self.text.index("sel.last")
  1164. return first, last
  1165. except TclError:
  1166. return None, None
  1167. # Return the text widget's current view of what a tab stop means
  1168. # (equivalent width in spaces).
  1169. def get_tk_tabwidth(self):
  1170. current = self.text['tabs'] or TK_TABWIDTH_DEFAULT
  1171. return int(current)
  1172. # Set the text widget's current view of what a tab stop means.
  1173. def set_tk_tabwidth(self, newtabwidth):
  1174. text = self.text
  1175. if self.get_tk_tabwidth() != newtabwidth:
  1176. # Set text widget tab width
  1177. pixels = text.tk.call("font", "measure", text["font"],
  1178. "-displayof", text.master,
  1179. "n" * newtabwidth)
  1180. text.configure(tabs=pixels)
  1181. ### begin autoindent code ### (configuration was moved to beginning of class)
  1182. def set_indentation_params(self, is_py_src, guess=True):
  1183. if is_py_src and guess:
  1184. i = self.guess_indent()
  1185. if 2 <= i <= 8:
  1186. self.indentwidth = i
  1187. if self.indentwidth != self.tabwidth:
  1188. self.usetabs = False
  1189. self.set_tk_tabwidth(self.tabwidth)
  1190. def smart_backspace_event(self, event):
  1191. text = self.text
  1192. first, last = self.get_selection_indices()
  1193. if first and last:
  1194. text.delete(first, last)
  1195. text.mark_set("insert", first)
  1196. return "break"
  1197. # Delete whitespace left, until hitting a real char or closest
  1198. # preceding virtual tab stop.
  1199. chars = text.get("insert linestart", "insert")
  1200. if chars == '':
  1201. if text.compare("insert", ">", "1.0"):
  1202. # easy: delete preceding newline
  1203. text.delete("insert-1c")
  1204. else:
  1205. text.bell() # at start of buffer
  1206. return "break"
  1207. if chars[-1] not in " \t":
  1208. # easy: delete preceding real char
  1209. text.delete("insert-1c")
  1210. return "break"
  1211. # Ick. It may require *inserting* spaces if we back up over a
  1212. # tab character! This is written to be clear, not fast.
  1213. tabwidth = self.tabwidth
  1214. have = len(chars.expandtabs(tabwidth))
  1215. assert have > 0
  1216. want = ((have - 1) // self.indentwidth) * self.indentwidth
  1217. # Debug prompt is multilined....
  1218. ncharsdeleted = 0
  1219. while True:
  1220. chars = chars[:-1]
  1221. ncharsdeleted = ncharsdeleted + 1
  1222. have = len(chars.expandtabs(tabwidth))
  1223. if have <= want or chars[-1] not in " \t":
  1224. break
  1225. text.undo_block_start()
  1226. text.delete("insert-%dc" % ncharsdeleted, "insert")
  1227. if have < want:
  1228. text.insert("insert", ' ' * (want - have),
  1229. self.user_input_insert_tags)
  1230. text.undo_block_stop()
  1231. return "break"
  1232. def smart_indent_event(self, event):
  1233. # if intraline selection:
  1234. # delete it
  1235. # elif multiline selection:
  1236. # do indent-region
  1237. # else:
  1238. # indent one level
  1239. text = self.text
  1240. first, last = self.get_selection_indices()
  1241. text.undo_block_start()
  1242. try:
  1243. if first and last:
  1244. if index2line(first) != index2line(last):
  1245. return self.fregion.indent_region_event(event)
  1246. text.delete(first, last)
  1247. text.mark_set("insert", first)
  1248. prefix = text.get("insert linestart", "insert")
  1249. raw, effective = get_line_indent(prefix, self.tabwidth)
  1250. if raw == len(prefix):
  1251. # only whitespace to the left
  1252. self.reindent_to(effective + self.indentwidth)
  1253. else:
  1254. # tab to the next 'stop' within or to right of line's text:
  1255. if self.usetabs:
  1256. pad = '\t'
  1257. else:
  1258. effective = len(prefix.expandtabs(self.tabwidth))
  1259. n = self.indentwidth
  1260. pad = ' ' * (n - effective % n)
  1261. text.insert("insert", pad, self.user_input_insert_tags)
  1262. text.see("insert")
  1263. return "break"
  1264. finally:
  1265. text.undo_block_stop()
  1266. def newline_and_indent_event(self, event):
  1267. """Insert a newline and indentation after Enter keypress event.
  1268. Properly position the cursor on the new line based on information
  1269. from the current line. This takes into account if the current line
  1270. is a shell prompt, is empty, has selected text, contains a block
  1271. opener, contains a block closer, is a continuation line, or
  1272. is inside a string.
  1273. """
  1274. text = self.text
  1275. first, last = self.get_selection_indices()
  1276. text.undo_block_start()
  1277. try: # Close undo block and expose new line in finally clause.
  1278. if first and last:
  1279. text.delete(first, last)
  1280. text.mark_set("insert", first)
  1281. line = text.get("insert linestart", "insert")
  1282. # Count leading whitespace for indent size.
  1283. i, n = 0, len(line)
  1284. while i < n and line[i] in " \t":
  1285. i += 1
  1286. if i == n:
  1287. # The cursor is in or at leading indentation in a continuation
  1288. # line; just inject an empty line at the start.
  1289. text.insert("insert linestart", '\n',
  1290. self.user_input_insert_tags)
  1291. return "break"
  1292. indent = line[:i]
  1293. # Strip whitespace before insert point unless it's in the prompt.
  1294. i = 0
  1295. while line and line[-1] in " \t":
  1296. line = line[:-1]
  1297. i += 1
  1298. if i:
  1299. text.delete("insert - %d chars" % i, "insert")
  1300. # Strip whitespace after insert point.
  1301. while text.get("insert") in " \t":
  1302. text.delete("insert")
  1303. # Insert new line.
  1304. text.insert("insert", '\n', self.user_input_insert_tags)
  1305. # Adjust indentation for continuations and block open/close.
  1306. # First need to find the last statement.
  1307. lno = index2line(text.index('insert'))
  1308. y = pyparse.Parser(self.indentwidth, self.tabwidth)
  1309. if not self.prompt_last_line:
  1310. for context in self.num_context_lines:
  1311. startat = max(lno - context, 1)
  1312. startatindex = repr(startat) + ".0"
  1313. rawtext = text.get(startatindex, "insert")
  1314. y.set_code(rawtext)
  1315. bod = y.find_good_parse_start(
  1316. self._build_char_in_string_func(startatindex))
  1317. if bod is not None or startat == 1:
  1318. break
  1319. y.set_lo(bod or 0)
  1320. else:
  1321. r = text.tag_prevrange("console", "insert")
  1322. if r:
  1323. startatindex = r[1]
  1324. else:
  1325. startatindex = "1.0"
  1326. rawtext = text.get(startatindex, "insert")
  1327. y.set_code(rawtext)
  1328. y.set_lo(0)
  1329. c = y.get_continuation_type()
  1330. if c != pyparse.C_NONE:
  1331. # The current statement hasn't ended yet.
  1332. if c == pyparse.C_STRING_FIRST_LINE:
  1333. # After the first line of a string do not indent at all.
  1334. pass
  1335. elif c == pyparse.C_STRING_NEXT_LINES:
  1336. # Inside a string which started before this line;
  1337. # just mimic the current indent.
  1338. text.insert("insert", indent, self.user_input_insert_tags)
  1339. elif c == pyparse.C_BRACKET:
  1340. # Line up with the first (if any) element of the
  1341. # last open bracket structure; else indent one
  1342. # level beyond the indent of the line with the
  1343. # last open bracket.
  1344. self.reindent_to(y.compute_bracket_indent())
  1345. elif c == pyparse.C_BACKSLASH:
  1346. # If more than one line in this statement already, just
  1347. # mimic the current indent; else if initial line
  1348. # has a start on an assignment stmt, indent to
  1349. # beyond leftmost =; else to beyond first chunk of
  1350. # non-whitespace on initial line.
  1351. if y.get_num_lines_in_stmt() > 1:
  1352. text.insert("insert", indent,
  1353. self.user_input_insert_tags)
  1354. else:
  1355. self.reindent_to(y.compute_backslash_indent())
  1356. else:
  1357. assert 0, f"bogus continuation type {c!r}"
  1358. return "break"
  1359. # This line starts a brand new statement; indent relative to
  1360. # indentation of initial line of closest preceding
  1361. # interesting statement.
  1362. indent = y.get_base_indent_string()
  1363. text.insert("insert", indent, self.user_input_insert_tags)
  1364. if y.is_block_opener():
  1365. self.smart_indent_event(event)
  1366. elif indent and y.is_block_closer():
  1367. self.smart_backspace_event(event)
  1368. return "break"
  1369. finally:
  1370. text.see("insert")
  1371. text.undo_block_stop()
  1372. # Our editwin provides an is_char_in_string function that works
  1373. # with a Tk text index, but PyParse only knows about offsets into
  1374. # a string. This builds a function for PyParse that accepts an
  1375. # offset.
  1376. def _build_char_in_string_func(self, startindex):
  1377. def inner(offset, _startindex=startindex,
  1378. _icis=self.is_char_in_string):
  1379. return _icis(_startindex + "+%dc" % offset)
  1380. return inner
  1381. # XXX this isn't bound to anything -- see tabwidth comments
  1382. ## def change_tabwidth_event(self, event):
  1383. ## new = self._asktabwidth()
  1384. ## if new != self.tabwidth:
  1385. ## self.tabwidth = new
  1386. ## self.set_indentation_params(0, guess=0)
  1387. ## return "break"
  1388. # Make string that displays as n leading blanks.
  1389. def _make_blanks(self, n):
  1390. if self.usetabs:
  1391. ntabs, nspaces = divmod(n, self.tabwidth)
  1392. return '\t' * ntabs + ' ' * nspaces
  1393. else:
  1394. return ' ' * n
  1395. # Delete from beginning of line to insert point, then reinsert
  1396. # column logical (meaning use tabs if appropriate) spaces.
  1397. def reindent_to(self, column):
  1398. text = self.text
  1399. text.undo_block_start()
  1400. if text.compare("insert linestart", "!=", "insert"):
  1401. text.delete("insert linestart", "insert")
  1402. if column:
  1403. text.insert("insert", self._make_blanks(column),
  1404. self.user_input_insert_tags)
  1405. text.undo_block_stop()
  1406. # Guess indentwidth from text content.
  1407. # Return guessed indentwidth. This should not be believed unless
  1408. # it's in a reasonable range (e.g., it will be 0 if no indented
  1409. # blocks are found).
  1410. def guess_indent(self):
  1411. opener, indented = IndentSearcher(self.text).run()
  1412. if opener and indented:
  1413. raw, indentsmall = get_line_indent(opener, self.tabwidth)
  1414. raw, indentlarge = get_line_indent(indented, self.tabwidth)
  1415. else:
  1416. indentsmall = indentlarge = 0
  1417. return indentlarge - indentsmall
  1418. def toggle_line_numbers_event(self, event=None):
  1419. if self.line_numbers is None:
  1420. return
  1421. if self.line_numbers.is_shown:
  1422. self.line_numbers.hide_sidebar()
  1423. menu_label = "Show"
  1424. else:
  1425. self.line_numbers.show_sidebar()
  1426. menu_label = "Hide"
  1427. self.update_menu_label(menu='options', index='*ine*umbers',
  1428. label=f'{menu_label} Line Numbers')
  1429. # "line.col" -> line, as an int
  1430. def index2line(index):
  1431. return int(float(index))
  1432. _line_indent_re = re.compile(r'[ \t]*')
  1433. def get_line_indent(line, tabwidth):
  1434. """Return a line's indentation as (# chars, effective # of spaces).
  1435. The effective # of spaces is the length after properly "expanding"
  1436. the tabs into spaces, as done by str.expandtabs(tabwidth).
  1437. """
  1438. m = _line_indent_re.match(line)
  1439. return m.end(), len(m.group().expandtabs(tabwidth))
  1440. class IndentSearcher:
  1441. "Manage initial indent guess, returned by run method."
  1442. def __init__(self, text):
  1443. self.text = text
  1444. self.i = self.finished = 0
  1445. self.blkopenline = self.indentedline = None
  1446. def readline(self):
  1447. if self.finished:
  1448. return ""
  1449. i = self.i = self.i + 1
  1450. mark = repr(i) + ".0"
  1451. if self.text.compare(mark, ">=", "end"):
  1452. return ""
  1453. return self.text.get(mark, mark + " lineend+1c")
  1454. def tokeneater(self, type, token, start, end, line,
  1455. INDENT=tokenize.INDENT,
  1456. NAME=tokenize.NAME,
  1457. OPENERS=('class', 'def', 'for', 'if', 'match', 'try',
  1458. 'while', 'with')):
  1459. if self.finished:
  1460. pass
  1461. elif type == NAME and token in OPENERS:
  1462. self.blkopenline = line
  1463. elif type == INDENT and self.blkopenline:
  1464. self.indentedline = line
  1465. self.finished = 1
  1466. def run(self):
  1467. """Return 2 lines containing block opener and and indent.
  1468. Either the indent line or both may be None.
  1469. """
  1470. try:
  1471. tokens = tokenize.generate_tokens(self.readline)
  1472. for token in tokens:
  1473. self.tokeneater(*token)
  1474. except (tokenize.TokenError, SyntaxError):
  1475. # Stopping the tokenizer early can trigger spurious errors.
  1476. pass
  1477. return self.blkopenline, self.indentedline
  1478. ### end autoindent code ###
  1479. def prepstr(s):
  1480. """Extract the underscore from a string.
  1481. For example, prepstr("Co_py") returns (2, "Copy").
  1482. Args:
  1483. s: String with underscore.
  1484. Returns:
  1485. Tuple of (position of underscore, string without underscore).
  1486. """
  1487. i = s.find('_')
  1488. if i >= 0:
  1489. s = s[:i] + s[i+1:]
  1490. return i, s
  1491. keynames = {
  1492. 'bracketleft': '[',
  1493. 'bracketright': ']',
  1494. 'slash': '/',
  1495. }
  1496. def get_accelerator(keydefs, eventname):
  1497. """Return a formatted string for the keybinding of an event.
  1498. Convert the first keybinding for a given event to a form that
  1499. can be displayed as an accelerator on the menu.
  1500. Args:
  1501. keydefs: Dictionary of valid events to keybindings.
  1502. eventname: Event to retrieve keybinding for.
  1503. Returns:
  1504. Formatted string of the keybinding.
  1505. """
  1506. keylist = keydefs.get(eventname)
  1507. # issue10940: temporary workaround to prevent hang with OS X Cocoa Tk 8.5
  1508. # if not keylist:
  1509. if (not keylist) or (macosx.isCocoaTk() and eventname in {
  1510. "<<open-module>>",
  1511. "<<goto-line>>",
  1512. "<<change-indentwidth>>"}):
  1513. return ""
  1514. s = keylist[0]
  1515. # Convert strings of the form -singlelowercase to -singleuppercase.
  1516. s = re.sub(r"-[a-z]\b", lambda m: m.group().upper(), s)
  1517. # Convert certain keynames to their symbol.
  1518. s = re.sub(r"\b\w+\b", lambda m: keynames.get(m.group(), m.group()), s)
  1519. # Remove Key- from string.
  1520. s = re.sub("Key-", "", s)
  1521. # Convert Cancel to Ctrl-Break.
  1522. s = re.sub("Cancel", "Ctrl-Break", s) # dscherer@cmu.edu
  1523. # Convert Control to Ctrl-.
  1524. s = re.sub("Control-", "Ctrl-", s)
  1525. # Change - to +.
  1526. s = re.sub("-", "+", s)
  1527. # Change >< to space.
  1528. s = re.sub("><", " ", s)
  1529. # Remove <.
  1530. s = re.sub("<", "", s)
  1531. # Remove >.
  1532. s = re.sub(">", "", s)
  1533. return s
  1534. def fixwordbreaks(root):
  1535. # On Windows, tcl/tk breaks 'words' only on spaces, as in Command Prompt.
  1536. # We want Motif style everywhere. See #21474, msg218992 and followup.
  1537. tk = root.tk
  1538. tk.call('tcl_wordBreakAfter', 'a b', 0) # make sure word.tcl is loaded
  1539. tk.call('set', 'tcl_wordchars', r'\w')
  1540. tk.call('set', 'tcl_nonwordchars', r'\W')
  1541. def _editor_window(parent): # htest #
  1542. # error if close master window first - timer event, after script
  1543. root = parent
  1544. fixwordbreaks(root)
  1545. if sys.argv[1:]:
  1546. filename = sys.argv[1]
  1547. else:
  1548. filename = None
  1549. macosx.setupApp(root, None)
  1550. edit = EditorWindow(root=root, filename=filename)
  1551. text = edit.text
  1552. text['height'] = 10
  1553. for i in range(20):
  1554. text.insert('insert', ' '*i + str(i) + '\n')
  1555. # text.bind("<<close-all-windows>>", edit.close_event)
  1556. # Does not stop error, neither does following
  1557. # edit.text.bind("<<close-window>>", edit.close_event)
  1558. if __name__ == '__main__':
  1559. from unittest import main
  1560. main('idlelib.idle_test.test_editor', verbosity=2, exit=False)
  1561. from idlelib.idle_test.htest import run
  1562. run(_editor_window)