Commit | Line | Data |
---|---|---|
7840ced1 GM |
1 | ;;; tooltip.el --- Show tooltip windows |
2 | ||
01b23b99 | 3 | ;; Copyright (C) 1997, 1999 Free Software Foundation, Inc. |
7840ced1 GM |
4 | |
5 | ;; Author: Gerd Moellmann <gerd@acm.org> | |
6 | ;; Keywords: help c mouse tools | |
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 2, or (at your option) | |
13 | ;; 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; see the file COPYING. If not, write to the | |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Put into your `.emacs' | |
28 | ||
29 | ;; (require 'tooltip) | |
30 | ;; (tooltip-mode 1) | |
31 | ||
32 | ||
33 | \f | |
34 | ;;; Code: | |
35 | ||
36 | (eval-when-compile | |
37 | (require 'cl) | |
38 | (require 'comint) | |
39 | (require 'gud)) | |
40 | ||
41 | (provide 'tooltip) | |
42 | ||
43 | \f | |
44 | ;;; Customizable settings | |
45 | ||
46 | (defgroup tooltip nil | |
47 | "Customization group for the `tooltip' package." | |
48 | :group 'help | |
49 | :group 'c | |
50 | :group 'mouse | |
51 | :group 'tools | |
01b23b99 | 52 | :version "21.1" |
7840ced1 GM |
53 | :tag "Tool Tips") |
54 | ||
01b23b99 | 55 | (defvar tooltip-mode) |
7840ced1 GM |
56 | |
57 | (defcustom tooltip-delay 1.0 | |
58 | "Seconds to wait before displaying a tooltip the first time." | |
59 | :tag "Delay" | |
60 | :type 'number | |
61 | :group 'tooltip) | |
62 | ||
63 | ||
64 | (defcustom tooltip-short-delay 0.1 | |
65 | "Seconds to wait between subsequent tooltips on different items." | |
66 | :tag "Short delay" | |
67 | :type 'number | |
68 | :group 'tooltip) | |
69 | ||
70 | ||
71 | (defcustom tooltip-recent-seconds 1 | |
01b23b99 DL |
72 | "Display tooltips if changing tip items within this many seconds. |
73 | Do so after `tooltip-short-delay'." | |
7840ced1 GM |
74 | :tag "Recent seconds" |
75 | :type 'number | |
76 | :group 'tooltip) | |
77 | ||
78 | ||
79 | (defcustom tooltip-frame-parameters | |
80 | '((name . "tooltip") | |
81 | (foreground-color . "black") | |
82 | (background-color . "lightyellow") | |
83 | (internal-border-width . 5) | |
84 | (border-color . "lightyellow") | |
85 | (border-width . 1)) | |
86 | "Frame parameters used for tooltips." | |
87 | :type 'sexp | |
88 | :tag "Frame Parameters" | |
89 | :group 'tooltip) | |
90 | ||
91 | ||
92 | (defcustom tooltip-gud-tips-p nil | |
024e5da6 | 93 | "*Non-nil means show tooltips in GUD sessions." |
7840ced1 GM |
94 | :type 'boolean |
95 | :tag "GUD" | |
96 | :group 'tooltip) | |
97 | ||
98 | ||
99 | (defcustom tooltip-gud-modes '(gud-mode c-mode c++-mode) | |
100 | "List of modes for which to enable GUD tips." | |
101 | :type 'sexp | |
102 | :tag "GUD modes" | |
103 | :group 'tooltip) | |
104 | ||
105 | ||
106 | (defcustom tooltip-gud-display | |
107 | '((eq (tooltip-event-buffer tooltip-gud-event) | |
108 | (marker-buffer overlay-arrow-position))) | |
109 | "List of forms determining where GUD tooltips are displayed. | |
110 | ||
111 | Forms in the list are combined with AND. The default is to display | |
112 | only tooltips in the buffer containing the overlay arrow." | |
113 | :type 'sexp | |
114 | :tag "GUD buffers predicate" | |
115 | :group 'tooltip) | |
116 | ||
117 | ||
118 | \f | |
119 | ;;; Variables that are not customizable. | |
120 | ||
121 | (defvar tooltip-hook nil | |
122 | "Functions to call to display tooltips. | |
123 | Each function is called with one argument EVENT which is a copy of | |
124 | the last mouse movement event that occurred.") | |
125 | ||
126 | ||
127 | (defvar tooltip-timeout-id nil | |
128 | "The id of the timeout started when Emacs becomes idle.") | |
129 | ||
130 | ||
131 | (defvar tooltip-last-mouse-motion-event nil | |
132 | "A copy of the last mouse motion event seen.") | |
133 | ||
134 | ||
135 | (defvar tooltip-hide-time nil | |
136 | "Time when the last tooltip was hidden.") | |
137 | ||
138 | ||
7840ced1 GM |
139 | (defvar tooltip-gud-debugger nil |
140 | "The debugger for which we show tooltips.") | |
141 | ||
142 | ||
143 | \f | |
144 | ;;; Event accessors | |
145 | ||
146 | (defun tooltip-event-buffer (event) | |
147 | "Return the buffer over which event EVENT occurred. | |
148 | This might return nil if the event did not occur over a buffer." | |
149 | (let ((window (posn-window (event-end event)))) | |
150 | (and window (window-buffer window)))) | |
151 | ||
152 | ||
153 | \f | |
154 | ;;; Switching tooltips on/off | |
155 | ||
156 | ;; We don't set track-mouse globally because this is a big redisplay | |
157 | ;; problem in buffers having a pre-command-hook or such installed, | |
158 | ;; which does a set-buffer, like the summary buffer of Gnus. Calling | |
159 | ;; set-buffer prevents redisplay optimizations, so every mouse motion | |
160 | ;; would be accompanied by a full redisplay. | |
161 | ||
162 | ;;;###autoload | |
163 | (defun tooltip-mode (&optional arg) | |
164 | "Mode for tooltip display. | |
165 | With ARG, turn tooltip mode on if and only if ARG is positive." | |
166 | (interactive "P") | |
167 | (let* ((on (if arg | |
168 | (> (prefix-numeric-value arg) 0) | |
169 | (not tooltip-mode))) | |
170 | (hook-fn (if on 'add-hook 'remove-hook))) | |
171 | (setq tooltip-mode on) | |
172 | (funcall hook-fn 'change-major-mode-hook 'tooltip-change-major-mode) | |
173 | (tooltip-activate-mouse-motions-if-enabled) | |
174 | (funcall hook-fn 'pre-command-hook 'tooltip-hide) | |
175 | (funcall hook-fn 'tooltip-hook 'tooltip-gud-tips) | |
176 | (funcall hook-fn 'tooltip-hook 'tooltip-help-tips) | |
177 | (setq show-help-function (if on 'tooltip-show-help-function nil)) | |
178 | ;; `ignore' is the default binding for mouse movements. | |
179 | (define-key global-map [mouse-movement] | |
180 | (if on 'tooltip-mouse-motion 'ignore)) | |
181 | (when (and on tooltip-gud-tips-p) | |
182 | (global-set-key [S-mouse-3] 'tooltip-gud-toggle-dereference) | |
183 | (add-hook 'gdb-mode-hook | |
184 | #'(lambda () (setq tooltip-gud-debugger 'gdb))) | |
185 | (add-hook 'sdb-mode-hook | |
186 | #'(lambda () (setq tooltip-gud-debugger 'sdb))) | |
187 | (add-hook 'dbx-mode-hook | |
188 | #'(lambda () (setq tooltip-gud-debugger 'dbx))) | |
189 | (add-hook 'xdb-mode-hook | |
190 | #'(lambda () (setq tooltip-gud-debugger 'xdb))) | |
191 | (add-hook 'perldb-mode-hook | |
192 | #'(lambda () (setq tooltip-gud-debugger 'perldb)))))) | |
193 | ||
194 | ||
195 | \f | |
196 | ;;; Timeout for tooltip display | |
197 | ||
198 | (defun tooltip-float-time () | |
199 | "Return the values of `current-time' as a float." | |
200 | (let ((now (current-time))) | |
201 | (+ (* 65536.0 (nth 0 now)) | |
202 | (nth 1 now) | |
203 | (/ (nth 2 now) 1000000.0)))) | |
204 | ||
205 | ||
206 | (defun tooltip-delay () | |
207 | "Return the delay in seconds for the next tooltip." | |
208 | (let ((delay tooltip-delay) | |
209 | (now (tooltip-float-time))) | |
210 | (when (and tooltip-hide-time | |
211 | (< (- now tooltip-hide-time) tooltip-recent-seconds)) | |
212 | (setq delay tooltip-short-delay)) | |
213 | delay)) | |
214 | ||
215 | ||
216 | (defun tooltip-disable-timeout () | |
217 | "Disable the tooltip timeout." | |
218 | (when tooltip-timeout-id | |
219 | (disable-timeout tooltip-timeout-id) | |
220 | (setq tooltip-timeout-id nil))) | |
221 | ||
222 | ||
223 | (defun tooltip-add-timeout () | |
224 | "Add a one-shot timeout to call function tooltip-timeout." | |
225 | (setq tooltip-timeout-id | |
226 | (add-timeout (tooltip-delay) 'tooltip-timeout nil))) | |
227 | ||
228 | ||
229 | (defun tooltip-timeout (object) | |
230 | "Function called when timer with id tooltip-timeout-id fires." | |
231 | (run-hook-with-args-until-success 'tooltip-hook | |
232 | tooltip-last-mouse-motion-event)) | |
233 | ||
234 | ||
235 | \f | |
236 | ;;; Reacting on mouse movements | |
237 | ||
238 | (defun tooltip-change-major-mode () | |
239 | "Function added to `change-major-mode-hook' when tooltip mode is on." | |
240 | (add-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled)) | |
241 | ||
242 | ||
243 | (defun tooltip-activate-mouse-motions-if-enabled () | |
244 | "Reconsider for all buffers whether mouse motion events are desired." | |
245 | (remove-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled) | |
246 | (let ((buffers (buffer-list))) | |
247 | (save-excursion | |
248 | (while buffers | |
249 | (set-buffer (car buffers)) | |
250 | (if (and tooltip-mode | |
251 | tooltip-gud-tips-p | |
252 | (memq major-mode tooltip-gud-modes)) | |
253 | (tooltip-activate-mouse-motions t) | |
254 | (tooltip-activate-mouse-motions nil)) | |
255 | (setq buffers (cdr buffers)))))) | |
256 | ||
257 | ||
258 | (defun tooltip-activate-mouse-motions (activatep) | |
259 | "Activate/deactivate mouse motion events for the current buffer. | |
260 | ACTIVATEP non-nil means activate mouse motion events." | |
261 | (if activatep | |
262 | (progn | |
263 | (make-local-variable 'track-mouse) | |
264 | (setq track-mouse t)) | |
265 | (kill-local-variable 'track-mouse))) | |
266 | ||
267 | ||
268 | (defun tooltip-mouse-motion (event) | |
269 | "Command handler for mouse movement events in `global-map'." | |
270 | (interactive "e") | |
271 | (tooltip-hide) | |
272 | (when (car (mouse-pixel-position)) | |
273 | (setq tooltip-last-mouse-motion-event (copy-sequence event)) | |
274 | (tooltip-add-timeout))) | |
275 | ||
276 | ||
277 | \f | |
278 | ;;; Displaying tips | |
279 | ||
280 | (defun tooltip-show (text) | |
281 | "Show a tooltip window at the current mouse position displaying TEXT." | |
282 | (x-show-tip text (selected-frame) tooltip-frame-parameters)) | |
283 | ||
284 | ||
285 | (defun tooltip-hide (&optional ignored-arg) | |
286 | "Hide a tooltip, if one is displayed. | |
287 | Value is non-nil if tooltip was open." | |
288 | (tooltip-disable-timeout) | |
289 | (when (x-hide-tip) | |
290 | (setq tooltip-hide-time (tooltip-float-time)))) | |
291 | ||
292 | ||
293 | \f | |
294 | ;;; Debugger-related functions | |
295 | ||
296 | (defun tooltip-identifier-from-point (point) | |
297 | "Extract the identifier at POINT, if any. | |
298 | Value is nil if no identifier exists at point. Identifier extraction | |
299 | is based on the current syntax table." | |
300 | (save-excursion | |
301 | (goto-char point) | |
302 | (let ((start (progn (skip-syntax-backward "w_") (point)))) | |
303 | (unless (looking-at "[0-9]") | |
304 | (skip-syntax-forward "w_") | |
305 | (when (> (point) start) | |
306 | (buffer-substring start (point))))))) | |
307 | ||
308 | ||
309 | (defmacro tooltip-region-active-p () | |
310 | "Value is non-nil if the region is currently active." | |
311 | (if (string-match "^GNU" (emacs-version)) | |
312 | `(and transient-mark-mode mark-active) | |
313 | `(region-active-p))) | |
314 | ||
315 | ||
316 | (defun tooltip-expr-to-print (event) | |
317 | "Return an expression that should be printed for EVENT. | |
318 | If a region is active and the mouse is inside the region, print | |
319 | the region. Otherwise, figure out the identifier around the point | |
320 | where the mouse is." | |
321 | (save-excursion | |
322 | (set-buffer (tooltip-event-buffer event)) | |
323 | (let ((point (posn-point (event-end event)))) | |
324 | (if (tooltip-region-active-p) | |
325 | (when (and (<= (region-beginning) point) (<= point (region-end))) | |
326 | (buffer-substring (region-beginning) (region-end))) | |
327 | (tooltip-identifier-from-point point))))) | |
328 | ||
329 | ||
330 | (defun tooltip-process-prompt-regexp (process) | |
331 | "Return regexp matching the prompt of PROCESS at the end of a string. | |
332 | The prompt is taken from the value of COMINT-PROMPT-REGEXP in the buffer | |
333 | of PROCESS." | |
334 | (let ((prompt-regexp (save-excursion | |
335 | (set-buffer (process-buffer process)) | |
336 | comint-prompt-regexp))) | |
337 | ;; Most start with `^' but the one for `sdb' cannot be easily | |
338 | ;; stripped. Code the prompt for `sdb' fixed here. | |
339 | (if (= (aref prompt-regexp 0) ?^) | |
340 | (setq prompt-regexp (substring prompt-regexp 1)) | |
341 | (setq prompt-regexp "\\*")) | |
342 | (concat "\n*" prompt-regexp "$"))) | |
343 | ||
344 | ||
345 | (defun tooltip-strip-prompt (process output) | |
346 | "Return OUTPUT with any prompt of PROCESS stripped from its end." | |
347 | (let ((prompt-regexp (tooltip-process-prompt-regexp process))) | |
348 | (save-match-data | |
349 | (when (string-match prompt-regexp output) | |
350 | (setq output (substring output 0 (match-beginning 0))))) | |
351 | output)) | |
352 | ||
353 | ||
354 | \f | |
355 | ;;; Tips for `gud' | |
356 | ||
357 | (defvar tooltip-gud-original-filter nil | |
358 | "Process filter to restore after GUD output has been received.") | |
359 | ||
360 | ||
361 | (defvar tooltip-gud-dereference nil | |
362 | "Non-nil means print expressions with a `*' in front of them. | |
363 | For C this would dereference a pointer expression.") | |
364 | ||
365 | ||
366 | (defvar tooltip-gud-event nil | |
367 | "The mouse movement event that led to a tooltip display. | |
368 | This event can be examined by forms in TOOLTIP-GUD-DISPLAY.") | |
369 | ||
370 | ||
371 | (defvar tooltip-gud-debugger nil | |
372 | "A symbol describing the debugger running under GUD.") | |
373 | ||
374 | ||
375 | (defun tooltip-gud-toggle-dereference () | |
024e5da6 | 376 | "Toggle whether tooltips should show `* expr' or `expr'." |
7840ced1 GM |
377 | (interactive) |
378 | (setq tooltip-gud-dereference (not tooltip-gud-dereference)) | |
379 | (when (interactive-p) | |
380 | (message "Dereferencing is now %s." | |
381 | (if tooltip-gud-dereference "on" "off")))) | |
382 | ||
383 | ||
384 | (defun tooltip-gud-process-output (process output) | |
385 | "Process debugger output and show it in a tooltip window." | |
386 | (set-process-filter process tooltip-gud-original-filter) | |
387 | (tooltip-show (tooltip-strip-prompt process output))) | |
388 | ||
389 | ||
390 | (defun tooltip-gud-print-command (expr) | |
391 | "Return a suitable command to print the expression EXPR. | |
392 | If TOOLTIP-GUD-DEREFERENCE is t, also prepend a `*' to EXPR." | |
393 | (when tooltip-gud-dereference | |
394 | (setq expr (concat "*" expr))) | |
395 | (case tooltip-gud-debugger | |
396 | ((gdb dbx) (concat "print " expr)) | |
397 | (xdb (concat "p " expr)) | |
398 | (sdb (concat expr "/")) | |
399 | (perldb expr))) | |
400 | ||
401 | ||
402 | (defun tooltip-gud-tips (event) | |
01b23b99 DL |
403 | "Show tip for identifier or selection under the mouse. |
404 | The mouse must either point at an identifier or inside a selected | |
405 | region for the tip window to be shown. If tooltip-gud-dereference is t, | |
406 | add a `*' in front of the printed expression. | |
7840ced1 GM |
407 | |
408 | This function must return nil if it doesn't handle EVENT." | |
409 | (let (gud-buffer process) | |
410 | (when (and (eventp event) | |
411 | tooltip-gud-tips-p | |
412 | (boundp 'gud-comint-buffer) | |
413 | (setq gud-buffer gud-comint-buffer) | |
414 | (setq process (get-buffer-process gud-buffer)) | |
415 | (posn-point (event-end event)) | |
416 | (progn (setq tooltip-gud-event event) | |
417 | (eval (cons 'and tooltip-gud-display)))) | |
418 | (let ((expr (tooltip-expr-to-print event))) | |
419 | (when expr | |
420 | (setq tooltip-gud-original-filter (process-filter process)) | |
421 | (set-process-filter process 'tooltip-gud-process-output) | |
422 | (process-send-string | |
423 | process (concat (tooltip-gud-print-command expr) "\n")) | |
424 | expr))))) | |
425 | ||
426 | ||
427 | \f | |
428 | ;;; Tooltip help. | |
429 | ||
430 | (defvar tooltip-help-message nil | |
431 | "The last help message received via `tooltip-show-help-function'.") | |
432 | ||
433 | ||
434 | (defun tooltip-show-help-function (msg) | |
435 | "Function installed as `show-help-function'. | |
436 | MSG is either a help string to display, or nil to cancel the display." | |
437 | (let ((previous-help tooltip-help-message)) | |
438 | (setq tooltip-help-message msg) | |
439 | (cond ((null msg) | |
440 | (tooltip-hide)) | |
441 | ((or (not (stringp previous-help)) | |
442 | (not (string= msg previous-help))) | |
443 | (tooltip-hide) | |
444 | (tooltip-add-timeout)) | |
445 | (t | |
446 | (tooltip-disable-timeout) | |
447 | (tooltip-add-timeout))))) | |
448 | ||
449 | ||
450 | (defun tooltip-help-tips (event) | |
451 | "Hook function to display a help tooltip. | |
452 | Value is non-nil if this function handled the tip." | |
453 | (when (stringp tooltip-help-message) | |
454 | (tooltip-show tooltip-help-message) | |
455 | (setq tooltip-help-message nil) | |
456 | t)) | |
457 | ||
458 | ||
459 | \f | |
460 | ;;; Do this after all functions have been defined that are called | |
461 | ;;; from `tooltip-mode'. | |
462 | ||
01b23b99 DL |
463 | |
464 | ;;;###autoload | |
465 | (defcustom tooltip-mode nil | |
466 | "Toggle tooltip-mode. | |
467 | Setting this variable directly does not take effect; | |
468 | use either \\[customize] or the function `tooltip-mode'." | |
469 | :set (lambda (symbol value) | |
470 | (tooltip-mode (or value 0))) | |
471 | :initialize 'custom-initialize-default | |
7840ced1 | 472 | :type 'boolean |
7840ced1 GM |
473 | :require 'tooltip |
474 | :group 'tooltip) | |
475 | ||
476 | ||
477 | ;;; tooltip.el ends here |