button.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. ;;; button.el --- clickable buttons
  2. ;;
  3. ;; Copyright (C) 2001-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Miles Bader <miles@gnu.org>
  6. ;; Keywords: extensions
  7. ;; Package: emacs
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  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. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;; Commentary:
  22. ;;
  23. ;; This package defines functions for inserting and manipulating
  24. ;; clickable buttons in Emacs buffers, such as might be used for help
  25. ;; hyperlinks, etc.
  26. ;;
  27. ;; In some ways it duplicates functionality also offered by the
  28. ;; `widget' package, but the button package has the advantage that it
  29. ;; is (1) much faster, (2) much smaller, and (3) much, much, simpler
  30. ;; (the code, that is, not the interface).
  31. ;;
  32. ;; Buttons can either use overlays, in which case the button is
  33. ;; represented by the overlay itself, or text-properties, in which case
  34. ;; the button is represented by a marker or buffer-position pointing
  35. ;; somewhere in the button. In the latter case, no markers into the
  36. ;; buffer are retained, which is important for speed if there are are
  37. ;; extremely large numbers of buttons. Note however that if there is
  38. ;; an existing face text-property at the site of the button, the
  39. ;; button face may not be visible. Using overlays avoids this.
  40. ;;
  41. ;; Using `define-button-type' to define default properties for buttons
  42. ;; is not necessary, but it is encouraged, since doing so makes the
  43. ;; resulting code clearer and more efficient.
  44. ;;
  45. ;;; Code:
  46. ;; Globals
  47. ;; Use color for the MS-DOS port because it doesn't support underline.
  48. ;; FIXME if MS-DOS correctly answers the (supports) question, it need
  49. ;; no longer be a special case.
  50. (defface button '((t :inherit link))
  51. "Default face used for buttons."
  52. :group 'basic-faces)
  53. (defvar button-map
  54. (let ((map (make-sparse-keymap)))
  55. ;; The following definition needs to avoid using escape sequences that
  56. ;; might get converted to ^M when building loaddefs.el
  57. (define-key map [(control ?m)] 'push-button)
  58. (define-key map [mouse-2] 'push-button)
  59. map)
  60. "Keymap used by buttons.")
  61. (defvar button-buffer-map
  62. (let ((map (make-sparse-keymap)))
  63. (define-key map [?\t] 'forward-button)
  64. (define-key map "\e\t" 'backward-button)
  65. (define-key map [backtab] 'backward-button)
  66. map)
  67. "Keymap useful for buffers containing buttons.
  68. Mode-specific keymaps may want to use this as their parent keymap.")
  69. ;; Default properties for buttons
  70. (put 'default-button 'face 'button)
  71. (put 'default-button 'mouse-face 'highlight)
  72. (put 'default-button 'keymap button-map)
  73. (put 'default-button 'type 'button)
  74. ;; action may be either a function to call, or a marker to go to
  75. (put 'default-button 'action 'ignore)
  76. (put 'default-button 'help-echo (purecopy "mouse-2, RET: Push this button"))
  77. ;; Make overlay buttons go away if their underlying text is deleted.
  78. (put 'default-button 'evaporate t)
  79. ;; Prevent insertions adjacent to the text-property buttons from
  80. ;; inheriting its properties.
  81. (put 'default-button 'rear-nonsticky t)
  82. ;; A `category-symbol' property for the default button type
  83. (put 'button 'button-category-symbol 'default-button)
  84. ;; Button types (which can be used to hold default properties for buttons)
  85. ;; Because button-type properties are inherited by buttons using the
  86. ;; special `category' property (implemented by both overlays and
  87. ;; text-properties), we need to store them on a symbol to which the
  88. ;; `category' properties can point. Instead of using the symbol that's
  89. ;; the name of each button-type, however, we use a separate symbol (with
  90. ;; `-button' appended, and uninterned) to store the properties. This is
  91. ;; to avoid name clashes.
  92. ;; [this is an internal function]
  93. (defsubst button-category-symbol (type)
  94. "Return the symbol used by button-type TYPE to store properties.
  95. Buttons inherit them by setting their `category' property to that symbol."
  96. (or (get type 'button-category-symbol)
  97. (error "Unknown button type `%s'" type)))
  98. (defun define-button-type (name &rest properties)
  99. "Define a `button type' called NAME (a symbol).
  100. The remaining arguments form a sequence of PROPERTY VALUE pairs,
  101. specifying properties to use as defaults for buttons with this type
  102. \(a button's type may be set by giving it a `type' property when
  103. creating the button, using the :type keyword argument).
  104. In addition, the keyword argument :supertype may be used to specify a
  105. button-type from which NAME inherits its default property values
  106. \(however, the inheritance happens only when NAME is defined; subsequent
  107. changes to a supertype are not reflected in its subtypes)."
  108. (let ((catsym (make-symbol (concat (symbol-name name) "-button")))
  109. (super-catsym
  110. (button-category-symbol
  111. (or (plist-get properties 'supertype)
  112. (plist-get properties :supertype)
  113. 'button))))
  114. ;; Provide a link so that it's easy to find the real symbol.
  115. (put name 'button-category-symbol catsym)
  116. ;; Initialize NAME's properties using the global defaults.
  117. (let ((default-props (symbol-plist super-catsym)))
  118. (while default-props
  119. (put catsym (pop default-props) (pop default-props))))
  120. ;; Add NAME as the `type' property, which will then be returned as
  121. ;; the type property of individual buttons.
  122. (put catsym 'type name)
  123. ;; Add the properties in PROPERTIES to the real symbol.
  124. (while properties
  125. (let ((prop (pop properties)))
  126. (when (eq prop :supertype)
  127. (setq prop 'supertype))
  128. (put catsym prop (pop properties))))
  129. ;; Make sure there's a `supertype' property
  130. (unless (get catsym 'supertype)
  131. (put catsym 'supertype 'button))
  132. name))
  133. (defun button-type-put (type prop val)
  134. "Set the button-type TYPE's PROP property to VAL."
  135. (put (button-category-symbol type) prop val))
  136. (defun button-type-get (type prop)
  137. "Get the property of button-type TYPE named PROP."
  138. (get (button-category-symbol type) prop))
  139. (defun button-type-subtype-p (type supertype)
  140. "Return t if button-type TYPE is a subtype of SUPERTYPE."
  141. (or (eq type supertype)
  142. (and type
  143. (button-type-subtype-p (button-type-get type 'supertype)
  144. supertype))))
  145. ;; Button properties and other attributes
  146. (defun button-start (button)
  147. "Return the position at which BUTTON starts."
  148. (if (overlayp button)
  149. (overlay-start button)
  150. ;; Must be a text-property button.
  151. (or (previous-single-property-change (1+ button) 'button)
  152. (point-min))))
  153. (defun button-end (button)
  154. "Return the position at which BUTTON ends."
  155. (if (overlayp button)
  156. (overlay-end button)
  157. ;; Must be a text-property button.
  158. (or (next-single-property-change button 'button)
  159. (point-max))))
  160. (defun button-get (button prop)
  161. "Get the property of button BUTTON named PROP."
  162. (if (overlayp button)
  163. (overlay-get button prop)
  164. ;; Must be a text-property button.
  165. (get-text-property button prop)))
  166. (defun button-put (button prop val)
  167. "Set BUTTON's PROP property to VAL."
  168. ;; Treat some properties specially.
  169. (cond ((memq prop '(type :type))
  170. ;; We translate a `type' property a `category' property, since
  171. ;; that's what's actually used by overlays/text-properties for
  172. ;; inheriting properties.
  173. (setq prop 'category)
  174. (setq val (button-category-symbol val)))
  175. ((eq prop 'category)
  176. ;; Disallow updating the `category' property directly.
  177. (error "Button `category' property may not be set directly")))
  178. ;; Add the property.
  179. (if (overlayp button)
  180. (overlay-put button prop val)
  181. ;; Must be a text-property button.
  182. (put-text-property
  183. (or (previous-single-property-change (1+ button) 'button)
  184. (point-min))
  185. (or (next-single-property-change button 'button)
  186. (point-max))
  187. prop val)))
  188. (defsubst button-activate (button &optional use-mouse-action)
  189. "Call BUTTON's action property.
  190. If USE-MOUSE-ACTION is non-nil, invoke the button's mouse-action
  191. instead of its normal action; if the button has no mouse-action,
  192. the normal action is used instead."
  193. (let ((action (or (and use-mouse-action (button-get button 'mouse-action))
  194. (button-get button 'action))))
  195. (if (markerp action)
  196. (save-selected-window
  197. (select-window (display-buffer (marker-buffer action)))
  198. (goto-char action)
  199. (recenter 0))
  200. (funcall action button))))
  201. (defun button-label (button)
  202. "Return BUTTON's text label."
  203. (buffer-substring-no-properties (button-start button) (button-end button)))
  204. (defsubst button-type (button)
  205. "Return BUTTON's button-type."
  206. (button-get button 'type))
  207. (defun button-has-type-p (button type)
  208. "Return t if BUTTON has button-type TYPE, or one of TYPE's subtypes."
  209. (button-type-subtype-p (button-get button 'type) type))
  210. ;; Creating overlay buttons
  211. (defun make-button (beg end &rest properties)
  212. "Make a button from BEG to END in the current buffer.
  213. The remaining arguments form a sequence of PROPERTY VALUE pairs,
  214. specifying properties to add to the button.
  215. In addition, the keyword argument :type may be used to specify a
  216. button-type from which to inherit other properties; see
  217. `define-button-type'.
  218. Also see `make-text-button', `insert-button'."
  219. (let ((overlay (make-overlay beg end nil t nil)))
  220. (while properties
  221. (button-put overlay (pop properties) (pop properties)))
  222. ;; Put a pointer to the button in the overlay, so it's easy to get
  223. ;; when we don't actually have a reference to the overlay.
  224. (overlay-put overlay 'button overlay)
  225. ;; If the user didn't specify a type, use the default.
  226. (unless (overlay-get overlay 'category)
  227. (overlay-put overlay 'category 'default-button))
  228. ;; OVERLAY is the button, so return it
  229. overlay))
  230. (defun insert-button (label &rest properties)
  231. "Insert a button with the label LABEL.
  232. The remaining arguments form a sequence of PROPERTY VALUE pairs,
  233. specifying properties to add to the button.
  234. In addition, the keyword argument :type may be used to specify a
  235. button-type from which to inherit other properties; see
  236. `define-button-type'.
  237. Also see `insert-text-button', `make-button'."
  238. (apply #'make-button
  239. (prog1 (point) (insert label))
  240. (point)
  241. properties))
  242. ;; Creating text-property buttons
  243. (defun make-text-button (beg end &rest properties)
  244. "Make a button from BEG to END in the current buffer.
  245. The remaining arguments form a sequence of PROPERTY VALUE pairs,
  246. specifying properties to add to the button.
  247. In addition, the keyword argument :type may be used to specify a
  248. button-type from which to inherit other properties; see
  249. `define-button-type'.
  250. This function is like `make-button', except that the button is actually
  251. part of the text instead of being a property of the buffer. That is,
  252. this function uses text properties, the other uses overlays.
  253. Creating large numbers of buttons can also be somewhat faster
  254. using `make-text-button'. Note, however, that if there is an existing
  255. face property at the site of the button, the button face may not be visible.
  256. You may want to use `make-button' in that case.
  257. BEG can also be a string, in which case it is made into a button.
  258. Also see `insert-text-button'."
  259. (let ((object nil)
  260. (type-entry
  261. (or (plist-member properties 'type)
  262. (plist-member properties :type))))
  263. (when (stringp beg)
  264. (setq object beg beg 0 end (length object)))
  265. ;; Disallow setting the `category' property directly.
  266. (when (plist-get properties 'category)
  267. (error "Button `category' property may not be set directly"))
  268. (if (null type-entry)
  269. ;; The user didn't specify a `type' property, use the default.
  270. (setq properties (cons 'category (cons 'default-button properties)))
  271. ;; The user did specify a `type' property. Translate it into a
  272. ;; `category' property, which is what's actually used by
  273. ;; text-properties for inheritance.
  274. (setcar type-entry 'category)
  275. (setcar (cdr type-entry)
  276. (button-category-symbol (car (cdr type-entry)))))
  277. ;; Now add all the text properties at once
  278. (add-text-properties beg end
  279. ;; Each button should have a non-eq `button'
  280. ;; property so that next-single-property-change can
  281. ;; detect boundaries reliably.
  282. (cons 'button (cons (list t) properties))
  283. object)
  284. ;; Return something that can be used to get at the button.
  285. beg))
  286. (defun insert-text-button (label &rest properties)
  287. "Insert a button with the label LABEL.
  288. The remaining arguments form a sequence of PROPERTY VALUE pairs,
  289. specifying properties to add to the button.
  290. In addition, the keyword argument :type may be used to specify a
  291. button-type from which to inherit other properties; see
  292. `define-button-type'.
  293. This function is like `insert-button', except that the button is
  294. actually part of the text instead of being a property of the buffer.
  295. Creating large numbers of buttons can also be somewhat faster using
  296. `insert-text-button'.
  297. Also see `make-text-button'."
  298. (apply #'make-text-button
  299. (prog1 (point) (insert label))
  300. (point)
  301. properties))
  302. ;; Finding buttons in a buffer
  303. (defun button-at (pos)
  304. "Return the button at position POS in the current buffer, or nil.
  305. If the button at POS is a text property button, the return value
  306. is a marker pointing to POS."
  307. (let ((button (get-char-property pos 'button)))
  308. (if (or (overlayp button) (null button))
  309. button
  310. ;; Must be a text-property button; return a marker pointing to it.
  311. (copy-marker pos t))))
  312. (defun next-button (pos &optional count-current)
  313. "Return the next button after position POS in the current buffer.
  314. If COUNT-CURRENT is non-nil, count any button at POS in the search,
  315. instead of starting at the next button."
  316. (unless count-current
  317. ;; Search for the next button boundary.
  318. (setq pos (next-single-char-property-change pos 'button)))
  319. (and (< pos (point-max))
  320. (or (button-at pos)
  321. ;; We must have originally been on a button, and are now in
  322. ;; the inter-button space. Recurse to find a button.
  323. (next-button pos))))
  324. (defun previous-button (pos &optional count-current)
  325. "Return the previous button before position POS in the current buffer.
  326. If COUNT-CURRENT is non-nil, count any button at POS in the search,
  327. instead of starting at the next button."
  328. (let ((button (button-at pos)))
  329. (if button
  330. (if count-current
  331. button
  332. ;; We started out on a button, so move to its start and look
  333. ;; for the previous button boundary.
  334. (setq pos (previous-single-char-property-change
  335. (button-start button) 'button))
  336. (let ((new-button (button-at pos)))
  337. (if new-button
  338. ;; We are in a button again; this can happen if there
  339. ;; are adjacent buttons (or at bob).
  340. (unless (= pos (button-start button)) new-button)
  341. ;; We are now in the space between buttons.
  342. (previous-button pos))))
  343. ;; We started out in the space between buttons.
  344. (setq pos (previous-single-char-property-change pos 'button))
  345. (or (button-at pos)
  346. (and (> pos (point-min))
  347. (button-at (1- pos)))))))
  348. ;; User commands
  349. (defun push-button (&optional pos use-mouse-action)
  350. "Perform the action specified by a button at location POS.
  351. POS may be either a buffer position or a mouse-event. If
  352. USE-MOUSE-ACTION is non-nil, invoke the button's mouse-action
  353. instead of its normal action; if the button has no mouse-action,
  354. the normal action is used instead. The action may be either a
  355. function to call or a marker to display.
  356. POS defaults to point, except when `push-button' is invoked
  357. interactively as the result of a mouse-event, in which case, the
  358. mouse event is used.
  359. If there's no button at POS, do nothing and return nil, otherwise
  360. return t."
  361. (interactive
  362. (list (if (integerp last-command-event) (point) last-command-event)))
  363. (if (and (not (integerp pos)) (eventp pos))
  364. ;; POS is a mouse event; switch to the proper window/buffer
  365. (let ((posn (event-start pos)))
  366. (with-current-buffer (window-buffer (posn-window posn))
  367. (push-button (posn-point posn) t)))
  368. ;; POS is just normal position
  369. (let ((button (button-at (or pos (point)))))
  370. (if (not button)
  371. nil
  372. (button-activate button use-mouse-action)
  373. t))))
  374. (defun forward-button (n &optional wrap display-message)
  375. "Move to the Nth next button, or Nth previous button if N is negative.
  376. If N is 0, move to the start of any button at point.
  377. If WRAP is non-nil, moving past either end of the buffer continues from the
  378. other end.
  379. If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
  380. Any button with a non-nil `skip' property is skipped over.
  381. Returns the button found."
  382. (interactive "p\nd\nd")
  383. (let (button)
  384. (if (zerop n)
  385. ;; Move to start of current button
  386. (if (setq button (button-at (point)))
  387. (goto-char (button-start button)))
  388. ;; Move to Nth next button
  389. (let ((iterator (if (> n 0) #'next-button #'previous-button))
  390. (wrap-start (if (> n 0) (point-min) (point-max)))
  391. opoint fail)
  392. (setq n (abs n))
  393. (setq button t) ; just to start the loop
  394. (while (and (null fail) (> n 0) button)
  395. (setq button (funcall iterator (point)))
  396. (when (and (not button) wrap)
  397. (setq button (funcall iterator wrap-start t)))
  398. (when button
  399. (goto-char (button-start button))
  400. ;; Avoid looping forever (e.g., if all the buttons have
  401. ;; the `skip' property).
  402. (cond ((null opoint)
  403. (setq opoint (point)))
  404. ((= opoint (point))
  405. (setq fail t)))
  406. (unless (button-get button 'skip)
  407. (setq n (1- n)))))))
  408. (if (null button)
  409. (error (if wrap "No buttons!" "No more buttons"))
  410. (let ((msg (and display-message (button-get button 'help-echo))))
  411. (when msg
  412. (message "%s" msg)))
  413. button)))
  414. (defun backward-button (n &optional wrap display-message)
  415. "Move to the Nth previous button, or Nth next button if N is negative.
  416. If N is 0, move to the start of any button at point.
  417. If WRAP is non-nil, moving past either end of the buffer continues from the
  418. other end.
  419. If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
  420. Any button with a non-nil `skip' property is skipped over.
  421. Returns the button found."
  422. (interactive "p\nd\nd")
  423. (forward-button (- n) wrap display-message))
  424. (provide 'button)
  425. ;;; button.el ends here