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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
;;; code-cells.el --- Lightweight notebooks with support for ipynb files -*- lexical-binding: t; -*-
;; Copyright (C) 2022, 2023 Free Software Foundation, Inc.
;; Author: Augusto Stoffel <arstoffel@gmail.com>
;; Keywords: convenience, outlines
;; URL: https://github.com/astoff/code-cells.el
;; Package-Requires: ((emacs "27.1") (compat "29.1"))
;; Version: 0.5
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; With this package, you can efficiently navigate, edit and execute
;; code split into cells according to certain magic comments. It also
;; allows you to open ipynb notebook files directly in Emacs. They
;; will be automatically converted to a script for editing, and
;; converted back to notebook format when saving. An external tool,
;; Jupytext by default, is required for this.
;;
;; A minor mode, `code-cells-mode', provides the following features:
;;
;; - Fontification of cell boundaries.
;;
;; - Keybindings for the cell navigation and evaluation commands,
;; under the `C-c %' prefix.
;;
;; - Outline mode integration: cell headers have outline level
;; determined by the number of percent signs or asterisks; within a
;; cell, outline headings are as determined by the major mode, but
;; they are demoted by an amount corresponding to the level of the
;; containing cell. This provides code folding and hierarchical
;; navigation, among other things, when `outline-minor-mode' is
;; active.
;;
;; This minor mode is automatically activated when opening an ipynb
;; file, but you can also activate it in any other buffer, either
;; manually or through a hook.
;;; News:
;; Version 0.5
;; - Several new editing commands.
;; - Integrate with repeat-mode and context-menu-mode.
;; - Some changed keybindings.
;; - More consistent handling of numeric arguments and cell ranges.
;;; Code:
(require 'outline)
(require 'pulse)
(eval-when-compile
(require 'cl-lib)
(require 'let-alist)
(require 'rx))
(defgroup code-cells nil
"Utilities for code split into cells."
:group 'convenience
:prefix "code-cells-")
(defcustom code-cells-boundary-regexp
(rx line-start
(+ (syntax comment-start))
(or (seq (* (syntax whitespace)) "%" (group-n 1 (+ "%")))
(seq " In[" (* (any space digit)) "]:")))
"Regular expression specifying cell boundaries.
It should match at the beginning of a line. The length of the
first capture determines the outline level."
:type 'regexp
:safe #'stringp)
(defface code-cells-header-line '((t :extend t
:overline t
:inherit font-lock-comment-face))
"Face used by `code-cells-mode' to highlight cell boundaries.")
(defcustom code-cells-major-mode-outline-min-level 0
"Minimal level of major-mode outline headings.
`code-cells-mode' integrates with `outline-minor-mode' by combining
major-mode-defined outline levels with cell boundaries. Major mode
headings are demoted by at least this amount."
:type 'natnum
:safe #'numberp)
(defcustom code-cells-eval-region-commands
`((drepl--current . drepl-eval-region)
(jupyter-repl-interaction-mode . ,(apply-partially 'jupyter-eval-region nil))
(python-mode . python-shell-send-region)
(python-ts-mode . python-shell-send-region) ;For Emacs ≤ 29
(emacs-lisp-mode . eval-region)
(lisp-interaction-mode . eval-region))
"Alist of commands to evaluate a region.
The keys are major or minor modes and the values are functions
taking region bounds as argument."
:type '(alist :key-type symbol :value-type symbol))
(defcustom code-cells-convert-ipynb-style
'(("jupytext" "--to" "ipynb")
("jupytext" "--to" "auto:percent")
code-cells--guess-mode
code-cells-convert-ipynb-hook)
"Determines how to convert ipynb files for editing.
The first two entries are lists of strings: the command name and
arguments used, respectively, to convert to and from ipynb format.
The third entry is a function called with no arguments to determine the
major mode to be called after conversion from ipynb format. The default
setting tries to guess it from the notebook metadata.
The fourth entry, also optional, is a hook run after the new major mode
is activated."
:type '(list (repeat string) (repeat string) function symbol))
(defcustom code-cells-convert-ipynb-hook '(code-cells-mode)
"Hook run after converting an ipynb notebook to a regular script."
:type 'hook)
;;; Cell navigation
;;;###autoload
(defun code-cells-forward-cell (&optional arg)
"Move to the next cell boundary, or end of buffer.
With ARG, repeat this that many times. If ARG is negative, move
backward."
(interactive "p")
(unless arg (setq arg 1))
(unless (zerop arg)
(when (or (cl-plusp arg) (not (bolp)))
(goto-char (pos-eol)))
(condition-case nil
(dotimes (_ (abs arg))
(re-search-forward code-cells-boundary-regexp
nil nil (cl-signum arg)))
(search-failed (goto-char (if (cl-plusp arg) (point-max) (point-min))))
(:success (when (cl-plusp arg) (goto-char (match-beginning 0)))))))
(put 'code-cell 'forward-op #'code-cells-forward-cell) ;For thing at point
;;;###autoload
(defun code-cells-backward-cell (&optional arg)
"Move to the previous cell boundary, or beginning of buffer.
With ARG, repeat this that many times. If ARG is negative, move
forward."
(interactive "p")
(code-cells-forward-cell (- (or arg 1))))
(defun code-cells--bounds (&optional count use-region no-header)
"Return the bounds of the current code cell, as a 2-element list.
If COUNT is non-nil, return instead a region containing COUNT cells and
starting or ending with the current cell, depending on the sign of
COUNT. The values 0 and -1 have a special meaning: they stand for the
half of the current cell below respectively above the current line.
If USE-REGION is non-nil and the region is active, return the
region bounds instead.
If NO-HEADER is non-nil, do not include the cell boundary line."
(if (and use-region (use-region-p))
(list (region-beginning) (region-end))
(setq count (or count 1))
(save-excursion
(let ((bol (pos-bol))
(end (progn (code-cells-forward-cell
(if (cl-plusp count) count 1))
(point))))
(code-cells-backward-cell (abs count))
(and no-header
(looking-at-p code-cells-boundary-regexp)
(forward-line))
(pcase count
(0 (list bol end))
(-1 (list (point) bol))
(_ (list (point) end)))))))
(defun code-cells--neighbor-bounds (distance)
"Return the bounds of the cell DISTANCE cells away from the current one."
(save-excursion
(unless (looking-at-p code-cells-boundary-regexp)
(code-cells-backward-cell))
(code-cells-forward-cell distance)
(code-cells--bounds)))
;;; Command-generating functions
;;;###autoload
(cl-defun code-cells-command (fun &key use-region pulse no-header)
"Return an anonymous command calling FUN on the current cell.
FUN must be a function that takes two character positions as argument.
Most interactive commands that act on a region are of this form and
can be used here.
If USE-REGION is non-nil, the command will act on the region instead of
the current cell when the region is active.
If PULSE is non-nil, provide visual feedback via
`pulse-momentary-highlight-region'.
If NO-HEADER is non-nil, exclude the cell header from the acted region."
(lambda (arg)
(interactive "p")
(pcase-let ((`(,start ,end) (code-cells--bounds arg use-region no-header)))
(when pulse (pulse-momentary-highlight-region start end))
(funcall fun start end))))
;;;###autoload
(defun code-cells-speed-key (command)
"Return a speed key definition, suitable for passing to `define-key'.
The resulting keybinding will only have any effect when the point
is at the beginning of a cell heading, in which case it executes
COMMAND."
`(menu-item nil ,command
:filter ,(lambda (d)
(and (bolp)
(looking-at code-cells-boundary-regexp)
d))))
;;; Text manipulation commands
;;;###autoload
(defun code-cells-move-cell-down (arg)
"Move current code cell vertically ARG cells.
Move up when ARG is negative and move down otherwise."
(interactive "p")
(pcase-let ((`(,current-beg ,current-end) (code-cells--bounds))
(`(,next-beg ,next-end) (code-cells--neighbor-bounds arg)))
(unless (save-excursion
(and (/= current-beg next-beg)
(goto-char current-beg)
(looking-at-p code-cells-boundary-regexp)
(goto-char next-beg)
(looking-at-p code-cells-boundary-regexp)))
(user-error "Can't move cell"))
(transpose-regions current-beg current-end next-beg next-end)))
;;;###autoload
(defun code-cells-move-cell-up (&optional arg)
"Move current code cell vertically up ARG cells."
(interactive "p")
(code-cells-move-cell-down (- arg)))
;;;###autoload
(defun code-cells-mark-cell (&optional arg)
"Put point at the beginning of this cell, mark at end.
If ARG is non-nil, mark that many cells."
(interactive "p")
(pcase-let ((`(,start ,end) (code-cells--bounds arg)))
(goto-char start)
(push-mark end nil t)))
;;;###autoload(autoload 'code-cells-comment-or-uncomment "code-cells" nil t)
(defalias 'code-cells-comment-or-uncomment
(code-cells-command #'comment-or-uncomment-region :no-header t)
"Comment or uncomment the current code cell.
If ARG is provided, act on that many cells.")
;;;###autoload(autoload 'code-cells-indent "code-cells" nil t)
(defalias 'code-cells-indent (code-cells-command #'indent-region :no-header t)
"Reindent the current code cell.
With a prefix argument, act on that many cells.")
;;;###autoload(autoload 'code-cells-delete "code-cells" nil t)
(defalias 'code-cells-delete (code-cells-command #'delete-region)
"Delete the current code cell without modifying the kill ring.
With a prefix argument, act on that many cells.")
;;;###autoload(autoload 'code-cells-kill "code-cells" nil t)
(defalias 'code-cells-kill (code-cells-command #'kill-region)
"Kill (\"cut\") the current code cell, saving it in the kill ring.
With a prefix argument, act on that many cells.")
;;;###autoload(autoload 'code-cells-copy "code-cells" nil t)
(defalias 'code-cells-copy (code-cells-command #'kill-ring-save :pulse t)
"Save the current code cell to the kill ring.
With a prefix argument, act on that many cells.")
;;;###autoload
(defun code-cells-duplicate (&optional arg)
"Duplicate the current code cell.
With a prefix argument, act on that many cells."
(interactive "p")
(pcase-let* ((`(,start ,end) (code-cells--bounds arg))
(text (buffer-substring start end)))
(save-excursion
(goto-char (if (and arg (cl-minusp arg)) start end))
(insert text))))
;;; Code evaluation
;;;###autoload
(defun code-cells-eval (start end)
"Evaluate code according to current modes.
The first suitable function from `code-cells-eval-region-commands'
is used to do the job.
Interactively, evaluate the region, if active, otherwise the
current code cell. With a numeric prefix, evaluate that many
code cells.
Called from Lisp, evaluate region between START and END."
(interactive (code-cells--bounds (prefix-numeric-value current-prefix-arg) t t))
(funcall
(or (seq-some (pcase-lambda (`(,mode . ,fun))
(when (or (and (boundp mode) (symbol-value mode))
(derived-mode-p mode))
fun))
code-cells-eval-region-commands)
(user-error
"No entry for the current modes in `code-cells-eval-region-commands'"))
start end)
(pulse-momentary-highlight-region start end))
;;;###autoload
(defun code-cells-eval-and-step (arg)
"Evaluate the current cell and move to the next one.
With a prefix argument ARG, act on that many cells."
(interactive "p")
(pcase-let* ((`(,start ,end) (code-cells--bounds arg nil t)))
(code-cells-eval start end)
(if (cl-plusp arg)
(goto-char end)
(goto-char start)
(code-cells-forward-cell -2))))
(defun code-cells--above-or-below-bound (raw)
"Evaluation bound for the given RAW prefix argument."
(list
(pcase (- (prefix-numeric-value raw))
(0 (pos-bol))
((and (pred cl-plusp) arg) (car (code-cells--neighbor-bounds arg)))
(arg (cadr (code-cells--neighbor-bounds arg))))))
;;;###autoload
(defun code-cells-eval-above (point)
"Evaluate all cells above the current one.
With a prefix argument, exclude that many extra cells.
From Lisp, just evaluate from beginning of buffer to POINT."
(interactive (code-cells--above-or-below-bound current-prefix-arg))
(code-cells-eval (point-min) point))
;;;###autoload
(defun code-cells-eval-below (point)
"Evaluate the current cell and all below.
With a prefix argument, include that many extra cells.
From Lisp, just evaluate from POINT to end of buffer."
(interactive (code-cells--above-or-below-bound current-prefix-arg))
(code-cells-eval point (point-max)))
;;;###autoload
(defun code-cells-eval-whole-buffer ()
"Evaluate the entire buffer."
(interactive)
(code-cells-eval (point-min) (point-max)))
;;; Minor mode
(defvar-local code-cells--saved-vars nil
"A place to save variables before activating `code-cells-mode'.")
(defun code-cells--outline-level ()
"Compute the outline level, taking code cells into account.
To be used as the value of the variable `outline-level'.
At a cell boundary, returns the cell outline level, as determined by
`code-cells-boundary-regexp'. Otherwise, returns the sum of the
outline level as determined by the major mode and the current cell
level."
(let* ((at-boundary (looking-at code-cells-boundary-regexp))
(cell-level (if (or at-boundary
(save-excursion
(re-search-backward
code-cells-boundary-regexp nil t)))
(if (match-beginning 1)
(- (match-end 1) (match-beginning 1))
1)
0))
(mm-level (and (not at-boundary)
(looking-at (car code-cells--saved-vars))
(funcall (cadr code-cells--saved-vars)))))
(if mm-level
(+ (max cell-level code-cells-major-mode-outline-min-level) mm-level)
cell-level)))
(defun code-cells--font-lock-keywords ()
"Font lock keywords to highlight cell boundaries."
`((,(rx (regexp code-cells-boundary-regexp) (* any) "\n")
0 'code-cells-header-line append)))
(defvar-keymap code-cells--prefix-map
:repeat t
"p" #'code-cells-backward-cell
"n" #'code-cells-forward-cell
"P" #'code-cells-move-cell-up
"N" #'code-cells-move-cell-down
";" #'code-cells-comment-or-uncomment
"w" #'code-cells-copy
"d" #'code-cells-duplicate
"e" #'code-cells-eval
"s" #'code-cells-eval-and-step
"a" #'code-cells-eval-above
"\\" #'code-cells-indent
"C-w" #'code-cells-kill
"@" #'code-cells-mark-cell)
;;;###autoload
(define-minor-mode code-cells-mode
"Minor mode for cell-oriented code."
:keymap (let ((map (make-sparse-keymap)))
(keymap-set map "C-c %" code-cells--prefix-map)
map)
(let ((vars '(outline-regexp
outline-level
outline-heading-end-regexp
paragraph-start)))
(cond
(code-cells-mode
(setq-local
code-cells--saved-vars (mapcar #'symbol-value vars)
outline-level 'code-cells--outline-level
outline-regexp (rx (or (regexp code-cells-boundary-regexp)
(regexp outline-regexp)))
outline-heading-end-regexp "\n"
paragraph-separate (rx (or (regexp paragraph-separate)
(regexp code-cells-boundary-regexp))))
(add-hook 'context-menu-functions 'code-cells--context-menu 20 t)
(font-lock-add-keywords nil (code-cells--font-lock-keywords)))
(t
(dolist (var vars)
(set (make-local-variable var) (pop code-cells--saved-vars)))
(remove-hook 'context-menu-functions 'code-cells--context-menu t)
(font-lock-remove-keywords nil (code-cells--font-lock-keywords)))))
(font-lock-flush))
;;;###autoload
(defun code-cells-mode-maybe ()
"Turn on `code-cells-mode' if the buffer appears to contain cells.
This function is useful when added to a major mode hook."
(when (save-excursion
(goto-char (point-min))
(re-search-forward code-cells-boundary-regexp 5000 t))
(code-cells-mode)))
(easy-menu-define code-cells-mode-menu code-cells-mode-map
"Menu for `code-cells-mode'."
'("Notebook"
["Previous" code-cells-backward-cell
:help "Go to the previous code cell boundary"]
["Next" code-cells-forward-cell
:help "Go to the next code cell boundary"]
["Move Cell Up" code-cells-move-cell-up
:help "Transpose the current code cell and the cell above"]
["Move Cell Down" code-cells-move-cell-down
:help "Transpose the current code cell and the cell below"]
"---"
["Cut" code-cells-kill
:help "Cut (kill) the current code cell"]
["Copy" code-cells-copy
:help "Copy the content of the current code cell"]
["Select" code-cells-mark-cell
:help "Mark region of the current code cell"]
["Duplicate" code-cells-duplicate
:help "Insert a copy of the current code cell below it"]
["Comment Out" code-cells-comment-or-uncomment
:help "Comment or uncomment the current code cell"]
"---"
["Evaluate" code-cells-eval
:help "Evaluate current code cell (or region, if active) in a suitable shell"]
["Evaluate and Advance" code-cells-eval-and-step
:help "Evaluate current code cell and go to the next one"]
["Evaluate Cells Above" code-cells-eval-above
:help "Evaluate all code cells above the current one"]
["Evaluate Cells Below" code-cells-eval-below
:help "Evaluate current code cell and all cells below it"]
["Evaluate Whole Buffer" code-cells-eval-whole-buffer
:help "Evaluate entire buffer in a suitable shell"]))
(defun code-cells--context-menu (menu click)
"Populate MENU with code cells commands."
(if (use-region-p)
(keymap-set menu "<code-cells-eval>"
'(menu-item "Evaluate Region" code-cells-eval
:help "Evaluate the marked region"))
(let ((bound (save-excursion
(mouse-set-point click)
(car (code-cells--bounds)))))
(keymap-set menu "<code-cells-eval-buffer>"
(if (eq bound (point-min))
`(menu-item "Evaluate Whole Buffer" ,(lambda (_) (interactive "i")
(code-cells-eval-whole-buffer))
:help "Evaluate the entire buffer")
`(menu-item "Evaluate Cells Above" ,(lambda (_) (interactive "i")
(code-cells-eval-above bound))
:help "Evaluate code cells above click"))))
(keymap-set menu "<code-cells-eval>"
`(menu-item
"Evaluate Cell"
,(lambda (e) (interactive "e")
(save-excursion
(mouse-set-point e)
(call-interactively #'code-cells-eval)))
:help "Evaluate the code cell at click")))
(keymap-set-after menu "<select-region> <mark-code-cell>"
`(menu-item "Cell" ,(lambda (e) (interactive "e")
(mark-thing-at-mouse e 'code-cell))
:help "Mark the code cell at click for a subsequent cut/copy")
'mark-whole-buffer)
menu)
;;; Jupyter notebook conversion
(defun code-cells--call-process (buffer command)
"Pipe BUFFER through COMMAND, with output to the current buffer.
Returns the process exit code. COMMAND is a list of strings, the
program name followed by arguments."
(unless (executable-find (car command))
(error "Can't find %s" (car command)))
(let ((logfile (make-temp-file "emacs-code-cells-")))
(unwind-protect
(prog1
(apply #'call-process-region nil nil (car command) nil
(list buffer logfile) nil
(cdr command))
(with-temp-buffer
(insert-file-contents logfile)
(unless (zerop (buffer-size))
(lwarn 'code-cells :warning
"Notebook conversion command %s said:\n%s"
command
(buffer-substring-no-properties
(point-min) (point-max))))))
(delete-file logfile))))
(defun code-cells--guess-mode ()
"Guess major mode associated to the current ipynb buffer."
(goto-char (point-min))
;; Skip over the possibly huge "cells" section
(search-forward "{")
(while (not (equal (json-parse-buffer) "metadata"))
(forward-sexp)
(search-forward ","))
(search-forward ":")
(let* ((metadata (json-parse-buffer :object-type 'alist))
(lang (let-alist metadata
(or .kernelspec.language
.jupytext.main_language)))
(mode (intern (concat lang "-mode"))))
(alist-get mode (bound-and-true-p major-mode-remap-alist) mode)))
;;;###autoload
(defun code-cells-convert-ipynb ()
"Convert buffer from ipynb format to a regular script."
(let* ((mode (funcall (caddr code-cells-convert-ipynb-style)))
(exit (progn
(goto-char (point-min))
(code-cells--call-process t (cadr code-cells-convert-ipynb-style)))))
(unless (zerop exit)
(delete-region (point-min) (point))
(error "Error converting notebook (exit code %s)" exit))
(delete-region (point) (point-max))
(goto-char (point-min))
(set-buffer-modified-p nil)
(add-hook 'write-file-functions #'code-cells-write-ipynb 80 t)
(when (fboundp mode)
(funcall mode)
(run-hooks (cadddr code-cells-convert-ipynb-style)))))
;;;###autoload
(defun code-cells-write-ipynb (&optional file)
"Convert buffer to ipynb format and write to FILE.
Interactively, asks for the file name. When called from Lisp,
FILE defaults to the current buffer file name."
(interactive "F")
(let* ((file (or file buffer-file-name))
(temp (generate-new-buffer " *cells--call-process output*"))
(exit (code-cells--call-process temp (car code-cells-convert-ipynb-style))))
(unless (eq 0 exit)
(error "Error converting notebook (exit code %s)" exit))
(with-current-buffer temp
(write-region nil nil file)
(kill-buffer))
(when (eq file buffer-file-name)
(set-buffer-modified-p nil)
(set-visited-file-modtime))
'job-done))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.ipynb\\'" . code-cells-convert-ipynb))
(provide 'code-cells)
;;; code-cells.el ends here
|