From f4d6d4e9a8034bf78b464dd581bdebbfba1f3a24 Mon Sep 17 00:00:00 2001 From: dickmao Date: Thu, 23 Jan 2020 10:33:05 -0500 Subject: [PATCH] remove pytools cruft, only print polymode-mode-name-aliases warning once --- Makefile | 2 +- README.rst | 7 +- features/step-definitions/ein-steps.el | 28 +-- lisp/ein-cell.el | 39 ++-- lisp/ein-kernel.el | 18 +- lisp/ein-notebook.el | 24 +-- lisp/ein-pytools.el | 261 ------------------------- lisp/ein-shared-output.el | 36 +--- lisp/ob-ein.el | 31 ++- lisp/poly-ein.el | 11 +- 10 files changed, 73 insertions(+), 384 deletions(-) diff --git a/Makefile b/Makefile index bd04667..fa3013b 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ endif .DEFAULT_GOAL := test-compile -README.rst: README.in.rst lisp/ein.el +README.rst: README.in.rst lisp/ein-notebook.el $(CASK) eval "(progn \ (add-to-list 'load-path \"./lisp\") \ (load \"ein-notebook\") \ diff --git a/README.rst b/README.rst index 782d2ac..e9b6b3c 100644 --- a/README.rst +++ b/README.rst @@ -117,7 +117,6 @@ Keymap (C-h m) C-c Prefix Command C-x Prefix Command ESC Prefix Command - C-: ein:shared-output-eval-string-km ein:worksheet-goto-next-input-km ein:worksheet-goto-prev-input-km ein:worksheet-execute-cell-and-insert-below-km @@ -130,15 +129,13 @@ Keymap (C-h m) M-RET ein:worksheet-execute-cell-and-goto-next-km M-, ein:pytools-jump-back-command M-. ein:pytools-jump-to-source-command - M-n ein:worksheet-next-input-history-km - M-p ein:worksheet-previous-input-history-km C-c C-a ein:worksheet-insert-cell-above-km C-c C-b ein:worksheet-insert-cell-below-km C-c C-c ein:worksheet-execute-cell-km C-c C-e ein:worksheet-toggle-output-km C-c C-f ein:file-open-km - C-c C-h ein:pytools-request-tooltip-or-help-km + C-c C-h ein:pytools-request-help-km C-c C-k ein:worksheet-kill-cell-km C-c C-l ein:worksheet-clear-output-km C-c RET ein:worksheet-merge-cell-km @@ -174,8 +171,6 @@ Keymap (C-h m) C-c C-# ein:notebook-close-km C-c C-$ ein:tb-show-km C-c C-' ein:worksheet-turn-on-autoexec-km - C-c C-, ein:pytools-jump-back-command - C-c C-. ein:pytools-jump-to-source-command C-c C-/ ein:notebook-scratchsheet-open-km C-c C-; ein:shared-output-show-code-cell-at-point-km C-c ein:worksheet-move-cell-down-km diff --git a/features/step-definitions/ein-steps.el b/features/step-definitions/ein-steps.el index 811e89b..6c9d66b 100644 --- a/features/step-definitions/ein-steps.el +++ b/features/step-definitions/ein-steps.el @@ -307,24 +307,26 @@ (When "^I keep clicking \"\\(.+\\)\" until \"\\(.+\\)\"$" (lambda (go stop) (cl-loop repeat 10 - until (search stop (buffer-string)) - do (And (format "I click on \"%s\"" go)) - do (sleep-for 0 1000) - finally do (should (search stop (buffer-string)))))) + until (search stop (buffer-string)) + do (And (format "I click on \"%s\"" go)) + do (sleep-for 0 1000) + finally do (should (search stop (buffer-string)))))) (When "^I click\\( without going top\\)? on \"\\(.+\\)\"$" (lambda (stay word) ;; from espuds "go to word" without the '\\b's (unless stay (goto-char (point-min))) - (let ((search (re-search-forward (format "\\[%s\\]" word) nil t)) - (msg "Cannot go to link '%s' in buffer: %s")) - (should search) - (backward-char) - (let ((was (widget-at))) - (When "I press \"RET\"") - (cl-loop until (not (equal was (widget-at))) - do (sleep-for 0 500)))))) + (cl-loop repeat 10 + for search = (re-search-forward (format "\\[%s\\]" word) nil t) + until search + do (sleep-for 0 1000) + finally do (should search)) + (backward-char) + (let ((was (widget-at))) + (When "I press \"RET\"") + (cl-loop until (not (equal was (widget-at))) + do (sleep-for 0 500))))) (When "^I click on dir \"\\(.+\\)\"$" (lambda (dir) @@ -499,7 +501,7 @@ (When "I evaluate the python code \"\\(.+\\)\"$" (lambda (code-str) - (ein:shared-output-eval-string nil code-str nil))) + (ein:shared-output-eval-string nil code-str))) (When "^text property at point includes \"\\(.+\\)\"$" (lambda (properties) diff --git a/lisp/ein-cell.el b/lisp/ein-cell.el index 0a72fd0..540da3d 100644 --- a/lisp/ein-cell.el +++ b/lisp/ein-cell.el @@ -182,8 +182,6 @@ a number will limit the number of lines in a cell output." `(when (slot-boundp ,obj ,slot) (slot-value ,obj ,slot))) -;;; Utils - (defun ein:make-mm-handle (image) (let ((mime-type (mailcap-extension-to-mime (symbol-name (plist-get (cdr image) :type))))) @@ -242,8 +240,6 @@ a number will limit the number of lines in a cell output." (insert-image image ".")) (error (ein:log 'warn "Could not insert image: %s" (error-message-string err))))) -;;; Cell factory - (defun ein:cell-class-from-type (type) (ein:case-equal type (("code") 'ein:codecell) @@ -413,8 +409,6 @@ Return language name as a string or `nil' when not defined. (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 @@ -633,7 +627,7 @@ Return language name as a string or `nil' when not defined. (slot-value cell 'element)) (cl-defmethod ein:cell-running-set ((cell ein:codecell) running) - ;; FIXME: change the appearance of the cell + "FIXME: change the appearance of the cell" (setf (slot-value cell 'running) running)) (cl-defmethod ein:cell-set-collapsed ((cell ein:codecell) collapsed) @@ -750,12 +744,10 @@ If END is non-`nil', return the location of next element." "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. + "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) @@ -810,10 +802,10 @@ If END is non-`nil', return the location of next element." (intern (format "output-%s" (plist-get json :stream))))))) (cl-defmethod ein:cell-append-output ((cell ein:codecell) json) - ;; 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. + "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 @@ -938,7 +930,7 @@ Called from ewoc pretty printer via `ein:cell-insert-output'." (ein:insert-read-only value))))) (defun ein:cell-append-text (data &rest properties) - ;; escape ANSI in plaintext: + "escape ANSI in plaintext:" (apply #'ein:insert-read-only (ansi-color-apply data) properties)) (defun ein:cell-safe-read-eval-insert (text) @@ -1000,8 +992,6 @@ Called from ewoc pretty printer via `ein:cell-insert-output'." (when (cl-typep cell 'ein:basecell) cell)))) -;;; Kernel related calls. - (cl-defmethod ein:cell-set-kernel ((cell ein:codecell) kernel) (setf (slot-value cell 'kernel) kernel)) @@ -1043,6 +1033,7 @@ Called from ewoc pretty printer via `ein:cell-insert-output'." (ein:events-trigger events 'maybe_reset_undo.Worksheet cell))) (cl-defmethod ein:cell--handle-output ((cell ein:codecell) msg-type content _metadata) + (ein:log 'debug "ein:cell--handle-output (cell ein:codecell): %s" msg-type) (let ((json `(:output_type ,msg-type))) (cl-macrolet ((copy-props (src tgt props) @@ -1070,7 +1061,7 @@ Called from ewoc pretty printer via `ein:cell-insert-output'." (cl-defmethod ein:cell--handle-clear-output ((cell ein:codecell) content _metadata) - ;; Jupyter messaging spec 5.0 no longer has stdout, stderr, or other fields for clear_output + "Jupyter messaging spec 5.0 no longer has stdout, stderr, or other fields for clear_output" (ein:cell-clear-output cell t ;;(plist-get content :stdout) t ;;(plist-get content :stderr) @@ -1078,8 +1069,6 @@ Called from ewoc pretty printer via `ein:cell-insert-output'." ) (ein:events-trigger (slot-value cell 'events) 'maybe_reset_undo.Worksheet cell)) -;;; Misc. - (cl-defmethod ein:cell-has-image-output-p ((cell ein:codecell)) "Return `t' if given cell has image output, `nil' otherwise." (seq-some (lambda (out) @@ -1093,9 +1082,9 @@ Called from ewoc pretty printer via `ein:cell-insert-output'." (cl-defmethod ein:cell-get-tb-data ((cell ein:codecell)) (cl-loop for out in (slot-value cell 'outputs) - when (and (plist-get out :traceback) - (member (plist-get out :output_type) '("pyerr" "error"))) - return (plist-get out :traceback))) + when (and (plist-get out :traceback) + (member (plist-get out :output_type) '("pyerr" "error"))) + return (plist-get out :traceback))) (provide 'ein-cell) diff --git a/lisp/ein-kernel.el b/lisp/ein-kernel.el index 992adf8..6a44aaf 100644 --- a/lisp/ein-kernel.el +++ b/lisp/ein-kernel.el @@ -669,18 +669,20 @@ Example:: (&key content metadata parent_header header &allow-other-keys) (ein:json-read-from-string packet) (let* ((msg-type (plist-get header :msg_type)) - (msg-id (plist-get parent_header :msg_id)) - (callbacks (ein:kernel-get-callbacks-for-msg kernel msg-id)) + (msg-id (plist-get header :msg_id)) + (parent-id (plist-get parent_header :msg_id)) + (callbacks (ein:kernel-get-callbacks-for-msg kernel parent-id)) (events (ein:$kernel-events kernel))) - (ein:log 'debug "ein:kernel--handle-iopub-reply: msg_type=%s msg_id=%s" - msg-type msg-id) + (ein:log 'debug + "ein:kernel--handle-iopub-reply: msg_type=%s msg_id=%s parent_id=%s" + msg-type msg-id parent-id) (ein:case-equal msg-type (("stream" "display_data" "pyout" "pyerr" "error" "execute_result") (aif (plist-get callbacks :output) ;; ein:cell--handle-output (ein:funcall-packed it msg-type content metadata) (ein:log 'warn (concat "ein:kernel--handle-iopub-reply: " - "No :output callback for msg_id=%s") - msg-id))) + "No :output callback for parent_id=%s") + parent-id))) (("status") (ein:case-equal (plist-get content :execution_state) (("busy") @@ -695,8 +697,8 @@ Example:: (aif (plist-get callbacks :clear_output) (ein:funcall-packed it content metadata) (ein:log 'info (concat "ein:kernel--handle-iopub-reply: " - "No :clear_output callback for msg_id=%s") - msg-id)))))))) + "No :clear_output callback for parent_id=%s") + parent-id)))))))) (defun ein:kernel-filename-to-python (kernel filename) "See: `ein:filename-to-python'." diff --git a/lisp/ein-notebook.el b/lisp/ein-notebook.el index dfd8c5c..3897488 100644 --- a/lisp/ein-notebook.el +++ b/lisp/ein-notebook.el @@ -149,11 +149,11 @@ Current buffer for these functions is set to the notebook buffer.") (cond ((ein:$kernelspec-p pre-kernelspec) pre-kernelspec) ((consp pre-kernelspec) (cl-loop for (name ks) on (ein:need-kernelspecs url-or-port) by 'cddr - when (and (ein:$kernelspec-p ks) - (string= (cdr pre-kernelspec) - (cl-struct-slot-value - 'ein:$kernelspec (car pre-kernelspec) ks))) - return ks)) + when (and (ein:$kernelspec-p ks) + (string= (cdr pre-kernelspec) + (cl-struct-slot-value + 'ein:$kernelspec (car pre-kernelspec) ks))) + return ks)) (t (ein:get-kernelspec url-or-port pre-kernelspec))))) (apply #'make-ein:$notebook :url-or-port url-or-port @@ -485,9 +485,6 @@ This is equivalent to do ``C-c`` in the console program." 'ein:notebook-eval-string 'ein:shared-output-eval-string "0.1.2") - -;;; Persistence and loading - (defun ein:notebook-set-notebook-name (notebook name) "Check NAME and change the name of NOTEBOOK to it." (if (ein:notebook-test-notebook-name name) @@ -1208,7 +1205,7 @@ Tried add-function: the &rest from :around is an emacs-25 compilation issue." (ein:notebook--define-key map (kbd "C-c ") ein:worksheet-move-cell-down) (ein:notebook--define-key map (kbd "M-") ein:worksheet-move-cell-up) (ein:notebook--define-key map (kbd "M-") ein:worksheet-move-cell-down) - (ein:notebook--define-key map "\C-c\C-h" ein:pytools-request-tooltip-or-help) + (ein:notebook--define-key map "\C-c\C-h" ein:pytools-request-help) (ein:notebook--define-key map (kbd "C-c C-$") ein:tb-show) (ein:notebook--define-key map "\C-c\C-x" nil) (ein:notebook--define-key map "\C-c\C-x\C-r" ein:notebook-restart-session-command) @@ -1216,15 +1213,12 @@ Tried add-function: the &rest from :around is an emacs-25 compilation issue." (ein:notebook--define-key map "\C-c\C-z" ein:notebook-kernel-interrupt-command) (ein:notebook--define-key map "\C-c\C-q" ein:notebook-kill-kernel-then-close-command) (ein:notebook--define-key map (kbd "C-c C-#") ein:notebook-close) - (ein:notebook--define-key map (kbd "C-:") ein:shared-output-eval-string) (ein:notebook--define-key map "\C-c\C-f" ein:file-open) (ein:notebook--define-key map "\C-c\C-o" ein:notebook-open) (ein:notebook--define-key map "\C-x\C-s" ein:notebook-save-notebook-command) (ein:notebook--define-key map "\C-x\C-w" ein:notebook-rename-command) (define-key map "\M-." 'ein:pytools-jump-to-source-command) - (define-key map (kbd "C-c C-.") 'ein:pytools-jump-to-source-command) (define-key map "\M-," 'ein:pytools-jump-back-command) - (define-key map (kbd "C-c C-,") 'ein:pytools-jump-back-command) (ein:notebook--define-key map (kbd "C-c C-/") ein:notebook-scratchsheet-open) ;; Worksheets (ein:notebook--define-key map (kbd "C-c !") ein:worksheet-rename-sheet) @@ -1284,10 +1278,6 @@ Tried add-function: the &rest from :around is an emacs-25 compilation issue." :active (ein:worksheet-at-codecell-p)) ("Execute all" ein:worksheet-execute-all-cell) - ("Turn on auto execution flag" ein:worksheet-turn-on-autoexec - :active (ein:worksheet-at-codecell-p)) - ("Evaluate code in minibuffer" ein:shared-output-eval-string) - ("Toggle instant cell execution mode" ein:iexec-mode) )) "---" ,@(ein:generate-menu @@ -1305,7 +1295,7 @@ Tried add-function: the &rest from :around is an emacs-25 compilation issue." "---" ,@(ein:generate-menu '(("Show object help" - ein:pytools-request-tooltip-or-help) + ein:pytools-request-help) ("Jump to definition" ein:pytools-jump-to-source-command) ("Go back to the previous jump point" ein:pytools-jump-back-command)))) diff --git a/lisp/ein-pytools.el b/lisp/ein-pytools.el index 2dab141..cdb6cb1 100644 --- a/lisp/ein-pytools.el +++ b/lisp/ein-pytools.el @@ -25,10 +25,6 @@ ;;; Code: -;; for `ein:pytools-pandas-to-ses' -(declare-function ses-yank-tsf "ses") -(declare-function ses-command-hook "ses") - (require 'ein-kernel) (require 'ein-notebook) @@ -50,97 +46,6 @@ If OTHER-WINDOW is non-`nil', open the file in the other window." (const :tag "No" nil)) :group 'ein) -(defun ein:pytools-setup-hooks (kernel notebook) - (push (cons #'ein:pytools-load-safely kernel) - (ein:$kernel-after-start-hook kernel))) - -(defun ein:pytools-load-safely (kernel) - (with-temp-buffer - (let ((pytools-file (format "%s/%s" ein:source-dir "ein_remote_safe.py"))) - (insert-file-contents pytools-file) - (ein:kernel-execute - kernel - (buffer-string))))) - -(defun ein:pytools-reinject () - "Re-send ein's pytools code to the current kernel. - -If the kernel is reset by the notebook server then it may become -necessary to call this command to ensure pytools continue -working." - (interactive) - (ein:pytools-load-safely (ein:get-kernel-or-error))) - -(defun ein:pytools-add-sys-path (kernel) - (ein:kernel-execute - kernel - (format "__import__('sys').path.append('%s')" ein:source-dir))) - -(defun ein:set-buffer-file-name (nb msg-type content -not-used-) - (let ((buf (ein:notebook-buffer nb))) - (ein:case-equal msg-type - (("stream" "output") - (with-current-buffer buf - (setq buffer-file-name - (expand-file-name - (format "%s" (ein:$notebook-notebook-name nb)) - (plist-get content :text)))))))) - -(defun ein:pytools-get-notebook-dir (packed) - (multiple-value-bind (kernel notebook) packed - (ein:kernel-execute - kernel - (format "print(__import__('os').getcwd(),end='')") - (list - :output (cons - #'ein:set-buffer-file-name - notebook))))) - - -;;; Tooltip and help - -;; We can probably be more sophisticated than this, but -;; as a hack it will do. -(defun ein:pytools-magic-func-p (fstr) - (string-prefix-p "%" fstr)) - -(defun ein:pytools-request-tooltip (kernel func) - (interactive (list (ein:get-kernel-or-error) - (ein:object-at-point-or-error))) - (unless (ein:pytools-magic-func-p func) - (if (>= (ein:$kernel-api-version kernel) 3) - (ein:kernel-execute - kernel - (format "__ein_print_object_info_for(%s)" func) - (list - :output (cons - (lambda (name msg-type content -metadata-not-used-) - (ein:case-equal msg-type - (("stream" "display_data") - (ein:pytools-finish-tooltip name (ein:json-read-from-string (or (plist-get content :text) (plist-get (plist-get content :data) :text/plain))) nil)))) - func))) - (ein:kernel-object-info-request - kernel func (list :object_info_reply - (cons #'ein:pytools-finish-tooltip nil)))))) - -(declare-function pos-tip-show "pos-tip") -(declare-function popup-tip "popup") - -(defun ein:pytools-finish-tooltip (-ignore- content -metadata-not-used-) - ;; See: Tooltip.prototype._show (tooltip.js) - (let ((tooltip (ein:kernel-construct-help-string content)) - (defstring (ein:kernel-construct-defstring content)) - (name (plist-get content :name))) - (if tooltip - (cond - ((and window-system (featurep 'pos-tip)) - (pos-tip-show tooltip 'ein:pos-tip-face nil nil 0)) - ((featurep 'popup) - (popup-tip tooltip)) - (t (when (stringp defstring) - (message (ein:trim (ansi-color-apply defstring)))))) - (ein:log 'info "no info for %s" name)))) - (defun ein:pytools-request-help (kernel func) (interactive (list (ein:get-kernel-or-error) (ein:object-at-point-or-error))) @@ -151,18 +56,6 @@ working." ;; not work in silent mode. :silent nil)) -(defun ein:pytools-request-tooltip-or-help (&optional pager) - "Show the help for the object at point using tooltip. -When the prefix argument ``C-u`` is given, open the help in the -pager buffer. You can explicitly specify the object by selecting it." - (interactive "P") - (call-interactively (if pager - #'ein:pytools-request-help - #'ein:pytools-request-tooltip))) - - -;;; Source jump - (defvar ein:pytools-jump-stack nil) (defvar ein:pytools-jump-to-source-not-found-regexp @@ -211,40 +104,6 @@ pager buffer. You can explicitly specify the object by selecting it." #'ein:pytools-jump-to-source-1 (list kernel object other-window notebook))))) -(defun ein:pytools-find-source (kernel object &optional callback) - "Find the file and line where object is defined. -This function mostly exists to support company-mode, but might be -useful for other purposes. If the definition for object can be -found and when callback isort specified, the callback will be -called with a cons of the filename and line number where object -is defined." - (ein:kernel-execute - kernel - (format "__ein_find_source('%s')" object) - (list - :output - (cons - #'ein:pytools-finish-find-source - (list kernel object callback))))) - -(defun ein:pytools-finish-find-source (packed msg-type content -ignored-) - (destructuring-bind (kernel object callback) packed - (if (or (string= msg-type "stream") - (string= msg-type "display_data")) - (aif (or (plist-get content :text) (plist-get (plist-get content :data) :text/plain)) - (if (string-match ein:pytools-jump-to-source-not-found-regexp it) - (ein:log 'info - "Source of %s not found" object) - (destructuring-bind (filename &optional lineno &rest ignore) - (split-string it "\n") - (if callback - (funcall callback - (cons (ein:kernel-filename-from-python kernel filename) - (string-to-number lineno))) - (cons (ein:kernel-filename-from-python kernel filename) - (string-to-number lineno)))))) ;; FIXME Generator? - (ein:log 'info "Source of %s notebook found" object)))) - (defun ein:pytools-jump-to-source-command (&optional other-window) "Jump to the source code of the object at point. When the prefix argument ``C-u`` is given, open the source code @@ -282,126 +141,6 @@ given, open the last point in the other window." 'ein:pytools-eval-string-internal 'ein:shared-output-eval-string "0.1.2") -(defun ein:pytools-doctest () - "Do the doctest of the object at point." - (interactive) - (let ((object (ein:object-at-point))) - (ein:shared-output-eval-string (ein:get-kernel) - (format "__ein_run_docstring_examples(%s)" object) - t))) - -(defun ein:pytools-whos () - "Execute ``%whos`` magic command and popup the result." - (interactive) - (ein:shared-output-eval-string (ein:get-kernel) "%whos" t)) - -(defun ein:pytools-hierarchy (&optional ask) - "Draw inheritance graph of the class at point. -hierarchymagic_ extension is needed to be installed. -You can explicitly specify the object by selecting it. - -.. _hierarchymagic: https://github.com/tkf/ipython-hierarchymagic" - (interactive "P") - (let ((object (ein:object-at-point))) - (when ask - (setq object (read-from-minibuffer "class or object: " object))) - (assert (and object (not (equal object ""))) - nil "Object at point not found.") - (ein:shared-output-eval-string (ein:get-kernel) (format "%%hierarchy %s" object) t))) - -(defun ein:pytools-pandas-to-ses (dataframe) - "View pandas_ DataFrame in SES_ (Simple Emacs Spreadsheet). -Open a `ses-mode' buffer and import DataFrame object into it. - -SES_ is distributed with Emacs since Emacs 22, so you don't need -to install it if you are using newer Emacs. - -.. _pandas: http://pandas.pydata.org -.. _SES: http://www.gnu.org/software/emacs/manual/html_node/ses/index.html" - (interactive (list (read-from-minibuffer "pandas DataFrame " - (ein:object-at-point)))) - (let ((buffer (get-buffer-create - (generate-new-buffer-name "*ein:ses pandas*")))) - ;; fetch TSV (tab separated values) via stdout - (ein:kernel-request-stream - (ein:get-kernel) - (concat dataframe ".to_csv(__import__('sys').stdout, sep='\\t')") - (lambda (tsv buffer) - (with-current-buffer buffer - (cl-flet ((y-or-n-p - (prompt) - (if (string-prefix-p "Yank will insert " prompt) - t - (error "Unexpected prompt: %s" prompt)))) - ;; Import DataFrame as TSV - (ses-yank-tsf tsv nil)) - ;; Force SES to update (equivalent to run `post-command-hook'). - (ses-command-hook))) - (list buffer)) - ;; Open `ses-mode' buffer - (with-current-buffer buffer - (ses-mode)) - (pop-to-buffer buffer))) - -(defun ein:pytools-export-buffer (buffer format) - "Export contents of notebook using nbconvert_ to user-specified format -\(options will depend on the version of nbconvert available\) to a new buffer. - -Currently EIN/IPython supports exporting to the following formats: - - - HTML - - JSON (this is basically the same as opening the ipynb file in a buffer). - - Latex - - Markdown - - Python - - RST - - Slides - -.. _nbconvert: http://ipython.org/ipython-doc/stable/notebook/nbconvert.html" - (interactive (list (read-buffer "Buffer: " (current-buffer) t) - (ein:completing-read "Export format: " - (list "html" - "json" - "latex" - "markdown" - "python" - "rst" - "slides")))) - (let* ((nb (first (ein:notebook-opened-notebooks - #'(lambda (nb) - (equal (buffer-name (ein:notebook-buffer nb)) - buffer))))) - (json (json-encode (ein:notebook-to-json nb))) - (name (format "*ein %s export: %s*" format (ein:$notebook-notebook-name nb))) - (buffer (get-buffer-create name))) - (if (equal format "json") - (with-current-buffer buffer - (erase-buffer) - (insert json) - (json-pretty-print (point-min) (point-max))) - (ein:kernel-request-stream - (ein:get-kernel) - (format "__ein_export_nb(r'%s', '%s')" - json - format) - (lambda (export buffer) - (with-current-buffer buffer - (erase-buffer) - (insert export))) - (list buffer))) - (switch-to-buffer buffer))) - - - -;;;; Helper functions for working with matplotlib - -(defun ein:pytools-set-figure-size (width height) - "Set the default figure size for matplotlib figures. Works by setting `rcParams['figure.figsize']`." - (interactive "nWidth: \nnHeight: ") - (ein:shared-output-eval-string (ein:get-kernel) - (format "__ein_set_figure_size(%s,%s)" width height) - nil)) - (provide 'ein-pytools) ;;; ein-pytools.el ends here diff --git a/lisp/ein-shared-output.el b/lisp/ein-shared-output.el index 955c17c..b6d18b5 100644 --- a/lisp/ein-shared-output.el +++ b/lisp/ein-shared-output.el @@ -34,13 +34,9 @@ (require 'ein-cell) - -;;; Classes and variables - (defclass ein:shared-output-cell (ein:codecell) ((cell-type :initarg :cell-type :initform "shared-output") ;; (element-names :initform (:prompt :output :footer)) - (popup :initarg :popup :initform nil :type boolean) (callback :initarg :callback :initform #'ignore :type function) (callback-called :initarg :callback-called :initform nil :type boolean)) "A singleton cell to show output from non-notebook buffers.") @@ -55,9 +51,6 @@ (defconst ein:shared-output-buffer-name "*ein:shared-output*") - -;;; Cell related - (cl-defmethod ein:cell-insert-prompt ((cell ein:shared-output-cell)) "Insert prompt of the CELL in the buffer. Called from ewoc pretty printer via `ein:cell-pp'." @@ -68,11 +61,10 @@ Called from ewoc pretty printer via `ein:cell-pp'." (when (slot-value cell 'autoexec) " %s" ein:cell-autoexec-prompt)) 'font-lock-face 'ein:cell-input-prompt)) -(cl-defmethod ein:cell-execute ((cell ein:shared-output-cell) kernel code popup +(cl-defmethod ein:cell-execute ((cell ein:shared-output-cell) kernel code &rest args) (unless (plist-get args :silent) (setq args (plist-put args :silent nil))) - (setf (slot-value cell 'popup) popup) (setf (slot-value cell 'kernel) kernel) (apply #'ein:cell-execute-internal cell kernel code args)) @@ -80,8 +72,6 @@ Called from ewoc pretty printer via `ein:cell-pp'." msg-type _content _metadata) (ein:log 'debug "ein:cell--handle-output (cell ein:shared-output-cell): %s" msg-type) - (when (slot-value cell 'popup) - (pop-to-buffer (ein:shared-output-create-buffer))) (cl-call-next-method) (aif (ein:oref-safe cell 'callback) (progn @@ -194,33 +184,15 @@ See also `ein:cell-max-num-outputs'." (ein:shared-output-show-code-cell cell) (error "No code cell at point.")))) -(defvar ein:shared-output-eval-string-history nil - "History of the `ein:shared-output-eval-string' prompt.") - ;;;###autoload -(defun ein:shared-output-eval-string (kernel code popup &rest args) - "Evaluate a piece of code. Prompt will appear asking the code to run. -This is handy when you want to execute something quickly without -making a cell. If the code outputs something, it will go to the -shared output buffer. You can open the buffer by the command -`ein:shared-output-pop-to-buffer'. - -.. ARGS is passed to `ein:kernel-execute'. Unlike `ein:kernel-execute', - `:silent' is `nil' by default." - (interactive - (list nil - (read-string - "IP[y]: " - (when (region-active-p) - (buffer-substring (region-beginning) (region-end))) - 'ein:shared-output-eval-string-history) - nil)) +(defun ein:shared-output-eval-string (kernel code &rest args) + "Entry to `ein:cell-execute-internal' from the shared output cell." (unless kernel (setq kernel (ein:get-kernel-or-error))) (let ((cell (ein:shared-output-get-cell))) (ein:kernel-when-ready kernel (lambda (ready-kernel) - (apply #'ein:cell-execute cell ready-kernel (ein:trim-indent code) popup args))))) + (apply #'ein:cell-execute cell ready-kernel (ein:trim-indent code) args))))) ;;; Generic getter diff --git a/lisp/ob-ein.el b/lisp/ob-ein.el index f88d8bc..d6b67ae 100644 --- a/lisp/ob-ein.el +++ b/lisp/ob-ein.el @@ -158,26 +158,20 @@ Based on ob-ipython--configure-kernel." 'ob-ein-default-header-args:ein) (fset (intern (concat "org-babel-execute:" lang-name)) `(lambda (body params) + "Should get rid of accommodating org-babel-variable-assignments. +We don't test it, and finding a module named ob-LANG-MODE won't work generally, +e.g., ob-c++ is not ob-C.el." (require (quote ,(intern (format "ob-%s" lang-mode))) nil t) + ;; hack because ob-ein loads independently of ein (custom-set-variables '(python-indent-guess-indent-offset-verbose nil)) (let ((parser (quote - ,(intern (format "org-babel-variable-assignments:%s" lang-mode)))) - (expander - (quote - ,(intern (format "org-babel-expand-body:%s" lang-mode))))) + ,(intern (format "org-babel-variable-assignments:%s" lang-mode))))) (ob-ein--execute-body - (cond ((fboundp expander) - (funcall (symbol-function expander) - (encode-coding-string body 'utf-8) - params)) - ((fboundp parser) - (org-babel-expand-body:generic - body params (funcall (symbol-function parser) params))) - (t (ein:log 'verbose "%s: No org-babel-expand-body:%s" - (concat "org-babel-execute:" ,lang-name) - (quote ,lang-mode)) - nil)) + (if (fboundp parser) + (org-babel-expand-body:generic + body params (funcall (symbol-function parser) params)) + body) params))))) (defun ob-ein--execute-body (body params) @@ -222,9 +216,8 @@ Based on ob-ipython--configure-kernel." do (sleep-for 0 interval) finally return (if pending - (progn - (ein:log 'error "ob-ein--execute-body: %s timed out" name) - "") + (prog1 "" + (ein:log 'error "ob-ein--execute-body: %s timed out" name)) (ob-ein--process-outputs (ein:oref-safe (ein:shared-output-get-cell) 'outputs) processed-params)))) @@ -278,7 +271,7 @@ one at a time. Further, we do not order the queued up blocks!" (deferred:nextc (deferred:wait 1200) self))))) (deferred:nextc it (lambda (_x) - (ein:shared-output-eval-string kernel body nil))))) + (ein:shared-output-eval-string kernel body))))) (defun ob-ein--parse-session (session) (multiple-value-bind (url-or-port _password) (ein:jupyter-server-conn-info) diff --git a/lisp/poly-ein.el b/lisp/poly-ein.el index ebd7285..b877d03 100644 --- a/lisp/poly-ein.el +++ b/lisp/poly-ein.el @@ -173,8 +173,15 @@ TYPE can be 'body, nil." (mode (pm-get-mode-symbol-from-name what)) (_ (not (equal mode (ein:oref-safe cm 'mode))))) (when (eq mode 'poly-fallback-mode) - (ein:display-warning - (format "pm:get-span: no major mode for kernelspec language '%s'" what))) + (let ((warning (format (concat "pm-get-span: Add (%s . [mode-prefix]) to " + "polymode-mode-name-aliases") + what))) + (when (or (not (get-buffer "*Warnings*")) + (not (with-current-buffer "*Warnings*" + (save-excursion + (goto-char (point-min)) + (re-search-forward (regexp-quote warning) nil t))))) + (ein:display-warning warning)))) (setq result-cm (cl-loop for ocm in (eieio-oref pm/polymode '-auto-innermodes) when (equal mode (ein:oref-safe ocm 'mode))