Skip to content

Commit e0f0428

Browse files
first pass on external editor feature
--HG-- branch : scroll-frontend
1 parent afcc47a commit e0f0428

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

bpython/scrollfrontend/repl.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import Queue
88
from cStringIO import StringIO
99
import traceback
10+
import subprocess
11+
import tempfile
1012

1113
from bpython.autocomplete import Autocomplete, SIMPLE
1214
from bpython.repl import Repl as BpythonRepl
@@ -33,6 +35,7 @@
3335
from bpython.scrollfrontend.friendly import NotImplementedError
3436

3537
INFOBOX_ONLY_BELOW = True #TODO make this a config option if it isn't already
38+
EDITOR_KEY = 'F7' #TODO put this in config if it gets to stay in
3639

3740
#TODO implement paste mode and figure out what the deal with config.paste_time is
3841
#TODO figure out how config.auto_display_list=False behaves and implement it
@@ -145,8 +148,8 @@ def __init__(self):
145148
config.cli_suggestion_width = 1
146149

147150
self.status_bar = StatusBar(_('welcome to bpython'), _(
148-
" <%s> Rewind <%s> Save <%s> Pastebin "
149-
) % (config.undo_key, config.save_key, config.pastebin_key))
151+
" <%s> Rewind <%s> Save <%s> Pastebin <%s> Editor"
152+
) % (config.undo_key, config.save_key, config.pastebin_key, EDITOR_KEY))
150153
self.rl_char_sequences = get_updated_char_sequences(key_dispatch, config)
151154
logging.debug("starting parent init")
152155
super(Repl, self).__init__(interp, config)
@@ -287,6 +290,8 @@ def process_event(self, e):
287290
logging.debug('starting pastebin thread')
288291
t.start()
289292
self.interact.wait_for_request_or_notify()
293+
elif e in key_dispatch[EDITOR_KEY]:
294+
self.external_editor()
290295
#TODO add PAD keys hack as in bpython.cli
291296
else:
292297
self.add_normal_character(e)
@@ -702,6 +707,18 @@ def getstdout(self):
702707
s = '\n'.join([x.s if isinstance(x, FmtStr) else x
703708
for x in lines]) if lines else ''
704709
return s
710+
def external_editor(self):
711+
editor = os.environ.get('VISUAL', os.environ.get('EDITOR', 'vim'))
712+
text = self.getstdout()
713+
with tempfile.NamedTemporaryFile(suffix='.py') as temp:
714+
temp.write('### current bpython session - file will be reevaluated, ### lines will not be run\n')
715+
temp.write('\n'.join(line[4:] if line[:4] in ('... ', '>>> ') else '### '+line
716+
for line in text.split('\n')))
717+
temp.flush()
718+
subprocess.call([editor, temp.name])
719+
self.history = [line for line in open(temp.name).read().split('\n')
720+
if (line[:4] != '### ' and line.split())]
721+
self.reevaluate()
705722

706723
def simple_repl():
707724
with Repl() as r:

0 commit comments

Comments
 (0)