Skip to content

Commit 0763cc7

Browse files
committed
initial implementation of pastemode
1 parent 3f60733 commit 0763cc7

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

bpython/cli.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ def __init__(self, scr, interp, statusbar=None, idle=None):
320320
self.list_win_visible = False
321321
self._C = {}
322322
sys.stdin = FakeStdin(self)
323+
self.paste_mode = False
324+
self.last_key_press = time.time()
323325
sys.path.insert(0, '.')
324326

325327
if not OPTS.arg_spec:
@@ -1025,7 +1027,7 @@ def repl(self):
10251027
# Keep two copies so you can go up and down in the hist:
10261028
if inp:
10271029
self.rl_hist.append(inp + '\n')
1028-
more = self.push(inp)
1030+
more = self.push(inp) or self.paste_mode
10291031

10301032
def size(self):
10311033
"""Set instance attributes for x and y top left corner coordinates
@@ -1408,7 +1410,7 @@ def lf(self):
14081410
self.inside_string)
14091411

14101412
# Reprint the line (as there was maybe a highlighted paren in it)
1411-
self.print_line(self.s)
1413+
self.print_line(self.s, newline=True)
14121414
self.echo("\n")
14131415

14141416
def addstr(self, s):
@@ -1420,16 +1422,17 @@ def addstr(self, s):
14201422
l = len(self.s)
14211423
self.s = self.s[:l - self.cpos] + s + self.s[l - self.cpos:]
14221424

1423-
self.complete()
1425+
if not self.paste_mode:
1426+
self.complete()
14241427

1425-
def print_line(self, s, clr=False):
1428+
def print_line(self, s, clr=False, newline=False):
14261429
"""Chuck a line of text through the highlighter, move the cursor
14271430
to the beginning of the line and output it to the screen."""
14281431

14291432
if not s:
14301433
clr = True
14311434

1432-
if OPTS.syntax:
1435+
if OPTS.syntax and (not self.paste_mode or newline):
14331436
if self.inside_string:
14341437
# A string started in another line is continued in this
14351438
# line
@@ -1449,7 +1452,7 @@ def reprint_line(lineno, s, to_replace=[]):
14491452
o = format(t, BPythonFormatter(OPTS.color_scheme))
14501453
self.scr.move(lineno, 4)
14511454
map(self.echo, o.split('\x04'))
1452-
1455+
14531456
y, x = self.scr.getyx()
14541457
if self.highlighted_paren:
14551458
# Clear previous highlighted paren
@@ -1540,23 +1543,19 @@ def get_line(self):
15401543

15411544
self.ts = ''
15421545

1543-
indent_spaces = 0
1544-
for c in self.s:
1545-
if c == ' ':
1546-
indent_spaces += 1
1547-
else:
1548-
break
1546+
indent_spaces = len(self.s) - len(self.s.lstrip(' '))
15491547

15501548
indent = self.s.rstrip().endswith(':')
15511549

15521550
self.s = ''
15531551
self.iy, self.ix = self.scr.getyx()
15541552

1555-
for _ in range(indent_spaces // OPTS.tab_length):
1556-
self.c = '\t'
1557-
self.p_key()
1553+
if not self.paste_mode:
1554+
for _ in range(indent_spaces // OPTS.tab_length):
1555+
self.c = '\t'
1556+
self.p_key()
15581557

1559-
if indent:
1558+
if indent and not self.paste_mode:
15601559
self.c = '\t'
15611560
self.p_key()
15621561

@@ -1598,6 +1597,9 @@ def get_key(self):
15981597
if key:
15991598
return key
16001599
else:
1600+
t = time.time()
1601+
self.paste_mode = (t - self.last_key_press <= 0.01)
1602+
self.last_key_press = t
16011603
return key
16021604
finally:
16031605
if self.idle:

0 commit comments

Comments
 (0)