@@ -446,6 +446,26 @@ def set_line_and_column(self, event=None):
446
446
self .status_bar .set_label ('column' , 'Col: %s' % column )
447
447
self .status_bar .set_label ('line' , 'Ln: %s' % line )
448
448
449
+
450
+ """ Menu definitions and functions.
451
+ * self.menubar - the always visible horizontal menu bar.
452
+ * mainmenu.menudefs - a list of tuples, one for each menubar item.
453
+ Each tuple pairs a lower-case name and list of dropdown items.
454
+ Each item is a name, virtual event pair or None for separator.
455
+ * mainmenu.default_keydefs - maps events to keys.
456
+ * text.keydefs - same.
457
+ * cls.menu_specs - menubar name, titlecase display form pairs
458
+ with Alt-hotkey indicator. A subset of menudefs items.
459
+ * self.menudict - map menu name to dropdown menu.
460
+ * self.recent_files_menu - 2nd level cascade in the file cascade.
461
+ * self.wmenu_end - set in __init__ (purpose unclear).
462
+
463
+ createmenubar, postwindowsmenu, update_menu_label, update_menu_state,
464
+ ApplyKeybings (2nd part), reset_help_menu_entries,
465
+ _extra_help_callback, update_recent_files_list,
466
+ apply_bindings, fill_menus, (other functions?)
467
+ """
468
+
449
469
menu_specs = [
450
470
("file" , "_File" ),
451
471
("edit" , "_Edit" ),
@@ -456,8 +476,22 @@ def set_line_and_column(self, event=None):
456
476
("help" , "_Help" ),
457
477
]
458
478
459
-
460
479
def createmenubar (self ):
480
+ """Populate the menu bar widget for the editor window.
481
+
482
+ Each option on the menubar is itself a cascade-type Menu widget
483
+ with the menubar as the parent. The names, labels, and menu
484
+ shortcuts for the menubar items are stored in menu_specs. Each
485
+ submenu is subsequently populated in fill_menus(), except for
486
+ 'Recent Files' which is added to the File menu here.
487
+
488
+ Instance variables:
489
+ menubar: Menu widget containing first level menu items.
490
+ menudict: Dictionary of {menuname: Menu instance} items. The keys
491
+ represent the valid menu items for this window and may be a
492
+ subset of all the menudefs available.
493
+ recent_files_menu: Menu widget contained within the 'file' menudict.
494
+ """
461
495
mbar = self .menubar
462
496
self .menudict = menudict = {}
463
497
for name , label in self .menu_specs :
@@ -480,7 +514,10 @@ def createmenubar(self):
480
514
self .reset_help_menu_entries ()
481
515
482
516
def postwindowsmenu (self ):
483
- # Only called when Window menu exists
517
+ """Callback to register window.
518
+
519
+ Only called when Window menu exists.
520
+ """
484
521
menu = self .menudict ['window' ]
485
522
end = menu .index ("end" )
486
523
if end is None :
@@ -859,8 +896,11 @@ def ResetFont(self):
859
896
self .set_width ()
860
897
861
898
def RemoveKeybindings (self ):
862
- "Remove the keybindings before they are changed."
863
- # Called from configdialog.py
899
+ """Remove the virtual, configurable keybindings.
900
+
901
+ Leaves the default Tk Text keybindings.
902
+ """
903
+ # Called from configdialog.deactivate_current_config.
864
904
self .mainmenu .default_keydefs = keydefs = idleConf .GetCurrentKeySet ()
865
905
for event , keylist in keydefs .items ():
866
906
self .text .event_delete (event , * keylist )
@@ -871,15 +911,19 @@ def RemoveKeybindings(self):
871
911
self .text .event_delete (event , * keylist )
872
912
873
913
def ApplyKeybindings (self ):
874
- "Update the keybindings after they are changed"
875
- # Called from configdialog.py
914
+ """Apply the virtual, configurable keybindings.
915
+
916
+ Alse update hotkeys to current keyset.
917
+ """
918
+ # Called from configdialog.activate_config_changes.
876
919
self .mainmenu .default_keydefs = keydefs = idleConf .GetCurrentKeySet ()
877
920
self .apply_bindings ()
878
921
for extensionName in self .get_standard_extension_names ():
879
922
xkeydefs = idleConf .GetExtensionBindings (extensionName )
880
923
if xkeydefs :
881
924
self .apply_bindings (xkeydefs )
882
- #update menu accelerators
925
+
926
+ # Update menu accelerators.
883
927
menuEventDict = {}
884
928
for menu in self .mainmenu .menudefs :
885
929
menuEventDict [menu [0 ]] = {}
@@ -914,25 +958,25 @@ def set_notabs_indentwidth(self):
914
958
type = 'int' )
915
959
916
960
def reset_help_menu_entries (self ):
917
- "Update the additional help entries on the Help menu"
961
+ """ Update the additional help entries on the Help menu."" "
918
962
help_list = idleConf .GetAllExtraHelpSourcesList ()
919
963
helpmenu = self .menudict ['help' ]
920
- # first delete the extra help entries, if any
964
+ # First delete the extra help entries, if any.
921
965
helpmenu_length = helpmenu .index (END )
922
966
if helpmenu_length > self .base_helpmenu_length :
923
967
helpmenu .delete ((self .base_helpmenu_length + 1 ), helpmenu_length )
924
- # then rebuild them
968
+ # Then rebuild them.
925
969
if help_list :
926
970
helpmenu .add_separator ()
927
971
for entry in help_list :
928
- cmd = self .__extra_help_callback (entry [1 ])
972
+ cmd = self ._extra_help_callback (entry [1 ])
929
973
helpmenu .add_command (label = entry [0 ], command = cmd )
930
- # and update the menu dictionary
974
+ # And update the menu dictionary.
931
975
self .menudict ['help' ] = helpmenu
932
976
933
- def __extra_help_callback (self , helpfile ):
934
- "Create a callback with the helpfile value frozen at definition time "
935
- def display_extra_help (helpfile = helpfile ):
977
+ def _extra_help_callback (self , resource ):
978
+ """Return a callback that loads resource (file or web page)."" "
979
+ def display_extra_help (helpfile = resource ):
936
980
if not helpfile .startswith (('www' , 'http' )):
937
981
helpfile = os .path .normpath (helpfile )
938
982
if sys .platform [:3 ] == 'win' :
@@ -1158,6 +1202,7 @@ def load_extension(self, name):
1158
1202
self .text .bind (vevent , getattr (ins , methodname ))
1159
1203
1160
1204
def apply_bindings (self , keydefs = None ):
1205
+ """Add events with keys to self.text."""
1161
1206
if keydefs is None :
1162
1207
keydefs = self .mainmenu .default_keydefs
1163
1208
text = self .text
@@ -1167,9 +1212,10 @@ def apply_bindings(self, keydefs=None):
1167
1212
text .event_add (event , * keylist )
1168
1213
1169
1214
def fill_menus (self , menudefs = None , keydefs = None ):
1170
- """Add appropriate entries to the menus and submenus
1215
+ """Fill in dropdown menus used by this window.
1171
1216
1172
- Menus that are absent or None in self.menudict are ignored.
1217
+ Items whose name begins with '!' become checkbuttons.
1218
+ Other names indicate commands. None becomes a separator.
1173
1219
"""
1174
1220
if menudefs is None :
1175
1221
menudefs = self .mainmenu .menudefs
@@ -1182,7 +1228,7 @@ def fill_menus(self, menudefs=None, keydefs=None):
1182
1228
if not menu :
1183
1229
continue
1184
1230
for entry in entrylist :
1185
- if not entry :
1231
+ if entry is None :
1186
1232
menu .add_separator ()
1187
1233
else :
1188
1234
label , eventname = entry
@@ -1218,11 +1264,13 @@ def setvar(self, name, value, vartype=None):
1218
1264
else :
1219
1265
raise NameError (name )
1220
1266
1221
- def get_var_obj (self , name , vartype = None ):
1222
- var = self .tkinter_vars .get (name )
1267
+ def get_var_obj (self , eventname , vartype = None ):
1268
+ """Return a tkinter variable instance for the event.
1269
+ """
1270
+ var = self .tkinter_vars .get (eventname )
1223
1271
if not var and vartype :
1224
- # create a Tkinter variable object with self.text as master:
1225
- self .tkinter_vars [name ] = var = vartype (self .text )
1272
+ # Create a Tkinter variable object.
1273
+ self .tkinter_vars [eventname ] = var = vartype (self .text )
1226
1274
return var
1227
1275
1228
1276
# Tk implementations of "virtual text methods" -- each platform
@@ -1613,8 +1661,16 @@ def run(self):
1613
1661
### end autoindent code ###
1614
1662
1615
1663
def prepstr (s ):
1616
- # Helper to extract the underscore from a string, e.g.
1617
- # prepstr("Co_py") returns (2, "Copy").
1664
+ """Extract the underscore from a string.
1665
+
1666
+ For example, prepstr("Co_py") returns (2, "Copy").
1667
+
1668
+ Args:
1669
+ s: String with underscore.
1670
+
1671
+ Returns:
1672
+ Tuple of (position of underscore, string without underscore).
1673
+ """
1618
1674
i = s .find ('_' )
1619
1675
if i >= 0 :
1620
1676
s = s [:i ] + s [i + 1 :]
@@ -1628,6 +1684,18 @@ def prepstr(s):
1628
1684
}
1629
1685
1630
1686
def get_accelerator (keydefs , eventname ):
1687
+ """Return a formatted string for the keybinding of an event.
1688
+
1689
+ Convert the first keybinding for a given event to a form that
1690
+ can be displayed as an accelerator on the menu.
1691
+
1692
+ Args:
1693
+ keydefs: Dictionary of valid events to keybindings.
1694
+ eventname: Event to retrieve keybinding for.
1695
+
1696
+ Returns:
1697
+ Formatted string of the keybinding.
1698
+ """
1631
1699
keylist = keydefs .get (eventname )
1632
1700
# issue10940: temporary workaround to prevent hang with OS X Cocoa Tk 8.5
1633
1701
# if not keylist:
@@ -1637,14 +1705,23 @@ def get_accelerator(keydefs, eventname):
1637
1705
"<<change-indentwidth>>" }):
1638
1706
return ""
1639
1707
s = keylist [0 ]
1708
+ # Convert strings of the form -singlelowercase to -singleuppercase.
1640
1709
s = re .sub (r"-[a-z]\b" , lambda m : m .group ().upper (), s )
1710
+ # Convert certain keynames to their symbol.
1641
1711
s = re .sub (r"\b\w+\b" , lambda m : keynames .get (m .group (), m .group ()), s )
1712
+ # Remove Key- from string.
1642
1713
s = re .sub ("Key-" , "" , s )
1643
- s = re .
sub (
"Cancel" ,
"Ctrl-Break" ,
s )
# [email protected]
1714
+ # Convert Cancel to Ctrl-Break.
1715
+ s = re .
sub (
"Cancel" ,
"Ctrl-Break" ,
s )
# [email protected]
1716
+ # Convert Control to Ctrl-.
1644
1717
s = re .sub ("Control-" , "Ctrl-" , s )
1718
+ # Change - to +.
1645
1719
s = re .sub ("-" , "+" , s )
1720
+ # Change >< to space.
1646
1721
s = re .sub ("><" , " " , s )
1722
+ # Remove <.
1647
1723
s = re .sub ("<" , "" , s )
1724
+ # Remove >.
1648
1725
s = re .sub (">" , "" , s )
1649
1726
return s
1650
1727
0 commit comments