help-at-pt.el 14 KB

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