@@ -277,9 +277,15 @@ def process_event(self, e):
277277 self .update_completion ()
278278 return
279279 elif isinstance (e , events .PasteEvent ):
280+ ctrl_char = compress_paste_event (e )
281+ if ctrl_char is not None :
282+ return self .process_event (ctrl_char )
280283 with self .in_paste_mode ():
281284 for ee in e .events :
282- self .process_simple_event (ee )
285+ if self .stdin .has_focus :
286+ self .stdin .process_event (ee )
287+ else :
288+ self .process_simple_event (ee )
283289 self .update_completion ()
284290
285291 elif e in self .rl_char_sequences :
@@ -416,7 +422,8 @@ def process_simple_event(self, e):
416422 elif isinstance (e , events .Event ):
417423 pass # ignore events
418424 else :
419- self .add_normal_character (e if len (e ) == 1 else e [- 1 ]) #strip control seq
425+ if len (e ) == 1 :
426+ self .add_normal_character (e if len (e ) == 1 else e [- 1 ]) #strip control seq
420427
421428 def send_current_block_to_external_editor (self , filename = None ):
422429 text = self .send_to_external_editor (self .get_current_block ())
@@ -923,6 +930,22 @@ def getstdout(self):
923930 s = '\n ' .join ([x .s if isinstance (x , FmtStr ) else x for x in lines ]
924931 ) if lines else ''
925932 return s
933+
934+ def compress_paste_event (paste_event ):
935+ """If all events in a paste event are identical and not simple characters, returns one of them
936+
937+ Useful for when the UI is running so slowly that repeated keypresses end up in a paste event.
938+ If we value not getting delayed and assume the user is holding down a key to produce such frequent
939+ key events, it makes sense to drop some of the events.
940+ """
941+ if not all (paste_event .events [0 ] == e for e in paste_event .events ):
942+ return None
943+ event = paste_event .events [0 ]
944+ if len (event ) > 1 :# basically "is there a special curses names for this key?"
945+ return event
946+ else :
947+ return None
948+
926949def simple_repl ():
927950 refreshes = []
928951 def request_refresh ():
0 commit comments