*** empty log message ***
[bpt/emacs.git] / lisp / help-at-pt.el
CommitLineData
cc531412
LT
1;;; help-at-pt.el --- local help through the keyboard
2
be203836 3;; Copyright (C) 2003, 2004 Free Software Foundation, Inc.
cc531412
LT
4
5;; Author: Luc Teirlinck <teirllm@auburn.edu>
6;; Keywords: help
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;; This file contains functionality to make the help provided by the
28;; help-echo text or overlay property available to the keyboard user.
29;; It also supports a more keyboard oriented alternative to
30;; `help-echo', namely a new text or overlay property `kbd-help'.
31;;
32;; It provides facilities to access the local help available at point
33;; either on demand, using the command `display-local-help', or
34;; automatically after a suitable idle time, through the customizable
35;; variable `help-at-pt-display-when-idle'.
36;;
37;; You can get a more global overview of the local help available in
38;; the buffer, using the commands `scan-buf-next-region' and
39;; `scan-buf-previous-region', which move to the start of the next or
40;; previous region with available local help and print the help found
41;; there.
42;;
f561ff53
JL
43;; Suggested key bindings:
44;;
45;; (global-set-key [C-tab] 'scan-buf-next-region)
46;; (global-set-key [C-M-tab] 'scan-buf-previous-region)
47;;
cc531412
LT
48;; You do not have to do anything special to use the functionality
49;; provided by this file, because all important functions autoload.
50
51;;; Code:
52
53(defgroup help-at-pt nil
54 "Features for displaying local help."
55 :group 'help
56 :version "21.4")
57
58;;;###autoload
59(defun help-at-pt-string (&optional kbd)
60 "Return the help-echo string at point.
61Normally, the string produced by the `help-echo' text or overlay
62property, or nil, is returned.
63If KBD is non-nil, `kbd-help' is used instead, and any
64`help-echo' property is ignored. In this case, the return value
65can also be t, if that is the value of the `kbd-help' property."
66 (let* ((prop (if kbd 'kbd-help 'help-echo))
67 (pair (get-char-property-and-overlay (point) prop))
68 (val (car pair))
69 (ov (cdr pair)))
70 (if (functionp val)
71 (funcall val (selected-window) (if ov ov (current-buffer)) (point))
72 (eval val))))
73
74;;;###autoload
75(defun help-at-pt-kbd-string ()
76 "Return the keyboard help string at point.
77If the `kbd-help' text or overlay property at point produces a
78string, return it. Otherwise, use the `help-echo' property. If
79this produces no string either, return nil."
80 (let ((kbd (help-at-pt-string t))
81 (echo (help-at-pt-string)))
82 (if (and kbd (not (eq kbd t))) kbd echo)))
83
84;;;###autoload
85(defun display-local-help (&optional arg)
86 "Display local help in the echo area.
87This displays a short help message, namely the string produced by
88the `kbd-help' property at point. If `kbd-help' does not produce
89a string, but the `help-echo' property does, then that string is
90printed instead.
91
92A numeric argument ARG prevents display of a message in case
93there is no help. While ARG can be used interactively, it is
94mainly meant for use from Lisp."
95 (interactive "P")
96 (let ((help (help-at-pt-kbd-string)))
97 (if help
98 (message "%s" help)
99 (if (not arg) (message "No local help at point")))))
100
be203836
LT
101(defvar help-at-pt-timer nil
102 "Non-nil means that a timer is set that checks for local help.
103If non-nil, this is the value returned by the call of
104`run-with-idle-timer' that set that timer. This variable is used
105internally to enable `help-at-pt-display-when-idle'. Do not set it
106yourself.")
107
cc531412
LT
108(defcustom help-at-pt-timer-delay 1
109 "*Delay before displaying local help.
110This is used if `help-at-pt-display-when-idle' is enabled.
111The value may be an integer or floating point number.
112
113If a timer is already active, there are two ways to make the new
114value take effect immediately. After setting the value, you can
115first call `help-at-pt-cancel-timer' and then set a new timer
e4ff63a2 116with `help-at-pt-set-timer'. Alternatively, you can set this
cc531412
LT
117variable through Custom. This will not set a timer if none is
118active, but if one is already active, Custom will make it use the
119new value."
120 :group 'help-at-pt
121 :type 'number
be203836 122 :initialize 'custom-initialize-default
cc531412
LT
123 :set (lambda (variable value)
124 (set-default variable value)
be203836
LT
125 (and (boundp 'help-at-pt-timer)
126 help-at-pt-timer
76515399 127 (timer-set-idle-time help-at-pt-timer value t))))
cc531412
LT
128
129;;;###autoload
130(defun help-at-pt-cancel-timer ()
131 "Cancel any timer set by `help-at-pt-set-timer'.
132This disables `help-at-pt-display-when-idle'."
133 (interactive)
134 (let ((inhibit-quit t))
135 (when help-at-pt-timer
136 (cancel-timer help-at-pt-timer)
137 (setq help-at-pt-timer nil))))
138
139;;;###autoload
140(defun help-at-pt-set-timer ()
141 "Enable `help-at-pt-display-when-idle'.
142This is done by setting a timer, if none is currently active."
143 (interactive)
144 (unless help-at-pt-timer
145 (setq help-at-pt-timer
146 (run-with-idle-timer
147 help-at-pt-timer-delay t #'help-at-pt-maybe-display))))
148
cc531412
LT
149(defcustom help-at-pt-display-when-idle 'never
150 "*Automatically show local help on point-over.
151If the value is t, the string obtained from any `kbd-help' or
152`help-echo' property at point is automatically printed in the
153echo area, if nothing else is already displayed there, or after a
154quit. If both `kbd-help' and `help-echo' produce help strings,
155`kbd-help' is used. If the value is a list, the help only gets
156printed if there is a text or overlay property at point that is
157included in this list. Suggested properties are `keymap',
158`local-map', `button' and `kbd-help'. Any value other than t or
159a non-empty list disables the feature.
160
161This variable only takes effect after a call to
162`help-at-pt-set-timer'. The help gets printed after Emacs has
163been idle for `help-at-pt-timer-delay' seconds. You can call
164`help-at-pt-cancel-timer' to cancel the timer set by, and the
165effect of, `help-at-pt-set-timer'.
166
167When this variable is set through Custom, `help-at-pt-set-timer'
168is called automatically, unless the value is `never', in which
169case `help-at-pt-cancel-timer' is called. Specifying an empty
170list of properties through Custom will set the timer, thus
171enabling buffer local values. It sets the actual value to nil.
172Thus, Custom distinguishes between a nil value and other values
173that disable the feature, which Custom identifies with `never'.
174The default is `never'."
175 :group 'help-at-pt
176 :type '(choice (const :tag "Always"
177 :format "%t\n%h"
178 :doc
179 "This choice can get noisy.
180The text printed from the `help-echo' property is often only
181relevant when using the mouse. If you mind about too many
182messages getting printed in the echo area, use \"In certain
183situations\". See the documentation there for more information."
184 t)
185 (repeat :tag "In certain situations"
186 ;; unless we specify 0 offset the doc string
187 ;; for this choice gets indented very
188 ;; differently than for the other two
189 ;; choices, when "More" is selected.
190 :offset 0
191 :format "%{%t%}:\n%v%i\n%h"
192 :doc
193 "This choice lets you specify a list of \
194text properties.
195Presence of any of these properties will trigger display of
196available local help on point-over.
197If you use this alternative through Custom without listing any
198properties, a timer will be set anyway. This will enable buffer
199local values. Use \"Never\" if you do not want a timer to be set.
200
201Suggested properties:
202The `keymap' and `local-map' properties change keybindings in
203parts of the buffer. Some of these keymaps are mode independent
204and are not mentioned in the mode documentation. Hence, the help
205text is likely to be useful.
206Specifying `button' is relevant in Custom and similar buffers.
207In these buffers, most, but not all, of the text shown this way is
208available by default when using tab, but not on regular point-over.
209The presence of a `kbd-help' property guarantees that non mouse
210specific help is available."
211 :value (keymap local-map button kbd-help)
212 symbol)
213 (other :tag "Never"
214 :format "%t\n%h"
215 :doc
216 "This choice normally disables buffer local values.
217If you choose this value through Custom and a timer checking for
218local help is currently active, it will be canceled. No new
219timer will be set. Call `help-at-pt-set-timer' after choosing
220this option, or use \"In certain situations\" and specify no text
221properties, to enable buffer local values."
222 never))
223 :initialize 'custom-initialize-default
224 :set #'(lambda (variable value)
225 (set-default variable value)
226 (if (eq value 'never)
227 (help-at-pt-cancel-timer)
228 (help-at-pt-set-timer)))
229 :set-after '(help-at-pt-timer-delay)
76515399 230 :require 'help-at-pt)
cc531412
LT
231
232;; Function for use in `help-at-pt-set-timer'.
233(defun help-at-pt-maybe-display ()
234 (and (or (eq help-at-pt-display-when-idle t)
235 (and (consp help-at-pt-display-when-idle)
236 (catch 'found
237 (dolist (prop help-at-pt-display-when-idle)
238 (if (get-char-property (point) prop)
239 (throw 'found t))))))
240 (or (not (current-message))
241 (string= (current-message) "Quit"))
242 (display-local-help t)))
243
244;;;###autoload
245(defun scan-buf-move-to-region (prop &optional arg hook)
246 "Go to the start of the next region with non-nil PROP property.
247Then run HOOK, which should be a quoted symbol that is a normal
248hook.variable, or an expression evaluating to such a symbol.
249Adjacent areas with different non-nil PROP properties are
250considered different regions.
251
252With numeric argument ARG, move to the start of the ARGth next
253such region, then run HOOK. If ARG is negative, move backward.
254If point is already in a region, then that region does not count
255toward ARG. If ARG is 0 and point is inside a region, move to
256the start of that region. If ARG is 0 and point is not in a
257region, print a message to that effect, but do not move point and
258do not run HOOK. If there are not enough regions to move over,
259an error results and the number of available regions is mentioned
260in the error message. Point is not moved and HOOK is not run."
261 (cond ((> arg 0)
262 (if (= (point) (point-max))
263 (error "No further `%s' regions" prop))
264 (let ((pos (point)))
265 (dotimes (x arg)
266 (setq pos (next-single-char-property-change pos prop))
267 (unless (get-char-property pos prop)
268 (setq pos (next-single-char-property-change pos prop))
269 (unless (get-char-property pos prop)
270 (cond ((= x 0)
271 (error "No further `%s' regions" prop))
272 ((= x 1)
273 (error "There is only one further `%s' region" prop))
274 (t
275 (error
276 "There are only %d further `%s' regions"
277 x prop))))))
278 (goto-char pos)
279 (run-hooks hook)))
280 ((= arg 0)
281 (let ((val (get-char-property (point) prop)))
282 (cond ((not val)
283 (message "Point is not in a `%s' region" prop))
284 ((eq val (get-char-property (1- (point)) prop))
285 (goto-char
286 (previous-single-char-property-change (point) prop))
287 (run-hooks hook))
288 (t (run-hooks hook)))))
289 ((< arg 0)
290 (let ((pos (point)) (val (get-char-property (point) prop)))
291 (and val
292 (eq val (get-char-property (1- pos) prop))
293 (setq pos
294 (previous-single-char-property-change pos prop)))
295 (if (= pos (point-min))
296 (error "No prior `%s' regions" prop))
297 (dotimes (x (- arg))
298 (setq pos (previous-single-char-property-change pos prop))
299 (unless (get-char-property pos prop)
300 (setq pos (previous-single-char-property-change pos prop))
301 (unless (get-char-property pos prop)
302 (cond ((= x 0)
303 (error "No prior `%s' regions" prop))
304 ((= x 1)
305 (error "There is only one prior `%s' region" prop))
306 (t
307 (error "There are only %d prior `%s' regions"
308 x prop))))))
309 (goto-char pos)
310 (run-hooks hook)))))
311
312;; To be moved to a different file and replaced by a defcustom in a
313;; future version.
314(defvar scan-buf-move-hook '(display-local-help)
315 "Normal hook run by `scan-buf-next-region'.
316Also used by `scan-buf-previous-region'. The hook is run after
317positioning point.")
318
319;;;###autoload
320(defun scan-buf-next-region (&optional arg)
321 "Go to the start of the next region with non-nil help-echo.
322Print the help found there using `display-local-help'. Adjacent
323areas with different non-nil help-echo properties are considered
324different regions.
325
326With numeric argument ARG, move to the start of the ARGth next
327help-echo region. If ARG is negative, move backward. If point
328is already in a help-echo region, then that region does not count
329toward ARG. If ARG is 0 and point is inside a help-echo region,
330move to the start of that region. If ARG is 0 and point is not
331in such a region, just print a message to that effect. If there
332are not enough regions to move over, an error results and the
333number of available regions is mentioned in the error message.
334
335A potentially confusing subtlety is that point can be in a
336help-echo region without any local help being available. This is
337because `help-echo' can be a function evaluating to nil. This
338rarely happens in practice."
339 (interactive "p")
340 (scan-buf-move-to-region 'help-echo arg 'scan-buf-move-hook))
341
342;;;###autoload
343(defun scan-buf-previous-region (&optional arg)
344 "Go to the start of the previous region with non-nil help-echo.
345Print the help found there using `display-local-help'. Adjacent
346areas with different non-nil help-echo properties are considered
347different regions. With numeric argument ARG, behaves like
348`scan-buf-next-region' with argument -ARG.."
349 (interactive "p")
350 (scan-buf-move-to-region 'help-echo (- arg) 'scan-buf-move-hook))
351
35f825bd 352(add-hook 'help-at-pt-unload-hook 'help-at-pt-cancel-timer)
cc531412 353
cc531412
LT
354(provide 'help-at-pt)
355
5074ca95 356;;; arch-tag: d0b8b86d-d23f-45d0-a82d-208d6205a583
cc531412 357;;; help-at-pt.el ends here