Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1311 lines
53 KiB

  1. ;;; ein-cell.el --- Cell module
  2. ;; (C) 2012 - Takafumi Arakaki
  3. ;; (C) 2017 - John M. Miller
  4. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  5. ;; Author: John Miller <millejoh at mac.com>
  6. ;; This file is NOT part of GNU Emacs.
  7. ;; ein-cell.el is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; ein-cell.el is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with ein-cell.el. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Implementation note. Current implementation of cell has redundant
  19. ;; and not-guaranteed-to-be consistent information: `element' and
  20. ;; `ein:$node'. This part must be moved to ein-node.el module to
  21. ;; make it well capsuled.
  22. ;; IPython has cell.js, codecell.js and textcell.js.
  23. ;; But let's start with one file.
  24. ;;; Code:
  25. (require 'ansi-color)
  26. (require 'comint)
  27. (require 'ein-core)
  28. (require 'ein-classes)
  29. (require 'ein-log)
  30. (require 'ein-node)
  31. (require 'ein-kernel)
  32. (require 'ein-output-area)
  33. (require 'ein-skewer)
  34. (require 'ein-hy)
  35. ;;; Faces
  36. (defface ein:cell-input-prompt
  37. '((t :inherit header-line))
  38. "Face for cell input prompt"
  39. :group 'ein)
  40. (defface ein:cell-input-area
  41. '((((class color) (background light))
  42. :background "honeydew1")
  43. (((class color) (background dark))
  44. :background "#383838"))
  45. "Face for cell input area"
  46. :group 'ein)
  47. (defface ein:cell-output-area
  48. '()
  49. "Face for cell output area"
  50. :group 'ein)
  51. (defface ein:cell-output-area-error
  52. '()
  53. "Face for cell output area errors"
  54. :group 'ein)
  55. (defface ein:cell-heading-1
  56. '((t :height 1.1 :inherit ein:cell-heading-2))
  57. "Face for level 1 heading."
  58. :group 'ein)
  59. (defface ein:cell-heading-2
  60. '((t :height 1.1 :inherit ein:cell-heading-3))
  61. "Face for level 2 heading."
  62. :group 'ein)
  63. (defface ein:cell-heading-3
  64. '((t :height 1.1 :inherit ein:cell-heading-4))
  65. "Face for level 3 heading."
  66. :group 'ein)
  67. (defface ein:cell-heading-4
  68. '((t :height 1.1 :inherit ein:cell-heading-5))
  69. "Face for level 4 heading."
  70. :group 'ein)
  71. (defface ein:cell-heading-5
  72. '((t :height 1.1 :inherit ein:cell-heading-6))
  73. "Face for level 5 heading."
  74. :group 'ein)
  75. (defface ein:cell-heading-6
  76. '((t :weight bold :inherit (variable-pitch ein:cell-input-area)))
  77. "Face for level 6 heading."
  78. :group 'ein)
  79. (defface ein:cell-output-prompt
  80. '((t :inherit header-line))
  81. "Face for cell output prompt"
  82. :group 'ein)
  83. (defface ein:cell-output-stderr
  84. '((((class color) (background light))
  85. :background "PeachPuff")
  86. (((class color) (background dark))
  87. :background "#8c5353"))
  88. "Face for stderr cell output"
  89. :group 'ein)
  90. (defface ein:pos-tip-face
  91. '((t (:inherit 'popup-tip-face)))
  92. "Face for tooltip when using pos-tip backend."
  93. :group 'ein)
  94. ;;; Customization
  95. (defcustom ein:enable-dynamic-javascript nil
  96. "[EXPERIMENTAL] When non-nil enable support in ein for
  97. executing dynamic javascript. This feature requires installation
  98. of the skewer package."
  99. :type 'boolean
  100. :group 'ein)
  101. (defcustom ein:cell-traceback-level 1
  102. "Number of traceback stack to show.
  103. Hidden tracebacks are not discarded.
  104. You can view them using \\[ein:tb-show]."
  105. :type '(choice (integer :tag "Depth of stack to show" 1)
  106. (const :tag "Show all traceback" nil))
  107. :group 'ein)
  108. (defcustom ein:cell-max-num-outputs nil
  109. "Number of maximum outputs to be shown by default.
  110. To view full output, use `ein:notebook-show-in-shared-output'."
  111. :type '(choice (integer :tag "Number of outputs to show" 5)
  112. (const :tag "Show all traceback" nil))
  113. :group 'ein)
  114. (defcustom ein:cell-autoexec-prompt ""
  115. "String shown in the cell prompt when the auto-execution flag
  116. is on. See also `ein:connect-aotoexec-lighter'."
  117. :type 'string
  118. :group 'ein)
  119. (defcustom ein:slice-image nil
  120. "[EXPERIMENTAL] When non-`nil', use `insert-sliced-image' when
  121. drawing images. If it is of the form of ``(ROWS COLS)``, it is
  122. passed to the corresponding arguments of `insert-sliced-image'.
  123. .. FIXME: ROWS and COLS must be determined dynamically by measuring
  124. the size of iamge and Emacs window.
  125. See also: https://github.com/tkf/emacs-ipython-notebook/issues/94"
  126. :type 'boolean
  127. :group 'ein)
  128. (defcustom ein:truncate-long-cell-output nil
  129. "When nil do not truncate cells with long outputs. When set to
  130. a number will limit the number of lines in a cell output."
  131. :type '(choice (integer :tag "Number of lines to show in a cell" 5)
  132. (const :tag "Do not truncate cells with long outputs" nil))
  133. :group 'ein)
  134. (defcustom ein:on-execute-reply-functions nil
  135. "List of functions to call after receiving an \"execute_reply\"
  136. message on the shell channel, just before updating the
  137. worksheet. Each function should have the same call signature as
  138. `ein:cell--handle-execute-reply'."
  139. :type 'list
  140. :group 'ein)
  141. ;;; EIEIO related utils
  142. (defmacro ein:oset-if-empty (obj slot value)
  143. `(unless (and (slot-boundp ,obj ,slot) (slot-value ,obj ,slot))
  144. (setf (slot-value ,obj, slot) ,value)))
  145. (defmacro ein:oref-safe (obj slot)
  146. `(when (slot-boundp ,obj ,slot)
  147. (slot-value ,obj ,slot)))
  148. ;;; Utils
  149. (defvar ein:mime-type-map
  150. '((image/svg+xml . svg) (image/png . png) (image/jpeg . jpeg)))
  151. (defun ein:insert-image (&rest args)
  152. ;; Try to insert the image, otherwise emit a warning message and proceed.
  153. (condition-case-unless-debug err
  154. (let* ((img (apply #'create-image args)))
  155. (if ein:slice-image
  156. (cl-destructuring-bind (&optional rows cols)
  157. (when (listp ein:slice-image) ein:slice-image)
  158. (insert-sliced-image img "." nil (or rows 20) cols))
  159. (insert-image img ".")))
  160. (error (ein:log 'warn "Could not insert image: %s" err) nil)))
  161. (defun ein:cell--markdown-heading-p (cell)
  162. "Check if cell is behaving like a heading cell.
  163. Returns true if cell is a markdown cell, has at least one # character at the start of the line and is only one line long (i.e. does not contain any newline characters)."
  164. (and (ein:markdowncell-p cell)
  165. (string-match "^#+ " (ein:cell-get-text cell))
  166. (= 0 (seq-count #'(lambda (c)
  167. (char-equal c ?\n))
  168. (ein:cell-get-text cell)))))
  169. (cl-defmethod ein:cell--markdown-heading-level ((cell ein:markdowncell))
  170. (progn
  171. (string-match "^#+" (ein:cell-get-text cell))
  172. (match-end 0)))
  173. ;;; Cell factory
  174. (defun ein:cell-class-from-type (type)
  175. (ein:case-equal type
  176. (("code") 'ein:codecell)
  177. (("hy-code") 'ein:hy-codecell)
  178. (("text") 'ein:textcell)
  179. (("html") 'ein:htmlcell)
  180. (("markdown") 'ein:markdowncell)
  181. (("raw") 'ein:rawcell)
  182. (("heading") 'ein:headingcell)
  183. (("shared-output") 'ein:shared-output-cell)
  184. (t (error "No cell type called %S" type))))
  185. (defun ein:get-slide-show (cell)
  186. (let ((slide-type (slot-value cell 'slidetype))
  187. (ss-table (make-hash-table)))
  188. (setf (gethash 'slide_type ss-table) slide-type)
  189. ss-table))
  190. (defun ein:cell-from-type (type &rest args)
  191. (apply (ein:cell-class-from-type type) args))
  192. (defun ein:cell--determine-cell-type (json-data)
  193. (let ((base-type (plist-get json-data :cell_type))
  194. (metadata (plist-get json-data :metadata)))
  195. (if (and (string-equal base-type "code")
  196. (plist-get :metadata :ein.hycell)
  197. (not (eql (plist-get metadata :ein.hycell) :json-false)))
  198. "hy-code"
  199. base-type)))
  200. (defun ein:cell-from-json (data &rest args)
  201. (let ((cell (ein:cell-init (apply #'ein:cell-from-type
  202. (ein:cell--determine-cell-type data) args)
  203. data)))
  204. (when (plist-get data :metadata)
  205. (ein:oset-if-empty cell 'metadata (plist-get data :metadata))
  206. (ein:aif (plist-get (slot-value cell 'metadata) :slideshow)
  207. (let ((slide-type (nth 0 (cdr it))))
  208. (setf (slot-value cell 'slidetype) slide-type))))
  209. cell))
  210. (cl-defmethod ein:cell-init ((cell ein:codecell) data)
  211. (ein:oset-if-empty cell 'outputs (plist-get data :outputs))
  212. (ein:oset-if-empty cell 'input (or (plist-get data :input)
  213. (plist-get data :source)))
  214. (ein:aif (plist-get data :prompt_number)
  215. (ein:oset-if-empty cell 'input-prompt-number it)
  216. (ein:aif (plist-get data :execution_count)
  217. (ein:oset-if-empty cell 'input-prompt-number it)))
  218. (ein:oset-if-empty cell 'collapsed
  219. (let ((v (or (plist-get data :collapsed)
  220. (plist-get (slot-value cell 'metadata)
  221. :collapsed))))
  222. (if (eql v json-false) nil v)))
  223. cell)
  224. (cl-defmethod ein:cell-init ((cell ein:textcell) data)
  225. (ein:aif (plist-get data :source)
  226. (setf (slot-value cell 'input) it))
  227. cell)
  228. (cl-defmethod ein:cell-init ((cell ein:headingcell) data) ;; FIXME: Was :after method
  229. (cl-call-next-method)
  230. (ein:aif (plist-get data :level)
  231. (setf (slot-value cell 'level) it))
  232. cell)
  233. (cl-defmethod ein:cell-convert ((cell ein:basecell) type)
  234. (let ((new (ein:cell-from-type type)))
  235. ;; copy attributes
  236. (cl-loop for k in '(read-only ewoc)
  237. do (setf (slot-value new k) (slot-value cell k)))
  238. ;; copy input
  239. (setf (slot-value new 'input) (if (ein:cell-active-p cell)
  240. (ein:cell-get-text cell)
  241. (slot-value cell 'input)))
  242. ;; copy slidetype
  243. (setf (slot-value new 'slidetype) (slot-value cell 'slidetype))
  244. ;; copy output when the new cell has it
  245. (when (memq :output (slot-value new 'element-names))
  246. (setf (slot-value new 'outputs) (mapcar 'identity (slot-value cell 'outputs))))
  247. new))
  248. (cl-defmethod ein:cell-convert ((cell ein:codecell) type)
  249. (let ((new (cl-call-next-method)))
  250. (when (and (cl-typep new 'ein:codecell)
  251. (slot-boundp cell :kernel))
  252. (setf (slot-value new 'kernel) (slot-value cell 'kernel)))
  253. new))
  254. (cl-defmethod ein:cell-convert ((cell ein:headingcell) type)
  255. (let ((new (cl-call-next-method)))
  256. (when (ein:headingcell-p new)
  257. (setf (slot-value new 'level) (slot-value cell 'level)))
  258. new))
  259. (cl-defmethod ein:cell-copy ((cell ein:basecell))
  260. (ein:cell-convert cell (slot-value cell 'cell-type)))
  261. (cl-defmethod ein:cell-convert-inplace ((cell ein:basecell) type)
  262. "Convert CELL to TYPE and redraw corresponding ewoc nodes."
  263. (let ((new (ein:cell-convert cell type)))
  264. ;; copy element attribute
  265. (cl-loop for k in (slot-value new 'element-names)
  266. with old-element = (slot-value cell 'element)
  267. do (setf (slot-value new 'element)
  268. (plist-put (slot-value new 'element) k
  269. (plist-get old-element k))))
  270. ;; setting ewoc nodes
  271. (cl-loop for en in (ein:cell-all-element cell)
  272. for node = (ewoc-data en)
  273. do (setf (ein:$node-data node) new))
  274. (let ((inhibit-read-only t)
  275. (buffer-undo-list t)) ; disable undo recording
  276. ;; delete ewoc nodes that is not copied
  277. (apply
  278. #'ewoc-delete (slot-value new 'ewoc)
  279. (apply
  280. #'append
  281. (cl-loop for name in (slot-value cell 'element-names)
  282. unless (memq name (slot-value new 'element-names))
  283. collect (let ((ens (ein:cell-element-get cell name)))
  284. (if (listp ens) ens (list ens))))))
  285. ;; draw ewoc node
  286. (cl-loop with ewoc = (slot-value new 'ewoc)
  287. for en in (ein:cell-all-element new)
  288. do (ewoc-invalidate ewoc en)))
  289. new))
  290. (cl-defmethod ein:cell-change-level ((cell ein:headingcell) level)
  291. (cl-assert (integerp level))
  292. (let ((inhibit-read-only t)
  293. (buffer-undo-list t)) ; disable undo recording
  294. (setf (slot-value cell 'level) level)
  295. ;; draw ewoc node
  296. (cl-loop with ewoc = (slot-value cell 'ewoc)
  297. for en in (ein:cell-all-element cell)
  298. do (ewoc-invalidate ewoc en))))
  299. ;;; Getter/setter
  300. (cl-defmethod ein:cell-num-outputs ((cell ein:codecell))
  301. (length (slot-value cell 'outputs)))
  302. (cl-defmethod ein:cell-num-outputs ((cell ein:textcell))
  303. 0)
  304. (cl-defmethod ein:cell-element-get ((cell ein:basecell) prop &rest args)
  305. "Return ewoc node named PROP in CELL.
  306. If PROP is `:output' a list of ewoc nodes is returned.
  307. A specific node can be specified using optional ARGS."
  308. (if (memq prop (slot-value cell 'element-names))
  309. (plist-get (slot-value cell 'element) prop)
  310. (error "PROP %s is not supported." prop)))
  311. (cl-defmethod ein:cell-element-get ((cell ein:codecell) prop &optional index)
  312. (let ((element (slot-value cell 'element)))
  313. (if index
  314. (progn
  315. (cl-assert (eql prop :output))
  316. (nth index (plist-get element prop)))
  317. (cl-case prop
  318. (:after-input
  319. (ein:aif (nth 0 (plist-get element :output))
  320. it
  321. (plist-get element :footer)))
  322. (:after-output (plist-get element :footer))
  323. (:before-input (plist-get element :prompt))
  324. (:before-output (plist-get element :input))
  325. (:last-output
  326. (ein:aif (plist-get element :output)
  327. (car (last it))
  328. (plist-get element :input)))
  329. (t (cl-call-next-method))))))
  330. (cl-defmethod ein:cell-element-get ((cell ein:textcell) prop &rest args)
  331. (let ((element (slot-value cell 'element)))
  332. (cl-case prop
  333. (:after-input (plist-get element :footer))
  334. (:before-input (plist-get element :prompt))
  335. (t (cl-call-next-method)))))
  336. (cl-defmethod ein:cell-all-element ((cell ein:basecell))
  337. (list (ein:cell-element-get cell :prompt)
  338. (ein:cell-element-get cell :input)
  339. (ein:cell-element-get cell :footer)))
  340. (cl-defmethod ein:cell-all-element ((cell ein:codecell))
  341. (append (cl-call-next-method)
  342. (ein:cell-element-get cell :output)))
  343. (cl-defmethod ein:cell-language ((cell ein:basecell))
  344. "Programming language used for CELL.
  345. Return language name as a string or `nil' when not defined.
  346. (fn cell)")
  347. (cl-defmethod ein:cell-language ((cell ein:codecell))
  348. (ein:and-let* ((kernel (slot-value cell 'kernel))
  349. (kernelspec (ein:$kernel-kernelspec kernel)))
  350. (ein:$kernelspec-language kernelspec)))
  351. (cl-defmethod ein:cell-language ((cell ein:markdowncell)) nil "markdown")
  352. (cl-defmethod ein:cell-language ((cell ein:htmlcell)) nil "html")
  353. (cl-defmethod ein:cell-language ((cell ein:rawcell)) nil "rst")
  354. ;; EWOC
  355. (defun ein:cell-make-element (make-node num-outputs)
  356. (let ((buffer-undo-list t)) ; disable undo recording
  357. (list
  358. :prompt (funcall make-node 'prompt)
  359. :input (funcall make-node 'input)
  360. :output (cl-loop for i from 0 below num-outputs
  361. collect (funcall make-node 'output i))
  362. :footer (funcall make-node 'footer))))
  363. (cl-defmethod ein:cell-enter-last ((cell ein:basecell))
  364. (let* ((ewoc (slot-value cell 'ewoc))
  365. ;; Use `cell' as data for ewoc. Use the whole cell data even
  366. ;; if it is not used, to access it from the notebook buffer.
  367. ;; It is equivalent to `this.element.data("cell", this)' in
  368. ;; IPython.Cell (see cell.js).
  369. (make-node
  370. (lambda (&rest path)
  371. (ewoc-enter-last ewoc (ein:node-new `(cell ,@path) cell))))
  372. (element (ein:cell-make-element make-node
  373. (ein:cell-num-outputs cell))))
  374. (setf (slot-value cell 'element) element)
  375. cell))
  376. (cl-defmethod ein:cell-enter-first ((cell ein:basecell))
  377. (let* ((ewoc (slot-value cell 'ewoc))
  378. (node nil)
  379. (make-node
  380. (lambda (&rest path)
  381. (let ((ewoc-data (ein:node-new `(cell ,@path) cell)))
  382. (setq node
  383. (if node
  384. (ewoc-enter-after ewoc node ewoc-data)
  385. (ewoc-enter-first ewoc ewoc-data))))))
  386. (element (ein:cell-make-element make-node
  387. (ein:cell-num-outputs cell))))
  388. (setf (slot-value cell 'element) element)
  389. cell))
  390. (cl-defmethod ein:cell-insert-below ((base-cell ein:basecell) other-cell)
  391. (let* ((ewoc (slot-value base-cell 'ewoc))
  392. (node (ein:cell-element-get base-cell :footer))
  393. (make-node
  394. (lambda (&rest path)
  395. (setq node (ewoc-enter-after
  396. ewoc node (ein:node-new `(cell ,@path) other-cell)))))
  397. (element (ein:cell-make-element make-node
  398. (ein:cell-num-outputs other-cell))))
  399. (setf (slot-value other-cell 'element) element)
  400. other-cell))
  401. (defun ein:cell-pp (path data)
  402. (cl-case (car path)
  403. (prompt (ein:cell-insert-prompt data))
  404. (input (ein:cell-insert-input data))
  405. (output (ein:cell-insert-output (cadr path) data))
  406. (footer (ein:cell-insert-footer data))))
  407. (defun ein:maybe-show-slideshow-data (cell)
  408. (unless (equal (slot-value cell 'slidetype) "-")
  409. (format " - Slide [%s]:" (slot-value cell 'slidetype))))
  410. (cl-defmethod ein:cell-insert-prompt ((cell ein:codecell))
  411. "Insert prompt of the CELL in the buffer.
  412. Called from ewoc pretty printer via `ein:cell-pp'."
  413. ;; Newline is inserted in `ein:cell-insert-input'.
  414. (ein:insert-read-only
  415. (concat
  416. (format "In [%s]:" (or (ein:oref-safe cell 'input-prompt-number) " "))
  417. (ein:maybe-show-slideshow-data cell)
  418. (when (slot-value cell 'autoexec) " %s" ein:cell-autoexec-prompt))
  419. 'font-lock-face 'ein:cell-input-prompt))
  420. (cl-defmethod ein:cell-insert-prompt ((cell ein:textcell))
  421. (ein:insert-read-only
  422. (concat
  423. (format "%s:" (slot-value cell 'cell-type))
  424. (ein:maybe-show-slideshow-data cell))
  425. 'font-lock-face 'ein:cell-input-prompt))
  426. (cl-defmethod ein:cell-insert-prompt ((cell ein:headingcell))
  427. (ein:insert-read-only
  428. (concat
  429. (format "h%s:" (slot-value cell 'level))
  430. (ein:maybe-show-slideshow-data cell))
  431. 'font-lock-face 'ein:cell-input-prompt))
  432. (cl-defmethod ein:cell-insert-input ((cell ein:basecell))
  433. "Insert input of the CELL in the buffer.
  434. Called from ewoc pretty printer via `ein:cell-pp'."
  435. (let ((start (1+ (point))))
  436. ;; Newlines must allow insertion before/after its position.
  437. (insert (propertize "\n" 'read-only t 'rear-nonsticky t))
  438. (insert (or (ein:oref-safe cell 'input) "")
  439. (propertize "\n" 'read-only t))
  440. ;; Highlight background using overlay.
  441. (let ((ol (make-overlay start (point))))
  442. (overlay-put ol 'face (ein:cell-get-input-area-face cell))
  443. ;; `evaporate' = `t': Overlay is deleted when the region become empty.
  444. (overlay-put ol 'evaporate t))))
  445. (cl-defmethod ein:cell-get-input-area-face ((cell ein:basecell))
  446. "Return the face (symbol) for input area."
  447. 'ein:cell-input-area)
  448. (cl-defmethod ein:cell-get-input-area-face ((cell ein:headingcell))
  449. (intern (format "ein:cell-heading-%d" (slot-value cell 'level))))
  450. (cl-defmethod ein:cell-get-output-area-face-for-output-type (output-type)
  451. "Return the face (symbol) for output area."
  452. (ein:case-equal output-type
  453. (("pyout") 'ein:cell-output-area)
  454. (("pyerr") 'ein:cell-output-area-error)
  455. (("error") 'ein:cell-output-area-error)
  456. (("display_data") 'ein:cell-output-area)
  457. (("execute_result") 'ein:cell-output-area)
  458. (("stream") 'ein:cell-output-area)))
  459. (defun ein:cell-insert-output (index cell)
  460. "Insert INDEX-th output of the CELL in the buffer.
  461. Called from ewoc pretty printer via `ein:cell-pp'."
  462. (if (or (slot-value cell 'collapsed)
  463. (and ein:cell-max-num-outputs
  464. (>= index ein:cell-max-num-outputs)))
  465. (progn
  466. (when (and (not (slot-value cell 'collapsed))
  467. (= index ein:cell-max-num-outputs)
  468. (> (point) (point-at-bol)))
  469. ;; The first output which exceeds `ein:cell-max-num-outputs'.
  470. (ein:insert-read-only "\n"))
  471. (ein:insert-read-only "."))
  472. (let ((out (nth index (slot-value cell 'outputs))))
  473. ;; Handle newline for previous stream output.
  474. ;; In IPython JS, it is handled in `append_stream' because JS
  475. ;; does not need to care about newline (DOM does it for JS).
  476. ;; FIXME: Maybe I should abstract ewoc in some way and get rid
  477. ;; of this.
  478. (let ((last-out (and (> index 0)
  479. (nth (1- index) (slot-value cell 'outputs)))))
  480. ;; If previous output is stream type, consider adding newline
  481. (when (and last-out
  482. (equal (plist-get last-out :output_type) "stream"))
  483. ;; Check if the last output is from the same stream.
  484. ;; If so, do *NOT* insert newline, otherwise insert newline.
  485. (unless (and (equal (plist-get out :output_type) "stream")
  486. (equal (plist-get out :stream)
  487. (plist-get last-out :stream)))
  488. (ein:cell-append-stream-text-fontified "\n" last-out))))
  489. ;; Finally insert real data
  490. (let ((start (point))
  491. (output-type (plist-get out :output_type)))
  492. (ein:case-equal output-type
  493. (("pyout") (ein:cell-append-pyout cell out))
  494. (("pyerr") (ein:cell-append-pyerr cell out))
  495. (("error") (ein:cell-append-pyerr cell out))
  496. (("display_data") (ein:cell-append-display-data cell out))
  497. (("execute_result") (ein:cell-append-pyout cell out))
  498. (("stream") (ein:cell-append-stream cell out)))
  499. (let ((ol (make-overlay start (point))))
  500. (overlay-put ol 'face (ein:cell-get-output-area-face-for-output-type output-type))
  501. (overlay-put ol 'evaporate t))))))
  502. (cl-defmethod ein:cell-insert-footer ((cell ein:basecell))
  503. "Insert footer (just a new line) of the CELL in the buffer.
  504. Called from ewoc pretty printer via `ein:cell-pp'."
  505. (ein:insert-read-only "\n"))
  506. (cl-defmethod ein:cell-insert-footer :before ((cell ein:codecell))
  507. (if (or (slot-value cell 'collapsed)
  508. (and ein:cell-max-num-outputs
  509. (> (ein:cell-num-outputs cell) ein:cell-max-num-outputs)))
  510. ;; Add a newline after the last ".".
  511. (ein:insert-read-only "\n")
  512. (let ((last-out (car (last (slot-value cell 'outputs)))))
  513. (when (equal (plist-get last-out :output_type) "stream")
  514. (ein:cell-append-stream-text-fontified "\n" last-out)))))
  515. (defun ein:cell-node-p (node &optional element-name)
  516. (let* ((path (ein:$node-path node))
  517. (p0 (car path))
  518. (p1 (cadr path))
  519. (cell (ein:$node-path node)))
  520. (and cell (eql p0 'cell) (or (not element-name) (eql p1 element-name)))))
  521. (defun ein:cell-ewoc-node-p (ewoc-node &optional element-name)
  522. (ein:cell-node-p (ewoc-data ewoc-node) element-name))
  523. (defun ein:cell-from-ewoc-node (ewoc-node)
  524. (ein:aand ewoc-node (ewoc-data it) (ein:$node-data it)))
  525. (cl-defmethod ein:cell-input-pos-min ((cell ein:basecell))
  526. "Return editable minimum point in the input area of the CELL.
  527. If the input area of the CELL does not exist, return `nil'"
  528. (let* ((input-node (ein:cell-element-get cell :input)))
  529. ;; 1+ for skipping newline
  530. (when input-node (1+ (ewoc-location input-node)))))
  531. (cl-defmethod ein:cell-input-pos-max ((cell ein:basecell))
  532. "Return editable maximum point in the input area of the CELL.
  533. If the input area of the CELL does not exist, return `nil'"
  534. (let* ((ewoc (slot-value cell 'ewoc))
  535. (input-node (ein:cell-element-get cell :input)))
  536. ;; 1- for skipping newline
  537. (when input-node (1- (ewoc-location (ewoc-next ewoc input-node))))))
  538. (cl-defmethod ein:cell-get-text ((cell ein:basecell))
  539. "Grab text in the input area of the cell at point."
  540. (if (ein:cell-active-p cell)
  541. (let* ((beg (ein:cell-input-pos-min cell))
  542. (end (ein:cell-input-pos-max cell)))
  543. (buffer-substring beg end))
  544. (slot-value cell 'input)))
  545. (cl-defmethod ein:cell-set-text ((cell ein:basecell) text)
  546. (let* ((input-node (ein:cell-element-get cell :input))
  547. (ewoc (slot-value cell 'ewoc))
  548. ;; 1+/1- is for skipping newline
  549. (beg (1+ (ewoc-location input-node)))
  550. (end (1- (ewoc-location (ewoc-next ewoc input-node)))))
  551. (save-excursion
  552. ;; probably it is better to set :input and update via ewoc?
  553. (goto-char beg)
  554. (delete-region beg end)
  555. (insert text))))
  556. (cl-defmethod ein:cell-save-text ((cell ein:basecell))
  557. (setf (slot-value cell 'input) (ein:cell-get-text cell)))
  558. (cl-defmethod ein:cell-deactivate ((cell ein:basecell))
  559. (setf (slot-value cell 'element) nil)
  560. cell)
  561. (cl-defmethod ein:cell-active-p ((cell ein:basecell))
  562. (slot-value cell 'element))
  563. (cl-defmethod ein:cell-running-set ((cell ein:codecell) running)
  564. ;; FIXME: change the appearance of the cell
  565. (setf (slot-value cell 'running) running))
  566. (cl-defmethod ein:cell-set-collapsed ((cell ein:codecell) collapsed)
  567. "Set `:collapsed' slot of CELL and invalidate output ewoc nodes."
  568. (unless (eq (slot-value cell 'collapsed) collapsed)
  569. (setf (slot-value cell 'collapsed) collapsed)
  570. (let ((inhibit-read-only t)
  571. (buffer-undo-list t))
  572. (apply #'ewoc-invalidate
  573. (slot-value cell 'ewoc)
  574. (append (ein:cell-element-get cell :output)
  575. (list (ein:cell-element-get cell :footer)))))))
  576. (cl-defmethod ein:cell-collapse ((cell ein:codecell))
  577. (ein:cell-set-collapsed cell t))
  578. (cl-defmethod ein:cell-expand ((cell ein:codecell))
  579. (ein:cell-set-collapsed cell nil))
  580. (cl-defmethod ein:cell-toggle-output ((cell ein:codecell))
  581. "Toggle `:collapsed' slot of CELL and invalidate output ewoc nodes."
  582. (ein:cell-set-collapsed cell (not (slot-value cell 'collapsed))))
  583. (cl-defmethod ein:cell-invalidate-prompt ((cell ein:codecell))
  584. (let ((inhibit-read-only t)
  585. (buffer-undo-list t)) ; disable undo recording
  586. (ewoc-invalidate (slot-value cell 'ewoc)
  587. (ein:cell-element-get cell :prompt))))
  588. (cl-defmethod ein:cell-set-input-prompt ((cell ein:codecell) &optional number)
  589. (setf (slot-value cell 'input-prompt-number) number)
  590. (ein:cell-invalidate-prompt cell))
  591. (cl-defmethod ein:cell-set-autoexec ((cell ein:codecell) bool)
  592. "Set auto-execution flag of CELL to BOOL and invalidate the
  593. prompt EWOC node."
  594. (setf (slot-value cell 'autoexec) bool)
  595. (ein:cell-invalidate-prompt cell))
  596. (cl-defmethod ein:cell-autoexec-p ((cell ein:basecell))
  597. "Auto-execution flag set to CELL.
  598. Return `nil' always for non-code cells."
  599. nil)
  600. (cl-defmethod ein:cell-autoexec-p ((cell ein:codecell))
  601. (slot-value cell 'autoexec))
  602. (cl-defmethod ein:cell-toggle-autoexec ((cell ein:codecell))
  603. "Toggle auto-execution flag of CELL to BOOL and invalidate the
  604. prompt EWOC node."
  605. (ein:cell-set-autoexec cell (not (ein:cell-autoexec-p cell))))
  606. (cl-defmethod ein:cell-goto ((cell ein:basecell) &optional relpos prop)
  607. "Go to the input area of the given CELL.
  608. RELPOS is the position relative to the input area. Default is 0.
  609. PROP is a name of cell element. Default is `:input'.
  610. \(fn cell relpos prop)"
  611. (unless relpos (setq relpos 0))
  612. (unless prop (setq prop :input))
  613. (ewoc-goto-node (slot-value cell 'ewoc) (ein:cell-element-get cell prop))
  614. (let ((offset (cl-case prop
  615. ((:input :before-output) 1)
  616. (:after-input -1)
  617. (t 0))))
  618. (forward-char (+ relpos offset))))
  619. (cl-defmethod ein:cell-goto-line ((cell ein:basecell) &optional inputline prop)
  620. "Go to the input area of the given CELL.
  621. INPUTLINE is the line number relative to the input area. Default is 1.
  622. PROP is a name of cell element. Default is `:input'.
  623. \(fn cell inputline prop)"
  624. (unless inputline (setq inputline 1))
  625. (unless prop (setq prop :input))
  626. (let ((goal-column nil))
  627. (ewoc-goto-node (slot-value cell 'ewoc) (ein:cell-element-get cell prop)))
  628. (let ((offset (cl-case prop
  629. ((:input :before-output) 1)
  630. (:after-input -1)
  631. (t 0))))
  632. (forward-char offset)
  633. (forward-line (- inputline 1))))
  634. (cl-defmethod ein:cell-relative-point ((cell ein:basecell) &optional pos)
  635. "Return the point relative to the input area of CELL.
  636. If the position POS is not given, current point is considered."
  637. (unless pos (setq pos (point)))
  638. (- pos (1+ (ewoc-location (ein:cell-element-get cell :input)))))
  639. (cl-defmethod ein:cell-location ((cell ein:basecell) &optional elm end)
  640. "Return the starting location of CELL.
  641. ELM is a name (keyword) of element that `ein:cell-element-get'
  642. understands. Note that you can't use `:output' since it returns
  643. a list. Use `:after-input' instead.
  644. If END is non-`nil', return the location of next element."
  645. (unless elm (setq elm :prompt))
  646. (let ((element (slot-value cell 'element)))
  647. (when end
  648. (setq elm (cl-case elm
  649. (:prompt :input)
  650. (:input :after-input)
  651. (:output :after-output)))
  652. (unless elm
  653. (setq cell (ein:cell-next cell))
  654. (setq elm :prompt)))
  655. (if cell
  656. (ewoc-location (ein:cell-element-get cell elm))
  657. (cl-assert end)
  658. (point-max))))
  659. (cl-defmethod ein:cell-buffer ((cell ein:basecell))
  660. "Return a buffer associated by CELL (if any)."
  661. (ein:aand (ein:oref-safe cell 'ewoc) (ewoc-buffer it)))
  662. ;; Data manipulation
  663. (cl-defmethod ein:cell-clear-output ((cell ein:codecell) stdout stderr other)
  664. ;; codecell.js in IPython implements it using timeout and callback.
  665. ;; As it is unclear why timeout is needed, just clear output
  666. ;; instantaneously for now.
  667. (ein:log 'debug "cell-clear-output stdout=%s stderr=%s other=%s"
  668. stdout stderr other)
  669. (setf (slot-value cell 'traceback) nil)
  670. (let ((ewoc (slot-value cell 'ewoc))
  671. (output-nodes (ein:cell-element-get cell :output)))
  672. (if (and stdout stderr other)
  673. (progn
  674. ;; clear all
  675. (let ((inhibit-read-only t)
  676. (buffer-undo-list t)) ; disable undo recording
  677. (apply #'ewoc-delete ewoc output-nodes))
  678. (plist-put (slot-value cell 'element) :output nil)
  679. (setf (slot-value cell 'outputs) nil))
  680. (let* ((ewoc-node-list
  681. (append
  682. (when stdout (ein:node-filter output-nodes :is 'output-stdout))
  683. (when stderr (ein:node-filter output-nodes :is 'output-stderr))
  684. (when stdout (ein:node-filter output-nodes
  685. :is 'output-subarea
  686. :not 'output-stderr
  687. :not 'output-stdout))))
  688. (indices
  689. (mapcar (lambda (n) (last (ein:$node-path (ewoc-data n))))
  690. ewoc-node-list)))
  691. ;; remove from buffer
  692. (let ((inhibit-read-only t)
  693. (buffer-undo-list t)) ; disable undo recording
  694. (apply #'ewoc-delete ewoc ewoc-node-list))
  695. ;; remove from `:element'
  696. (let* ((element (slot-value cell 'element))
  697. (old-output (plist-get element :output))
  698. (new-ouptut (ein:remove-by-index old-output indices)))
  699. (plist-put element :output new-ouptut))
  700. ;; remove cleared outputs from internal data
  701. (setf (slot-value cell 'outputs)
  702. (ein:remove-by-index (slot-value cell 'outputs) indices))))
  703. ;; Footer may have extra (possibly colored) newline due to the
  704. ;; last output type. So invalidate it here.
  705. ;; See `ein:cell-insert-footer' (for codecell).
  706. (let ((buffer-undo-list t)) ; disable undo recording
  707. (ewoc-invalidate ewoc (ein:cell-element-get cell :footer)))))
  708. (defun ein:cell-output-json-to-class (json)
  709. (ein:case-equal (plist-get json :output_type)
  710. (("pyout")
  711. '(output-subarea))
  712. (("pyerr")
  713. '(output-subarea))
  714. (("error")
  715. '(output-subarea))
  716. (("display_data")
  717. '(output-subarea))
  718. (("execute_result")
  719. '(output-subarea))
  720. (("stream")
  721. (list 'output-stream 'output-subarea
  722. (intern (format "output-%s" (plist-get json :stream)))))))
  723. (cl-defmethod ein:cell-append-output ((cell ein:codecell) json dynamic)
  724. ;; When there is a python error, we actually get two identical tracebacks back
  725. ;; from the kernel, one from the "shell" channel, and one from the "iopub"
  726. ;; channel. As a workaround, we remember the cell's traceback and ignore
  727. ;; traceback outputs that are identical to the one we already have.
  728. (let ((new-tb (plist-get json :traceback))
  729. (old-tb (slot-value cell 'traceback)))
  730. (when (or
  731. (null old-tb)
  732. (null new-tb)
  733. (not (cl-equalp new-tb old-tb)))
  734. (ein:cell-actually-append-output cell json dynamic))
  735. (setf (slot-value cell 'traceback) new-tb)))
  736. (cl-defmethod ein:cell-actually-append-output ((cell ein:codecell) json dynamic)
  737. (ein:cell-expand cell)
  738. ;; (ein:flush-clear-timeout)
  739. (setf (slot-value cell 'outputs)
  740. (append (slot-value cell 'outputs) (list json)))
  741. ;; enter last output element
  742. (let* ((inhibit-read-only t)
  743. (buffer-undo-list t) ; disable undo recording
  744. (ewoc (slot-value cell 'ewoc))
  745. (index (1- (ein:cell-num-outputs cell)))
  746. (path `(cell output ,index))
  747. (class (ein:cell-output-json-to-class json))
  748. (data (ein:node-new path cell class))
  749. (last-node (ein:cell-element-get cell :last-output))
  750. (ewoc-node (ewoc-enter-after ewoc last-node data))
  751. (element (slot-value cell 'element)))
  752. (plist-put element :output
  753. (append (plist-get element :output) (list ewoc-node)))
  754. (ewoc-invalidate ewoc (ein:cell-element-get cell :footer))))
  755. (cl-defmethod ein:cell-append-pyout ((cell ein:codecell) json)
  756. "Insert pyout type output in the buffer.
  757. Called from ewoc pretty printer via `ein:cell-insert-output'."
  758. (ein:insert-read-only (format "Out [%s]:"
  759. (or (plist-get json :prompt_number) " "))
  760. 'font-lock-face 'ein:cell-output-prompt)
  761. (ein:insert-read-only "\n")
  762. (ein:cell-append-mime-type json (slot-value cell 'dynamic))
  763. (ein:insert-read-only "\n"))
  764. (cl-defmethod ein:cell-append-pyerr ((cell ein:codecell) json)
  765. "Insert pyerr type output in the buffer.
  766. Called from ewoc pretty printer via `ein:cell-insert-output'."
  767. (mapc (lambda (tb)
  768. (ein:cell-append-text tb)
  769. (ein:cell-append-text "\n"))
  770. (let ((tb (plist-get json :traceback))
  771. (level ein:cell-traceback-level))
  772. (if (and level (> (- (length tb) 2) level))
  773. (cons (substitute-command-keys
  774. "\nTruncated Traceback (Use \\[ein:tb-show] to view full TB):")
  775. (last tb (1+ level)))
  776. tb)))
  777. (ein:insert-read-only "\n"))
  778. (ein:deflocal ein:%cell-append-stream-last-cell% nil
  779. "The last cell in which `ein:cell-append-stream' is used.")
  780. (cl-defmethod ein:cell-append-stream ((cell ein:codecell) json)
  781. "Insert stream type output in the buffer.
  782. Called from ewoc pretty printer via `ein:cell-insert-output'."
  783. ;; (unless (plist-get json :stream)
  784. ;; (plist-put json :stream "stdout"))
  785. (unless (eq cell ein:%cell-append-stream-last-cell%)
  786. ;; Avoid applying unclosed ANSI escape code in the cell. Note
  787. ;; that I don't need to distinguish stdout/stderr because it looks
  788. ;; like normal terminal does not.
  789. (setq ansi-color-context nil))
  790. (let ((start (point)))
  791. (ein:cell-append-stream-text-fontified (or (plist-get json :text) "") json)
  792. (comint-carriage-motion start (point)))
  793. ;; NOTE: newlines for stream is handled in `ein:cell-insert-output'.
  794. ;; So do not insert newline here.
  795. (setq ein:%cell-append-stream-last-cell% cell))
  796. (defun ein:cell-append-stream-text-fontified (text json)
  797. "Insert TEXT with font properties defined by JSON data."
  798. (if (equal (plist-get json :stream) "stderr")
  799. (ein:cell-append-text text 'font-lock-face 'ein:cell-output-stderr)
  800. (ein:cell-append-text text)))
  801. (cl-defmethod ein:cell-append-display-data ((cell ein:codecell) json)
  802. "Insert display-data type output in the buffer.
  803. Called from ewoc pretty printer via `ein:cell-insert-output'."
  804. (if (and (or (plist-get json :javascript)
  805. (plist-get json :html))
  806. (slot-value cell 'dynamic) ein:enable-dynamic-javascript)
  807. (ein:execute-javascript cell json)
  808. (progn
  809. (ein:cell-append-mime-type json (slot-value cell 'dynamic))
  810. (ein:insert-read-only "\n"))))
  811. (defcustom ein:output-type-preference
  812. (if (and (fboundp 'shr-insert-document)
  813. (fboundp 'libxml-parse-xml-region))
  814. #'ein:output-type-prefer-pretty-text-over-html
  815. '(emacs-lisp svg image/svg+xml png image/png jpeg image/jpeg html text/html latex text/latex javascript text/javascript text text/plain))
  816. "Output types to be used in notebook.
  817. First output-type found in this list will be used.
  818. This variable can be a list or a function returning a list given
  819. DATA plist.
  820. See also `ein:output-type-prefer-pretty-text-over-html'.
  821. **Example**:
  822. If you prefer HTML type over text type, you can set it as::
  823. (setq ein:output-type-preference
  824. '(emacs-lisp svg png jpeg html text latex javascript))
  825. Note that ``html`` comes before ``text``."
  826. :type '(choice function (repeat symbol))
  827. :group 'ein)
  828. (defvar ein:output-types-text-preferred
  829. '(emacs-lisp svg image/svg+xml png image/png jpeg image/jpeg text text/plain html text/html latex text/latex javascript text/javascript))
  830. (defvar ein:output-types-html-preferred
  831. '(emacs-lisp svg image/svg+xml png image/png jpeg image/jpeg html text/html latex text/latex javascript text/javascript text text/plain))
  832. (defun ein:output-type-prefer-pretty-text-over-html (data)
  833. "Use text type if it is a \"prettified\" text instead of HTML.
  834. This is mostly for *not* using HTML table for pandas but using
  835. HTML for other object.
  836. If the text type output contains a newline, it is assumed be a
  837. prettified text thus be used instead of HTML type."
  838. (if (ein:aand (or (plist-get data :text)
  839. (plist-get data :text/plain))
  840. (string-match-p "\n" it))
  841. ein:output-types-text-preferred
  842. ein:output-types-html-preferred))
  843. (defun ein:fix-mime-type (type)
  844. (ein:aif (assoc type ein:mime-type-map)
  845. (cdr it)
  846. type))
  847. (defun ein:cell-append-mime-type (json dynamic)
  848. (when (plist-get json :data)
  849. (setq json (plist-get json :data))) ;; For nbformat v4 support.
  850. (cl-loop
  851. for key in (cond
  852. ((functionp ein:output-type-preference)
  853. (funcall ein:output-type-preference json))
  854. (t ein:output-type-preference))
  855. for type = (intern (format ":%s" key)) ; something like `:text'
  856. for value = (plist-get json type) ; FIXME: optimize
  857. when (plist-member json type)
  858. return
  859. (cl-case key
  860. ;; NOTE: Normally `javascript' and `html' will not be inserted as
  861. ;; they come out after `text'. Maybe it is better to inform user
  862. ;; when one of them is inserted.
  863. ((javascript text/javascript)
  864. (when dynamic
  865. (ein:log 'info (concat "ein:cell-append-mime-type does not support "
  866. "dynamic javascript. got: %s") value))
  867. (ein:insert-read-only (plist-get json type)))
  868. (emacs-lisp
  869. (when dynamic
  870. (ein:cell-safe-read-eval-insert (plist-get json type))))
  871. ((html text/html)
  872. (funcall (ein:output-area-get-html-renderer) (plist-get json type)))
  873. ((latex text/latex text text/plain)
  874. (ein:insert-read-only (ansi-color-apply (plist-get json type))))
  875. ((svg image/svg+xml)
  876. (ein:insert-image value (ein:fix-mime-type key) t))
  877. ((png image/png jpeg image/jpeg)
  878. (ein:insert-image (base64-decode-string value) (ein:fix-mime-type key) t)))))
  879. (defun ein:cell-append-text (data &rest properties)
  880. ;; escape ANSI in plaintext:
  881. (apply #'ein:insert-read-only (ansi-color-apply data) properties))
  882. (defun ein:cell-safe-read-eval-insert (text)
  883. (ein:insert-read-only
  884. (condition-case err
  885. (save-excursion
  886. ;; given code can be `pop-to-buffer' or something.
  887. (format "%S" (eval (read text))))
  888. (error
  889. (ein:log 'warn "Got an error while executing: '%s'"
  890. text)
  891. (format "Error: %S" err)))))
  892. (cl-defmethod ein:cell-to-json ((cell ein:codecell) &optional discard-output)
  893. "Return json-ready alist."
  894. `((input . ,(ein:cell-get-text cell))
  895. (cell_type . "code")
  896. ,@(ein:aif (ein:oref-safe cell 'input-prompt-number)
  897. `((prompt_number . ,it)))
  898. (outputs . ,(if discard-output [] (apply #'vector (slot-value cell 'outputs))))
  899. (language . "python")
  900. (collapsed . ,(if (slot-value cell 'collapsed) t json-false))))
  901. (defvar ein:output-type-map
  902. '((:svg . :image/svg+xml) (:png . :image/png) (:jpeg . :image/jpeg)
  903. (:text . :text/plain)
  904. (:html . :text/html) (:latex . :text/latex) (:javascript . :text/javascript)))
  905. (defun ein:output-property-p (maybe-property)
  906. (assoc maybe-property ein:output-type-map))
  907. (cl-defmethod ein:cell-to-nb4-json ((cell ein:codecell) wsidx &optional discard-output)
  908. (let* ((ss-table (ein:get-slide-show cell))
  909. (metadata (slot-value cell 'metadata))
  910. (outputs (if discard-output []
  911. (slot-value cell 'outputs)))
  912. (renamed-outputs '())
  913. (execute-count (ein:aif (ein:oref-safe cell 'input-prompt-number)
  914. (and (numberp it) it))))
  915. (setq metadata (plist-put metadata :collapsed (if (slot-value cell 'collapsed) t json-false)))
  916. (setq metadata (plist-put metadata :autoscroll json-false))
  917. (setq metadata (plist-put metadata :ein.tags (format "worksheet-%s" wsidx)))
  918. (setq metadata (plist-put metadata :ein.hycell (if (ein:hy-codecell-p cell)
  919. t
  920. json-false)))
  921. (setq metadata (plist-put metadata :slideshow ss-table))
  922. (unless discard-output
  923. (dolist (output outputs)
  924. (let ((otype (plist-get output :output_type)))
  925. (ein:log 'debug "Saving output of type %S" otype)
  926. (if (and (or (equal otype "display_data")
  927. (equal otype "execute_result"))
  928. (null (plist-get output :metadata)))
  929. (plist-put output :metadata (make-hash-table)))
  930. (setq renamed-outputs
  931. (append renamed-outputs
  932. (list (let ((ocopy (cl-copy-list output))
  933. (new-output '()))
  934. (cl-loop while ocopy
  935. do (let ((prop (pop ocopy))
  936. (value (pop ocopy)))
  937. (ein:log 'debug "Checking property %s for output type '%s'"
  938. prop otype)
  939. (cond
  940. ((equal prop :stream) (progn (push value new-output)
  941. (push :name new-output)))
  942. ((and (or (equal otype "display_data")
  943. (equal otype "execute_result"))
  944. (ein:output-property-p prop))
  945. (let ((new-prop (cdr (ein:output-property-p prop))))
  946. (if (plist-member new-output :data)
  947. (setq new-output (plist-put new-output :data
  948. (append (plist-get new-output :data)
  949. (list new-prop (list value))
  950. )))
  951. (push (list new-prop (list value)) new-output)
  952. (push :data new-output))
  953. ))
  954. ((and (equal otype "display_data")
  955. (equal prop :text))
  956. (ein:log 'debug "SAVE-NOTEBOOK: Skipping unnecessary :text data."))
  957. ;; ((and (equal otype "execute_result")
  958. ;; (ein:output-property-p prop)
  959. ;; ;; (or (equal prop :text)
  960. ;; ;; (equal prop :html)
  961. ;; ;; (equal prop :latex))
  962. ;; )
  963. ;; (ein:log 'debug "Fixing execute_result (%s?)." otype)
  964. ;; (let ((new-prop (cdr (ein:output-property-p prop))))
  965. ;; (push (list new-prop (list value)) new-output)
  966. ;; (push :data new-output)))
  967. ((and (equal otype "execute_result")
  968. (equal prop :prompt_number))
  969. (ein:log 'debug "SAVE-NOTEBOOK: Fixing prompt_number property.")
  970. (push value new-output)
  971. (push :execution_count new-output))
  972. (t (progn (push value new-output) (push prop new-output)))))
  973. finally return new-output))))
  974. ))))
  975. `((source . ,(ein:cell-get-text cell))
  976. (cell_type . "code")
  977. ,@(if execute-count
  978. `((execution_count . ,execute-count))
  979. `((execution_count)))
  980. (outputs . ,(apply #'vector (or renamed-outputs outputs)))
  981. (metadata . ,metadata))))
  982. (cl-defmethod ein:cell-to-json ((cell ein:textcell) &optional discard-output)
  983. `((cell_type . ,(slot-value cell 'cell-type))
  984. (source . ,(ein:cell-get-text cell))))
  985. (cl-defmethod ein:cell-to-nb4-json ((cell ein:textcell) wsidx &optional discard-output)
  986. (let ((metadata (slot-value cell 'metadata))
  987. (ss-table (ein:get-slide-show cell)))
  988. (setq metadata (plist-put metadata :ein.tags (format "worksheet-%s" wsidx)))
  989. (setq metadata (plist-put metadata :slideshow ss-table))
  990. `((cell_type . ,(slot-value cell 'cell-type))
  991. (source . ,(ein:cell-get-text cell))
  992. (metadata . ,metadata))))
  993. (cl-defmethod ein:cell-to-nb4-json ((cell ein:headingcell) wsidx &optional discard-output)
  994. (let ((metadata (slot-value cell 'metadata))
  995. (ss-table (ein:get-slide-show cell))
  996. (header (make-string (slot-value cell 'level) ?#)))
  997. (setq metadata (plist-put metadata :ein.tags (format "worksheet-%s" wsidx)))
  998. (setq metadata (plist-put metadata :slideshow ss-table))
  999. `((cell_type . "markdown")
  1000. (source . ,(format "%s %s" header (ein:cell-get-text cell)))
  1001. (metadata . ,metadata))))
  1002. (cl-defmethod ein:cell-to-json ((cell ein:headingcell) &optional discard-output)
  1003. (let ((json (cl-call-next-method)))
  1004. (append json `((level . ,(slot-value cell 'level))))))
  1005. (cl-defmethod ein:cell-next ((cell ein:basecell))
  1006. "Return next cell of the given CELL or nil if CELL is the last one."
  1007. (ein:aif (ewoc-next (slot-value cell 'ewoc)
  1008. (ein:cell-element-get cell :footer))
  1009. (let ((cell (ein:$node-data (ewoc-data it))))
  1010. (when (cl-typep cell 'ein:basecell)
  1011. cell))))
  1012. (cl-defmethod ein:cell-prev ((cell ein:basecell))
  1013. "Return previous cell of the given CELL or nil if CELL is the first one."
  1014. (ein:aif (ewoc-prev (slot-value cell 'ewoc)
  1015. (ein:cell-element-get cell :prompt))
  1016. (let ((cell (ein:$node-data (ewoc-data it))))
  1017. (when (cl-typep cell 'ein:basecell)
  1018. cell))))
  1019. ;;; Kernel related calls.
  1020. (cl-defmethod ein:cell-set-kernel ((cell ein:codecell) kernel)
  1021. (setf (slot-value cell 'kernel) kernel))
  1022. (cl-defmethod ein:cell-execute ((cell ein:codecell))
  1023. (ein:cell-execute-internal cell
  1024. (slot-value cell 'kernel)
  1025. (ein:cell-get-text cell)
  1026. :silent nil))
  1027. (cl-defmethod ein:cell-execute-internal ((cell ein:codecell)
  1028. kernel code &rest args)
  1029. (ein:cell-running-set cell t)
  1030. (ein:cell-clear-output cell t t t)
  1031. (ein:cell-set-input-prompt cell "*")
  1032. (setf (slot-value cell 'dynamic) t)
  1033. (apply #'ein:kernel-execute kernel code (ein:cell-make-callbacks cell) args))
  1034. (cl-defmethod ein:cell-make-callbacks ((cell ein:codecell))
  1035. (list
  1036. :execute_reply (cons #'ein:cell--handle-execute-reply cell)
  1037. :output (cons #'ein:cell--handle-output cell)
  1038. :clear_output (cons #'ein:cell--handle-clear-output cell)
  1039. :set_next_input (cons #'ein:cell--handle-set-next-input cell)))
  1040. (cl-defmethod ein:cell--handle-execute-reply ((cell ein:codecell) content
  1041. metadata)
  1042. (run-hook-with-args 'ein:on-execute-reply-functions cell content metadata)
  1043. (ein:cell-set-input-prompt cell (plist-get content :execution_count))
  1044. (ein:cell-running-set cell nil)
  1045. (if (equal (plist-get content :status) "error")
  1046. (ein:cell--handle-output cell "error" content metadata)
  1047. (let ((events (slot-value cell 'events)))
  1048. (ein:events-trigger events 'set_dirty.Worksheet (list :value t :cell cell))
  1049. (ein:events-trigger events 'maybe_reset_undo.Worksheet cell))))
  1050. (cl-defmethod ein:cell--handle-set-next-input ((cell ein:codecell) text)
  1051. (let ((events (slot-value cell 'events)))
  1052. (ein:events-trigger events 'set_next_input.Worksheet
  1053. (list :cell cell :text text))
  1054. (ein:events-trigger events 'maybe_reset_undo.Worksheet cell)
  1055. ))
  1056. ;;; Output area
  1057. ;; These function should go to ein-output-area.el. But as cell and
  1058. ;; EWOC is connected in complicated way, I will leave them in
  1059. ;; ein-cell.el.
  1060. (cl-defmethod ein:cell--handle-output ((cell ein:codecell) msg-type content
  1061. _metadata)
  1062. (let* ((json (list :output_type msg-type)))
  1063. (ein:case-equal msg-type
  1064. (("stream")
  1065. (plist-put json :text (or (plist-get content :data)
  1066. (plist-get content :text))) ;; Horrible hack to deal with version 5.0 of messaging protocol.
  1067. (plist-put json :stream (plist-get content :name)))
  1068. (("display_data" "pyout" "execute_result") ;; in v4 nbformat execute_result == pyout
  1069. (when (or (equal msg-type "pyout")
  1070. (equal msg-type "execute_result"))
  1071. (plist-put json :prompt_number (plist-get content :execution_count)))
  1072. (setq json
  1073. (ein:output-area-convert-mime-types json (plist-get content :data))))
  1074. (("pyerr" "error")
  1075. (plist-put json :ename (plist-get content :ename))
  1076. (plist-put json :evalue (plist-get content :evalue))
  1077. (plist-put json :traceback (plist-get content :traceback))))
  1078. (ein:cell-append-output cell json t)
  1079. ;; (setf (slot-value cell 'dirty) t)
  1080. (ein:events-trigger (slot-value cell 'events) 'maybe_reset_undo.Worksheet cell)))
  1081. (defun ein:output-area-convert-mime-types (json data)
  1082. (cl-loop for (prop . mime) in '((:text . :text/plain)
  1083. (:html . :text/html)
  1084. (:svg . :image/svg+xml)
  1085. (:png . :image/png)
  1086. (:jpeg . :image/jpeg)
  1087. (:latex . :text/latex)
  1088. (:json . :application/json)
  1089. (:javascript . :application/javascript)
  1090. (:emacs-lisp . :application/emacs-lisp))
  1091. when (plist-member data mime)
  1092. do (plist-put json prop (plist-get data mime)))
  1093. json)
  1094. (cl-defmethod ein:cell--handle-clear-output ((cell ein:codecell) content
  1095. _metadata)
  1096. ;; Jupyter messaging spec 5.0 no longer has stdout, stderr, or other fields for clear_output
  1097. (ein:cell-clear-output cell
  1098. t ;;(plist-get content :stdout)
  1099. t ;;(plist-get content :stderr)
  1100. t ;;(plist-get content :other))
  1101. )
  1102. (ein:events-trigger (slot-value cell 'events) 'maybe_reset_undo.Worksheet cell))
  1103. ;;; Misc.
  1104. (cl-defmethod ein:cell-has-image-ouput-p ((cell ein:codecell))
  1105. "Return `t' if given cell has image output, `nil' otherwise."
  1106. (cl-loop for out in (slot-value cell 'outputs)
  1107. when (or (plist-member out :svg)
  1108. (plist-member out :image/svg+xml)
  1109. (plist-member out :png)
  1110. (plist-member out :image/png)
  1111. (plist-member out :jpeg)
  1112. (plist-member out :image/jpeg))
  1113. return t))
  1114. (cl-defmethod ein:cell-has-image-ouput-p ((cell ein:textcell))
  1115. nil)
  1116. (cl-defmethod ein:cell-get-tb-data ((cell ein:codecell))
  1117. (cl-loop for out in (slot-value cell 'outputs)
  1118. when (and (plist-get out :traceback)
  1119. (member (plist-get out :output_type) '("pyerr" "error")))
  1120. return (plist-get out :traceback)))
  1121. (provide 'ein-cell)
  1122. ;;; ein-cell.el ends here