1 ;;; wid-browse.el --- functions for browsing widgets
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
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, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Widget browser. See `widget.el'.
35 (eval-when-compile (require 'cl
))
37 (defgroup widget-browse nil
38 "Customization support for browsing widgets."
43 (defvar widget-browse-mode-map nil
44 "Keymap for `widget-browse-mode'.")
46 (unless widget-browse-mode-map
47 (setq widget-browse-mode-map
(make-sparse-keymap))
48 (set-keymap-parent widget-browse-mode-map widget-keymap
)
49 (define-key widget-browse-mode-map
"q" 'bury-buffer
))
51 (easy-menu-define widget-browse-mode-customize-menu
52 widget-browse-mode-map
53 "Menu used in widget browser buffers."
54 (customize-menu-create 'widgets
))
56 (easy-menu-define widget-browse-mode-menu
57 widget-browse-mode-map
58 "Menu used in widget browser buffers."
60 ["Browse" widget-browse t
]
61 ["Browse At" widget-browse-at t
]))
63 (defcustom widget-browse-mode-hook nil
64 "Hook called when entering widget-browse-mode."
66 :group
'widget-browse
)
68 (defun widget-browse-mode ()
69 "Major mode for widget browser buffers.
71 The following commands are available:
73 \\[widget-forward] Move to next button or editable field.
74 \\[widget-backward] Move to previous button or editable field.
75 \\[widget-button-click] Activate button under the mouse pointer.
76 \\[widget-button-press] Activate button under point.
78 Entry to this mode calls the value of `widget-browse-mode-hook'
79 if that value is non-nil."
80 (kill-all-local-variables)
81 (setq major-mode
'widget-browse-mode
83 (use-local-map widget-browse-mode-map
)
84 (easy-menu-add widget-browse-mode-customize-menu
)
85 (easy-menu-add widget-browse-mode-menu
)
86 (run-mode-hooks 'widget-browse-mode-hook
))
88 (put 'widget-browse-mode
'mode-class
'special
)
93 (defun widget-browse-at (pos)
94 "Browse the widget under point."
96 (let* ((field (get-char-property pos
'field
))
97 (button (get-char-property pos
'button
))
98 (doc (get-char-property pos
'widget-doc
))
99 (text (cond (field "This is an editable text area.")
100 (button "This is an active area.")
101 (doc "This is documentation text.")
102 (t "This is unidentified text.")))
103 (widget (or field button doc
)))
105 (widget-browse widget
))
108 (defvar widget-browse-history nil
)
111 (defun widget-browse (widget)
112 "Create a widget browser for WIDGET."
113 (interactive (list (completing-read "Widget: "
116 (get symbol
'widget-type
))
117 t nil
'widget-browse-history
)))
119 (setq widget
(intern widget
)))
120 (unless (if (symbolp widget
)
121 (get widget
'widget-type
)
123 (get (widget-type widget
) 'widget-type
)))
124 (error "Not a widget"))
125 ;; Create the buffer.
127 (let ((buffer (format "*Browse %s Widget*" widget
)))
128 (kill-buffer (get-buffer-create buffer
))
129 (switch-to-buffer (get-buffer-create buffer
)))
130 (kill-buffer (get-buffer-create "*Browse Widget*"))
131 (switch-to-buffer (get-buffer-create "*Browse Widget*")))
134 ;; Quick way to get out.
135 ;; (widget-create 'push-button
136 ;; :action (lambda (widget &optional event)
139 ;; (widget-insert "\n")
141 ;; Top text indicating whether it is a class or object browser.
143 (widget-insert "Widget object browser.\n\nClass: ")
144 (widget-insert "Widget class browser.\n\n")
145 (widget-create 'widget-browse
147 :doc
(get widget
'widget-documentation
)
149 (unless (eq (preceding-char) ?
\n)
150 (widget-insert "\n"))
151 (widget-insert "\nSuper: ")
152 (setq widget
(get widget
'widget-type
)))
154 ;; Now show the attributes.
155 (let ((name (car widget
))
158 (widget-create 'widget-browse
163 (setq key
(nth 0 items
)
165 printer
(or (get key
'widget-keyword-printer
)
167 items
(cdr (cdr items
)))
168 (widget-insert "\n" (symbol-name key
) "\n\t")
169 (funcall printer widget key value
)
170 (widget-insert "\n")))
172 (goto-char (point-min)))
175 (defun widget-browse-other-window (&optional widget
)
176 "Show widget browser for WIDGET in other window."
178 (let ((window (selected-window)))
179 (switch-to-buffer-other-window "*Browse Widget*")
181 (widget-browse widget
)
182 (call-interactively 'widget-browse
))
183 (select-window window
)))
186 ;;; The `widget-browse' Widget.
188 (define-widget 'widget-browse
'push-button
189 "Button for creating a widget browser.
190 The :value of the widget shuld be the widget to be browsed."
192 :value-create
'widget-browse-value-create
193 :action
'widget-browse-action
)
195 (defun widget-browse-action (widget &optional event
)
196 ;; Create widget browser for WIDGET's :value.
197 (widget-browse (widget-get widget
:value
)))
199 (defun widget-browse-value-create (widget)
201 (let ((value (widget-get widget
:value
)))
202 (cond ((symbolp value
)
203 (insert (symbol-name value
)))
205 (insert (symbol-name (widget-type value
))))
207 (insert "strange")))))
209 ;;; Keyword Printer Functions.
211 (defun widget-browse-widget (widget key value
)
212 "Insert description of WIDGET's KEY VALUE.
213 VALUE is assumed to be a widget."
214 (widget-create 'widget-browse value
))
216 (defun widget-browse-widgets (widget key value
)
217 "Insert description of WIDGET's KEY VALUE.
218 VALUE is assumed to be a list of widgets."
220 (widget-create 'widget-browse
222 (setq value
(cdr value
))
224 (widget-insert " "))))
226 (defun widget-browse-sexp (widget key value
)
227 "Insert description of WIDGET's KEY VALUE.
228 Nothing is assumed about value."
229 (let ((pp (condition-case signal
231 (error (prin1-to-string signal
)))))
232 (when (string-match "\n\\'" pp
)
233 (setq pp
(substring pp
0 (1- (length pp
)))))
234 (if (cond ((string-match "\n" pp
)
236 ((> (length pp
) (- (window-width) (current-column)))
240 (widget-create 'push-button
242 :action
(lambda (widget &optional event
)
243 (with-output-to-temp-buffer
245 (princ (widget-get widget
:value
))))
248 (defun widget-browse-sexps (widget key value
)
249 "Insert description of WIDGET's KEY VALUE.
250 VALUE is assumed to be a list of widgets."
251 (let ((target (current-column)))
253 (widget-browse-sexp widget key
(car value
))
254 (setq value
(cdr value
))
256 (widget-insert "\n" (make-string target ?\
))))))
258 ;;; Keyword Printers.
260 (put :parent
'widget-keyword-printer
'widget-browse-widget
)
261 (put :children
'widget-keyword-printer
'widget-browse-widgets
)
262 (put :buttons
'widget-keyword-printer
'widget-browse-widgets
)
263 (put :button
'widget-keyword-printer
'widget-browse-widget
)
264 (put :args
'widget-keyword-printer
'widget-browse-sexps
)
266 ;;; Widget Minor Mode.
268 (defvar widget-minor-mode nil
269 "If non-nil, we are in Widget Minor Mode.")
270 (make-variable-buffer-local 'widget-minor-mode
)
272 (defvar widget-minor-mode-map nil
273 "Keymap used in Widget Minor Mode.")
275 (unless widget-minor-mode-map
276 (setq widget-minor-mode-map
(make-sparse-keymap))
277 (set-keymap-parent widget-minor-mode-map widget-keymap
))
280 (defun widget-minor-mode (&optional arg
)
281 "Togle minor mode for traversing widgets.
282 With arg, turn widget mode on if and only if arg is positive."
285 (setq widget-minor-mode
(not widget-minor-mode
)))
287 (setq widget-minor-mode nil
))
289 (setq widget-minor-mode t
)))
290 (force-mode-line-update))
292 (add-to-list 'minor-mode-alist
'(widget-minor-mode " Widget"))
294 (add-to-list 'minor-mode-map-alist
295 (cons 'widget-minor-mode widget-minor-mode-map
))
299 (provide 'wid-browse
)
301 ;;; arch-tag: d5ffb18f-8984-4735-8502-edf70456db21
302 ;;; wid-browse.el ends here