|
7 | 7 | import Queue |
8 | 8 | from cStringIO import StringIO |
9 | 9 | import traceback |
| 10 | +import subprocess |
| 11 | +import tempfile |
10 | 12 |
|
11 | 13 | from bpython.autocomplete import Autocomplete, SIMPLE |
12 | 14 | from bpython.repl import Repl as BpythonRepl |
|
33 | 35 | from bpython.scrollfrontend.friendly import NotImplementedError |
34 | 36 |
|
35 | 37 | 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 |
36 | 39 |
|
37 | 40 | #TODO implement paste mode and figure out what the deal with config.paste_time is |
38 | 41 | #TODO figure out how config.auto_display_list=False behaves and implement it |
@@ -145,8 +148,8 @@ def __init__(self): |
145 | 148 | config.cli_suggestion_width = 1 |
146 | 149 |
|
147 | 150 | 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)) |
150 | 153 | self.rl_char_sequences = get_updated_char_sequences(key_dispatch, config) |
151 | 154 | logging.debug("starting parent init") |
152 | 155 | super(Repl, self).__init__(interp, config) |
@@ -287,6 +290,8 @@ def process_event(self, e): |
287 | 290 | logging.debug('starting pastebin thread') |
288 | 291 | t.start() |
289 | 292 | self.interact.wait_for_request_or_notify() |
| 293 | + elif e in key_dispatch[EDITOR_KEY]: |
| 294 | + self.external_editor() |
290 | 295 | #TODO add PAD keys hack as in bpython.cli |
291 | 296 | else: |
292 | 297 | self.add_normal_character(e) |
@@ -702,6 +707,18 @@ def getstdout(self): |
702 | 707 | s = '\n'.join([x.s if isinstance(x, FmtStr) else x |
703 | 708 | for x in lines]) if lines else '' |
704 | 709 | 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() |
705 | 722 |
|
706 | 723 | def simple_repl(): |
707 | 724 | with Repl() as r: |
|
0 commit comments