-
Notifications
You must be signed in to change notification settings - Fork 122
/
ein-cell.el
1297 lines (1112 loc) · 52.2 KB
/
ein-cell.el
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; ein-cell.el --- Cell module
;; (C) 2012 - Takafumi Arakaki
;; (C) 2017 - John M. Miller
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
;; Author: John Miller <millejoh at mac.com>
;; This file is NOT part of GNU Emacs.
;; ein-cell.el 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.
;; ein-cell.el 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 ein-cell.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Implementation note. Current implementation of cell has redundant
;; and not-guaranteed-to-be consistent information: `element' and
;; `ein:$node'. This part must be moved to ein-node.el module to
;; make it well capsuled.
;; IPython has cell.js, codecell.js and textcell.js.
;; But let's start with one file.
;;; Code:
(eval-when-compile (require 'cl))
(require 'ansi-color)
(require 'comint)
(require 'ein-core)
(require 'ein-classes)
(require 'ein-log)
(require 'ein-node)
(require 'ein-kernel)
(require 'ein-output-area)
(require 'ein-skewer)
(require 'ein-hy)
;;; Faces
(defface ein:cell-input-prompt
'((t :inherit header-line))
"Face for cell input prompt"
:group 'ein)
(defface ein:cell-input-area
'((((class color) (background light))
:background "honeydew1")
(((class color) (background dark))
:background "#383838"))
"Face for cell input area"
:group 'ein)
(defface ein:cell-output-area
'()
"Face for cell output area"
:group 'ein)
(defface ein:cell-output-area-error
'()
"Face for cell output area errors"
:group 'ein)
(defface ein:cell-heading-1
'((t :height 1.1 :inherit ein:cell-heading-2))
"Face for level 1 heading."
:group 'ein)
(defface ein:cell-heading-2
'((t :height 1.1 :inherit ein:cell-heading-3))
"Face for level 2 heading."
:group 'ein)
(defface ein:cell-heading-3
'((t :height 1.1 :inherit ein:cell-heading-4))
"Face for level 3 heading."
:group 'ein)
(defface ein:cell-heading-4
'((t :height 1.1 :inherit ein:cell-heading-5))
"Face for level 4 heading."
:group 'ein)
(defface ein:cell-heading-5
'((t :height 1.1 :inherit ein:cell-heading-6))
"Face for level 5 heading."
:group 'ein)
(defface ein:cell-heading-6
'((t :weight bold :inherit (variable-pitch ein:cell-input-area)))
"Face for level 6 heading."
:group 'ein)
(defface ein:cell-output-prompt
'((t :inherit header-line))
"Face for cell output prompt"
:group 'ein)
(defface ein:cell-output-stderr
'((((class color) (background light))
:background "PeachPuff")
(((class color) (background dark))
:background "#8c5353"))
"Face for stderr cell output"
:group 'ein)
(defface ein:pos-tip-face
'((t (:inherit 'popup-tip-face)))
"Face for tooltip when using pos-tip backend."
:group 'ein)
;;; Customization
(defcustom ein:enable-dynamic-javascript nil
"[EXPERIMENTAL] When non-nil enable support in ein for
executing dynamic javascript. This feature requires installation
of the skewer package."
:type 'boolean
:group 'ein)
(defcustom ein:cell-traceback-level 1
"Number of traceback stack to show.
Hidden tracebacks are not discarded.
You can view them using \\[ein:tb-show]."
:type '(choice (integer :tag "Depth of stack to show" 1)
(const :tag "Show all traceback" nil))
:group 'ein)
(defcustom ein:cell-max-num-outputs nil
"Number of maximum outputs to be shown by default.
To view full output, use `ein:notebook-show-in-shared-output'."
:type '(choice (integer :tag "Number of outputs to show" 5)
(const :tag "Show all traceback" nil))
:group 'ein)
(defcustom ein:cell-autoexec-prompt "⚡"
"String shown in the cell prompt when the auto-execution flag
is on. See also `ein:connect-aotoexec-lighter'."
:type 'string
:group 'ein)
(defcustom ein:slice-image nil
"[EXPERIMENTAL] When non-`nil', use `insert-sliced-image' when
drawing images. If it is of the form of ``(ROWS COLS)``, it is
passed to the corresponding arguments of `insert-sliced-image'.
.. FIXME: ROWS and COLS must be determined dynamically by measuring
the size of iamge and Emacs window.
See also: https://github.com/tkf/emacs-ipython-notebook/issues/94"
:type 'boolean
:group 'ein)
(defcustom ein:truncate-long-cell-output nil
"When nil do not truncate cells with long outputs. When set to
a number will limit the number of lines in a cell output."
:type '(choice (integer :tag "Number of lines to show in a cell" 5)
(const :tag "Do not truncate cells with long outputs" nil))
:group 'ein)
(defcustom ein:on-execute-reply-functions nil
"List of functions to call after receiving an \"execute_reply\"
message on the shell channel, just before updating the
worksheet. Each function should have the same call signature as
`ein:cell--handle-execute-reply'."
:type 'list
:group 'ein)
;;; EIEIO related utils
(defmacro ein:oset-if-empty (obj slot value)
`(unless (and (slot-boundp ,obj ,slot) (slot-value ,obj ,slot))
(setf (slot-value ,obj, slot) ,value)))
(defmacro ein:oref-safe (obj slot)
`(when (slot-boundp ,obj ,slot)
(slot-value ,obj ,slot)))
;;; Utils
(defvar ein:mime-type-map
'((image/svg+xml . svg) (image/png . png) (image/jpeg . jpeg)))
(defun ein:insert-image (&rest args)
;; Try to insert the image, otherwise emit a warning message and proceed.
(condition-case-unless-debug err
(let* ((img (apply #'create-image args)))
(if ein:slice-image
(destructuring-bind (&optional rows cols)
(when (listp ein:slice-image) ein:slice-image)
(insert-sliced-image img "." nil (or rows 20) cols))
(insert-image img ".")))
(error (ein:log 'warn "Could not insert image: %s" err) nil)))
;;; Cell factory
(defun ein:cell-class-from-type (type)
(ein:case-equal type
(("code") 'ein:codecell)
(("hy-code") 'ein:hy-codecell)
(("text") 'ein:textcell)
(("html") 'ein:htmlcell)
(("markdown") 'ein:markdowncell)
(("raw") 'ein:rawcell)
(("heading") 'ein:headingcell)
(("shared-output") 'ein:shared-output-cell)
(t (error "No cell type called %S" type))))
(defun ein:get-slide-show (cell)
(let ((slide-type (slot-value cell 'slidetype))
(ss-table (make-hash-table)))
(setf (gethash 'slide_type ss-table) slide-type)
ss-table))
(defun ein:cell-from-type (type &rest args)
(apply (ein:cell-class-from-type type) args))
(defun ein:cell--determine-cell-type (json-data)
(let ((base-type (plist-get json-data :cell_type))
(metadata (plist-get json-data :metadata)))
(if (and (string-equal base-type "code")
(plist-get :metadata :ein.hycell)
(not (eql (plist-get metadata :ein.hycell) :json-false)))
"hy-code"
base-type)))
(defun ein:cell-from-json (data &rest args)
(let ((cell (ein:cell-init (apply #'ein:cell-from-type
(ein:cell--determine-cell-type data) args)
data)))
(when (plist-get data :metadata)
(ein:oset-if-empty cell 'metadata (plist-get data :metadata))
(ein:aif (plist-get (slot-value cell 'metadata) :slideshow)
(let ((slide-type (nth 0 (cdr it))))
(setf (slot-value cell 'slidetype) slide-type))))
cell))
(cl-defmethod ein:cell-init ((cell ein:codecell) data)
(ein:oset-if-empty cell 'outputs (plist-get data :outputs))
(ein:oset-if-empty cell 'input (or (plist-get data :input)
(plist-get data :source)))
(ein:aif (plist-get data :prompt_number)
(ein:oset-if-empty cell 'input-prompt-number it)
(ein:aif (plist-get data :execution_count)
(ein:oset-if-empty cell 'input-prompt-number it)))
(ein:oset-if-empty cell 'collapsed
(let ((v (or (plist-get data :collapsed)
(plist-get (slot-value cell 'metadata)
:collapsed))))
(if (eql v json-false) nil v)))
cell)
(cl-defmethod ein:cell-init ((cell ein:textcell) data)
(ein:aif (plist-get data :source)
(setf (slot-value cell 'input) it))
cell)
(cl-defmethod ein:cell-init ((cell ein:headingcell) data) ;; FIXME: Was :after method
(cl-call-next-method)
(ein:aif (plist-get data :level)
(setf (slot-value cell 'level) it))
cell)
(cl-defmethod ein:cell-convert ((cell ein:basecell) type)
(let ((new (ein:cell-from-type type)))
;; copy attributes
(loop for k in '(read-only ewoc)
do (setf (slot-value new k) (slot-value cell k)))
;; copy input
(setf (slot-value new 'input) (if (ein:cell-active-p cell)
(ein:cell-get-text cell)
(slot-value cell 'input)))
;; copy slidetype
(setf (slot-value new 'slidetype) (slot-value cell 'slidetype))
;; copy output when the new cell has it
(when (memq :output (slot-value new 'element-names))
(setf (slot-value new 'outputs) (mapcar 'identity (slot-value cell 'outputs))))
new))
(cl-defmethod ein:cell-convert ((cell ein:codecell) type)
(let ((new (cl-call-next-method)))
(when (and (cl-typep new 'ein:codecell)
(slot-boundp cell :kernel))
(setf (slot-value new 'kernel) (slot-value cell 'kernel)))
new))
(cl-defmethod ein:cell-convert ((cell ein:headingcell) type)
(let ((new (cl-call-next-method)))
(when (ein:headingcell-p new)
(setf (slot-value new 'level) (slot-value cell 'level)))
new))
(cl-defmethod ein:cell-copy ((cell ein:basecell))
(ein:cell-convert cell (slot-value cell 'cell-type)))
(cl-defmethod ein:cell-convert-inplace ((cell ein:basecell) type)
"Convert CELL to TYPE and redraw corresponding ewoc nodes."
(let ((new (ein:cell-convert cell type)))
;; copy element attribute
(loop for k in (slot-value new 'element-names)
with old-element = (slot-value cell 'element)
do (progn
(setf (slot-value new 'element)
(plist-put (slot-value new 'element) k
(plist-get old-element k)))))
;; setting ewoc nodes
(loop for en in (ein:cell-all-element cell)
for node = (ewoc-data en)
do (setf (ein:$node-data node) new))
(let ((inhibit-read-only t)
(buffer-undo-list t)) ; disable undo recording
;; delete ewoc nodes that is not copied
(apply
#'ewoc-delete (slot-value new 'ewoc)
(apply
#'append
(loop for name in (slot-value cell 'element-names)
unless (memq name (slot-value new 'element-names))
collect (let ((ens (ein:cell-element-get cell name)))
(if (listp ens) ens (list ens))))))
;; draw ewoc node
(loop with ewoc = (slot-value new 'ewoc)
for en in (ein:cell-all-element new)
do (ewoc-invalidate ewoc en)))
new))
(cl-defmethod ein:cell-change-level ((cell ein:headingcell) level)
(assert (integerp level))
(let ((inhibit-read-only t)
(buffer-undo-list t)) ; disable undo recording
(setf (slot-value cell 'level) level)
;; draw ewoc node
(loop with ewoc = (slot-value cell 'ewoc)
for en in (ein:cell-all-element cell)
do (ewoc-invalidate ewoc en))))
;;; Getter/setter
(cl-defmethod ein:cell-num-outputs ((cell ein:codecell))
(length (slot-value cell 'outputs)))
(cl-defmethod ein:cell-num-outputs ((cell ein:textcell))
0)
(cl-defmethod ein:cell-element-get ((cell ein:basecell) prop &rest args)
"Return ewoc node named PROP in CELL.
If PROP is `:output' a list of ewoc nodes is returned.
A specific node can be specified using optional ARGS."
(if (memq prop (slot-value cell 'element-names))
(plist-get (slot-value cell 'element) prop)
(error "PROP %s is not supported." prop)))
(cl-defmethod ein:cell-element-get ((cell ein:codecell) prop &optional index)
(let ((element (slot-value cell 'element)))
(if index
(progn
(assert (eql prop :output))
(nth index (plist-get element prop)))
(case prop
(:after-input
(ein:aif (nth 0 (plist-get element :output))
it
(plist-get element :footer)))
(:after-output (plist-get element :footer))
(:before-input (plist-get element :prompt))
(:before-output (plist-get element :input))
(:last-output
(ein:aif (plist-get element :output)
(car (last it))
(plist-get element :input)))
(t (cl-call-next-method))))))
(cl-defmethod ein:cell-element-get ((cell ein:textcell) prop &rest args)
(let ((element (slot-value cell 'element)))
(case prop
(:after-input (plist-get element :footer))
(:before-input (plist-get element :prompt))
(t (cl-call-next-method)))))
(cl-defmethod ein:cell-all-element ((cell ein:basecell))
(list (ein:cell-element-get cell :prompt)
(ein:cell-element-get cell :input)
(ein:cell-element-get cell :footer)))
(cl-defmethod ein:cell-all-element ((cell ein:codecell))
(append (cl-call-next-method)
(ein:cell-element-get cell :output)))
(cl-defmethod ein:cell-language ((cell ein:basecell))
"Programming language used for CELL.
Return language name as a string or `nil' when not defined.
(fn cell)")
(cl-defmethod ein:cell-language ((cell ein:codecell))
(ein:and-let* ((kernel (slot-value cell 'kernel))
(kernelspec (ein:$kernel-kernelspec kernel)))
(ein:$kernelspec-language kernelspec)))
(cl-defmethod ein:cell-language ((cell ein:markdowncell)) nil "markdown")
(cl-defmethod ein:cell-language ((cell ein:htmlcell)) nil "html")
(cl-defmethod ein:cell-language ((cell ein:rawcell)) nil "rst")
;; EWOC
(defun ein:cell-make-element (make-node num-outputs)
(let ((buffer-undo-list t)) ; disable undo recording
(list
:prompt (funcall make-node 'prompt)
:input (funcall make-node 'input)
:output (loop for i from 0 below num-outputs
collect (funcall make-node 'output i))
:footer (funcall make-node 'footer))))
(cl-defmethod ein:cell-enter-last ((cell ein:basecell))
(let* ((ewoc (slot-value cell 'ewoc))
;; Use `cell' as data for ewoc. Use the whole cell data even
;; if it is not used, to access it from the notebook buffer.
;; It is equivalent to `this.element.data("cell", this)' in
;; IPython.Cell (see cell.js).
(make-node
(lambda (&rest path)
(ewoc-enter-last ewoc (ein:node-new `(cell ,@path) cell))))
(element (ein:cell-make-element make-node
(ein:cell-num-outputs cell))))
(setf (slot-value cell 'element) element)
cell))
(cl-defmethod ein:cell-enter-first ((cell ein:basecell))
(let* ((ewoc (slot-value cell 'ewoc))
(node nil)
(make-node
(lambda (&rest path)
(let ((ewoc-data (ein:node-new `(cell ,@path) cell)))
(setq node
(if node
(ewoc-enter-after ewoc node ewoc-data)
(ewoc-enter-first ewoc ewoc-data))))))
(element (ein:cell-make-element make-node
(ein:cell-num-outputs cell))))
(setf (slot-value cell 'element) element)
cell))
(cl-defmethod ein:cell-insert-below ((base-cell ein:basecell) other-cell)
(let* ((ewoc (slot-value base-cell 'ewoc))
(node (ein:cell-element-get base-cell :footer))
(make-node
(lambda (&rest path)
(setq node (ewoc-enter-after
ewoc node (ein:node-new `(cell ,@path) other-cell)))))
(element (ein:cell-make-element make-node
(ein:cell-num-outputs other-cell))))
(setf (slot-value other-cell 'element) element)
other-cell))
(defun ein:cell-pp (path data)
(case (car path)
(prompt (ein:cell-insert-prompt data))
(input (ein:cell-insert-input data))
(output (ein:cell-insert-output (cadr path) data))
(footer (ein:cell-insert-footer data))))
(defun ein:maybe-show-slideshow-data (cell)
(unless (equal (slot-value cell 'slidetype) "-")
(format " - Slide [%s]:" (slot-value cell 'slidetype))))
(cl-defmethod ein:cell-insert-prompt ((cell ein:codecell))
"Insert prompt of the CELL in the buffer.
Called from ewoc pretty printer via `ein:cell-pp'."
;; Newline is inserted in `ein:cell-insert-input'.
(ein:insert-read-only
(concat
(format "In [%s]:" (or (ein:oref-safe cell 'input-prompt-number) " "))
(ein:maybe-show-slideshow-data cell)
(when (slot-value cell 'autoexec) " %s" ein:cell-autoexec-prompt))
'font-lock-face 'ein:cell-input-prompt))
(cl-defmethod ein:cell-insert-prompt ((cell ein:textcell))
(ein:insert-read-only
(concat
(format "%s:" (slot-value cell 'cell-type))
(ein:maybe-show-slideshow-data cell))
'font-lock-face 'ein:cell-input-prompt))
(cl-defmethod ein:cell-insert-prompt ((cell ein:headingcell))
(ein:insert-read-only
(concat
(format "h%s:" (slot-value cell 'level))
(ein:maybe-show-slideshow-data cell))
'font-lock-face 'ein:cell-input-prompt))
(cl-defmethod ein:cell-insert-input ((cell ein:basecell))
"Insert input of the CELL in the buffer.
Called from ewoc pretty printer via `ein:cell-pp'."
(let ((start (1+ (point))))
;; Newlines must allow insertion before/after its position.
(insert (propertize "\n" 'read-only t 'rear-nonsticky t))
(insert (or (ein:oref-safe cell 'input) "")
(propertize "\n" 'read-only t))
;; Highlight background using overlay.
(let ((ol (make-overlay start (point))))
(overlay-put ol 'face (ein:cell-get-input-area-face cell))
;; `evaporate' = `t': Overlay is deleted when the region become empty.
(overlay-put ol 'evaporate t))))
(cl-defmethod ein:cell-get-input-area-face ((cell ein:basecell))
"Return the face (symbol) for input area."
'ein:cell-input-area)
(cl-defmethod ein:cell-get-input-area-face ((cell ein:headingcell))
(intern (format "ein:cell-heading-%d" (slot-value cell 'level))))
(cl-defmethod ein:cell-get-output-area-face-for-output-type (output-type)
"Return the face (symbol) for output area."
(ein:case-equal output-type
(("pyout") 'ein:cell-output-area)
(("pyerr") 'ein:cell-output-area-error)
(("error") 'ein:cell-output-area-error)
(("display_data") 'ein:cell-output-area)
(("execute_result") 'ein:cell-output-area)
(("stream") 'ein:cell-output-area)))
(defun ein:cell-insert-output (index cell)
"Insert INDEX-th output of the CELL in the buffer.
Called from ewoc pretty printer via `ein:cell-pp'."
(if (or (slot-value cell 'collapsed)
(and ein:cell-max-num-outputs
(>= index ein:cell-max-num-outputs)))
(progn
(when (and (not (slot-value cell 'collapsed))
(= index ein:cell-max-num-outputs)
(> (point) (point-at-bol)))
;; The first output which exceeds `ein:cell-max-num-outputs'.
(ein:insert-read-only "\n"))
(ein:insert-read-only "."))
(let ((out (nth index (slot-value cell 'outputs))))
;; Handle newline for previous stream output.
;; In IPython JS, it is handled in `append_stream' because JS
;; does not need to care about newline (DOM does it for JS).
;; FIXME: Maybe I should abstract ewoc in some way and get rid
;; of this.
(let ((last-out (and (> index 0)
(nth (1- index) (slot-value cell 'outputs)))))
;; If previous output is stream type, consider adding newline
(when (and last-out
(equal (plist-get last-out :output_type) "stream"))
;; Check if the last output is from the same stream.
;; If so, do *NOT* insert newline, otherwise insert newline.
(unless (and (equal (plist-get out :output_type) "stream")
(equal (plist-get out :stream)
(plist-get last-out :stream)))
(ein:cell-append-stream-text-fontified "\n" last-out))))
;; Finally insert real data
(let ((start (point))
(output-type (plist-get out :output_type)))
(ein:case-equal output-type
(("pyout") (ein:cell-append-pyout cell out))
(("pyerr") (ein:cell-append-pyerr cell out))
(("error") (ein:cell-append-pyerr cell out))
(("display_data") (ein:cell-append-display-data cell out))
(("execute_result") (ein:cell-append-pyout cell out))
(("stream") (ein:cell-append-stream cell out)))
(let ((ol (make-overlay start (point))))
(overlay-put ol 'face (ein:cell-get-output-area-face-for-output-type output-type))
(overlay-put ol 'evaporate t))))))
(cl-defmethod ein:cell-insert-footer ((cell ein:basecell))
"Insert footer (just a new line) of the CELL in the buffer.
Called from ewoc pretty printer via `ein:cell-pp'."
(ein:insert-read-only "\n"))
(cl-defmethod ein:cell-insert-footer :before ((cell ein:codecell))
(if (or (slot-value cell 'collapsed)
(and ein:cell-max-num-outputs
(> (ein:cell-num-outputs cell) ein:cell-max-num-outputs)))
;; Add a newline after the last ".".
(ein:insert-read-only "\n")
(let ((last-out (car (last (slot-value cell 'outputs)))))
(when (equal (plist-get last-out :output_type) "stream")
(ein:cell-append-stream-text-fontified "\n" last-out)))))
(defun ein:cell-node-p (node &optional element-name)
(let* ((path (ein:$node-path node))
(p0 (car path))
(p1 (cadr path))
(cell (ein:$node-path node)))
(and cell (eql p0 'cell) (or (not element-name) (eql p1 element-name)))))
(defun ein:cell-ewoc-node-p (ewoc-node &optional element-name)
(ein:cell-node-p (ewoc-data ewoc-node) element-name))
(defun ein:cell-from-ewoc-node (ewoc-node)
(ein:aand ewoc-node (ewoc-data it) (ein:$node-data it)))
(cl-defmethod ein:cell-input-pos-min ((cell ein:basecell))
"Return editable minimum point in the input area of the CELL.
If the input area of the CELL does not exist, return `nil'"
(let* ((input-node (ein:cell-element-get cell :input)))
;; 1+ for skipping newline
(when input-node (1+ (ewoc-location input-node)))))
(cl-defmethod ein:cell-input-pos-max ((cell ein:basecell))
"Return editable maximum point in the input area of the CELL.
If the input area of the CELL does not exist, return `nil'"
(let* ((ewoc (slot-value cell 'ewoc))
(input-node (ein:cell-element-get cell :input)))
;; 1- for skipping newline
(when input-node (1- (ewoc-location (ewoc-next ewoc input-node))))))
(cl-defmethod ein:cell-get-text ((cell ein:basecell))
"Grab text in the input area of the cell at point."
(if (ein:cell-active-p cell)
(let* ((beg (ein:cell-input-pos-min cell))
(end (ein:cell-input-pos-max cell)))
(buffer-substring beg end))
(slot-value cell 'input)))
(cl-defmethod ein:cell-set-text ((cell ein:basecell) text)
(let* ((input-node (ein:cell-element-get cell :input))
(ewoc (slot-value cell 'ewoc))
;; 1+/1- is for skipping newline
(beg (1+ (ewoc-location input-node)))
(end (1- (ewoc-location (ewoc-next ewoc input-node)))))
(save-excursion
;; probably it is better to set :input and update via ewoc?
(goto-char beg)
(delete-region beg end)
(insert text))))
(cl-defmethod ein:cell-save-text ((cell ein:basecell))
(setf (slot-value cell 'input) (ein:cell-get-text cell)))
(cl-defmethod ein:cell-deactivate ((cell ein:basecell))
(setf (slot-value cell 'element) nil)
cell)
(cl-defmethod ein:cell-active-p ((cell ein:basecell))
(slot-value cell 'element))
(cl-defmethod ein:cell-running-set ((cell ein:codecell) running)
;; FIXME: change the appearance of the cell
(setf (slot-value cell 'running) running))
(cl-defmethod ein:cell-set-collapsed ((cell ein:codecell) collapsed)
"Set `:collapsed' slot of CELL and invalidate output ewoc nodes."
(unless (eq (slot-value cell 'collapsed) collapsed)
(setf (slot-value cell 'collapsed) collapsed)
(let ((inhibit-read-only t)
(buffer-undo-list t))
(apply #'ewoc-invalidate
(slot-value cell 'ewoc)
(append (ein:cell-element-get cell :output)
(list (ein:cell-element-get cell :footer)))))))
(cl-defmethod ein:cell-collapse ((cell ein:codecell))
(ein:cell-set-collapsed cell t))
(cl-defmethod ein:cell-expand ((cell ein:codecell))
(ein:cell-set-collapsed cell nil))
(cl-defmethod ein:cell-toggle-output ((cell ein:codecell))
"Toggle `:collapsed' slot of CELL and invalidate output ewoc nodes."
(ein:cell-set-collapsed cell (not (slot-value cell 'collapsed))))
(cl-defmethod ein:cell-invalidate-prompt ((cell ein:codecell))
(let ((inhibit-read-only t)
(buffer-undo-list t)) ; disable undo recording
(ewoc-invalidate (slot-value cell 'ewoc)
(ein:cell-element-get cell :prompt))))
(cl-defmethod ein:cell-set-input-prompt ((cell ein:codecell) &optional number)
(setf (slot-value cell 'input-prompt-number) number)
(ein:cell-invalidate-prompt cell))
(cl-defmethod ein:cell-set-autoexec ((cell ein:codecell) bool)
"Set auto-execution flag of CELL to BOOL and invalidate the
prompt EWOC node."
(setf (slot-value cell 'autoexec) bool)
(ein:cell-invalidate-prompt cell))
(cl-defmethod ein:cell-autoexec-p ((cell ein:basecell))
"Auto-execution flag set to CELL.
Return `nil' always for non-code cells."
nil)
(cl-defmethod ein:cell-autoexec-p ((cell ein:codecell))
(slot-value cell 'autoexec))
(cl-defmethod ein:cell-toggle-autoexec ((cell ein:codecell))
"Toggle auto-execution flag of CELL to BOOL and invalidate the
prompt EWOC node."
(ein:cell-set-autoexec cell (not (ein:cell-autoexec-p cell))))
(cl-defmethod ein:cell-goto ((cell ein:basecell) &optional relpos prop)
"Go to the input area of the given CELL.
RELPOS is the position relative to the input area. Default is 0.
PROP is a name of cell element. Default is `:input'.
\(fn cell relpos prop)"
(unless relpos (setq relpos 0))
(unless prop (setq prop :input))
(ewoc-goto-node (slot-value cell 'ewoc) (ein:cell-element-get cell prop))
(let ((offset (case prop
((:input :before-output) 1)
(:after-input -1)
(t 0))))
(forward-char (+ relpos offset))))
(cl-defmethod ein:cell-goto-line ((cell ein:basecell) &optional inputline prop)
"Go to the input area of the given CELL.
INPUTLINE is the line number relative to the input area. Default is 1.
PROP is a name of cell element. Default is `:input'.
\(fn cell inputline prop)"
(unless inputline (setq inputline 1))
(unless prop (setq prop :input))
(let ((goal-column nil))
(ewoc-goto-node (slot-value cell 'ewoc) (ein:cell-element-get cell prop)))
(let ((offset (case prop
((:input :before-output) 1)
(:after-input -1)
(t 0))))
(forward-char offset)
(forward-line (- inputline 1))))
(cl-defmethod ein:cell-relative-point ((cell ein:basecell) &optional pos)
"Return the point relative to the input area of CELL.
If the position POS is not given, current point is considered."
(unless pos (setq pos (point)))
(- pos (1+ (ewoc-location (ein:cell-element-get cell :input)))))
(cl-defmethod ein:cell-location ((cell ein:basecell) &optional elm end)
"Return the starting location of CELL.
ELM is a name (keyword) of element that `ein:cell-element-get'
understands. Note that you can't use `:output' since it returns
a list. Use `:after-input' instead.
If END is non-`nil', return the location of next element."
(unless elm (setq elm :prompt))
(let ((element (slot-value cell 'element)))
(when end
(setq elm (case elm
(:prompt :input)
(:input :after-input)
(:output :after-output)))
(unless elm
(setq cell (ein:cell-next cell))
(setq elm :prompt)))
(if cell
(ewoc-location (ein:cell-element-get cell elm))
(assert end)
(point-max))))
(cl-defmethod ein:cell-buffer ((cell ein:basecell))
"Return a buffer associated by CELL (if any)."
(ein:aand (ein:oref-safe cell 'ewoc) (ewoc-buffer it)))
;; Data manipulation
(cl-defmethod ein:cell-clear-output ((cell ein:codecell) stdout stderr other)
;; codecell.js in IPython implements it using timeout and callback.
;; As it is unclear why timeout is needed, just clear output
;; instantaneously for now.
(ein:log 'debug "cell-clear-output stdout=%s stderr=%s other=%s"
stdout stderr other)
(setf (slot-value cell 'traceback) nil)
(let ((ewoc (slot-value cell 'ewoc))
(output-nodes (ein:cell-element-get cell :output)))
(if (and stdout stderr other)
(progn
;; clear all
(let ((inhibit-read-only t)
(buffer-undo-list t)) ; disable undo recording
(apply #'ewoc-delete ewoc output-nodes))
(plist-put (slot-value cell 'element) :output nil)
(setf (slot-value cell 'outputs) nil))
(let* ((ewoc-node-list
(append
(when stdout (ein:node-filter output-nodes :is 'output-stdout))
(when stderr (ein:node-filter output-nodes :is 'output-stderr))
(when stdout (ein:node-filter output-nodes
:is 'output-subarea
:not 'output-stderr
:not 'output-stdout))))
(indices
(mapcar (lambda (n) (last (ein:$node-path (ewoc-data n))))
ewoc-node-list)))
;; remove from buffer
(let ((inhibit-read-only t)
(buffer-undo-list t)) ; disable undo recording
(apply #'ewoc-delete ewoc ewoc-node-list))
;; remove from `:element'
(let* ((element (slot-value cell 'element))
(old-output (plist-get element :output))
(new-ouptut (ein:remove-by-index old-output indices)))
(plist-put element :output new-ouptut))
;; remove cleared outputs from internal data
(setf (slot-value cell 'outputs)
(ein:remove-by-index (slot-value cell 'outputs) indices))))
;; Footer may have extra (possibly colored) newline due to the
;; last output type. So invalidate it here.
;; See `ein:cell-insert-footer' (for codecell).
(let ((buffer-undo-list t)) ; disable undo recording
(ewoc-invalidate ewoc (ein:cell-element-get cell :footer)))))
(defun ein:cell-output-json-to-class (json)
(ein:case-equal (plist-get json :output_type)
(("pyout")
'(output-subarea))
(("pyerr")
'(output-subarea))
(("error")
'(output-subarea))
(("display_data")
'(output-subarea))
(("execute_result")
'(output-subarea))
(("stream")
(list 'output-stream 'output-subarea
(intern (format "output-%s" (plist-get json :stream)))))))
(cl-defmethod ein:cell-append-output ((cell ein:codecell) json dynamic)
;; When there is a python error, we actually get two identical tracebacks back
;; from the kernel, one from the "shell" channel, and one from the "iopub"
;; channel. As a workaround, we remember the cell's traceback and ignore
;; traceback outputs that are identical to the one we already have.
(let ((new-tb (plist-get json :traceback))
(old-tb (slot-value cell 'traceback)))
(when (or
(null old-tb)
(null new-tb)
(not (equalp new-tb old-tb)))
(ein:cell-actually-append-output cell json dynamic))
(setf (slot-value cell 'traceback) new-tb)))
(cl-defmethod ein:cell-actually-append-output ((cell ein:codecell) json dynamic)
(ein:cell-expand cell)
;; (ein:flush-clear-timeout)
(setf (slot-value cell 'outputs)
(append (slot-value cell 'outputs) (list json)))
;; enter last output element
(let* ((inhibit-read-only t)
(buffer-undo-list t) ; disable undo recording
(ewoc (slot-value cell 'ewoc))
(index (1- (ein:cell-num-outputs cell)))
(path `(cell output ,index))
(class (ein:cell-output-json-to-class json))
(data (ein:node-new path cell class))
(last-node (ein:cell-element-get cell :last-output))
(ewoc-node (ewoc-enter-after ewoc last-node data))
(element (slot-value cell 'element)))
(plist-put element :output
(append (plist-get element :output) (list ewoc-node)))
(ewoc-invalidate ewoc (ein:cell-element-get cell :footer))))
(cl-defmethod ein:cell-append-pyout ((cell ein:codecell) json)
"Insert pyout type output in the buffer.
Called from ewoc pretty printer via `ein:cell-insert-output'."
(ein:insert-read-only (format "Out [%s]:"
(or (plist-get json :prompt_number) " "))
'font-lock-face 'ein:cell-output-prompt)
(ein:insert-read-only "\n")
(ein:cell-append-mime-type json (slot-value cell 'dynamic))
(ein:insert-read-only "\n"))
(cl-defmethod ein:cell-append-pyerr ((cell ein:codecell) json)
"Insert pyerr type output in the buffer.
Called from ewoc pretty printer via `ein:cell-insert-output'."
(mapc (lambda (tb)
(ein:cell-append-text tb)
(ein:cell-append-text "\n"))
(let ((tb (plist-get json :traceback))
(level ein:cell-traceback-level))
(if (and level (> (- (length tb) 2) level))
(cons (substitute-command-keys
"\nTruncated Traceback (Use \\[ein:tb-show] to view full TB):")
(last tb (1+ level)))
tb)))
(ein:insert-read-only "\n"))
(ein:deflocal ein:%cell-append-stream-last-cell% nil
"The last cell in which `ein:cell-append-stream' is used.")
(cl-defmethod ein:cell-append-stream ((cell ein:codecell) json)
"Insert stream type output in the buffer.
Called from ewoc pretty printer via `ein:cell-insert-output'."
;; (unless (plist-get json :stream)
;; (plist-put json :stream "stdout"))
(unless (eq cell ein:%cell-append-stream-last-cell%)
;; Avoid applying unclosed ANSI escape code in the cell. Note
;; that I don't need to distinguish stdout/stderr because it looks
;; like normal terminal does not.
(setq ansi-color-context nil))
(let ((start (point)))
(ein:cell-append-stream-text-fontified (or (plist-get json :text) "") json)
(comint-carriage-motion start (point)))
;; NOTE: newlines for stream is handled in `ein:cell-insert-output'.
;; So do not insert newline here.
(setq ein:%cell-append-stream-last-cell% cell))
(defun ein:cell-append-stream-text-fontified (text json)
"Insert TEXT with font properties defined by JSON data."
(if (equal (plist-get json :stream) "stderr")
(ein:cell-append-text text 'font-lock-face 'ein:cell-output-stderr)
(ein:cell-append-text text)))
(cl-defmethod ein:cell-append-display-data ((cell ein:codecell) json)
"Insert display-data type output in the buffer.
Called from ewoc pretty printer via `ein:cell-insert-output'."
(if (and (or (plist-get json :javascript)
(plist-get json :html))
(slot-value cell 'dynamic) ein:enable-dynamic-javascript)
(ein:execute-javascript cell json)
(progn
(ein:cell-append-mime-type json (slot-value cell 'dynamic))
(ein:insert-read-only "\n"))))
(defcustom ein:output-type-preference
(if (and (fboundp 'shr-insert-document)
(fboundp 'libxml-parse-xml-region))
#'ein:output-type-prefer-pretty-text-over-html
'(emacs-lisp svg image/svg+xml png image/png jpeg image/jpeg html text/html latex text/latex javascript text/javascript text text/plain))
"Output types to be used in notebook.
First output-type found in this list will be used.
This variable can be a list or a function returning a list given
DATA plist.
See also `ein:output-type-prefer-pretty-text-over-html'.
**Example**:
If you prefer HTML type over text type, you can set it as::
(setq ein:output-type-preference
'(emacs-lisp svg png jpeg html text latex javascript))
Note that ``html`` comes before ``text``."
:type '(choice function (repeat symbol))
:group 'ein)
(defvar ein:output-types-text-preferred
'(emacs-lisp svg image/svg+xml png image/png jpeg image/jpeg text text/plain html text/html latex text/latex javascript text/javascript))
(defvar ein:output-types-html-preferred
'(emacs-lisp svg image/svg+xml png image/png jpeg image/jpeg html text/html latex text/latex javascript text/javascript text text/plain))
(defun ein:output-type-prefer-pretty-text-over-html (data)
"Use text type if it is a \"prettified\" text instead of HTML.
This is mostly for *not* using HTML table for pandas but using
HTML for other object.
If the text type output contains a newline, it is assumed be a
prettified text thus be used instead of HTML type."
(if (ein:aand (or (plist-get data :text)
(plist-get data :text/plain))
(string-match-p "\n" it))
ein:output-types-text-preferred
ein:output-types-html-preferred))
(defun ein:fix-mime-type (type)
(ein:aif (assoc type ein:mime-type-map)
(cdr it)
type))
(defun ein:cell-append-mime-type (json dynamic)
(when (plist-get json :data)
(setq json (plist-get json :data))) ;; For nbformat v4 support.
(loop
for key in (cond
((functionp ein:output-type-preference)
(funcall ein:output-type-preference json))
(t ein:output-type-preference))
for type = (intern (format ":%s" key)) ; something like `:text'
for value = (plist-get json type) ; FIXME: optimize
when (plist-member json type)
return
(case key
;; NOTE: Normally `javascript' and `html' will not be inserted as
;; they come out after `text'. Maybe it is better to inform user
;; when one of them is inserted.
((javascript text/javascript)
(when dynamic
(ein:log 'info (concat "ein:cell-append-mime-type does not support "
"dynamic javascript. got: %s") value))
(ein:insert-read-only (plist-get json type)))
(emacs-lisp
(when dynamic
(ein:cell-safe-read-eval-insert (plist-get json type))))