@@ -372,7 +372,6 @@ def clean_up_current_line_for_exit(self):
372
372
def process_event (self , e ):
373
373
"""Returns True if shutting down, otherwise returns None.
374
374
Mostly mutates state of Repl object"""
375
- # for a full list of what should have pretty names, try python -m curtsies.events
376
375
377
376
logger .debug ("processing event %r" , e )
378
377
if isinstance (e , events .Event ):
@@ -425,30 +424,15 @@ def proccess_control_event(self, e):
425
424
raise ValueError ("don't know how to handle this event type: %r" % e )
426
425
427
426
def process_key_event (self , e ):
427
+ # To find the curtsies name for a keypress, try python -m curtsies.events
428
+ if self .status_bar .has_focus : return self .status_bar .process_event (e )
429
+ if self .stdin .has_focus : return self .stdin .process_event (e )
428
430
429
- if self .status_bar .has_focus :
430
- return self .status_bar .process_event (e )
431
-
432
- elif self .stdin .has_focus :
433
- return self .stdin .process_event (e )
434
-
435
- elif e in key_dispatch [self .config .toggle_file_watch_key ]:
436
- if self .watcher :
437
- msg = "Auto-reloading active, watching for file changes..."
438
- if self .watching_files :
439
- self .watcher .deactivate ()
440
- self .watching_files = False
441
- self .status_bar .pop_permanent_message (msg )
442
- else :
443
- self .watching_files = True
444
- self .status_bar .push_permanent_message (msg )
445
- self .watcher .activate ()
446
- else :
447
- self .status_bar .message ('Autoreloading not available because watchdog not installed' )
431
+ if e in key_dispatch [self .config .toggle_file_watch_key ]:
432
+ return self .toggle_file_watch ()
448
433
449
434
elif e in key_dispatch [self .config .reimport_key ]:
450
435
self .clear_modules_and_reevaluate ()
451
- self .status_bar .message ('Reloaded at ' + time .strftime ('%H:%M:%S' ) + ' by user' )
452
436
453
437
elif (e in ("<RIGHT>" , '<Ctrl-f>' ) and self .config .curtsies_right_arrow_completion
454
438
and self .cursor_offset == len (self .current_line )):
@@ -457,75 +441,46 @@ def process_key_event(self, e):
457
441
458
442
elif e in self .rl_char_sequences :
459
443
self .cursor_offset , self .current_line = self .rl_char_sequences [e ](self .cursor_offset , self .current_line )
460
- self .rl_history .reset ()
461
444
462
445
# readline history commands
463
446
elif e in ("<UP>" ,) + key_dispatch [self .config .up_one_line_key ]:
464
447
self .rl_history .enter (self .current_line )
465
- self .current_line = self .rl_history .back (False ,
466
- search = self . config . curtsies_right_arrow_completion )
467
- self .cursor_offset = len (self .current_line )
448
+ self ._set_current_line ( self .rl_history .back (False , search = self . config . curtsies_right_arrow_completion ) ,
449
+ reset_rl_history = False )
450
+ self ._set_cursor_offset ( len (self .current_line ), reset_rl_history = False )
468
451
469
452
elif e in ("<DOWN>" ,) + key_dispatch [self .config .down_one_line_key ]:
470
453
self .rl_history .enter (self .current_line )
471
- self .current_line = self .rl_history .forward (False ,
472
- search = self .config .curtsies_right_arrow_completion )
473
- self .cursor_offset = len (self .current_line )
474
-
475
- elif e in key_dispatch [self .config .search_key ]: #TODO Not Implemented
476
- pass
477
- #TODO add rest of history commands
478
-
479
- # Need to figure out what these are, but I think they belong in manual_realine
480
- # under slightly different names
481
- elif e in key_dispatch [self .config .cut_to_buffer_key ]: #TODO Not Implemented
482
- pass
483
- elif e in key_dispatch [self .config .yank_from_buffer_key ]: #TODO Not Implemented
484
- pass
454
+ self ._set_current_line (self .rl_history .forward (False , search = self .config .curtsies_right_arrow_completion ),
455
+ reset_rl_history = False )
456
+ self ._set_cursor_offset (len (self .current_line ), reset_rl_history = False )
485
457
486
458
elif e in key_dispatch [self .config .clear_screen_key ]:
487
459
self .request_paint_to_clear_screen = True
488
- elif e in key_dispatch [self .config .last_output_key ]: #TODO Not Implemented
489
- pass
490
460
elif e in key_dispatch [self .config .show_source_key ]:
491
- source = self .get_source_of_current_name ()
492
- if source is None :
493
- self .status_bar .message (_ ('Cannot show source.' ))
494
- else :
495
- if self .config .highlight_show_source :
496
- source = format (PythonLexer ().get_tokens (source ), TerminalFormatter ())
497
- self .pager (source )
461
+ self .show_source ()
498
462
elif e in key_dispatch [self .config .help_key ]:
499
463
self .pager (self .help_text ())
500
464
elif e in key_dispatch [self .config .suspend_key ]:
501
465
raise SystemExit ()
502
466
elif e in ("<Ctrl-d>" ,):
503
- if self .current_line == '' :
504
- raise SystemExit ()
505
- else :
506
- self .current_line = self .current_line [:self .cursor_offset ] + self .current_line [self .cursor_offset + 1 :]
507
- self .rl_history .reset ()
467
+ self .on_control_d ()
508
468
elif e in key_dispatch [self .config .exit_key ]:
509
- raise SystemExit ()
469
+ raise SystemExit ()
510
470
elif e in ("\n " , "\r " , "<PADENTER>" , "<Ctrl-j>" , "<Ctrl-m>" ):
511
471
self .on_enter ()
512
472
elif e == '<TAB>' : # tab
513
473
self .on_tab ()
514
- self .rl_history .reset ()
515
474
elif e in ("<Shift-TAB>" ,):
516
475
self .on_tab (back = True )
517
- self .rl_history .reset ()
518
476
elif e in key_dispatch [self .config .undo_key ]: #ctrl-r for undo
519
477
self .undo ()
520
478
elif e in key_dispatch [self .config .save_key ]: # ctrl-s for save
521
- g = greenlet .greenlet (self .write2file )
522
- g .switch ()
479
+ greenlet .greenlet (self .write2file ).switch ()
523
480
elif e in key_dispatch [self .config .pastebin_key ]: # F8 for pastebin
524
- g = greenlet .greenlet (self .pastebin )
525
- g .switch ()
481
+ greenlet .greenlet (self .pastebin ).switch ()
526
482
elif e in key_dispatch [self .config .external_editor_key ]:
527
483
self .send_session_to_external_editor ()
528
- self .rl_history .reset ()
529
484
#TODO add PAD keys hack as in bpython.cli
530
485
elif e in key_dispatch [self .config .edit_current_block_key ]:
531
486
self .send_current_block_to_external_editor ()
@@ -535,7 +490,6 @@ def process_key_event(self, e):
535
490
self .add_normal_character (' ' )
536
491
else :
537
492
self .add_normal_character (e )
538
- self .rl_history .reset ()
539
493
540
494
def on_enter (self , insert_into_history = True ):
541
495
self .cursor_offset = - 1 # so the cursor isn't touching a paren
@@ -588,6 +542,12 @@ def only_whitespace_left_of_cursor():
588
542
self ._cursor_offset , self ._current_line = self .matches_iter .cur_line ()
589
543
# using _current_line so we don't trigger a completion reset
590
544
545
+ def on_control_d (self ):
546
+ if self .current_line == '' :
547
+ raise SystemExit ()
548
+ else :
549
+ self .current_line = self .current_line [:self .cursor_offset ] + self .current_line [self .cursor_offset + 1 :]
550
+
591
551
def process_simple_keypress (self , e ):
592
552
if e in (u"<Ctrl-j>" , u"<Ctrl-m>" , u"<PADENTER>" ):
593
553
self .on_enter ()
@@ -633,6 +593,21 @@ def clear_modules_and_reevaluate(self):
633
593
del sys .modules [modname ]
634
594
self .reevaluate (insert_into_history = True )
635
595
self .cursor_offset , self .current_line = cursor , line
596
+ self .status_bar .message ('Reloaded at ' + time .strftime ('%H:%M:%S' ) + ' by user' )
597
+
598
+ def toggle_file_watch (self ):
599
+ if self .watcher :
600
+ msg = "Auto-reloading active, watching for file changes..."
601
+ if self .watching_files :
602
+ self .watcher .deactivate ()
603
+ self .watching_files = False
604
+ self .status_bar .pop_permanent_message (msg )
605
+ else :
606
+ self .watching_files = True
607
+ self .status_bar .push_permanent_message (msg )
608
+ self .watcher .activate ()
609
+ else :
610
+ self .status_bar .message ('Autoreloading not available because watchdog not installed' )
636
611
637
612
## Handler Helpers
638
613
def add_normal_character (self , char ):
@@ -650,12 +625,9 @@ def update_completion(self, tab=False):
650
625
"""Update visible docstring and matches, and possibly hide/show completion box"""
651
626
#Update autocomplete info; self.matches_iter and self.argspec
652
627
#Should be called whenever the completion box might need to appear / dissapear
653
- # * when current line changes, unless via selecting a match
654
- # * when cursor position changes
655
- # *
628
+ #when current line or cursor offset changes, unless via selecting a match
656
629
self .current_match = None
657
630
self .list_win_visible = BpythonRepl .complete (self , tab )
658
- #look for history stuff
659
631
660
632
def push (self , line , insert_into_history = True ):
661
633
"""Push a line of code onto the buffer, start running the buffer
@@ -1046,15 +1018,22 @@ def __repr__(self):
1046
1018
1047
1019
def _get_current_line (self ):
1048
1020
return self ._current_line
1049
- def _set_current_line (self , line ):
1021
+ def _set_current_line (self , line , update_completion = True , reset_rl_history = True ):
1050
1022
self ._current_line = line
1051
- self .update_completion ()
1023
+ if update_completion :
1024
+ self .update_completion ()
1025
+ if reset_rl_history :
1026
+ self .rl_history .reset ()
1052
1027
current_line = property (_get_current_line , _set_current_line , None ,
1053
1028
"The current line" )
1054
1029
def _get_cursor_offset (self ):
1055
1030
return self ._cursor_offset
1056
- def _set_cursor_offset (self , line ):
1057
- self ._cursor_offset = line
1031
+ def _set_cursor_offset (self , offset , update_completion = True , reset_rl_history = True ):
1032
+ if update_completion :
1033
+ self .update_completion ()
1034
+ if reset_rl_history :
1035
+ self .rl_history .reset ()
1036
+ self ._cursor_offset = offset
1058
1037
self .update_completion ()
1059
1038
cursor_offset = property (_get_cursor_offset , _set_cursor_offset , None ,
1060
1039
"The current cursor offset from the front of the line" )
@@ -1137,6 +1116,15 @@ def pager(self, text):
1137
1116
tmp .flush ()
1138
1117
self .focus_on_subprocess (command + [tmp .name ])
1139
1118
1119
+ def show_source (self ):
1120
+ source = self .get_source_of_current_name ()
1121
+ if source is None :
1122
+ self .status_bar .message (_ ('Cannot show source.' ))
1123
+ else :
1124
+ if self .config .highlight_show_source :
1125
+ source = format (PythonLexer ().get_tokens (source ), TerminalFormatter ())
1126
+ self .pager (source )
1127
+
1140
1128
def help_text (self ):
1141
1129
return self .version_help_text () + '\n ' + self .key_help_text ()
1142
1130
0 commit comments