Mercurial > emacs
comparison lisp/tooltip.el @ 90180:62afea0771d8
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-51
Merge from emacs--cvs-trunk--0
Patches applied:
* emacs--cvs-trunk--0 (patch 289-301)
- Update from CVS
- Merge from gnus--rel--5.10
* gnus--rel--5.10 (patch 68)
- Update from CVS
author | Miles Bader <miles@gnu.org> |
---|---|
date | Thu, 12 May 2005 03:41:19 +0000 |
parents | 08185296b491 70a3dba2b7ea |
children | a1b34dec1104 |
comparison
equal
deleted
inserted
replaced
90179:b745036dab36 | 90180:62afea0771d8 |
---|---|
25 | 25 |
26 ;;; Commentary: | 26 ;;; Commentary: |
27 | 27 |
28 ;;; Code: | 28 ;;; Code: |
29 | 29 |
30 (eval-when-compile (require 'cl)) ; for case macro | |
31 | |
32 | |
33 ;;; Customizable settings | 30 ;;; Customizable settings |
34 | 31 |
35 (defgroup tooltip nil | 32 (defgroup tooltip nil |
36 "Customization group for the `tooltip' package." | 33 "Customization group for the `tooltip' package." |
37 :group 'help | 34 :group 'help |
114 (t | 111 (t |
115 :inherit variable-pitch)) | 112 :inherit variable-pitch)) |
116 "Face for tooltips." | 113 "Face for tooltips." |
117 :group 'tooltip) | 114 :group 'tooltip) |
118 | 115 |
119 (defcustom tooltip-gud-tips-p nil | |
120 "*Non-nil means show tooltips in GUD sessions. | |
121 | |
122 This allows you to display a variable's value in a tooltip simply | |
123 by pointing at it with the mouse. In the case of a C program | |
124 controlled by GDB, it shows the associated #define directives | |
125 when program is not executing." | |
126 :type 'boolean | |
127 :tag "GUD" | |
128 :group 'tooltip) | |
129 | |
130 (defcustom tooltip-gud-modes '(gud-mode c-mode c++-mode fortran-mode) | |
131 "List of modes for which to enable GUD tips." | |
132 :type 'sexp | |
133 :tag "GUD modes" | |
134 :group 'tooltip) | |
135 | |
136 (defcustom tooltip-gud-display | |
137 '((eq (tooltip-event-buffer tooltip-gud-event) | |
138 (marker-buffer gud-overlay-arrow-position))) | |
139 "List of forms determining where GUD tooltips are displayed. | |
140 | |
141 Forms in the list are combined with AND. The default is to display | |
142 only tooltips in the buffer containing the overlay arrow." | |
143 :type 'sexp | |
144 :tag "GUD buffers predicate" | |
145 :group 'tooltip) | |
146 | |
147 (defcustom tooltip-gud-echo-area nil | |
148 "Use the echo area instead of frames for GUD tooltips." | |
149 :type 'boolean | |
150 :tag "Use echo area" | |
151 :group 'tooltip) | |
152 | |
153 (defvaralias 'tooltip-use-echo-area 'tooltip-gud-echo-area) | |
154 (make-obsolete-variable 'tooltip-use-echo-area 'tooltip-gud-echo-area "22.1") | |
155 | 116 |
156 ;;; Variables that are not customizable. | 117 ;;; Variables that are not customizable. |
157 | 118 |
158 (defvar tooltip-hook nil | 119 (defvar tooltip-hook nil |
159 "Functions to call to display tooltips. | 120 "Functions to call to display tooltips. |
167 "A copy of the last mouse motion event seen.") | 128 "A copy of the last mouse motion event seen.") |
168 | 129 |
169 (defvar tooltip-hide-time nil | 130 (defvar tooltip-hide-time nil |
170 "Time when the last tooltip was hidden.") | 131 "Time when the last tooltip was hidden.") |
171 | 132 |
172 | |
173 ;;; Event accessors | 133 ;;; Event accessors |
174 | 134 |
175 (defun tooltip-event-buffer (event) | 135 (defun tooltip-event-buffer (event) |
176 "Return the buffer over which event EVENT occurred. | 136 "Return the buffer over which event EVENT occurred. |
177 This might return nil if the event did not occur over a buffer." | 137 This might return nil if the event did not occur over a buffer." |
178 (let ((window (posn-window (event-end event)))) | 138 (let ((window (posn-window (event-end event)))) |
179 (and window (window-buffer window)))) | 139 (and window (window-buffer window)))) |
180 | 140 |
181 | |
182 ;;; Switching tooltips on/off | 141 ;;; Switching tooltips on/off |
183 | 142 |
184 ;; We don't set track-mouse globally because this is a big redisplay | 143 ;; We don't set track-mouse globally because this is a big redisplay |
185 ;; problem in buffers having a pre-command-hook or such installed, | 144 ;; problem in buffers having a pre-command-hook or such installed, |
186 ;; which does a set-buffer, like the summary buffer of Gnus. Calling | 145 ;; which does a set-buffer, like the summary buffer of Gnus. Calling |
200 (display-graphic-p))) | 159 (display-graphic-p))) |
201 (not (fboundp 'x-show-tip)))) | 160 (not (fboundp 'x-show-tip)))) |
202 :group 'tooltip | 161 :group 'tooltip |
203 (unless (or (null tooltip-mode) (fboundp 'x-show-tip)) | 162 (unless (or (null tooltip-mode) (fboundp 'x-show-tip)) |
204 (error "Sorry, tooltips are not yet available on this system")) | 163 (error "Sorry, tooltips are not yet available on this system")) |
205 (let ((hook-fn (if tooltip-mode 'add-hook 'remove-hook))) | 164 (if tooltip-mode |
206 (funcall hook-fn 'change-major-mode-hook 'tooltip-change-major-mode) | 165 (progn |
207 (tooltip-activate-mouse-motions-if-enabled) | 166 (add-hook 'pre-command-hook 'tooltip-hide) |
208 (funcall hook-fn 'pre-command-hook 'tooltip-hide) | 167 (add-hook 'tooltip-hook 'tooltip-help-tips)) |
209 (funcall hook-fn 'tooltip-hook 'tooltip-gud-tips) | 168 (unless (and (boundp 'gud-tooltip-mode) gud-tooltip-mode) |
210 (funcall hook-fn 'tooltip-hook 'tooltip-help-tips) | 169 (remove-hook 'pre-command-hook 'tooltip-hide)) |
211 (setq show-help-function (if tooltip-mode 'tooltip-show-help-function nil)) | 170 (remove-hook 'tooltip-hook 'tooltip-help-tips)) |
212 ;; `ignore' is the default binding for mouse movements. | 171 (setq show-help-function |
213 (define-key global-map [mouse-movement] | 172 (if tooltip-mode 'tooltip-show-help-function nil))) |
214 (if tooltip-mode 'tooltip-mouse-motion 'ignore)))) | |
215 | 173 |
216 | 174 |
217 ;;; Timeout for tooltip display | 175 ;;; Timeout for tooltip display |
218 | 176 |
219 (defun tooltip-delay () | 177 (defun tooltip-delay () |
238 | 196 |
239 (defun tooltip-timeout (object) | 197 (defun tooltip-timeout (object) |
240 "Function called when timer with id tooltip-timeout-id fires." | 198 "Function called when timer with id tooltip-timeout-id fires." |
241 (run-hook-with-args-until-success 'tooltip-hook | 199 (run-hook-with-args-until-success 'tooltip-hook |
242 tooltip-last-mouse-motion-event)) | 200 tooltip-last-mouse-motion-event)) |
243 | |
244 | |
245 ;;; Reacting on mouse movements | |
246 | |
247 (defun tooltip-change-major-mode () | |
248 "Function added to `change-major-mode-hook' when tooltip mode is on." | |
249 (add-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled)) | |
250 | |
251 (defun tooltip-activate-mouse-motions-if-enabled () | |
252 "Reconsider for all buffers whether mouse motion events are desired." | |
253 (remove-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled) | |
254 (dolist (buffer (buffer-list)) | |
255 (save-excursion | |
256 (set-buffer buffer) | |
257 (if (and tooltip-mode | |
258 tooltip-gud-tips-p | |
259 (memq major-mode tooltip-gud-modes)) | |
260 (tooltip-activate-mouse-motions t) | |
261 (tooltip-activate-mouse-motions nil))))) | |
262 | |
263 (defvar tooltip-mouse-motions-active nil | |
264 "Locally t in a buffer if tooltip processing of mouse motion is enabled.") | |
265 | |
266 (defun tooltip-activate-mouse-motions (activatep) | |
267 "Activate/deactivate mouse motion events for the current buffer. | |
268 ACTIVATEP non-nil means activate mouse motion events." | |
269 (if activatep | |
270 (progn | |
271 (make-local-variable 'tooltip-mouse-motions-active) | |
272 (setq tooltip-mouse-motions-active t) | |
273 (make-local-variable 'track-mouse) | |
274 (setq track-mouse t)) | |
275 (when tooltip-mouse-motions-active | |
276 (kill-local-variable 'tooltip-mouse-motions-active) | |
277 (kill-local-variable 'track-mouse)))) | |
278 | |
279 (defun tooltip-mouse-motion (event) | |
280 "Command handler for mouse movement events in `global-map'." | |
281 (interactive "e") | |
282 (tooltip-hide) | |
283 (when (car (mouse-pixel-position)) | |
284 (setq tooltip-last-mouse-motion-event (copy-sequence event)) | |
285 (tooltip-start-delayed-tip))) | |
286 | 201 |
287 | 202 |
288 ;;; Displaying tips | 203 ;;; Displaying tips |
289 | 204 |
290 (defun tooltip-set-param (alist key value) | 205 (defun tooltip-set-param (alist key value) |
394 (when (string-match prompt-regexp output) | 309 (when (string-match prompt-regexp output) |
395 (setq output (substring output 0 (match-beginning 0))))) | 310 (setq output (substring output 0 (match-beginning 0))))) |
396 output)) | 311 output)) |
397 | 312 |
398 | 313 |
399 ;;; Tips for `gud' | |
400 | |
401 (defvar tooltip-gud-original-filter nil | |
402 "Process filter to restore after GUD output has been received.") | |
403 | |
404 (defvar tooltip-gud-dereference nil | |
405 "Non-nil means print expressions with a `*' in front of them. | |
406 For C this would dereference a pointer expression.") | |
407 | |
408 (defvar tooltip-gud-event nil | |
409 "The mouse movement event that led to a tooltip display. | |
410 This event can be examined by forms in TOOLTIP-GUD-DISPLAY.") | |
411 | |
412 (defun tooltip-gud-toggle-dereference () | |
413 "Toggle whether tooltips should show `* expr' or `expr'." | |
414 (interactive) | |
415 (setq tooltip-gud-dereference (not tooltip-gud-dereference)) | |
416 (when (interactive-p) | |
417 (message "Dereferencing is now %s." | |
418 (if tooltip-gud-dereference "on" "off")))) | |
419 | |
420 (defun tooltip-toggle-gud-tips () | |
421 "Toggle the display of GUD tooltips." | |
422 (interactive) | |
423 (setq tooltip-gud-tips-p (not tooltip-gud-tips-p)) | |
424 ;; Reconsider for all buffers whether mouse motion events are desired. | |
425 (tooltip-change-major-mode) | |
426 (when (interactive-p) | |
427 (message (format "GUD tooltips %sabled" | |
428 (if tooltip-gud-tips-p "en" "dis"))))) | |
429 | |
430 ; This will only display data that comes in one chunk. | |
431 ; Larger arrays (say 400 elements) are displayed in | |
432 ; the tootip incompletely and spill over into the gud buffer. | |
433 ; Switching the process-filter creates timing problems and | |
434 ; it may be difficult to do better. Using annotations as in | |
435 ; gdb-ui.el gets round this problem. | |
436 (defun tooltip-gud-process-output (process output) | |
437 "Process debugger output and show it in a tooltip window." | |
438 (set-process-filter process tooltip-gud-original-filter) | |
439 (tooltip-show (tooltip-strip-prompt process output) | |
440 tooltip-gud-echo-area)) | |
441 | |
442 (defun tooltip-gud-print-command (expr) | |
443 "Return a suitable command to print the expression EXPR. | |
444 If TOOLTIP-GUD-DEREFERENCE is t, also prepend a `*' to EXPR." | |
445 (when tooltip-gud-dereference | |
446 (setq expr (concat "*" expr))) | |
447 (case gud-minor-mode | |
448 ((gdb gdba) (concat "server print " expr)) | |
449 (dbx (concat "print " expr)) | |
450 (xdb (concat "p " expr)) | |
451 (sdb (concat expr "/")) | |
452 (perldb expr))) | |
453 | |
454 (defun tooltip-gud-tips (event) | |
455 "Show tip for identifier or selection under the mouse. | |
456 The mouse must either point at an identifier or inside a selected | |
457 region for the tip window to be shown. If tooltip-gud-dereference is t, | |
458 add a `*' in front of the printed expression. In the case of a C program | |
459 controlled by GDB, show the associated #define directives when program is | |
460 not executing. | |
461 | |
462 This function must return nil if it doesn't handle EVENT." | |
463 (let (process) | |
464 (when (and (eventp event) | |
465 tooltip-gud-tips-p | |
466 (boundp 'gud-comint-buffer) | |
467 gud-comint-buffer | |
468 (buffer-name gud-comint-buffer); gud-comint-buffer might be killed | |
469 (setq process (get-buffer-process gud-comint-buffer)) | |
470 (posn-point (event-end event)) | |
471 (or (eq gud-minor-mode 'gdba) | |
472 (progn (setq tooltip-gud-event event) | |
473 (eval (cons 'and tooltip-gud-display))))) | |
474 (let ((expr (tooltip-expr-to-print event))) | |
475 (when expr | |
476 (if (and (eq gud-minor-mode 'gdba) | |
477 (not gdb-active-process)) | |
478 (progn | |
479 (with-current-buffer | |
480 (window-buffer (let ((mouse (mouse-position))) | |
481 (window-at (cadr mouse) | |
482 (cddr mouse)))) | |
483 (let ((define-elt (assoc expr gdb-define-alist))) | |
484 (unless (null define-elt) | |
485 (tooltip-show (cdr define-elt)) | |
486 expr)))) | |
487 (let ((cmd (tooltip-gud-print-command expr))) | |
488 (unless (null cmd) ; CMD can be nil if unknown debugger | |
489 (case gud-minor-mode | |
490 (gdba (gdb-enqueue-input | |
491 (list (concat cmd "\n") 'gdb-tooltip-print))) | |
492 (t | |
493 (setq tooltip-gud-original-filter (process-filter process)) | |
494 (set-process-filter process 'tooltip-gud-process-output) | |
495 (gud-basic-call cmd))) | |
496 expr)))))))) | |
497 | |
498 (defun gdb-tooltip-print () | |
499 (tooltip-show | |
500 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer) | |
501 (let ((string (buffer-string))) | |
502 ;; remove newline for tooltip-gud-echo-area | |
503 (substring string 0 (- (length string) 1)))) | |
504 tooltip-gud-echo-area)) | |
505 | |
506 | |
507 ;;; Tooltip help. | 314 ;;; Tooltip help. |
508 | 315 |
509 (defvar tooltip-help-message nil | 316 (defvar tooltip-help-message nil |
510 "The last help message received via `tooltip-show-help-function'.") | 317 "The last help message received via `tooltip-show-help-function'.") |
511 | 318 |