popup.elを使ってRオブジェクトの情報をpopupするess-R-object-popup.el

NO IMAGE

以前似たようなものを紹介したのですが、いろいろとしょぼかったので最新のpopup.elを使ったものに変えてみました。

使うには最新のauto-complete.elが必要です。
popup.elによるポップアップメニュー、カスケードポップアップメニュー、ツールチップの実現 – Monthly Emacs

それと、ESSが必要になります。debian系ならばapt-get install ESSでインストールできるはずです。
そうでない方は、以前導入方法諸々について書いたのでそちらを見てもらえるとよくわかるかもしれません。
これからEmacsでR使う人のための設定まとめ

R-object-popup.elはgistに置いてます。
gist: 318365 – GitHub
スクリプトはこんな感じ。
[cpp]
;; ess-R-object-popup.el
;;
;; I have defined a function, ess-R-object-popup, that when
;; invoked, will return a popup with some information about
;; the object at point. The information returned is
;; determined by which R function is called. This is controlled
;; by an alist, called ess-R-object-popup-alist. The default is
;; given below. The keys are the classes of R object that will
;; use the associated function. For example, when the function
;; is called while point is on a factor object, a table of that
;; factor will be shown in the popup.

;; the alist
(setq ess-R-object-popup-alist
‘((numeric . "summary")
(factor . "table")
(integer . "summary")
(lm . "summary")
(other . "str")))

(defun ess-R-object-popup ()
"Get info for object at point, and display it in a popup."
(interactive)
(let ((objname (current-word))
(curbuf (current-buffer))
(tmpbuf (get-buffer-create "**ess-R-object-popup**")))
(if objname
(progn
(ess-command (concat "class(" objname ")n") tmpbuf )
(set-buffer tmpbuf)
(let ((bs (buffer-string)))
(if (not(string-match "(object .* not found)|unexpected" bs))
(let* ((objcls (buffer-substring
(+ 2 (string-match "".*"" bs))
(- (point-max) 2)))
(myfun (cdr(assoc-string objcls
ess-R-object-popup-alist))))
(progn
(if (eq myfun nil)
(setq myfun
(cdr(assoc-string "other"
ess-R-object-popup-alist))))
(ess-command (concat myfun "(" objname ")n") tmpbuf)
(let ((bs (buffer-string)))
(progn
(set-buffer curbuf)
(popup-tip bs)))))))))
(kill-buffer tmpbuf)))

;; my default key map
(define-key ess-mode-map "C-cC-g" ‘ess-R-object-popup)

(provide ‘ess-R-object-popup)
[/cpp]
ここからファイルを落としてきて.emacsに(require ‘ess-R-object-popup.el)としておきましょう。

デフォルトではキーバインドはC-c C-gとなってます。何かのオブジェクトの上にカーソルを合わせてC-c C-gすることでそのオブジェクトをstr()なりsummary()した結果がpopupするんではないかと思います。

今回は雰囲気を見てもらうために動画も撮ってみました。しょっぼいですがこんな感じです。

popup.elはmouse.elと違って、マウス使用を前提とせずにツールチップを表示してくれるます。なのでカーソルが変な位置にあっても表示がおかしくなるということはありませんし、emacs -nwな環境でもツールチップが表示されます。これ以外でもいろいろな用途が考えられそうです。