Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jupyter-describe function for interactive inspect #238

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,18 @@ associate with the =current-buffer= and enable the minor mode
=jupyter-repl-interaction-mode=. This minor mode populates the following
keybindings for interacting with the REPL:

| 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 @@ -946,24 +946,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."
(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
Copy link
Contributor Author

@non-Jedi non-Jedi Apr 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be using completing-read. Any idea why it was written this way instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied it over from read--expression called by eval-expression (M-:). My intention with jupyter-read-expression is to parallel read--expression. Also completing-read is more for completing collections right? jupyter-read-expression is supposed to read a line of code for evaluation from the minibuffer while also allowing completion from the kernel within the line.

Although the documentation for jupyter-read-expression does say for completion instead of something more approriate like for evalution so its purpose isn't that clear.

Copy link
Contributor Author

@non-Jedi non-Jedi Apr 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. That makes sense. So for this new purpose I'm putting this code to, using completing-read probably makes more sense, but it doesn't make sense in the context of jupyter-read-expression which is likely to complete multiple symbols. I think this is an acceptable hack though until I finish a completing-read based implementation (I started to do so over at non-Jedi@72b84b8, and it's probably only an hour or so more work on my part to finish, but with 2 young children in the house, I have no idea when I'll have time to finish)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In either case, I think it probably makes more sense to just refactor jupyter-read-expression to take optional parameters rather than define an internal jupyter--read-with-completion. :/

'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 @@ -1419,6 +1427,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 @@ -1947,6 +1948,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