]> code.delx.au - gnu-emacs-elpa/blob - packages/eldoc-eval/eldoc-eval.el
* eldoc-eval.el (eldoc-display-message-no-interference-p): Force
[gnu-emacs-elpa] / packages / eldoc-eval / eldoc-eval.el
1 ;;; eldoc-eval.el --- Enable eldoc support when minibuffer is in use.
2
3 ;; Copyright (C) 2011, 2012 Free Software Foundation, Inc.
4
5 ;; Author: Thierry Volpiatto <thierry.volpiatto@gmail.com>
6 ;; Version: 0.1
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24 ;;
25 ;; This package enables eldoc support when minibuffer is in use.
26 ;;
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').
30 ;;
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'.
34 ;;
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'.
38 ;;
39 ;; Users of own minibuffer frame will have to set
40 ;; `eldoc-in-minibuffer-own-frame-p' to non-nil.
41 ;;
42 ;; You can turn off eldoc support in minibuffer any time
43 ;; by setting `eldoc-in-minibuffer' to nil.
44
45 ;;; Code:
46 (require 'eldoc)
47
48
49 ;;; Minibuffer support.
50 ;; Enable displaying eldoc info in something else
51 ;; Than minibuffer when this one is in use.
52 ;;
53 (defcustom eldoc-in-minibuffer t
54 "Turn on eldoc in minibuffer."
55 :group 'eldoc
56 :type 'bolean)
57
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"
61 :group 'eldoc
62 :type 'function)
63
64 (defcustom eldoc-show-in-mode-line-delay 12
65 "The time we show eldoc when Emacs is idle."
66 :group 'eldoc
67 :type 'number)
68
69 (defcustom eval-preferred-function 'pp-eval-expression
70 "Preferred function to use with `M-:'."
71 :group 'lisp
72 :type 'function)
73
74 (defcustom eldoc-in-minibuffer-own-frame-p nil
75 "Whether minibuffer has its own frame or not."
76 :group 'lisp
77 :type 'boolean)
78
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.
83
84 (when (boundp 'eldoc-message-function)
85 (setq eldoc-message-function 'message)
86
87 (defun eldoc-display-message-no-interference-p ()
88 (and eldoc-mode
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))))))
95
96 ;; Internal.
97 (defvar eldoc-active-minibuffers-list nil
98 "List of active minibuffers with eldoc enabled.")
99 (defvar eldoc-mode-line-rolling-flag nil)
100
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)))
107
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
112 (run-with-idle-timer
113 eldoc-idle-delay
114 'repeat 'eldoc-mode-in-minibuffer))))
115 (unwind-protect
116 (minibuffer-with-setup-hook
117 ;; When minibuffer is activated in body,
118 ;; store it.
119 'eldoc-store-minibuffer
120 ,@body)
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
125 ;; on top of stack.
126 (setq eldoc-active-minibuffers-list
127 (cdr eldoc-active-minibuffers-list)))))
128
129 (defun eldoc-current-buffer ()
130 "Return the current buffer prior to activating the minibuffer."
131 (with-selected-frame (last-nonminibuffer-frame)
132 (window-buffer
133 (cond (eldoc-in-minibuffer-own-frame-p
134 (selected-window))
135 ((fboundp 'window-in-direction)
136 (window-in-direction
137 'above (minibuffer-window)))
138 (t (minibuffer-selected-window))))))
139
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))))
148
149 (defun eldoc-maybe-roll-message-in-mode-line (str)
150 (let* ((max (window-width (get-buffer-window (eldoc-current-buffer))))
151 (len (length str))
152 (tmp-str str))
153 (if (and (> len max) eldoc-mode-line-rolling-flag)
154 (while (sit-for 0.3)
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))))
161
162 (defun eldoc-mode-line-toggle-rolling ()
163 (interactive)
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)
166
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.
173 (condition-case err
174 (when (member buf eldoc-active-minibuffers-list)
175 (with-current-buffer buf
176 (let* ((sym (save-excursion
177 (unless (looking-back ")\\|\"")
178 (forward-char -1))
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)))))
185 (scan-error nil)
186 (beginning-of-buffer nil)
187 (error (message "Eldoc in minibuffer error: %S" err)))))
188
189 (defun eval-expression-with-eldoc ()
190 "Eval expression with eldoc support in mode-line."
191 (interactive)
192 (with-eldoc-in-minibuffer
193 (call-interactively eval-preferred-function)))
194
195 ;; Bind it to `M-:'.
196 (global-set-key [remap eval-expression] 'eval-expression-with-eldoc)
197
198
199 (provide 'eldoc-eval)
200 ;;; eldoc-eval.el ends here