Skip to content

Commit

Permalink
Add jupyter-describe function for interactive inspect
Browse files Browse the repository at this point in the history
jupyter-describe implements an interface (with completion) similar to
emacs builtin describe-function for interacting with jupyter kernels.

Closes emacs-jupyter#199
  • Loading branch information
non-Jedi committed Jan 19, 2022
1 parent 70d290e commit 5858ccb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
23 changes: 12 additions & 11 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,18 @@ send the current line for evaluation by the client's kernel.
When =jupyter-repl-interaction-mode= is enabled, the following
keybindings are available

| Key binding | Command |
|-------------+-------------------------------|
| =C-M-x= | =jupyter-eval-defun= |
| =M-i= | =jupyter-inspect-at-point= |
| =C-c C-b= | =jupyter-eval-buffer= |
| =C-c C-c= | =jupyter-eval-line-or-region= |
| =C-c C-i= | =jupyter-repl-interrupt-kernel= |
| =C-c C-r= | =jupyter-repl-restart-kernel= |
| =C-c C-s= | =jupyter-repl-scratch-buffer= |
| =C-c C-o= | =jupyter-eval-remove-overlays= |
| =C-c M-:= | =jupyter-eval-string= |
| Key binding | Command |
|-------------+---------------------------------|
| =C-M-x= | =jupyter-eval-defun= |
| =M-i= | =jupyter-inspect-at-point= |
| =C-c C-d= | =jupyter-describe= |
| =C-c C-b= | =jupyter-eval-buffer= |
| =C-c C-c= | =jupyter-eval-line-or-region= |
| =C-c C-i= | =jupyter-repl-interrupt-kernel= |
| =C-c C-r= | =jupyter-repl-restart-kernel= |
| =C-c C-s= | =jupyter-repl-scratch-buffer= |
| =C-c C-o= | =jupyter-eval-remove-overlays= |
| =C-c M-:= | =jupyter-eval-string= |

**** Integration with =emacsclient=

Expand Down
48 changes: 33 additions & 15 deletions jupyter-client.el
Original file line number Diff line number Diff line change
Expand Up @@ -692,24 +692,32 @@ client local variable.
Methods that extend this generic function should
`cl-call-next-method' as a last step."
(cl-check-type jupyter-current-client jupyter-kernel-client
"Need a client to read an expression")
(let* ((client jupyter-current-client)
(jupyter--read-expression-history
(jupyter-get client 'jupyter-eval-expression-history)))
(minibuffer-with-setup-hook
(lambda ()
(setq jupyter-current-client client)
(add-hook 'completion-at-point-functions
'jupyter-completion-at-point nil t)
(add-hook 'minibuffer-exit-hook
'jupyter--teardown-minibuffer nil t))
(prog1 (read-from-minibuffer
(format "Eval (%s): " (jupyter-kernel-language client))
nil read-expression-map
nil 'jupyter--read-expression-history)
(jupyter-set client 'jupyter-eval-expression-history
jupyter--read-expression-history)))))
(prog1
(jupyter--read-with-completion
client "Eval (%s): " jupyter--read-expression-history)
(jupyter-set client 'jupyter-eval-expression-history
jupyter--read-expression-history))))

(defun jupyter--read-with-completion (client prompt &optional history)
"Read an expression using CLIENT for completion.
The expression is read from the minibuffer with PROMPT and expression
history HISTORY."
(cl-check-type client jupyter-kernel-client
"Need a client to read an expression")
(minibuffer-with-setup-hook
(lambda ()
(setq jupyter-current-client client)
(add-hook 'completion-at-point-functions
'jupyter-completion-at-point nil t)
(add-hook 'minibuffer-exit-hook
'jupyter--teardown-minibuffer nil t))
(read-from-minibuffer
(format prompt (jupyter-kernel-language client))
nil read-expression-map
nil history)))

(defun jupyter-eval (code &optional mime)
"Send an execute request for CODE, wait for the execute result.
Expand Down Expand Up @@ -1140,6 +1148,16 @@ BUFFER and DETAIL have the same meaning as in `jupyter-inspect'."
(jupyter-code-context 'inspect)
(jupyter-inspect code pos buffer detail)))

(cl-defgeneric jupyter-describe (code &optional buffer detail)
"Inspect CODE provided interactively.
Call `jupter-inspect' for CODE which can be interactively supplied by user.
BUFFER and DETAIL have the same meaning a in `jupyter-inspect'."
(interactive (list (jupyter--read-with-completion
jupyter-current-client "Describe (%s): ")
nil 0))
(jupyter-inspect code nil buffer detail))

(cl-defgeneric jupyter-inspect (code &optional pos buffer detail)
"Inspect CODE.
Send an `:inspect-request' with the `jupyter-current-client' and
Expand Down
2 changes: 2 additions & 0 deletions jupyter-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
;; C-c C-c `jupyter-eval-line-or-region'
;; C-c C-l `jupyter-eval-file'
;; M-i `jupyter-inspect-at-point'
;; C-c C-d `jupyter-describe'
;; C-c C-r `jupyter-repl-restart-kernel'
;; C-c C-i `jupyter-repl-interrupt-kernel'
;; C-c C-z `jupyter-repl-pop-to-buffer'
Expand Down Expand Up @@ -1833,6 +1834,7 @@ the updated state."
(define-key map (kbd "C-c C-l") #'jupyter-load-file)
(define-key map (kbd "C-c M-:") #'jupyter-eval-string-command)
(define-key map (kbd "M-i") #'jupyter-inspect-at-point)
(define-key map (kbd "C-c C-d") #'jupyter-describe)
(define-key map (kbd "C-c C-r") #'jupyter-repl-restart-kernel)
(define-key map (kbd "C-c C-i") #'jupyter-repl-interrupt-kernel)
(define-key map (kbd "C-c C-z") #'jupyter-repl-pop-to-buffer)
Expand Down

0 comments on commit 5858ccb

Please sign in to comment.