@@ -1022,6 +1022,36 @@ def send_to_external_editor(self, text, filename=None):
10221022 else :
10231023 return text
10241024
1025+ def open_in_external_editor (self , filename ):
1026+ editor_args = shlex .split (self .config .editor )
1027+ if subprocess .call (editor_args + [filename ]) == 0 :
1028+ return True
1029+ return False
1030+
1031+ def edit_config (self ):
1032+ if not (os .path .isfile (self .config .config_path )):
1033+ if self .interact .confirm (_ ("Config file does not exist - create new from default? (y/N)" )):
1034+ try :
1035+ bpython_dir , script_name = os .path .split (__file__ )
1036+ with open (os .path .join (bpython_dir , "sample-config" )) as f :
1037+ default_config = f .read ()
1038+
1039+ containing_dir = os .path .dirname (os .path .abspath (self .config .config_path ))
1040+ if not os .path .exists (containing_dir ):
1041+ os .makedirs (containing_dir )
1042+ with open (self .config .config_path , 'w' ) as f :
1043+ f .write (default_config )
1044+ except (IOError , OSError ) as e :
1045+ self .interact .notify ('error creating file: %r' % e )
1046+ return False
1047+ else :
1048+ return False
1049+
1050+ if self .open_in_external_editor (self .config .config_path ):
1051+ self .interact .notify ('bpython config file edited. Restart bpython for changes to take effect.' )
1052+ else :
1053+ self .interact .notify ('error editing config file' )
1054+
10251055
10261056def next_indentation (line , tab_length ):
10271057 """Given a code line, return the indentation of the next line."""
0 commit comments