Skip to content

Commit 4cc3912

Browse files
F3 to edit current config file
Remaining to do: figure out how to get default config somewhere that bpython can find it (actually install it somewhere)
1 parent bd11ff7 commit 4cc3912

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

bpython/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def loadini(struct, configfile):
6969
'down_one_line': 'C-n',
7070
'exit': '',
7171
'external_editor': 'F7',
72+
'edit_config': 'F3',
7273
'edit_current_block': 'C-x',
7374
'help': 'F1',
7475
'last_output': 'F9',
@@ -99,6 +100,8 @@ def loadini(struct, configfile):
99100
"%s\n" % default_config_path())
100101
sys.exit(1)
101102

103+
struct.config_path = config_path
104+
102105
struct.dedent_after = config.getint('general', 'dedent_after')
103106
struct.tab_length = config.getint('general', 'tab_length')
104107
struct.auto_display_list = config.getboolean('general',
@@ -131,6 +134,7 @@ def loadini(struct, configfile):
131134
struct.delete_key = config.get('keyboard', 'delete')
132135
struct.exit_key = config.get('keyboard', 'exit')
133136
struct.last_output_key = config.get('keyboard', 'last_output')
137+
struct.edit_config_key = config.get('keyboard', 'edit_config')
134138
struct.edit_current_block_key = config.get('keyboard', 'edit_current_block')
135139
struct.external_editor_key = config.get('keyboard', 'external_editor')
136140
struct.help_key = config.get('keyboard', 'help')

bpython/curtsiesfrontend/repl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ def process_key_event(self, e):
491491
greenlet.greenlet(self.pastebin).switch()
492492
elif e in key_dispatch[self.config.external_editor_key]:
493493
self.send_session_to_external_editor()
494+
elif e in key_dispatch[self.config.edit_config_key]:
495+
greenlet.greenlet(self.edit_config).switch()
494496
#TODO add PAD keys hack as in bpython.cli
495497
elif e in key_dispatch[self.config.edit_current_block_key]:
496498
self.send_current_block_to_external_editor()

bpython/repl.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,41 @@ 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+
#TODO put default-config somewhere accessible to this code
1033+
DEFAULT = ("[general]\n"
1034+
"syntax = True\n"
1035+
"[keyboard]\n"
1036+
"pastebin = F8\n"
1037+
"save = C-s\n")
1038+
1039+
open('a.txt', 'w').write(repr(self.config.config_path))
1040+
if not (os.path.isfile(self.config.config_path)):
1041+
if self.interact.confirm(_("Config file does not exist - create new from default? (y/N)")):
1042+
try:
1043+
containing_dir = os.path.dirname(os.path.abspath(self.config.config_path))
1044+
if not os.path.exists(containing_dir):
1045+
os.makedirs(containing_dir)
1046+
with open(self.config.config_path, 'w') as f:
1047+
f.write(DEFAULT)
1048+
except (IOError, OSError) as e:
1049+
self.interact.notify('error creating file: %r' % e)
1050+
raise e
1051+
return False
1052+
else:
1053+
return False
1054+
1055+
if self.open_in_external_editor(self.config.config_path):
1056+
self.interact.notify('bpython config file edited. Restart bpython for changes to take effect.')
1057+
else:
1058+
self.interact.notify('error editing config file')
1059+
10251060

10261061
def next_indentation(line, tab_length):
10271062
"""Given a code line, return the indentation of the next line."""

0 commit comments

Comments
 (0)