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