@@ -338,7 +338,7 @@ def process_event(self, e):
338338 g = greenlet .greenlet (self .pastebin )
339339 g .switch ()
340340 elif e in key_dispatch [self .config .external_editor_key ]:
341- self .send_to_external_editor ()
341+ self .send_session_to_external_editor ()
342342 #TODO add PAD keys hack as in bpython.cli
343343 elif e in ["\x18 " ]:
344344 self .send_current_block_to_external_editor ()
@@ -861,19 +861,13 @@ def getstdout(self):
861861 s = '\n ' .join ([x .s if isinstance (x , FmtStr ) else x for x in lines ]
862862 ) if lines else ''
863863 return s
864- def send_to_external_editor (self , filename = None ):
865- editor = os .environ .get ('VISUAL' , os .environ .get ('EDITOR' , 'vim' ))
866- editor_args = editor .split ()
867- text = self .getstdout ()
868- with tempfile .NamedTemporaryFile (suffix = '.py' ) as temp :
869- temp .write ('### current bpython session - file will be reevaluated, ### lines will not be run\n ' .encode ('utf8' ))
870- temp .write ('\n ' .join (line [4 :] if line [:4 ] in ('... ' , '>>> ' ) else '### ' + line
871- for line in text .split ('\n ' )).encode ('utf8' ))
872- temp .flush ()
873- subprocess .call (editor_args + [temp .name ])
874- lines = open (temp .name ).read ().split ('\n ' )
875- self .history = [line for line in lines
876- if line [:4 ] != '### ' ]
864+ def send_session_to_external_editor (self , filename = None ):
865+ for_editor = '### current bpython session - file will be reevaluated, ### lines will not be run\n ' .encode ('utf8' )
866+ for_editor += ('\n ' .join (line [4 :] if line [:4 ] in ('... ' , '>>> ' ) else '### ' + line
867+ for line in self .getstdout ().split ('\n ' )).encode ('utf8' ))
868+ text = self .send_to_external_editor (for_editor )
869+ lines = open (text ).split ('\n ' )
870+ self .history = [line for line in lines if line [:4 ] != '### ' ]
877871 self .reevaluate (insert_into_history = True )
878872 self ._current_line = lines [- 1 ][4 :]
879873 self .cursor_offset_in_line = len (self ._current_line )
@@ -892,18 +886,12 @@ def clear_current_block(self):
892886 self .done = True
893887
894888 def send_current_block_to_external_editor (self , filename = None ):
895- editor = os .environ .get ('VISUAL' , os .environ .get ('EDITOR' , 'vim' ))
896- editor_args = editor .split ()
897- text = self .get_current_block ()
889+ text = self .send_to_external_editor (self .get_current_block ())
890+ lines = [line for line in text .split ('\n ' )]
891+ while not lines [- 1 ].split ():
892+ lines .pop ()
893+ events = '\n ' .join (lines + (['' ] if len (lines ) == 1 else ['' , '' ]))
898894 self .clear_current_block ()
899- with tempfile .NamedTemporaryFile (suffix = '.py' ) as temp :
900- temp .write (text .encode ('utf8' ))
901- temp .flush ()
902- subprocess .call (editor_args + [temp .name ])
903- lines = [line for line in open (temp .name ).read ().split ('\n ' )]
904- while not lines [- 1 ].split ():
905- lines .pop ()
906- events = '\n ' .join (lines + (['' ] if len (lines ) == 1 else ['' , '' ]))
907895 self .paste_mode = True
908896 for e in events :
909897 self .process_simple_event (e )
0 commit comments