-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathipython_cell.py
More file actions
591 lines (459 loc) · 16.6 KB
/
ipython_cell.py
File metadata and controls
591 lines (459 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
from __future__ import print_function
import re
from subprocess import Popen, PIPE
import sys
try:
import vim
except ImportError:
print("warning: importing ipython_cell outside vim, some functions will "
"not work")
CTRL_C = '\x03'
CTRL_N = '\x0e'
CTRL_O = '\x0f'
CTRL_P = '\x10'
CTRL_U = '\x15'
def execute_cell(use_cpaste=False):
"""Execute code within cell.
Parameters
----------
use_cpaste : bool
Set to True to use %cpaste instead of %paste to send cell to ipython.
"""
current_row, _ = vim.current.window.cursor
cell_boundaries = _get_cell_boundaries(auto_include_first_line=False)
# Include first line of buffer if necessary
first_line_contains_cell_header = 1 in cell_boundaries
if not first_line_contains_cell_header:
cell_boundaries.insert(0, 1)
start_row, end_row = _get_current_cell_boundaries(current_row,
cell_boundaries)
# Required for Python 2
if end_row is None:
end_row = len(vim.current.buffer)
_clear_prompt()
# Send tags?
if (vim.eval('g:ipython_cell_delimit_cells_by') == 'tags'
and vim.eval('g:ipython_cell_send_cell_headers') != '0'):
if start_row == 1 and not first_line_contains_cell_header:
cell_header = "# cell 0"
else:
cell_header = vim.current.buffer[start_row-1]
_slimesend0(cell_header)
_slimesend0(CTRL_O)
_slimesend0(CTRL_N)
# Do not send the tag over
if vim.eval('g:ipython_cell_delimit_cells_by') == 'tags':
if first_line_contains_cell_header or start_row != 1:
start_row += 1
# start_row and end_row are 1-indexed, need to subtract 1
cell = "\n".join(vim.current.buffer[start_row-1:end_row])
cell_is_empty = not cell
if vim.eval('g:ipython_cell_update_file_variable') != '0':
f = vim.eval("expand('%:p')")
# Make sure the indentation is the same as the first line of the cell
first_row = vim.current.buffer[start_row-1]
indentation = re.match(r"[\t ]*", first_row).group()
cell = indentation + "__file__ = '{}'\n".format(f) + cell
if not use_cpaste:
if cell_is_empty:
_slimesend("# empty cell")
else:
_copy_to_clipboard(cell)
paste_command = vim.eval('g:ipython_cell_cell_command')
_slimesend(paste_command)
else:
try:
slime_python_ipython = vim.eval('g:slime_python_ipython')
except vim.error:
slime_python_ipython = False
if slime_python_ipython:
# Add a newline at the start of the cell to avoid problems if there
# is a global indentation level, see Issue #37 on GitHub
cell = "\n" + cell
_slimesend(cell)
else:
_slimesend("%cpaste -q")
# Send 25 lines at a time to avoid potential issues when sending
# a large number of lines
remaining_chunks = cell.splitlines()
while remaining_chunks:
chunk = remaining_chunks[:25]
remaining_chunks[:25] = []
_slimesend("\n".join(chunk))
_slimesend("--")
def jump_next_cell():
"""Move cursor to the start of the next cell."""
current_row, _ = vim.current.window.cursor
cell_boundaries = _get_cell_boundaries()
next_cell_row = _get_next_cell(current_row, cell_boundaries)
if next_cell_row != current_row:
try:
vim.current.window.cursor = (next_cell_row, 0)
except vim.error:
vim.command("echo 'Cell header is outside the buffer boundaries'")
def jump_prev_cell():
"""Move cursor to the start of the current or previous cell."""
current_row, _ = vim.current.window.cursor
cell_boundaries = _get_cell_boundaries()
prev_cell_row = _get_prev_cell(current_row, cell_boundaries)
if prev_cell_row != current_row:
try:
vim.current.window.cursor = (prev_cell_row, 0)
except vim.error:
vim.command("echo 'Cell header is outside the buffer boundaries'")
def insert_cell_below():
insert_tag = vim.eval('g:ipython_cell_insert_tag')
current_row, _ = vim.current.window.cursor
cell_boundaries = _get_cell_boundaries()
_, end_row = _get_current_cell_boundaries(current_row, cell_boundaries)
# Required for Python 2
if end_row is None:
end_row = len(vim.current.buffer)
# Jump cursor to end_row
if end_row != current_row:
try:
vim.current.window.cursor = (end_row, 0)
except vim.error:
vim.command("echo 'Cell header is outside the buffer boundaries'")
# Insert tag bellow
if vim.current.line != '':
vim.command("normal!o")
current_row, _ = vim.current.window.cursor
if current_row != 1:
vim.command("normal!o")
current_row += 1
if current_row != len(vim.current.buffer):
vim.command("normal!O")
vim.command("normal!i" + insert_tag)
def insert_cell_above():
insert_tag = vim.eval('g:ipython_cell_insert_tag')
current_row, _ = vim.current.window.cursor
cell_boundaries = _get_cell_boundaries(auto_include_first_line=False)
# Include first line of buffer if necessary
first_line_contains_cell_header = 1 in cell_boundaries
if not first_line_contains_cell_header:
cell_boundaries.insert(0, 1)
start_row, _ = _get_current_cell_boundaries(current_row, cell_boundaries)
# If the cursor not at the header of the current cell,
# we move the cursor to the header
if current_row != start_row:
try:
vim.current.window.cursor = (start_row, 0)
except vim.error:
vim.command("echo 'Cell header is outside the buffer boundaries'")
# If the start_row is the first line and not contains header
# We instert a cell header for the current cell
if start_row == 1 and not first_line_contains_cell_header:
vim.command("normal!O")
vim.command("normal!i" + insert_tag)
else:
vim.command("normal!O")
vim.command("normal!O")
vim.command("normal!i" + insert_tag)
def to_markdown():
insert_tag = vim.eval('g:ipython_cell_insert_tag')
current_row, _ = vim.current.window.cursor
cell_boundaries = _get_cell_boundaries(auto_include_first_line=False)
# Include first line of buffer if necessary
first_line_contains_cell_header = 1 in cell_boundaries
if not first_line_contains_cell_header:
cell_boundaries.insert(0, 1)
start_row, end_row = _get_current_cell_boundaries(current_row,
cell_boundaries)
if end_row is None:
end_row = len(vim.current.buffer)
# Switch to end_row first as start_row will not change after insert """
# line
if current_row != end_row:
try:
vim.current.window.cursor = (end_row, 0)
except vim.error:
vim.command("echo 'Cell is outside the buffer boundaries'")
if vim.current.line != '':
vim.command("normal!o")
current_row, _ = vim.current.window.cursor
if current_row != len(vim.current.buffer):
vim.command('normal!O')
vim.command('normal!i"""')
# We move the cursor to the header
try:
vim.current.window.cursor = (start_row, 0)
except vim.error:
vim.command("echo 'Cell is outside the buffer boundaries'")
# If the start_row is the first line and not contains header
# We instert a cell header for the current cell
if start_row == 1 and not first_line_contains_cell_header:
vim.command("normal!O")
vim.command("normal!i" + insert_tag)
# Now we at the header row
vim.command('normal!A [markdown]')
vim.command('normal!o')
vim.command('normal!i"""')
vim.command('normal!j')
def previous_command():
"""Run previous command."""
_clear_prompt()
_slimesend(CTRL_P)
def restart_ipython(shell_prev_cmd):
"""Quit ipython and start it again."""
ctrl_p = re.compile('<c-p>|<ctrl-p>', re.IGNORECASE)
shell_prev_cmd = ctrl_p.sub(CTRL_P, shell_prev_cmd)
_clear_prompt()
_slimesend("exit")
_slimesend(shell_prev_cmd)
def run(*args):
"""Run script."""
options = " ".join(args)
run_command = vim.eval('g:ipython_cell_run_command')
run_command = run_command.format(options=options,
filepath=vim.current.buffer.name)
_clear_prompt()
_slimesend(run_command)
def clear():
"""Clear screen."""
_clear_prompt()
_slimesend("%clear")
def close_all():
"""Close all figure windows."""
_clear_prompt()
_slimesend("plt.close('all')")
def _clear_prompt():
if vim.eval('g:ipython_cell_send_ctrl_u') != '0':
_slimesend0(CTRL_U)
if vim.eval('g:ipython_cell_send_ctrl_c') != '0':
_slimesend0("i") # enter insert mode
_slimesend0(CTRL_C)
def _copy_to_clipboard(string, prefer_program=None):
"""Copy ``string`` to primary clipboard.
If the +clipboard feature flag in Vim is present, the function will use Vim
to copy the string, otherwise it will attempt to use an external program.
Parameters
----------
string : str
String to copy to clipboard.
prefer_program : None or str
Which external program to use to copy to clipboard if +clipboard is
absent.
"""
if vim.eval('g:ipython_cell_prefer_external_copy') != '0':
_copy_to_clipboard_external(string, prefer_program)
else:
copy_successful = _copy_to_clipboard_internal(string)
if not copy_successful:
_copy_to_clipboard_external(string, prefer_program)
def _copy_to_clipboard_external(string, prefer_program=None):
"""Copy ``string`` to primary clipboard using pbcopy, xclip or xsel.
Parameters
----------
string : str
String to copy to clipboard.
prefer_program : None or str
Which external program to use to copy to clipboard.
"""
PROGRAMS = [
["pbcopy"],
["xclip", "-i", "-selection", "clipboard"],
["xsel", "-i", "--clipboard"],
]
# Python 2 compatibility
try:
FileNotFoundError
except NameError:
FileNotFoundError = OSError
for program in PROGRAMS:
if prefer_program is not None and program[0] != prefer_program:
continue
try:
p = Popen(program, stdin=PIPE)
except FileNotFoundError:
program_found = False
else:
program_found = True
break
if not program_found:
_error("Could not find xclip or xsel executable")
return
byte = string.encode()
p.communicate(input=byte)
def _copy_to_clipboard_internal(string):
"""Copy ``string`` to primary clipboard using Vim.
Return True if the copy is successful, otherwise return False.
"""
if vim.eval("has('clipboard')") == '1':
vim.command('let @+=' + _sanitize(string))
return True
else:
return False
def _error(*args, **kwargs):
"""Print error message to stderr. Same parameters as print."""
print(*args, file=sys.stderr, **kwargs)
def _get_cell_boundaries(auto_include_first_line=True):
"""Return a list of rows (1-indexed) for all cell boundaries.
Parameters
----------
auto_include_first_line : bool
If True (default), include first line of the buffer automatically in
the returned list even if it does not contain a cell header.
"""
buffer = vim.current.buffer
delimiter = vim.eval('g:ipython_cell_delimit_cells_by').strip()
if delimiter == 'marks':
valid_marks = vim.eval('g:ipython_cell_valid_marks').strip()
cell_boundaries = _get_rows_with_marks(buffer, valid_marks)
elif delimiter == 'tags':
tag = vim.eval('g:ipython_cell_tag')
regex_option = vim.eval('g:ipython_cell_regex').strip().lower()
use_regex = regex_option in ['1', 'y', 'yes', 't', 'true']
cell_boundaries = _get_rows_with_tag(buffer, tag, use_regex)
else:
_error("Invalid option value for g:ipython_cell_valid_marks: {}"
.format(delimiter))
return
if auto_include_first_line:
# Include beginning of buffer as a cell boundary
cell_boundaries.append(1)
return sorted(set(cell_boundaries))
def _get_current_cell_boundaries(current_row, cell_boundaries):
"""Return the start and end row numbers (1-indexed) for the current cell.
Parameters
----------
current_row : int
Current row number.
cell_boundaries : list
A list of row numbers for the cell boundaries.
Returns
-------
int:
Start row number for the current cell.
int:
End row number for the current cell.
"""
next_cell_row = None
for boundary in cell_boundaries:
if boundary <= current_row:
start_row = boundary
else:
next_cell_row = boundary
break
if next_cell_row is None:
end_row = None # end of file
else:
end_row = next_cell_row - 1
return start_row, end_row
def _get_next_cell(current_row, cell_boundaries):
"""Return start row number of the next cell.
If there is no next cell, the current row number is returned.
Parameters
----------
current_row : int
Current row number.
cell_boundaries : list
A list of row numbers for the cell boundaries.
Returns
-------
int:
Start row number for the next cell.
"""
next_cell_row = None
for boundary in cell_boundaries:
if boundary > current_row:
next_cell_row = boundary
break
if next_cell_row is None:
return current_row
else:
return next_cell_row
def _get_prev_cell(current_row, cell_boundaries):
"""Return start row number of the current or previous cell.
If ``current_row`` is a cell header, the previous cell header is returned,
otherwise the current cell header is returned.
If there is no previous cell, the current row number is returned.
Parameters
----------
current_row : int
Current row number.
cell_boundaries : list
A list of row numbers for the cell boundaries.
Returns
-------
int:
Start row number for the current or previous cell.
"""
prev_cell_row = None
for boundary in cell_boundaries:
if boundary < current_row:
prev_cell_row = boundary
else:
break
if prev_cell_row is None:
return current_row
else:
return prev_cell_row
def _get_rows_with_tag(buffer, tags, use_regex=False):
"""Return a list of row numbers for lines containing tag in ``tags``.
Parameters
----------
buffer : iterable
An iterable object that contains the lines of a buffer.
tags : list or str
Tag(s) to search for.
Returns
-------
list:
List of row numbers.
"""
if not isinstance(tags, list):
tags = [tags]
rows_containing_tag = []
for i, line in enumerate(buffer):
for tag in tags:
if not use_regex:
tag_found = tag in line
else:
match = re.search(tag, line)
tag_found = match is not None
if tag_found:
rows_containing_tag.append(i + 1) # rows are counted from 1
break
return rows_containing_tag
def _get_rows_with_marks(buffer, valid_marks):
"""Return a list of row numbers for lines containing a mark.
Parameters
----------
buffer : buffer object
An object with a ``mark`` method.
valid_marks : list
A list of marks to search for.
Returns
-------
list:
List of row numbers.
"""
rows_containing_marks = []
for mark in valid_marks:
mark_loc = buffer.mark(mark)
if mark_loc is not None and mark_loc[0] != 0:
rows_containing_marks.append(mark_loc[0])
return rows_containing_marks
def _sanitize(string):
return "'" + re.sub(re.compile("'"), "''", string) + "'"
def _slimesend(string):
"""Send ``string`` using vim-slime."""
if not string:
return
try:
vim.command('SlimeSend1 {}'.format(string))
except vim.error:
_error("Could not execute SlimeSend1 command, make sure vim-slime is "
"installed")
def _slimesend0(string):
"""Similar to _slimesend, but use SlimeSend0 (do not include carriage
return) instead of SlimeSend1.
"""
if not string:
return
try:
vim.command('SlimeSend0 "{}"'.format(string))
except vim.error:
_error("Could not execute SlimeSend0 command, make sure vim-slime is "
"installed")