wid-browse.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ;;; wid-browse.el --- functions for browsing widgets
  2. ;;
  3. ;; Copyright (C) 1997, 2001-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
  6. ;; Keywords: extensions
  7. ;; Package: emacs
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;
  21. ;; Widget browser. See `widget.el'.
  22. ;;; Code:
  23. (require 'easymenu)
  24. (require 'custom)
  25. (require 'wid-edit)
  26. (eval-when-compile (require 'cl))
  27. (defgroup widget-browse nil
  28. "Customization support for browsing widgets."
  29. :group 'widgets)
  30. ;;; The Mode.
  31. (defvar widget-browse-mode-map
  32. (let ((map (make-sparse-keymap)))
  33. (set-keymap-parent map widget-keymap)
  34. (define-key map "q" 'bury-buffer)
  35. map)
  36. "Keymap for `widget-browse-mode'.")
  37. (easy-menu-define widget-browse-mode-customize-menu
  38. widget-browse-mode-map
  39. "Menu used in widget browser buffers."
  40. (customize-menu-create 'widgets))
  41. (easy-menu-define widget-browse-mode-menu
  42. widget-browse-mode-map
  43. "Menu used in widget browser buffers."
  44. '("Widget"
  45. ["Browse" widget-browse t]
  46. ["Browse At" widget-browse-at t]))
  47. (defcustom widget-browse-mode-hook nil
  48. "Hook called when entering widget-browse-mode."
  49. :type 'hook
  50. :group 'widget-browse)
  51. (defun widget-browse-mode ()
  52. "Major mode for widget browser buffers.
  53. The following commands are available:
  54. \\[widget-forward] Move to next button or editable field.
  55. \\[widget-backward] Move to previous button or editable field.
  56. \\[widget-button-click] Activate button under the mouse pointer.
  57. \\[widget-button-press] Activate button under point.
  58. Entry to this mode calls the value of `widget-browse-mode-hook'
  59. if that value is non-nil."
  60. (kill-all-local-variables)
  61. (setq major-mode 'widget-browse-mode
  62. mode-name "Widget")
  63. (use-local-map widget-browse-mode-map)
  64. (easy-menu-add widget-browse-mode-customize-menu)
  65. (easy-menu-add widget-browse-mode-menu)
  66. (run-mode-hooks 'widget-browse-mode-hook))
  67. (put 'widget-browse-mode 'mode-class 'special)
  68. ;;; Commands.
  69. ;;;###autoload
  70. (defun widget-browse-at (pos)
  71. "Browse the widget under point."
  72. (interactive "d")
  73. (let* ((field (get-char-property pos 'field))
  74. (button (get-char-property pos 'button))
  75. (doc (get-char-property pos 'widget-doc))
  76. (text (cond (field "This is an editable text area.")
  77. (button "This is an active area.")
  78. (doc "This is documentation text.")
  79. (t "This is unidentified text.")))
  80. (widget (or field button doc)))
  81. (when widget
  82. (widget-browse widget))
  83. (message text)))
  84. (defvar widget-browse-history nil)
  85. ;;;###autoload
  86. (defun widget-browse (widget)
  87. "Create a widget browser for WIDGET."
  88. (interactive (list (completing-read "Widget: "
  89. obarray
  90. (lambda (symbol)
  91. (get symbol 'widget-type))
  92. t nil 'widget-browse-history)))
  93. (if (stringp widget)
  94. (setq widget (intern widget)))
  95. (unless (if (symbolp widget)
  96. (get widget 'widget-type)
  97. (and (consp widget)
  98. (get (widget-type widget) 'widget-type)))
  99. (error "Not a widget"))
  100. ;; Create the buffer.
  101. (if (symbolp widget)
  102. (let ((buffer (format "*Browse %s Widget*" widget)))
  103. (kill-buffer (get-buffer-create buffer))
  104. (switch-to-buffer (get-buffer-create buffer)))
  105. (kill-buffer (get-buffer-create "*Browse Widget*"))
  106. (switch-to-buffer (get-buffer-create "*Browse Widget*")))
  107. (widget-browse-mode)
  108. ;; Quick way to get out.
  109. ;; (widget-create 'push-button
  110. ;; :action (lambda (widget &optional event)
  111. ;; (bury-buffer))
  112. ;; "Quit")
  113. ;; (widget-insert "\n")
  114. ;; Top text indicating whether it is a class or object browser.
  115. (if (listp widget)
  116. (widget-insert "Widget object browser.\n\nClass: ")
  117. (widget-insert "Widget class browser.\n\n")
  118. (widget-create 'widget-browse
  119. :format "%[%v%]\n%d"
  120. :doc (get widget 'widget-documentation)
  121. widget)
  122. (unless (eq (preceding-char) ?\n)
  123. (widget-insert "\n"))
  124. (widget-insert "\nSuper: ")
  125. (setq widget (get widget 'widget-type)))
  126. ;; Now show the attributes.
  127. (let ((name (car widget))
  128. (items (cdr widget))
  129. key value printer)
  130. (widget-create 'widget-browse
  131. :format "%[%v%]"
  132. name)
  133. (widget-insert "\n")
  134. (while items
  135. (setq key (nth 0 items)
  136. value (nth 1 items)
  137. printer (or (get key 'widget-keyword-printer)
  138. 'widget-browse-sexp)
  139. items (cdr (cdr items)))
  140. (widget-insert "\n" (symbol-name key) "\n\t")
  141. (funcall printer widget key value)
  142. (widget-insert "\n")))
  143. (widget-setup)
  144. (goto-char (point-min)))
  145. ;;;###autoload
  146. (defun widget-browse-other-window (&optional widget)
  147. "Show widget browser for WIDGET in other window."
  148. (interactive)
  149. (let ((window (selected-window)))
  150. (switch-to-buffer-other-window "*Browse Widget*")
  151. (if widget
  152. (widget-browse widget)
  153. (call-interactively 'widget-browse))
  154. (select-window window)))
  155. ;;; The `widget-browse' Widget.
  156. (define-widget 'widget-browse 'push-button
  157. "Button for creating a widget browser.
  158. The :value of the widget shuld be the widget to be browsed."
  159. :format "%[[%v]%]"
  160. :value-create 'widget-browse-value-create
  161. :action 'widget-browse-action)
  162. (defun widget-browse-action (widget &optional _event)
  163. ;; Create widget browser for WIDGET's :value.
  164. (widget-browse (widget-get widget :value)))
  165. (defun widget-browse-value-create (widget)
  166. ;; Insert type name.
  167. (let ((value (widget-get widget :value)))
  168. (cond ((symbolp value)
  169. (insert (symbol-name value)))
  170. ((consp value)
  171. (insert (symbol-name (widget-type value))))
  172. (t
  173. (insert "strange")))))
  174. ;;; Keyword Printer Functions.
  175. (defun widget-browse-widget (_widget _key value)
  176. "Insert description of WIDGET's KEY VALUE.
  177. VALUE is assumed to be a widget."
  178. (widget-create 'widget-browse value))
  179. (defun widget-browse-widgets (_widget _key value)
  180. "Insert description of WIDGET's KEY VALUE.
  181. VALUE is assumed to be a list of widgets."
  182. (while value
  183. (widget-create 'widget-browse
  184. (car value))
  185. (setq value (cdr value))
  186. (when value
  187. (widget-insert " "))))
  188. (defun widget-browse-sexp (_widget _key value)
  189. "Insert description of WIDGET's KEY VALUE.
  190. Nothing is assumed about value."
  191. (let ((pp (condition-case signal
  192. (pp-to-string value)
  193. (error (prin1-to-string signal)))))
  194. (when (string-match "\n\\'" pp)
  195. (setq pp (substring pp 0 (1- (length pp)))))
  196. (if (cond ((string-match "\n" pp)
  197. nil)
  198. ((> (length pp) (- (window-width) (current-column)))
  199. nil)
  200. (t t))
  201. (widget-insert pp)
  202. (widget-create 'push-button
  203. :tag "show"
  204. :action (lambda (widget &optional _event)
  205. (with-output-to-temp-buffer
  206. "*Pp Eval Output*"
  207. (princ (widget-get widget :value))))
  208. pp))))
  209. (defun widget-browse-sexps (widget key value)
  210. "Insert description of WIDGET's KEY VALUE.
  211. VALUE is assumed to be a list of widgets."
  212. (let ((target (current-column)))
  213. (while value
  214. (widget-browse-sexp widget key (car value))
  215. (setq value (cdr value))
  216. (when value
  217. (widget-insert "\n" (make-string target ?\ ))))))
  218. ;;; Keyword Printers.
  219. (put :parent 'widget-keyword-printer 'widget-browse-widget)
  220. (put :children 'widget-keyword-printer 'widget-browse-widgets)
  221. (put :buttons 'widget-keyword-printer 'widget-browse-widgets)
  222. (put :button 'widget-keyword-printer 'widget-browse-widget)
  223. (put :args 'widget-keyword-printer 'widget-browse-sexps)
  224. ;;; Widget Minor Mode.
  225. (defvar widget-minor-mode-map
  226. (let ((map (make-sparse-keymap)))
  227. (set-keymap-parent map widget-keymap)
  228. map)
  229. "Keymap used in Widget Minor Mode.")
  230. ;;;###autoload
  231. (define-minor-mode widget-minor-mode
  232. "Minor mode for traversing widgets.
  233. With a prefix argument ARG, enable the mode if ARG is positive,
  234. and disable it otherwise. If called from Lisp, enable the mode
  235. if ARG is omitted or nil."
  236. :lighter " Widget")
  237. ;;; The End:
  238. (provide 'wid-browse)
  239. ;;; wid-browse.el ends here