Skip to content

Commit 231dc91

Browse files
ctrl-x send line to editor works in bpython
1 parent 32e2964 commit 231dc91

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

bpython/cli.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,9 @@ def p_key(self, key):
995995
self.do_exit = True
996996
return None
997997

998+
elif key == '\x18':
999+
return self.send_current_line_to_editor()
1000+
9981001
elif key[0:3] == 'PAD' and not key in ('PAD0', 'PADSTOP'):
9991002
pad_keys = {
10001003
'PADMINUS': '-',
@@ -1108,10 +1111,10 @@ def repl(self):
11081111
self.push('from bpython._internal import _help as help\n', False)
11091112

11101113
self.iy, self.ix = self.scr.getyx()
1111-
more = False
1114+
self.more = False
11121115
while not self.do_exit:
11131116
self.f_string = ''
1114-
self.prompt(more)
1117+
self.prompt(self.more)
11151118
try:
11161119
inp = self.get_line()
11171120
except KeyboardInterrupt:
@@ -1132,8 +1135,8 @@ def repl(self):
11321135
else:
11331136
self.stdout_hist += inp.encode(getpreferredencoding()) + '\n'
11341137
stdout_position = len(self.stdout_hist)
1135-
more = self.push(inp)
1136-
if not more:
1138+
self.more = self.push(inp)
1139+
if not self.more:
11371140
self.prev_block_finished = stdout_position
11381141
self.s = ''
11391142
return self.exit_value
@@ -1203,8 +1206,8 @@ def reevaluate(self):
12031206
# I decided it was easier to just do this manually
12041207
# than to make the print_line and history stuff more flexible.
12051208
self.scr.addstr('\n')
1206-
more = self.push(line)
1207-
self.prompt(more)
1209+
self.more = self.push(line)
1210+
self.prompt(self.more)
12081211
self.iy, self.ix = self.scr.getyx()
12091212

12101213
self.cpos = 0
@@ -1505,6 +1508,46 @@ def yank_from_buffer(self):
15051508
self.addstr(self.cut_buffer)
15061509
self.print_line(self.s, clr=True)
15071510

1511+
def send_current_line_to_editor(self):
1512+
lines = repl.send_to_external_editor(self.s).split('\n')
1513+
self.s = ''
1514+
self.print_line(self.s)
1515+
while lines and not lines[-1]:
1516+
lines.pop()
1517+
if not lines:
1518+
return ''
1519+
1520+
self.f_string = ''
1521+
self.cpos = -1 # Set cursor position to -1 to prevent paren matching
1522+
1523+
self.iy, self.ix = self.scr.getyx()
1524+
self.evaluating = True
1525+
for line in lines:
1526+
if py3:
1527+
self.stdout_hist += line + '\n'
1528+
else:
1529+
self.stdout_hist += line.encode(getpreferredencoding()) + '\n'
1530+
self.history.append(line)
1531+
self.print_line(line)
1532+
self.s_hist[-1] += self.f_string
1533+
self.scr.addstr('\n')
1534+
self.more = self.push(line)
1535+
self.prompt(self.more)
1536+
self.iy, self.ix = self.scr.getyx()
1537+
self.evaluating = False
1538+
1539+
self.cpos = 0
1540+
indent = repl.next_indentation(self.s, self.config.tab_length)
1541+
self.s = ''
1542+
self.scr.refresh()
1543+
1544+
if self.buffer:
1545+
for _ in xrange(indent):
1546+
self.tab()
1547+
1548+
self.print_line(self.s)
1549+
self.scr.redrawwin()
1550+
return ''
15081551

15091552
class Statusbar(object):
15101553
"""This class provides the status bar at the bottom of the screen.

bpython/curtsiesfrontend/repl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def process_event(self, e):
323323
elif e in key_dispatch[self.config.external_editor_key]:
324324
self.send_to_external_editor()
325325
#TODO add PAD keys hack as in bpython.cli
326-
elif e in [""]:
326+
elif e in ["\x18"]:
327327
self.send_current_block_to_external_editor()
328328
else:
329329
self.add_normal_character(e if len(e) == 1 else e[-1]) #strip control seq

bpython/repl.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import pydoc
3131
import subprocess
3232
import sys
33+
import tempfile
3334
import textwrap
3435
import traceback
3536
import unicodedata
@@ -1064,3 +1065,12 @@ def extract_exit_value(args):
10641065
return args[0]
10651066
else:
10661067
return args
1068+
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()

0 commit comments

Comments
 (0)