@@ -132,6 +132,8 @@ def __init__(self, flist=None, filename=None, key=None, root=None):
132132 text .bind ("<<untabify-region>>" ,self .untabify_region_event )
133133 text .bind ("<<toggle-tabs>>" ,self .toggle_tabs_event )
134134 text .bind ("<<change-indentwidth>>" ,self .change_indentwidth_event )
135+ text .bind ("<Left>" , self .move_at_edge_if_selection (0 ))
136+ text .bind ("<Right>" , self .move_at_edge_if_selection (1 ))
135137
136138 if flist :
137139 flist .inversedict [self ] = key
@@ -344,6 +346,26 @@ def remove_selection(self, event=None):
344346 self .text .tag_remove ("sel" , "1.0" , "end" )
345347 self .text .see ("insert" )
346348
349+ def move_at_edge_if_selection (self , edge_index ):
350+ """Cursor move begins at start or end of selection
351+
352+ When a left/right cursor key is pressed create and return to Tkinter a
353+ function which causes a cursor move from the associated edge of the
354+ selection.
355+
356+ """
357+ self_text_index = self .text .index
358+ self_text_mark_set = self .text .mark_set
359+ edges_table = ("sel.first+1c" , "sel.last-1c" )
360+ def move_at_edge (event ):
361+ if (event .state & 5 ) == 0 : # no shift(==1) or control(==4) pressed
362+ try :
363+ self_text_index ("sel.first" )
364+ self_text_mark_set ("insert" , edges_table [edge_index ])
365+ except TclError :
366+ pass
367+ return move_at_edge
368+
347369 def find_event (self , event ):
348370 SearchDialog .find (self .text )
349371 return "break"
0 commit comments