Skip to content

Commit 9b64a08

Browse files
cleaned up process_event more
1 parent 9a6d7ca commit 9b64a08

File tree

1 file changed

+58
-70
lines changed

1 file changed

+58
-70
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 58 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ def clean_up_current_line_for_exit(self):
372372
def process_event(self, e):
373373
"""Returns True if shutting down, otherwise returns None.
374374
Mostly mutates state of Repl object"""
375-
# for a full list of what should have pretty names, try python -m curtsies.events
376375

377376
logger.debug("processing event %r", e)
378377
if isinstance(e, events.Event):
@@ -425,30 +424,15 @@ def proccess_control_event(self, e):
425424
raise ValueError("don't know how to handle this event type: %r" % e)
426425

427426
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)
428430

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()
448433

449434
elif e in key_dispatch[self.config.reimport_key]:
450435
self.clear_modules_and_reevaluate()
451-
self.status_bar.message('Reloaded at ' + time.strftime('%H:%M:%S') + ' by user')
452436

453437
elif (e in ("<RIGHT>", '<Ctrl-f>') and self.config.curtsies_right_arrow_completion
454438
and self.cursor_offset == len(self.current_line)):
@@ -457,75 +441,46 @@ def process_key_event(self, e):
457441

458442
elif e in self.rl_char_sequences:
459443
self.cursor_offset, self.current_line = self.rl_char_sequences[e](self.cursor_offset, self.current_line)
460-
self.rl_history.reset()
461444

462445
# readline history commands
463446
elif e in ("<UP>",) + key_dispatch[self.config.up_one_line_key]:
464447
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)
468451

469452
elif e in ("<DOWN>",) + key_dispatch[self.config.down_one_line_key]:
470453
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)
485457

486458
elif e in key_dispatch[self.config.clear_screen_key]:
487459
self.request_paint_to_clear_screen = True
488-
elif e in key_dispatch[self.config.last_output_key]: #TODO Not Implemented
489-
pass
490460
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()
498462
elif e in key_dispatch[self.config.help_key]:
499463
self.pager(self.help_text())
500464
elif e in key_dispatch[self.config.suspend_key]:
501465
raise SystemExit()
502466
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()
508468
elif e in key_dispatch[self.config.exit_key]:
509-
raise SystemExit()
469+
raise SystemExit()
510470
elif e in ("\n", "\r", "<PADENTER>", "<Ctrl-j>", "<Ctrl-m>"):
511471
self.on_enter()
512472
elif e == '<TAB>': # tab
513473
self.on_tab()
514-
self.rl_history.reset()
515474
elif e in ("<Shift-TAB>",):
516475
self.on_tab(back=True)
517-
self.rl_history.reset()
518476
elif e in key_dispatch[self.config.undo_key]: #ctrl-r for undo
519477
self.undo()
520478
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()
523480
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()
526482
elif e in key_dispatch[self.config.external_editor_key]:
527483
self.send_session_to_external_editor()
528-
self.rl_history.reset()
529484
#TODO add PAD keys hack as in bpython.cli
530485
elif e in key_dispatch[self.config.edit_current_block_key]:
531486
self.send_current_block_to_external_editor()
@@ -535,7 +490,6 @@ def process_key_event(self, e):
535490
self.add_normal_character(' ')
536491
else:
537492
self.add_normal_character(e)
538-
self.rl_history.reset()
539493

540494
def on_enter(self, insert_into_history=True):
541495
self.cursor_offset = -1 # so the cursor isn't touching a paren
@@ -588,6 +542,12 @@ def only_whitespace_left_of_cursor():
588542
self._cursor_offset, self._current_line = self.matches_iter.cur_line()
589543
# using _current_line so we don't trigger a completion reset
590544

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+
591551
def process_simple_keypress(self, e):
592552
if e in (u"<Ctrl-j>", u"<Ctrl-m>", u"<PADENTER>"):
593553
self.on_enter()
@@ -633,6 +593,21 @@ def clear_modules_and_reevaluate(self):
633593
del sys.modules[modname]
634594
self.reevaluate(insert_into_history=True)
635595
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')
636611

637612
## Handler Helpers
638613
def add_normal_character(self, char):
@@ -650,12 +625,9 @@ def update_completion(self, tab=False):
650625
"""Update visible docstring and matches, and possibly hide/show completion box"""
651626
#Update autocomplete info; self.matches_iter and self.argspec
652627
#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
656629
self.current_match = None
657630
self.list_win_visible = BpythonRepl.complete(self, tab)
658-
#look for history stuff
659631

660632
def push(self, line, insert_into_history=True):
661633
"""Push a line of code onto the buffer, start running the buffer
@@ -1046,15 +1018,22 @@ def __repr__(self):
10461018

10471019
def _get_current_line(self):
10481020
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):
10501022
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()
10521027
current_line = property(_get_current_line, _set_current_line, None,
10531028
"The current line")
10541029
def _get_cursor_offset(self):
10551030
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
10581037
self.update_completion()
10591038
cursor_offset = property(_get_cursor_offset, _set_cursor_offset, None,
10601039
"The current cursor offset from the front of the line")
@@ -1137,6 +1116,15 @@ def pager(self, text):
11371116
tmp.flush()
11381117
self.focus_on_subprocess(command + [tmp.name])
11391118

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+
11401128
def help_text(self):
11411129
return self.version_help_text() + '\n' + self.key_help_text()
11421130

0 commit comments

Comments
 (0)