jupyter-repl-after-change (julia): Use :user-expressions key to compute Pkg prompt (#73)

Instead of using `jupyter-eval` which creates unnecessary `:execute-input` and
`:execute-result` messages, use the `:user-expressions` key of an execute
request to compute the Pkg prompt. This way the only message generated is an
`:execute-reply` and we avoid modifying the execution count.

This requires JuliaLang/IJulia.jl@85eb54ae17
This commit is contained in:
Nathaniel Nicandro 2019-04-06 12:40:17 -05:00 committed by GitHub
parent 96c872fb7c
commit 53c2ffdd51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -116,6 +116,26 @@ Make the character after `point' invisible."
(overlay-put ov 'after-string (propertize " " 'display md))
(overlay-put ov 'evaporate t)))
(defun jupyter-julia-pkg-prompt ()
"Return the Pkg prompt.
If the Pkg prompt can't be retrieved from the kernel, return
nil."
(when-let* ((msg (jupyter-wait-until-received :execute-reply
(jupyter-send-execute-request jupyter-current-client
:code ""
:silent t
:user-expressions
(list :prompt "import Pkg; Pkg.REPLMode.promptf()"))
;; Longer timeout to account for initial Pkg import and
;; compilation.
jupyter-long-timeout)))
(cl-destructuring-bind (&key prompt &allow-other-keys)
(jupyter-message-get msg :user_expressions)
(cl-destructuring-bind (&key status data &allow-other-keys)
prompt
(when (equal status "ok")
(plist-get data :text/plain))))))
(cl-defmethod jupyter-repl-after-change ((_type (eql insert)) beg _end
&context (jupyter-lang julia))
"Change the REPL prompt when a REPL mode is entered."
@ -129,16 +149,10 @@ Make the character after `point' invisible."
(setq last-command-event ?\[))
(cl-case (char-after)
(?\]
;; Longer timeout to account for initial Pkg import and compilation.
(let* ((jupyter-default-timeout jupyter-long-timeout)
(pkg-prompt
;; FIXME: This modifies the ans variable in IJulia. Maybe
;; evaluate this in the user-expressions of an execute-request?
(jupyter-eval "import Pkg; Pkg.REPLMode.promptf()")))
(when pkg-prompt
(jupyter-julia-add-prompt
(substring pkg-prompt 1 (1- (length pkg-prompt)))
(aref ansi-color-names-vector 5))))) ; magenta
(when-let* ((pkg-prompt (jupyter-julia-pkg-prompt)))
(jupyter-julia-add-prompt
(substring pkg-prompt 1 (1- (length pkg-prompt)))
(aref ansi-color-names-vector 5)))) ; magenta
(?\;
(jupyter-julia-add-prompt
"shell> " (aref ansi-color-names-vector 1))) ; red