Skip to content

Commit dd1373f

Browse files
fixes for external editor functionality
1 parent 5d8e4ee commit dd1373f

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

bpython/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ def yank_from_buffer(self):
15231523
self.print_line(self.s, clr=True)
15241524

15251525
def send_current_line_to_editor(self):
1526-
lines = repl.send_to_external_editor(self.s).split('\n')
1526+
lines = self.send_to_external_editor(self.s).split('\n')
15271527
self.s = ''
15281528
self.print_line(self.s)
15291529
while lines and not lines[-1]:

bpython/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def loadini(struct, configfile):
7272
'pastebin_private': True,
7373
'pastebin_show_url': 'http://bpaste.net/show/$paste_id/',
7474
'pastebin_helper': '',
75-
'save_append_py': False
75+
'save_append_py': False,
76+
'editor': 'vi',
7677
},
7778
'keyboard': {
7879
'clear_line': 'C-u',
@@ -121,6 +122,7 @@ def loadini(struct, configfile):
121122
struct.highlight_show_source = config.getboolean('general',
122123
'highlight_show_source')
123124
struct.hist_file = config.get('general', 'hist_file')
125+
struct.editor = config.get('general', 'editor')
124126
struct.hist_length = config.getint('general', 'hist_length')
125127
struct.hist_duplicates = config.getboolean('general', 'hist_duplicates')
126128
struct.flush_output = config.getboolean('general', 'flush_output')

bpython/curtsiesfrontend/repl.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

bpython/repl.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import inspect
2929
import os
3030
import pydoc
31+
import shlex
3132
import subprocess
3233
import sys
3334
import tempfile
@@ -993,6 +994,17 @@ def clear_current_line(self):
993994
"""This is used as the exception callback for the Interpreter instance.
994995
It prevents autoindentation from occuring after a traceback."""
995996

997+
def send_to_external_editor(self, text, filename=None):
998+
editor = (self.config.editor or
999+
os.environ.get('VISUAL', os.environ.get('EDITOR', 'vim')))
1000+
editor_args = shlex.split(editor)
1001+
with tempfile.NamedTemporaryFile(suffix='.py') as temp:
1002+
temp.write(text)
1003+
temp.flush()
1004+
if subprocess.call(editor_args + [temp.name]) == 0:
1005+
with open(temp.name) as f:
1006+
return f.read()
1007+
9961008

9971009
def next_indentation(line, tab_length):
9981010
"""Given a code line, return the indentation of the next line."""
@@ -1066,11 +1078,3 @@ def extract_exit_value(args):
10661078
else:
10671079
return args
10681080

1069-
def send_to_external_editor(text, filename=None):
1070-
editor = os.environ.get('VISUAL', os.environ.get('EDITOR', 'vim'))
1071-
editor_args = editor.split()
1072-
with tempfile.NamedTemporaryFile(suffix='.py') as temp:
1073-
temp.write(text)
1074-
temp.flush()
1075-
subprocess.call(editor_args + [temp.name])
1076-
return open(temp.name).read()

sample-config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ tab_length = 4
3232
# to "default" to use the default theme
3333
color_scheme = default
3434

35+
# External editor to use for editing the current line, block, or full history
36+
editor = vi
37+
3538
[keyboard]
3639
pastebin = F8
3740
save = C-s

0 commit comments

Comments
 (0)