1 ;;; eldoc-eval.el --- Enable eldoc support when minibuffer is in use.
3 ;; Copyright (C) 2011, 2012 Free Software Foundation, Inc.
5 ;; Author: Thierry Volpiatto <thierry.volpiatto@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This package enables eldoc support when minibuffer is in use.
27 ;; Eldoc info is shown by default in mode-line,
28 ;; but you can have eldoc info somewhere else by setting
29 ;; `eldoc-in-minibuffer-show-fn' to another function (e.g `tooltip-show').
31 ;; By default with this package `M-:' will use `pp-eval-expression'
32 ;; instead of `eval-expression'; you can change that by setting
33 ;; `eval-preferred-function'.
35 ;; It also provides a convenient macro to enable eldoc support
36 ;; in your own functions using minibuffer or in your defadvices,
37 ;; that is `with-eldoc-in-minibuffer'.
39 ;; Users of own minibuffer frame will have to set
40 ;; `eldoc-in-minibuffer-own-frame-p' to non-nil.
42 ;; You can turn off eldoc support in minibuffer any time
43 ;; by setting `eldoc-in-minibuffer' to nil.
49 ;;; Minibuffer support.
50 ;; Enable displaying eldoc info in something else
51 ;; Than minibuffer when this one is in use.
53 (defcustom eldoc-in-minibuffer t
54 "Turn on eldoc in minibuffer."
58 (defcustom eldoc-in-minibuffer-show-fn 'eldoc-show-in-mode-line
59 "A function to display eldoc info.
60 Should take one arg: the string to display"
64 (defcustom eldoc-show-in-mode-line-delay 12
65 "The time we show eldoc when Emacs is idle."
69 (defcustom eval-preferred-function 'pp-eval-expression
70 "Preferred function to use with `M-:'."
74 (defcustom eldoc-in-minibuffer-own-frame-p nil
75 "Whether minibuffer has its own frame or not."
79 ;;; Compatibility with Emacs-24.4
80 ;; New implementation of eldoc in minibuffer that come
81 ;; with Emacs-24.4 show the eldoc info of current-buffer while
82 ;; minibuffer is in use, disable this and inline old Emacs behavior.
84 (when (boundp 'eldoc-message-function)
85 (setq eldoc-message-function 'message)
87 (defun eldoc-display-message-no-interference-p ()
89 (not executing-kbd-macro)
90 (not (and (boundp 'edebug-active) edebug-active))
91 ;; Having this mode operate in an active minibuffer/echo area causes
92 ;; interference with what's going on there.
93 (not cursor-in-echo-area)
94 (not (eq (selected-window) (minibuffer-window))))))
97 (defvar eldoc-active-minibuffers-list nil
98 "List of active minibuffers with eldoc enabled.")
99 (defvar eldoc-mode-line-rolling-flag nil)
101 (defun eldoc-store-minibuffer ()
102 "Store minibuffer buffer name in `eldoc-active-minibuffers-list'.
103 This function is called by each minibuffer started with eldoc support.
104 See `with-eldoc-in-minibuffer'."
105 (with-selected-window (minibuffer-window)
106 (push (buffer-name) eldoc-active-minibuffers-list)))
108 (defmacro with-eldoc-in-minibuffer (&rest body)
109 "Enable eldoc support for minibuffer input that runs in BODY."
110 (declare (indent 0) (debug t))
111 `(let ((timer (and eldoc-in-minibuffer
114 'repeat 'eldoc-mode-in-minibuffer))))
116 (minibuffer-with-setup-hook
117 ;; When minibuffer is activated in body,
119 'eldoc-store-minibuffer
121 (and timer (cancel-timer timer))
122 ;; Each time a minibuffer exits or aborts
123 ;; its buffer is removed from stack,
124 ;; assuming we can only exit the active minibuffer
126 (setq eldoc-active-minibuffers-list
127 (cdr eldoc-active-minibuffers-list)))))
129 (defun eldoc-current-buffer ()
130 "Return the current buffer prior to activating the minibuffer."
131 (with-selected-frame (last-nonminibuffer-frame)
133 (cond (eldoc-in-minibuffer-own-frame-p
135 ((fboundp 'window-in-direction)
137 'above (minibuffer-window)))
138 (t (minibuffer-selected-window))))))
140 (defun eldoc-show-in-mode-line (str)
141 "Display string STR in the mode-line next to minibuffer."
142 (let (mode-line-in-non-selected-windows)
143 (with-current-buffer (eldoc-current-buffer)
144 (make-local-variable 'mode-line-format)
145 (let ((mode-line-format (concat " " str)))
146 (eldoc-maybe-roll-message-in-mode-line mode-line-format))
147 (force-mode-line-update))))
149 (defun eldoc-maybe-roll-message-in-mode-line (str)
150 (let* ((max (window-width (get-buffer-window (eldoc-current-buffer))))
153 (if (and (> len max) eldoc-mode-line-rolling-flag)
155 (setq tmp-str (substring tmp-str 2)
156 mode-line-format (concat tmp-str " [<]" str))
157 (force-mode-line-update nil)
158 (when (< (length tmp-str) 2) (setq tmp-str str)))
159 (force-mode-line-update nil)
160 (sit-for eldoc-show-in-mode-line-delay))))
162 (defun eldoc-mode-line-toggle-rolling ()
164 (setq eldoc-mode-line-rolling-flag (not eldoc-mode-line-rolling-flag)))
165 (define-key minibuffer-local-map (kbd "<C-M-right>") 'eldoc-mode-line-toggle-rolling)
167 (defun eldoc-mode-in-minibuffer ()
168 "Show eldoc for current minibuffer input."
169 (let ((buf (buffer-name (window-buffer (active-minibuffer-window)))))
170 ;; If this minibuffer have been started with
171 ;;`with-eldoc-in-minibuffer' give it eldoc support
172 ;; and update mode-line, otherwise do nothing.
174 (when (member buf eldoc-active-minibuffers-list)
175 (with-current-buffer buf
176 (let* ((sym (save-excursion
177 (unless (looking-back ")\\|\"")
179 (eldoc-current-symbol)))
180 (info-fn (eldoc-fnsym-in-current-sexp))
181 (doc (or (eldoc-get-var-docstring sym)
182 (eldoc-get-fnsym-args-string
183 (car info-fn) (cadr info-fn)))))
184 (when doc (funcall eldoc-in-minibuffer-show-fn doc)))))
186 (beginning-of-buffer nil)
187 (error (message "Eldoc in minibuffer error: %S" err)))))
189 (defun eval-expression-with-eldoc ()
190 "Eval expression with eldoc support in mode-line."
192 (with-eldoc-in-minibuffer
193 (call-interactively eval-preferred-function)))
196 (global-set-key [remap eval-expression] 'eval-expression-with-eldoc)
199 (provide 'eldoc-eval)
200 ;;; eldoc-eval.el ends here