Move tooltip and help command to pytools

ein-notebook.el and ein-connect.el can share same command thanks to
the generic getter function.
This commit is contained in:
Takafumi Arakaki 2012-08-19 21:36:35 +02:00
parent e2be2ab3b0
commit f32a169920
6 changed files with 63 additions and 77 deletions

View file

@ -625,24 +625,6 @@ Return `nil' always for non-code cells."
prompt EWOC node." prompt EWOC node."
(ein:cell-set-autoexec cell (not (ein:cell-autoexec-p cell)))) (ein:cell-set-autoexec cell (not (ein:cell-autoexec-p cell))))
(declare-function pos-tip-show "pos-tip")
(declare-function popup-tip "popup")
(defun ein:cell-finish-tooltip (cell 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))))
(defmethod ein:cell-goto ((cell ein:basecell) &optional relpos) (defmethod ein:cell-goto ((cell ein:basecell) &optional relpos)
"Go to the input area of the given CELL. "Go to the input area of the given CELL.
RELPOS is the position relative to the input area. Default is 0." RELPOS is the position relative to the input area. Default is 0."

View file

@ -246,24 +246,9 @@ See also: `ein:connect-run-buffer', `ein:connect-eval-buffer'."
'ein:connect-eval-string-internal 'ein:connect-eval-string-internal
'ein:shared-output-eval-string "0.1.2") 'ein:shared-output-eval-string "0.1.2")
(defun ein:connect-request-tool-tip-command () (define-obsolete-function-alias
(interactive) 'ein:connect-request-tool-tip-or-help-command
(let ((notebook (ein:connect-get-notebook))) 'ein:pytools-request-tooltip-or-help "0.1.2")
(ein:kernel-if-ready (ein:$notebook-kernel notebook)
(let ((func (ein:object-at-point)))
;; Set cell=nil. In fact, the argument cell is not used.
;; FIXME: refactor `ein:notebook-request-tool-tip'
(ein:notebook-request-tool-tip notebook nil func)))))
(defun ein:connect-request-help-command ()
(interactive)
(ein:notebook-request-help (ein:connect-get-notebook)))
(defun ein:connect-request-tool-tip-or-help-command (&optional pager)
(interactive "P")
(if pager
(ein:connect-request-help-command)
(ein:connect-request-tool-tip-command)))
(defun ein:connect-complete-command () (defun ein:connect-complete-command ()
(interactive) (interactive)
@ -344,7 +329,7 @@ change the cells to run."
(define-key map "\C-c\C-l" 'ein:connect-reload-buffer) (define-key map "\C-c\C-l" 'ein:connect-reload-buffer)
(define-key map "\C-c\C-r" 'ein:connect-eval-region) (define-key map "\C-c\C-r" 'ein:connect-eval-region)
(define-key map (kbd "C-:") 'ein:shared-output-eval-string) (define-key map (kbd "C-:") 'ein:shared-output-eval-string)
(define-key map "\C-c\C-f" 'ein:connect-request-tool-tip-or-help-command) (define-key map "\C-c\C-f" 'ein:pytools-request-tooltip-or-help)
(define-key map "\C-c\C-i" 'ein:connect-complete-command) (define-key map "\C-c\C-i" 'ein:connect-complete-command)
(define-key map "\C-c\C-z" 'ein:connect-pop-to-notebook) (define-key map "\C-c\C-z" 'ein:connect-pop-to-notebook)
(define-key map "\C-c\C-a" 'ein:connect-toggle-autoexec) (define-key map "\C-c\C-a" 'ein:connect-toggle-autoexec)

View file

@ -530,14 +530,14 @@ http://ipython.org/ipython-doc/dev/development/messaging.html#complete
(defun ein:kernel-construct-defstring (content) (defun ein:kernel-construct-defstring (content)
"Construct call signature from CONTENT of ``:object_info_reply``. "Construct call signature from CONTENT of ``:object_info_reply``.
Used in `ein:cell-finish-tooltip', etc." Used in `ein:pytools-finish-tooltip', etc."
(or (plist-get content :call_def) (or (plist-get content :call_def)
(plist-get content :init_definition) (plist-get content :init_definition)
(plist-get content :definition))) (plist-get content :definition)))
(defun ein:kernel-construct-help-string (content) (defun ein:kernel-construct-help-string (content)
"Construct help string from CONTENT of ``:object_info_reply``. "Construct help string from CONTENT of ``:object_info_reply``.
Used in `ein:cell-finish-tooltip', etc." Used in `ein:pytools-finish-tooltip', etc."
(ein:log 'debug "KERNEL-CONSTRUCT-HELP-STRING") (ein:log 'debug "KERNEL-CONSTRUCT-HELP-STRING")
(let* ((defstring (ein:aand (let* ((defstring (ein:aand
(ein:kernel-construct-defstring content) (ein:kernel-construct-defstring content)

View file

@ -941,43 +941,9 @@ next cell, or insert if none."
do (ein:cell-execute cell)) do (ein:cell-execute cell))
(ein:log 'error "Not in notebook buffer!"))) (ein:log 'error "Not in notebook buffer!")))
(defun ein:notebook-request-tool-tip (notebook cell func) (define-obsolete-function-alias
(let ((kernel (ein:$notebook-kernel notebook)) 'ein:notebook-request-tool-tip-or-help-command
(callbacks 'ein:pytools-request-tooltip-or-help "0.1.2")
(list :object_info_reply
(cons #'ein:cell-finish-tooltip cell))))
(ein:kernel-object-info-request kernel func callbacks)))
(defun ein:notebook-request-tool-tip-command ()
(interactive)
(ein:notebook-with-cell #'ein:codecell-p
(ein:kernel-if-ready (ein:$notebook-kernel ein:%notebook%)
(let ((func (ein:object-at-point)))
(ein:notebook-request-tool-tip ein:%notebook% cell func)))))
(defun ein:notebook-request-help (notebook)
(ein:kernel-if-ready (ein:$notebook-kernel notebook)
(let ((func (ein:object-at-point)))
(when func
(ein:kernel-execute (ein:$notebook-kernel notebook)
(format "%s?" func) ; = code
nil ; = callbacks
;; It looks like that magic command does
;; not work in silent mode.
:silent nil)))))
(defun ein:notebook-request-help-command ()
(interactive)
(ein:notebook-request-help ein:%notebook%))
(defun ein:notebook-request-tool-tip-or-help-command (&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")
(if pager
(ein:notebook-request-help-command)
(ein:notebook-request-tool-tip-command)))
(defun ein:notebook-complete-at-point (notebook) (defun ein:notebook-complete-at-point (notebook)
(let ((kernel (ein:$notebook-kernel notebook)) (let ((kernel (ein:$notebook-kernel notebook))
@ -1385,7 +1351,7 @@ Do not use `python-mode'. Use plain mode when MuMaMo is not installed::
(define-key map (kbd "C-c <down>") 'ein:notebook-move-cell-down-command) (define-key map (kbd "C-c <down>") 'ein:notebook-move-cell-down-command)
(define-key map (kbd "M-<up>") 'ein:notebook-move-cell-up-command) (define-key map (kbd "M-<up>") 'ein:notebook-move-cell-up-command)
(define-key map (kbd "M-<down>") 'ein:notebook-move-cell-down-command) (define-key map (kbd "M-<down>") 'ein:notebook-move-cell-down-command)
(define-key map "\C-c\C-f" 'ein:notebook-request-tool-tip-or-help-command) (define-key map "\C-c\C-f" 'ein:pytools-request-tooltip-or-help)
(define-key map "\C-c\C-i" 'ein:notebook-complete-command) (define-key map "\C-c\C-i" 'ein:notebook-complete-command)
(define-key map "\C-c\C-x" 'ein:tb-show) (define-key map "\C-c\C-x" 'ein:tb-show)
(define-key map "\C-c\C-r" 'ein:notebook-restart-kernel-command) (define-key map "\C-c\C-r" 'ein:notebook-restart-kernel-command)

View file

@ -61,6 +61,56 @@ If OTHER-WINDOW is non-`nil', open the file in the other window."
kernel kernel
(format "__import__('sys').path.append('%s')" ein:source-dir))) (format "__import__('sys').path.append('%s')" ein:source-dir)))
;;; Tooltip and help
(defun ein:pytools-request-tooltip (kernel func)
(interactive (list (ein:get-kernel-or-error)
(ein:object-at-point-or-error)))
(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)))
(ein:kernel-execute kernel
(format "%s?" func) ; = code
nil ; = callbacks
;; It looks like that magic command does
;; 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-stack nil)
(defvar ein:pytools-jump-to-source-not-found-regexp (defvar ein:pytools-jump-to-source-not-found-regexp

View file

@ -198,6 +198,9 @@ before previous opening parenthesis."
(search-backward "(" (point-at-bol) t)) (search-backward "(" (point-at-bol) t))
(thing-at-point 'word)))))) (thing-at-point 'word))))))
(defun ein:object-at-point-or-error ()
(or (ein:object-at-point) (error "No object found at the point")))
;;; URL utils ;;; URL utils