help-at-pt.el 14 KB

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