28
|
1 ;;; wid-edit.el --- Functions for creating and using widgets.
|
|
2 ;;
|
|
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
|
4 ;;
|
|
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
|
|
6 ;; Keywords: extensions
|
173
|
7 ;; Version: 1.9951
|
28
|
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
|
|
9
|
149
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
|
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
13 ;; it under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;; any later version.
|
|
16
|
|
17 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 ;; GNU General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
|
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
25 ;; Boston, MA 02111-1307, USA.
|
|
26
|
28
|
27 ;;; Commentary:
|
|
28 ;;
|
|
29 ;; See `widget.el'.
|
|
30
|
|
31 ;;; Code:
|
|
32
|
|
33 (require 'widget)
|
155
|
34 (eval-when-compile (require 'cl))
|
30
|
35
|
|
36 ;;; Compatibility.
|
28
|
37
|
30
|
38 (eval-and-compile
|
|
39 (autoload 'pp-to-string "pp")
|
|
40 (autoload 'Info-goto-node "info")
|
173
|
41 (autoload 'finder-commentary "finder" nil t)
|
30
|
42
|
116
|
43 (when (string-match "XEmacs" emacs-version)
|
118
|
44 (condition-case nil
|
|
45 (require 'overlay)
|
|
46 (error (load-library "x-overlay"))))
|
116
|
47
|
30
|
48 (if (string-match "XEmacs" emacs-version)
|
149
|
49 (defun widget-event-point (event)
|
|
50 "Character position of the end of event if that exists, or nil."
|
|
51 (if (mouse-event-p event)
|
|
52 (event-point event)
|
|
53 nil))
|
|
54 (defun widget-event-point (event)
|
|
55 "Character position of the end of event if that exists, or nil."
|
|
56 (posn-point (event-end event))))
|
|
57
|
167
|
58 (defalias 'widget-read-event (if (string-match "XEmacs" emacs-version)
|
155
|
59 'next-event
|
|
60 'read-event))
|
|
61
|
|
62 ;; The following should go away when bundled with Emacs.
|
28
|
63 (condition-case ()
|
|
64 (require 'custom)
|
|
65 (error nil))
|
|
66
|
|
67 (unless (and (featurep 'custom) (fboundp 'custom-declare-variable))
|
|
68 ;; We have the old custom-library, hack around it!
|
|
69 (defmacro defgroup (&rest args) nil)
|
|
70 (defmacro defcustom (var value doc &rest args)
|
149
|
71 (` (defvar (, var) (, value) (, doc))))
|
28
|
72 (defmacro defface (&rest args) nil)
|
|
73 (define-widget-keywords :prefix :tag :load :link :options :type :group)
|
|
74 (when (fboundp 'copy-face)
|
|
75 (copy-face 'default 'widget-documentation-face)
|
|
76 (copy-face 'bold 'widget-button-face)
|
30
|
77 (copy-face 'italic 'widget-field-face)))
|
28
|
78
|
149
|
79 (unless (fboundp 'button-release-event-p)
|
|
80 ;; XEmacs function missing from Emacs.
|
|
81 (defun button-release-event-p (event)
|
|
82 "Non-nil if EVENT is a mouse-button-release event object."
|
|
83 (and (eventp event)
|
|
84 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
|
|
85 (or (memq 'click (event-modifiers event))
|
|
86 (memq 'drag (event-modifiers event))))))
|
28
|
87
|
167
|
88 (unless (fboundp 'functionp)
|
|
89 ;; Missing from Emacs 19.34 and earlier.
|
|
90 (defun functionp (object)
|
|
91 "Non-nil of OBJECT is a type of object that can be called as a function."
|
|
92 (or (subrp object) (byte-code-function-p object)
|
|
93 (eq (car-safe object) 'lambda)
|
|
94 (and (symbolp object) (fboundp object)))))
|
|
95
|
30
|
96 (unless (fboundp 'error-message-string)
|
|
97 ;; Emacs function missing in XEmacs.
|
|
98 (defun error-message-string (obj)
|
|
99 "Convert an error value to an error message."
|
|
100 (let ((buf (get-buffer-create " *error-message*")))
|
|
101 (erase-buffer buf)
|
|
102 (display-error obj buf)
|
|
103 (buffer-string buf)))))
|
28
|
104
|
|
105 ;;; Customization.
|
|
106
|
|
107 (defgroup widgets nil
|
|
108 "Customization support for the Widget Library."
|
|
109 :link '(custom-manual "(widget)Top")
|
|
110 :link '(url-link :tag "Development Page"
|
|
111 "http://www.dina.kvl.dk/~abraham/custom/")
|
173
|
112 :link '(emacs-library-link :tag "Lisp File" "widget.el")
|
28
|
113 :prefix "widget-"
|
|
114 :group 'extensions
|
|
115 :group 'hypermedia)
|
|
116
|
161
|
117 (defgroup widget-documentation nil
|
|
118 "Options controling the display of documentation strings."
|
|
119 :group 'widgets)
|
|
120
|
|
121 (defgroup widget-faces nil
|
|
122 "Faces used by the widget library."
|
|
123 :group 'widgets
|
|
124 :group 'faces)
|
|
125
|
165
|
126 (defvar widget-documentation-face 'widget-documentation-face
|
|
127 "Face used for documentation strings in widges.
|
|
128 This exists as a variable so it can be set locally in certain buffers.")
|
|
129
|
161
|
130 (defface widget-documentation-face '((((class color)
|
|
131 (background dark))
|
|
132 (:foreground "lime green"))
|
|
133 (((class color)
|
|
134 (background light))
|
|
135 (:foreground "dark green"))
|
|
136 (t nil))
|
|
137 "Face used for documentation text."
|
|
138 :group 'widget-documentation
|
|
139 :group 'widget-faces)
|
|
140
|
173
|
141 (defvar widget-button-face 'widget-button-face
|
|
142 "Face used for buttons in widges.
|
|
143 This exists as a variable so it can be set locally in certain buffers.")
|
|
144
|
28
|
145 (defface widget-button-face '((t (:bold t)))
|
|
146 "Face used for widget buttons."
|
161
|
147 :group 'widget-faces)
|
28
|
148
|
|
149 (defcustom widget-mouse-face 'highlight
|
|
150 "Face used for widget buttons when the mouse is above them."
|
|
151 :type 'face
|
161
|
152 :group 'widget-faces)
|
28
|
153
|
|
154 (defface widget-field-face '((((class grayscale color)
|
|
155 (background light))
|
149
|
156 (:background "gray85"))
|
28
|
157 (((class grayscale color)
|
|
158 (background dark))
|
153
|
159 (:background "dim gray"))
|
28
|
160 (t
|
|
161 (:italic t)))
|
|
162 "Face used for editable fields."
|
161
|
163 :group 'widget-faces)
|
28
|
164
|
167
|
165 (defface widget-single-line-field-face '((((class grayscale color)
|
|
166 (background light))
|
|
167 (:background "gray85"))
|
|
168 (((class grayscale color)
|
|
169 (background dark))
|
|
170 (:background "dim gray"))
|
|
171 (t
|
|
172 (:italic t)))
|
|
173 "Face used for editable fields spanning only a single line."
|
|
174 :group 'widget-faces)
|
|
175
|
|
176 (defvar widget-single-line-display-table
|
|
177 (let ((table (make-display-table)))
|
|
178 (aset table 9 "^I")
|
|
179 (aset table 10 "^J")
|
|
180 table)
|
|
181 "Display table used for single-line editable fields.")
|
|
182
|
|
183 (when (fboundp 'set-face-display-table)
|
|
184 (set-face-display-table 'widget-single-line-field-face
|
|
185 widget-single-line-display-table))
|
|
186
|
28
|
187 ;;; Utility functions.
|
|
188 ;;
|
|
189 ;; These are not really widget specific.
|
|
190
|
|
191 (defsubst widget-plist-member (plist prop)
|
|
192 ;; Return non-nil if PLIST has the property PROP.
|
|
193 ;; PLIST is a property list, which is a list of the form
|
|
194 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
|
|
195 ;; Unlike `plist-get', this allows you to distinguish between a missing
|
|
196 ;; property and a property with the value nil.
|
|
197 ;; The value is actually the tail of PLIST whose car is PROP.
|
|
198 (while (and plist (not (eq (car plist) prop)))
|
|
199 (setq plist (cdr (cdr plist))))
|
|
200 plist)
|
|
201
|
|
202 (defun widget-princ-to-string (object)
|
|
203 ;; Return string representation of OBJECT, any Lisp object.
|
|
204 ;; No quoting characters are used; no delimiters are printed around
|
|
205 ;; the contents of strings.
|
|
206 (save-excursion
|
|
207 (set-buffer (get-buffer-create " *widget-tmp*"))
|
|
208 (erase-buffer)
|
|
209 (let ((standard-output (current-buffer)))
|
|
210 (princ object))
|
|
211 (buffer-string)))
|
|
212
|
|
213 (defun widget-clear-undo ()
|
|
214 "Clear all undo information."
|
|
215 (buffer-disable-undo (current-buffer))
|
|
216 (buffer-enable-undo))
|
|
217
|
149
|
218 (defcustom widget-menu-max-size 40
|
|
219 "Largest number of items allowed in a popup-menu.
|
|
220 Larger menus are read through the minibuffer."
|
|
221 :group 'widgets
|
|
222 :type 'integer)
|
|
223
|
173
|
224 (defcustom widget-menu-minibuffer-flag (string-match "XEmacs" emacs-version)
|
165
|
225 "*Control how to ask for a choice from the keyboard.
|
|
226 Non-nil means use the minibuffer;
|
|
227 nil means read a single character."
|
|
228 :group 'widgets
|
|
229 :type 'boolean)
|
|
230
|
28
|
231 (defun widget-choose (title items &optional event)
|
|
232 "Choose an item from a list.
|
|
233
|
|
234 First argument TITLE is the name of the list.
|
149
|
235 Second argument ITEMS is an list whose members are either
|
|
236 (NAME . VALUE), to indicate selectable items, or just strings to
|
|
237 indicate unselectable items.
|
28
|
238 Optional third argument EVENT is an input event.
|
|
239
|
|
240 The user is asked to choose between each NAME from the items alist,
|
|
241 and the VALUE of the chosen element will be returned. If EVENT is a
|
|
242 mouse event, and the number of elements in items is less than
|
|
243 `widget-menu-max-size', a popup menu will be used, otherwise the
|
|
244 minibuffer."
|
|
245 (cond ((and (< (length items) widget-menu-max-size)
|
|
246 event (fboundp 'x-popup-menu) window-system)
|
|
247 ;; We are in Emacs-19, pressed by the mouse
|
|
248 (x-popup-menu event
|
|
249 (list title (cons "" items))))
|
|
250 ((and (< (length items) widget-menu-max-size)
|
|
251 event (fboundp 'popup-menu) window-system)
|
|
252 ;; We are in XEmacs, pressed by the mouse
|
|
253 (let ((val (get-popup-menu-response
|
|
254 (cons title
|
|
255 (mapcar
|
|
256 (function
|
|
257 (lambda (x)
|
149
|
258 (if (stringp x)
|
|
259 (vector x nil nil)
|
|
260 (vector (car x) (list (car x)) t))))
|
28
|
261 items)))))
|
|
262 (setq val (and val
|
|
263 (listp (event-object val))
|
|
264 (stringp (car-safe (event-object val)))
|
|
265 (car (event-object val))))
|
|
266 (cdr (assoc val items))))
|
165
|
267 (widget-menu-minibuffer-flag
|
|
268 ;; Read the choice of name from the minibuffer.
|
155
|
269 (setq items (widget-remove-if 'stringp items))
|
30
|
270 (let ((val (completing-read (concat title ": ") items nil t)))
|
|
271 (if (stringp val)
|
|
272 (let ((try (try-completion val items)))
|
|
273 (when (stringp try)
|
|
274 (setq val try))
|
|
275 (cdr (assoc val items)))
|
165
|
276 nil)))
|
|
277 (t
|
|
278 ;; Construct a menu of the choices
|
|
279 ;; and then use it for prompting for a single character.
|
|
280 (let* ((overriding-terminal-local-map
|
|
281 (make-sparse-keymap))
|
|
282 map choice (next-digit ?0)
|
|
283 value)
|
|
284 ;; Define SPC as a prefix char to get to this menu.
|
|
285 (define-key overriding-terminal-local-map " "
|
|
286 (setq map (make-sparse-keymap title)))
|
173
|
287 (save-excursion
|
|
288 (set-buffer (get-buffer-create " widget-choose"))
|
|
289 (erase-buffer)
|
|
290 (insert "Available choices:\n\n")
|
|
291 (while items
|
|
292 (setq choice (car items) items (cdr items))
|
|
293 (if (consp choice)
|
|
294 (let* ((name (car choice))
|
|
295 (function (cdr choice)))
|
|
296 (insert (format "%c = %s\n" next-digit name))
|
|
297 (define-key map (vector next-digit) function)))
|
|
298 ;; Allocate digits to disabled alternatives
|
|
299 ;; so that the digit of a given alternative never varies.
|
|
300 (setq next-digit (1+ next-digit)))
|
|
301 (insert "\nC-g = Quit"))
|
|
302 (define-key map [?\C-g] 'keyboard-quit)
|
165
|
303 (define-key map [t] 'keyboard-quit)
|
|
304 (setcdr map (nreverse (cdr map)))
|
|
305 ;; Unread a SPC to lead to our new menu.
|
|
306 (setq unread-command-events (cons ?\ unread-command-events))
|
|
307 ;; Read a char with the menu, and return the result
|
|
308 ;; that corresponds to it.
|
173
|
309 (save-window-excursion
|
|
310 (display-buffer (get-buffer " widget-choose"))
|
|
311 (let ((cursor-in-echo-area t))
|
|
312 (setq value
|
|
313 (lookup-key overriding-terminal-local-map
|
|
314 (read-key-sequence title) t))))
|
165
|
315 (when (eq value 'keyboard-quit)
|
|
316 (error "Canceled"))
|
|
317 value))))
|
28
|
318
|
155
|
319 (defun widget-remove-if (predictate list)
|
|
320 (let (result (tail list))
|
|
321 (while tail
|
|
322 (or (funcall predictate (car tail))
|
|
323 (setq result (cons (car tail) result)))
|
|
324 (setq tail (cdr tail)))
|
|
325 (nreverse result)))
|
|
326
|
28
|
327 ;;; Widget text specifications.
|
|
328 ;;
|
|
329 ;; These functions are for specifying text properties.
|
|
330
|
161
|
331 (defcustom widget-field-add-space
|
|
332 (or (< emacs-major-version 20)
|
|
333 (and (eq emacs-major-version 20)
|
|
334 (< emacs-minor-version 3))
|
|
335 (not (string-match "XEmacs" emacs-version)))
|
|
336 "Non-nil means add extra space at the end of editable text fields.
|
|
337
|
|
338 This is needed on all versions of Emacs, and on XEmacs before 20.3.
|
|
339 If you don't add the space, it will become impossible to edit a zero
|
|
340 size field."
|
|
341 :type 'boolean
|
|
342 :group 'widgets)
|
|
343
|
165
|
344 (defcustom widget-field-use-before-change
|
173
|
345 (and (or (> emacs-minor-version 34)
|
|
346 (> emacs-major-version 19))
|
|
347 (not (string-match "XEmacs" emacs-version)))
|
165
|
348 "Non-nil means use `before-change-functions' to track editable fields.
|
173
|
349 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
|
165
|
350 Using before hooks also means that the :notify function can't know the
|
|
351 new value."
|
|
352 :type 'boolean
|
|
353 :group 'widgets)
|
|
354
|
28
|
355 (defun widget-specify-field (widget from to)
|
155
|
356 "Specify editable button for WIDGET between FROM and TO."
|
|
357 ;; Terminating space is not part of the field, but necessary in
|
|
358 ;; order for local-map to work. Remove next sexp if local-map works
|
|
359 ;; at the end of the overlay.
|
|
360 (save-excursion
|
|
361 (goto-char to)
|
173
|
362 (cond ((null (widget-get widget :size))
|
|
363 (forward-char 1))
|
|
364 (widget-field-add-space
|
|
365 (insert-and-inherit " ")))
|
155
|
366 (setq to (point)))
|
28
|
367 (let ((map (widget-get widget :keymap))
|
155
|
368 (face (or (widget-get widget :value-face) 'widget-field-face))
|
30
|
369 (help-echo (widget-get widget :help-echo))
|
173
|
370 (overlay (make-overlay from to nil
|
|
371 nil (or (not widget-field-add-space)
|
|
372 (widget-get widget :size)))))
|
30
|
373 (unless (or (stringp help-echo) (null help-echo))
|
155
|
374 (setq help-echo 'widget-mouse-help))
|
|
375 (widget-put widget :field-overlay overlay)
|
|
376 (overlay-put overlay 'detachable nil)
|
|
377 (overlay-put overlay 'field widget)
|
|
378 (overlay-put overlay 'local-map map)
|
|
379 (overlay-put overlay 'keymap map)
|
|
380 (overlay-put overlay 'face face)
|
|
381 (overlay-put overlay 'balloon-help help-echo)
|
|
382 (overlay-put overlay 'help-echo help-echo)))
|
28
|
383
|
|
384 (defun widget-specify-button (widget from to)
|
155
|
385 "Specify button for WIDGET between FROM and TO."
|
30
|
386 (let ((face (widget-apply widget :button-face-get))
|
|
387 (help-echo (widget-get widget :help-echo))
|
155
|
388 (overlay (make-overlay from to nil t nil)))
|
|
389 (widget-put widget :button-overlay overlay)
|
30
|
390 (unless (or (null help-echo) (stringp help-echo))
|
|
391 (setq help-echo 'widget-mouse-help))
|
155
|
392 (overlay-put overlay 'button widget)
|
|
393 (overlay-put overlay 'mouse-face widget-mouse-face)
|
|
394 (overlay-put overlay 'balloon-help help-echo)
|
|
395 (overlay-put overlay 'help-echo help-echo)
|
|
396 (overlay-put overlay 'face face)))
|
28
|
397
|
30
|
398 (defun widget-mouse-help (extent)
|
|
399 "Find mouse help string for button in extent."
|
|
400 (let* ((widget (widget-at (extent-start-position extent)))
|
|
401 (help-echo (and widget (widget-get widget :help-echo))))
|
|
402 (cond ((stringp help-echo)
|
|
403 help-echo)
|
|
404 ((and (symbolp help-echo) (fboundp help-echo)
|
|
405 (stringp (setq help-echo (funcall help-echo widget))))
|
|
406 help-echo)
|
|
407 (t
|
|
408 (format "(widget %S :help-echo %S)" widget help-echo)))))
|
|
409
|
28
|
410 (defun widget-specify-sample (widget from to)
|
|
411 ;; Specify sample for WIDGET between FROM and TO.
|
173
|
412 (let ((face (widget-apply widget :sample-face-get))
|
|
413 (overlay (make-overlay from to nil t nil)))
|
|
414 (overlay-put overlay 'face face)
|
|
415 (widget-put widget :sample-overlay overlay)))
|
|
416
|
28
|
417 (defun widget-specify-doc (widget from to)
|
|
418 ;; Specify documentation for WIDGET between FROM and TO.
|
173
|
419 (let ((overlay (make-overlay from to nil t nil)))
|
|
420 (overlay-put overlay 'widget-doc widget)
|
|
421 (overlay-put overlay 'face widget-documentation-face)
|
|
422 (widget-put widget :doc-overlay overlay)))
|
28
|
423
|
|
424 (defmacro widget-specify-insert (&rest form)
|
|
425 ;; Execute FORM without inheriting any text properties.
|
149
|
426 (`
|
|
427 (save-restriction
|
28
|
428 (let ((inhibit-read-only t)
|
|
429 result
|
163
|
430 before-change-functions
|
28
|
431 after-change-functions)
|
|
432 (insert "<>")
|
|
433 (narrow-to-region (- (point) 2) (point))
|
|
434 (goto-char (1+ (point-min)))
|
149
|
435 (setq result (progn (,@ form)))
|
28
|
436 (delete-region (point-min) (1+ (point-min)))
|
|
437 (delete-region (1- (point-max)) (point-max))
|
|
438 (goto-char (point-max))
|
149
|
439 result))))
|
28
|
440
|
116
|
441 (defface widget-inactive-face '((((class grayscale color)
|
|
442 (background dark))
|
|
443 (:foreground "light gray"))
|
|
444 (((class grayscale color)
|
|
445 (background light))
|
163
|
446 (:foreground "dim gray"))
|
116
|
447 (t
|
|
448 (:italic t)))
|
|
449 "Face used for inactive widgets."
|
161
|
450 :group 'widget-faces)
|
116
|
451
|
|
452 (defun widget-specify-inactive (widget from to)
|
|
453 "Make WIDGET inactive for user modifications."
|
|
454 (unless (widget-get widget :inactive)
|
|
455 (let ((overlay (make-overlay from to nil t nil)))
|
|
456 (overlay-put overlay 'face 'widget-inactive-face)
|
161
|
457 ;; This is disabled, as it makes the mouse cursor change shape.
|
|
458 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
|
149
|
459 (overlay-put overlay 'evaporate t)
|
|
460 (overlay-put overlay 'priority 100)
|
116
|
461 (overlay-put overlay (if (string-match "XEmacs" emacs-version)
|
|
462 'read-only
|
|
463 'modification-hooks) '(widget-overlay-inactive))
|
|
464 (widget-put widget :inactive overlay))))
|
|
465
|
|
466 (defun widget-overlay-inactive (&rest junk)
|
|
467 "Ignoring the arguments, signal an error."
|
|
468 (unless inhibit-read-only
|
|
469 (error "Attempt to modify inactive widget")))
|
|
470
|
|
471
|
|
472 (defun widget-specify-active (widget)
|
|
473 "Make WIDGET active for user modifications."
|
|
474 (let ((inactive (widget-get widget :inactive)))
|
|
475 (when inactive
|
|
476 (delete-overlay inactive)
|
|
477 (widget-put widget :inactive nil))))
|
|
478
|
28
|
479 ;;; Widget Properties.
|
|
480
|
|
481 (defsubst widget-type (widget)
|
|
482 "Return the type of WIDGET, a symbol."
|
|
483 (car widget))
|
|
484
|
|
485 (defun widget-put (widget property value)
|
|
486 "In WIDGET set PROPERTY to VALUE.
|
|
487 The value can later be retrived with `widget-get'."
|
|
488 (setcdr widget (plist-put (cdr widget) property value)))
|
|
489
|
|
490 (defun widget-get (widget property)
|
|
491 "In WIDGET, get the value of PROPERTY.
|
|
492 The value could either be specified when the widget was created, or
|
|
493 later with `widget-put'."
|
|
494 (let ((missing t)
|
|
495 value tmp)
|
|
496 (while missing
|
|
497 (cond ((setq tmp (widget-plist-member (cdr widget) property))
|
|
498 (setq value (car (cdr tmp))
|
|
499 missing nil))
|
|
500 ((setq tmp (car widget))
|
|
501 (setq widget (get tmp 'widget-type)))
|
|
502 (t
|
|
503 (setq missing nil))))
|
|
504 value))
|
|
505
|
163
|
506 (defun widget-get-indirect (widget property)
|
|
507 "In WIDGET, get the value of PROPERTY.
|
|
508 If the value is a symbol, return its binding.
|
|
509 Otherwise, just return the value."
|
|
510 (let ((value (widget-get widget property)))
|
|
511 (if (symbolp value)
|
|
512 (symbol-value value)
|
|
513 value)))
|
|
514
|
28
|
515 (defun widget-member (widget property)
|
|
516 "Non-nil iff there is a definition in WIDGET for PROPERTY."
|
|
517 (cond ((widget-plist-member (cdr widget) property)
|
|
518 t)
|
|
519 ((car widget)
|
|
520 (widget-member (get (car widget) 'widget-type) property))
|
|
521 (t nil)))
|
|
522
|
118
|
523 ;;;###autoload
|
28
|
524 (defun widget-apply (widget property &rest args)
|
|
525 "Apply the value of WIDGET's PROPERTY to the widget itself.
|
30
|
526 ARGS are passed as extra arguments to the function."
|
28
|
527 (apply (widget-get widget property) widget args))
|
|
528
|
|
529 (defun widget-value (widget)
|
|
530 "Extract the current value of WIDGET."
|
|
531 (widget-apply widget
|
|
532 :value-to-external (widget-apply widget :value-get)))
|
|
533
|
|
534 (defun widget-value-set (widget value)
|
|
535 "Set the current value of WIDGET to VALUE."
|
|
536 (widget-apply widget
|
|
537 :value-set (widget-apply widget
|
|
538 :value-to-internal value)))
|
|
539
|
|
540 (defun widget-match-inline (widget vals)
|
|
541 ;; In WIDGET, match the start of VALS.
|
|
542 (cond ((widget-get widget :inline)
|
|
543 (widget-apply widget :match-inline vals))
|
|
544 ((and vals
|
|
545 (widget-apply widget :match (car vals)))
|
|
546 (cons (list (car vals)) (cdr vals)))
|
|
547 (t nil)))
|
|
548
|
116
|
549 (defun widget-apply-action (widget &optional event)
|
|
550 "Apply :action in WIDGET in response to EVENT."
|
163
|
551 (if (widget-apply widget :active)
|
|
552 (widget-apply widget :action event)
|
|
553 (error "Attempt to perform action on inactive widget")))
|
149
|
554
|
|
555 ;;; Helper functions.
|
|
556 ;;
|
|
557 ;; These are widget specific.
|
|
558
|
|
559 ;;;###autoload
|
|
560 (defun widget-prompt-value (widget prompt &optional value unbound)
|
|
561 "Prompt for a value matching WIDGET, using PROMPT.
|
|
562 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
|
|
563 (unless (listp widget)
|
|
564 (setq widget (list widget)))
|
|
565 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
|
|
566 (setq widget (widget-convert widget))
|
|
567 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
|
|
568 (unless (widget-apply widget :match answer)
|
|
569 (error "Value does not match %S type." (car widget)))
|
|
570 answer))
|
|
571
|
|
572 (defun widget-get-sibling (widget)
|
|
573 "Get the item WIDGET is assumed to toggle.
|
|
574 This is only meaningful for radio buttons or checkboxes in a list."
|
|
575 (let* ((parent (widget-get widget :parent))
|
|
576 (children (widget-get parent :children))
|
|
577 child)
|
|
578 (catch 'child
|
|
579 (while children
|
|
580 (setq child (car children)
|
|
581 children (cdr children))
|
|
582 (when (eq (widget-get child :button) widget)
|
|
583 (throw 'child child)))
|
|
584 nil)))
|
|
585
|
161
|
586 (defun widget-map-buttons (function &optional buffer maparg)
|
|
587 "Map FUNCTION over the buttons in BUFFER.
|
|
588 FUNCTION is called with the arguments WIDGET and MAPARG.
|
|
589
|
|
590 If FUNCTION returns non-nil, the walk is cancelled.
|
|
591
|
|
592 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
|
|
593 respectively."
|
|
594 (let ((cur (point-min))
|
|
595 (widget nil)
|
|
596 (parent nil)
|
|
597 (overlays (if buffer
|
|
598 (save-excursion (set-buffer buffer) (overlay-lists))
|
|
599 (overlay-lists))))
|
|
600 (setq overlays (append (car overlays) (cdr overlays)))
|
|
601 (while (setq cur (pop overlays))
|
|
602 (setq widget (overlay-get cur 'button))
|
|
603 (if (and widget (funcall function widget maparg))
|
|
604 (setq overlays nil)))))
|
|
605
|
28
|
606 ;;; Glyphs.
|
|
607
|
|
608 (defcustom widget-glyph-directory (concat data-directory "custom/")
|
|
609 "Where widget glyphs are located.
|
|
610 If this variable is nil, widget will try to locate the directory
|
153
|
611 automatically."
|
28
|
612 :group 'widgets
|
|
613 :type 'directory)
|
|
614
|
|
615 (defcustom widget-glyph-enable t
|
|
616 "If non nil, use glyphs in images when available."
|
|
617 :group 'widgets
|
|
618 :type 'boolean)
|
|
619
|
153
|
620 (defcustom widget-image-conversion
|
|
621 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
|
|
622 (xbm ".xbm"))
|
|
623 "Conversion alist from image formats to file name suffixes."
|
|
624 :group 'widgets
|
|
625 :type '(repeat (cons :format "%v"
|
|
626 (symbol :tag "Image Format" unknown)
|
|
627 (repeat :tag "Suffixes"
|
|
628 (string :format "%v")))))
|
|
629
|
155
|
630 (defun widget-glyph-find (image tag)
|
|
631 "Create a glyph corresponding to IMAGE with string TAG as fallback.
|
|
632 IMAGE should either already be a glyph, or be a file name sans
|
|
633 extension (xpm, xbm, gif, jpg, or png) located in
|
|
634 `widget-glyph-directory'."
|
|
635 (cond ((not (and image
|
|
636 (string-match "XEmacs" emacs-version)
|
30
|
637 widget-glyph-enable
|
|
638 (fboundp 'make-glyph)
|
153
|
639 (fboundp 'locate-file)
|
30
|
640 image))
|
|
641 ;; We don't want or can't use glyphs.
|
155
|
642 nil)
|
30
|
643 ((and (fboundp 'glyphp)
|
|
644 (glyphp image))
|
155
|
645 ;; Already a glyph. Use it.
|
|
646 image)
|
153
|
647 ((stringp image)
|
|
648 ;; A string. Look it up in relevant directories.
|
|
649 (let* ((dirlist (list (or widget-glyph-directory
|
|
650 (concat data-directory
|
|
651 "custom/"))
|
|
652 data-directory))
|
|
653 (formats widget-image-conversion)
|
|
654 file)
|
|
655 (while (and formats (not file))
|
|
656 (when (valid-image-instantiator-format-p (car (car formats)))
|
|
657 (setq file (locate-file image dirlist
|
155
|
658 (mapconcat 'identity
|
|
659 (cdr (car formats))
|
153
|
660 ":"))))
|
155
|
661 (unless file
|
|
662 (setq formats (cdr formats))))
|
|
663 (and file
|
|
664 ;; We create a glyph with the file as the default image
|
|
665 ;; instantiator, and the TAG fallback
|
|
666 (make-glyph (list (vector (car (car formats)) ':file file)
|
|
667 (vector 'string ':data tag))))))
|
153
|
668 ((valid-instantiator-p image 'image)
|
155
|
669 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
|
|
670 (make-glyph (list image
|
|
671 (vector 'string ':data tag))))
|
|
672 ((consp image)
|
|
673 ;; This could be virtually anything. Let `make-glyph' sort it out.
|
|
674 (make-glyph image))
|
30
|
675 (t
|
153
|
676 ;; Oh well.
|
155
|
677 nil)))
|
|
678
|
|
679 (defun widget-glyph-insert (widget tag image &optional down inactive)
|
|
680 "In WIDGET, insert the text TAG or, if supported, IMAGE.
|
|
681 IMAGE should either be a glyph, an image instantiator, or an image file
|
|
682 name sans extension (xpm, xbm, gif, jpg, or png) located in
|
|
683 `widget-glyph-directory'.
|
|
684
|
|
685 Optional arguments DOWN and INACTIVE is used instead of IMAGE when the
|
|
686 glyph is pressed or inactive, respectively.
|
|
687
|
|
688 WARNING: If you call this with a glyph, and you want the user to be
|
|
689 able to invoke the glyph, make sure it is unique. If you use the
|
|
690 same glyph for multiple widgets, invoking any of the glyphs will
|
|
691 cause the last created widget to be invoked.
|
|
692
|
|
693 Instead of an instantiator, you can also use a list of instantiators,
|
|
694 or whatever `make-glyph' will accept. However, in that case you must
|
|
695 provide the fallback TAG as a part of the instantiator yourself."
|
|
696 (let ((glyph (widget-glyph-find image tag)))
|
|
697 (if glyph
|
|
698 (widget-glyph-insert-glyph widget
|
|
699 glyph
|
|
700 (widget-glyph-find down tag)
|
|
701 (widget-glyph-find inactive tag))
|
|
702 (insert tag))))
|
28
|
703
|
153
|
704 (defun widget-glyph-insert-glyph (widget glyph &optional down inactive)
|
155
|
705 "In WIDGET, insert GLYPH.
|
|
706 If optional arguments DOWN and INACTIVE are given, they should be
|
|
707 glyphs used when the widget is pushed and inactive, respectively."
|
163
|
708 (when widget
|
|
709 (set-glyph-property glyph 'widget widget)
|
|
710 (when down
|
|
711 (set-glyph-property down 'widget widget))
|
|
712 (when inactive
|
|
713 (set-glyph-property inactive 'widget widget)))
|
28
|
714 (insert "*")
|
155
|
715 (let ((ext (make-extent (point) (1- (point))))
|
163
|
716 (help-echo (and widget (widget-get widget :help-echo))))
|
155
|
717 (set-extent-property ext 'invisible t)
|
163
|
718 (set-extent-property ext 'start-open t)
|
|
719 (set-extent-property ext 'end-open t)
|
155
|
720 (set-extent-end-glyph ext glyph)
|
|
721 (when help-echo
|
|
722 (set-extent-property ext 'balloon-help help-echo)
|
|
723 (set-extent-property ext 'help-echo help-echo)))
|
163
|
724 (when widget
|
|
725 (widget-put widget :glyph-up glyph)
|
|
726 (when down (widget-put widget :glyph-down down))
|
|
727 (when inactive (widget-put widget :glyph-inactive inactive))))
|
28
|
728
|
153
|
729 ;;; Buttons.
|
|
730
|
|
731 (defgroup widget-button nil
|
|
732 "The look of various kinds of buttons."
|
|
733 :group 'widgets)
|
|
734
|
|
735 (defcustom widget-button-prefix ""
|
|
736 "String used as prefix for buttons."
|
|
737 :type 'string
|
155
|
738 :group 'widget-button)
|
153
|
739
|
|
740 (defcustom widget-button-suffix ""
|
|
741 "String used as suffix for buttons."
|
|
742 :type 'string
|
155
|
743 :group 'widget-button)
|
153
|
744
|
28
|
745 ;;; Creating Widgets.
|
|
746
|
|
747 ;;;###autoload
|
|
748 (defun widget-create (type &rest args)
|
|
749 "Create widget of TYPE.
|
|
750 The optional ARGS are additional keyword arguments."
|
|
751 (let ((widget (apply 'widget-convert type args)))
|
|
752 (widget-apply widget :create)
|
|
753 widget))
|
|
754
|
|
755 (defun widget-create-child-and-convert (parent type &rest args)
|
|
756 "As part of the widget PARENT, create a child widget TYPE.
|
|
757 The child is converted, using the keyword arguments ARGS."
|
|
758 (let ((widget (apply 'widget-convert type args)))
|
|
759 (widget-put widget :parent parent)
|
|
760 (unless (widget-get widget :indent)
|
|
761 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
|
|
762 (or (widget-get widget :extra-offset) 0)
|
|
763 (widget-get parent :offset))))
|
|
764 (widget-apply widget :create)
|
|
765 widget))
|
|
766
|
|
767 (defun widget-create-child (parent type)
|
|
768 "Create widget of TYPE."
|
149
|
769 (let ((widget (copy-sequence type)))
|
28
|
770 (widget-put widget :parent parent)
|
|
771 (unless (widget-get widget :indent)
|
|
772 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
|
|
773 (or (widget-get widget :extra-offset) 0)
|
|
774 (widget-get parent :offset))))
|
|
775 (widget-apply widget :create)
|
|
776 widget))
|
|
777
|
|
778 (defun widget-create-child-value (parent type value)
|
|
779 "Create widget of TYPE with value VALUE."
|
149
|
780 (let ((widget (copy-sequence type)))
|
28
|
781 (widget-put widget :value (widget-apply widget :value-to-internal value))
|
|
782 (widget-put widget :parent parent)
|
|
783 (unless (widget-get widget :indent)
|
|
784 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
|
|
785 (or (widget-get widget :extra-offset) 0)
|
|
786 (widget-get parent :offset))))
|
|
787 (widget-apply widget :create)
|
|
788 widget))
|
|
789
|
|
790 ;;;###autoload
|
|
791 (defun widget-delete (widget)
|
|
792 "Delete WIDGET."
|
|
793 (widget-apply widget :delete))
|
|
794
|
|
795 (defun widget-convert (type &rest args)
|
|
796 "Convert TYPE to a widget without inserting it in the buffer.
|
|
797 The optional ARGS are additional keyword arguments."
|
|
798 ;; Don't touch the type.
|
|
799 (let* ((widget (if (symbolp type)
|
|
800 (list type)
|
149
|
801 (copy-sequence type)))
|
28
|
802 (current widget)
|
|
803 (keys args))
|
|
804 ;; First set the :args keyword.
|
|
805 (while (cdr current) ;Look in the type.
|
|
806 (let ((next (car (cdr current))))
|
|
807 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
808 (setq current (cdr (cdr current)))
|
|
809 (setcdr current (list :args (cdr current)))
|
|
810 (setq current nil))))
|
|
811 (while args ;Look in the args.
|
|
812 (let ((next (nth 0 args)))
|
|
813 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
814 (setq args (nthcdr 2 args))
|
|
815 (widget-put widget :args args)
|
|
816 (setq args nil))))
|
|
817 ;; Then Convert the widget.
|
|
818 (setq type widget)
|
|
819 (while type
|
|
820 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
|
|
821 (if convert-widget
|
|
822 (setq widget (funcall convert-widget widget))))
|
|
823 (setq type (get (car type) 'widget-type)))
|
|
824 ;; Finally set the keyword args.
|
|
825 (while keys
|
|
826 (let ((next (nth 0 keys)))
|
|
827 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
828 (progn
|
|
829 (widget-put widget next (nth 1 keys))
|
|
830 (setq keys (nthcdr 2 keys)))
|
|
831 (setq keys nil))))
|
|
832 ;; Convert the :value to internal format.
|
|
833 (if (widget-member widget :value)
|
|
834 (let ((value (widget-get widget :value)))
|
|
835 (widget-put widget
|
|
836 :value (widget-apply widget :value-to-internal value))))
|
|
837 ;; Return the newly create widget.
|
|
838 widget))
|
|
839
|
|
840 (defun widget-insert (&rest args)
|
|
841 "Call `insert' with ARGS and make the text read only."
|
|
842 (let ((inhibit-read-only t)
|
163
|
843 before-change-functions
|
28
|
844 after-change-functions
|
|
845 (from (point)))
|
173
|
846 (apply 'insert args)))
|
28
|
847
|
161
|
848 (defun widget-convert-text (type from to
|
|
849 &optional button-from button-to
|
|
850 &rest args)
|
|
851 "Return a widget of type TYPE with endpoint FROM TO.
|
|
852 Optional ARGS are extra keyword arguments for TYPE.
|
|
853 and TO will be used as the widgets end points. If optional arguments
|
|
854 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
|
|
855 button end points.
|
|
856 Optional ARGS are extra keyword arguments for TYPE."
|
|
857 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
|
|
858 (from (copy-marker from))
|
|
859 (to (copy-marker to)))
|
|
860 (set-marker-insertion-type from t)
|
|
861 (set-marker-insertion-type to nil)
|
|
862 (widget-put widget :from from)
|
|
863 (widget-put widget :to to)
|
|
864 (when button-from
|
|
865 (widget-specify-button widget button-from button-to))
|
|
866 widget))
|
|
867
|
|
868 (defun widget-convert-button (type from to &rest args)
|
|
869 "Return a widget of type TYPE with endpoint FROM TO.
|
|
870 Optional ARGS are extra keyword arguments for TYPE.
|
|
871 No text will be inserted to the buffer, instead the text between FROM
|
|
872 and TO will be used as the widgets end points, as well as the widgets
|
|
873 button end points."
|
|
874 (apply 'widget-convert-text type from to from to args))
|
|
875
|
|
876 (defun widget-leave-text (widget)
|
|
877 "Remove markers and overlays from WIDGET and its children."
|
|
878 (let ((from (widget-get widget :from))
|
|
879 (to (widget-get widget :to))
|
|
880 (button (widget-get widget :button-overlay))
|
173
|
881 (sample (widget-get widget :sample-overlay))
|
|
882 (doc (widget-get widget :doc-overlay))
|
161
|
883 (field (widget-get widget :field-overlay))
|
|
884 (children (widget-get widget :children)))
|
|
885 (set-marker from nil)
|
|
886 (set-marker to nil)
|
163
|
887 (when button
|
|
888 (delete-overlay button))
|
173
|
889 (when sample
|
|
890 (delete-overlay sample))
|
|
891 (when doc
|
|
892 (delete-overlay doc))
|
163
|
893 (when field
|
|
894 (delete-overlay field))
|
161
|
895 (mapcar 'widget-leave-text children)))
|
|
896
|
30
|
897 ;;; Keymap and Commands.
|
28
|
898
|
|
899 (defvar widget-keymap nil
|
|
900 "Keymap containing useful binding for buffers containing widgets.
|
|
901 Recommended as a parent keymap for modes using widgets.")
|
|
902
|
|
903 (unless widget-keymap
|
|
904 (setq widget-keymap (make-sparse-keymap))
|
|
905 (define-key widget-keymap "\t" 'widget-forward)
|
|
906 (define-key widget-keymap [(shift tab)] 'widget-backward)
|
|
907 (define-key widget-keymap [backtab] 'widget-backward)
|
149
|
908 (if (string-match "XEmacs" emacs-version)
|
28
|
909 (progn
|
149
|
910 ;;Glyph support.
|
|
911 (define-key widget-keymap [button1] 'widget-button1-click)
|
|
912 (define-key widget-keymap [button2] 'widget-button-click))
|
28
|
913 (define-key widget-keymap [down-mouse-2] 'widget-button-click))
|
|
914 (define-key widget-keymap "\C-m" 'widget-button-press))
|
|
915
|
|
916 (defvar widget-global-map global-map
|
|
917 "Keymap used for events the widget does not handle themselves.")
|
|
918 (make-variable-buffer-local 'widget-global-map)
|
|
919
|
|
920 (defvar widget-field-keymap nil
|
|
921 "Keymap used inside an editable field.")
|
|
922
|
|
923 (unless widget-field-keymap
|
|
924 (setq widget-field-keymap (copy-keymap widget-keymap))
|
|
925 (unless (string-match "XEmacs" (emacs-version))
|
|
926 (define-key widget-field-keymap [menu-bar] 'nil))
|
155
|
927 (define-key widget-field-keymap "\C-k" 'widget-kill-line)
|
|
928 (define-key widget-field-keymap "\M-\t" 'widget-complete)
|
28
|
929 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
|
|
930 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
|
|
931 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
|
|
932 (set-keymap-parent widget-field-keymap global-map))
|
|
933
|
|
934 (defvar widget-text-keymap nil
|
|
935 "Keymap used inside a text field.")
|
|
936
|
|
937 (unless widget-text-keymap
|
|
938 (setq widget-text-keymap (copy-keymap widget-keymap))
|
|
939 (unless (string-match "XEmacs" (emacs-version))
|
|
940 (define-key widget-text-keymap [menu-bar] 'nil))
|
|
941 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
|
|
942 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
|
|
943 (set-keymap-parent widget-text-keymap global-map))
|
|
944
|
|
945 (defun widget-field-activate (pos &optional event)
|
155
|
946 "Invoke the ediable field at point."
|
28
|
947 (interactive "@d")
|
155
|
948 (let ((field (get-char-property pos 'field)))
|
28
|
949 (if field
|
116
|
950 (widget-apply-action field event)
|
28
|
951 (call-interactively
|
|
952 (lookup-key widget-global-map (this-command-keys))))))
|
|
953
|
149
|
954 (defface widget-button-pressed-face
|
|
955 '((((class color))
|
|
956 (:foreground "red"))
|
|
957 (t
|
|
958 (:bold t :underline t)))
|
|
959 "Face used for pressed buttons."
|
161
|
960 :group 'widget-faces)
|
149
|
961
|
28
|
962 (defun widget-button-click (event)
|
155
|
963 "Invoke button below mouse pointer."
|
28
|
964 (interactive "@e")
|
|
965 (cond ((and (fboundp 'event-glyph)
|
|
966 (event-glyph event))
|
149
|
967 (widget-glyph-click event))
|
|
968 ((widget-event-point event)
|
|
969 (let* ((pos (widget-event-point event))
|
155
|
970 (button (get-char-property pos 'button)))
|
28
|
971 (if button
|
155
|
972 (let* ((overlay (widget-get button :button-overlay))
|
|
973 (face (overlay-get overlay 'face))
|
|
974 (mouse-face (overlay-get overlay 'mouse-face)))
|
149
|
975 (unwind-protect
|
|
976 (let ((track-mouse t))
|
155
|
977 (overlay-put overlay
|
|
978 'face 'widget-button-pressed-face)
|
149
|
979 (overlay-put overlay
|
|
980 'mouse-face 'widget-button-pressed-face)
|
|
981 (unless (widget-apply button :mouse-down-action event)
|
|
982 (while (not (button-release-event-p event))
|
155
|
983 (setq event (widget-read-event)
|
149
|
984 pos (widget-event-point event))
|
|
985 (if (and pos
|
155
|
986 (eq (get-char-property pos 'button)
|
149
|
987 button))
|
|
988 (progn
|
|
989 (overlay-put overlay
|
|
990 'face
|
|
991 'widget-button-pressed-face)
|
|
992 (overlay-put overlay
|
|
993 'mouse-face
|
|
994 'widget-button-pressed-face))
|
155
|
995 (overlay-put overlay 'face face)
|
|
996 (overlay-put overlay 'mouse-face mouse-face))))
|
149
|
997 (when (and pos
|
155
|
998 (eq (get-char-property pos 'button) button))
|
149
|
999 (widget-apply-action button event)))
|
155
|
1000 (overlay-put overlay 'face face)
|
|
1001 (overlay-put overlay 'mouse-face mouse-face)))
|
165
|
1002 (let ((up t)
|
|
1003 command)
|
155
|
1004 ;; Find the global command to run, and check whether it
|
|
1005 ;; is bound to an up event.
|
|
1006 (cond ((setq command ;down event
|
165
|
1007 (lookup-key widget-global-map [ button2 ]))
|
|
1008 (setq up nil))
|
155
|
1009 ((setq command ;down event
|
165
|
1010 (lookup-key widget-global-map [ down-mouse-2 ]))
|
|
1011 (setq up nil))
|
155
|
1012 ((setq command ;up event
|
165
|
1013 (lookup-key widget-global-map [ button2up ])))
|
|
1014 ((setq command ;up event
|
|
1015 (lookup-key widget-global-map [ mouse-2]))))
|
|
1016 (when up
|
155
|
1017 ;; Don't execute up events twice.
|
165
|
1018 (while (not (button-release-event-p event))
|
|
1019 (setq event (widget-read-event))))
|
|
1020 (when command
|
155
|
1021 (call-interactively command))))))
|
28
|
1022 (t
|
|
1023 (message "You clicked somewhere weird."))))
|
|
1024
|
|
1025 (defun widget-button1-click (event)
|
155
|
1026 "Invoke glyph below mouse pointer."
|
28
|
1027 (interactive "@e")
|
|
1028 (if (and (fboundp 'event-glyph)
|
|
1029 (event-glyph event))
|
149
|
1030 (widget-glyph-click event)
|
|
1031 (call-interactively (lookup-key widget-global-map (this-command-keys)))))
|
|
1032
|
|
1033 (defun widget-glyph-click (event)
|
|
1034 "Handle click on a glyph."
|
|
1035 (let* ((glyph (event-glyph event))
|
|
1036 (widget (glyph-property glyph 'widget))
|
|
1037 (extent (event-glyph-extent event))
|
|
1038 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
|
|
1039 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
|
|
1040 (last event))
|
|
1041 ;; Wait for the release.
|
|
1042 (while (not (button-release-event-p last))
|
|
1043 (if (eq extent (event-glyph-extent last))
|
|
1044 (set-extent-property extent 'end-glyph down-glyph)
|
|
1045 (set-extent-property extent 'end-glyph up-glyph))
|
|
1046 (setq last (next-event event)))
|
|
1047 ;; Release glyph.
|
|
1048 (when down-glyph
|
|
1049 (set-extent-property extent 'end-glyph up-glyph))
|
|
1050 ;; Apply widget action.
|
|
1051 (when (eq extent (event-glyph-extent last))
|
28
|
1052 (let ((widget (glyph-property (event-glyph event) 'widget)))
|
149
|
1053 (cond ((null widget)
|
|
1054 (message "You clicked on a glyph."))
|
|
1055 ((not (widget-apply widget :active))
|
|
1056 (message "This glyph is inactive."))
|
|
1057 (t
|
|
1058 (widget-apply-action widget event)))))))
|
28
|
1059
|
|
1060 (defun widget-button-press (pos &optional event)
|
155
|
1061 "Invoke button at POS."
|
28
|
1062 (interactive "@d")
|
155
|
1063 (let ((button (get-char-property pos 'button)))
|
28
|
1064 (if button
|
116
|
1065 (widget-apply-action button event)
|
28
|
1066 (let ((command (lookup-key widget-global-map (this-command-keys))))
|
|
1067 (when (commandp command)
|
|
1068 (call-interactively command))))))
|
|
1069
|
161
|
1070 (defun widget-tabable-at (&optional pos)
|
|
1071 "Return the tabable widget at POS, or nil.
|
|
1072 POS defaults to the value of (point)."
|
|
1073 (unless pos
|
|
1074 (setq pos (point)))
|
|
1075 (let ((widget (or (get-char-property (point) 'button)
|
|
1076 (get-char-property (point) 'field))))
|
|
1077 (if widget
|
|
1078 (let ((order (widget-get widget :tab-order)))
|
|
1079 (if order
|
|
1080 (if (>= order 0)
|
|
1081 widget
|
|
1082 nil)
|
|
1083 widget))
|
|
1084 nil)))
|
|
1085
|
173
|
1086 (defcustom widget-use-overlay-change (string-match "XEmacs" emacs-version)
|
|
1087 "If non-nil, use overlay change functions to tab around in the buffer.
|
|
1088 This is much faster, but doesn't work reliably on Emacs 19.34."
|
|
1089 :type 'boolean
|
|
1090 :group 'widgets)
|
|
1091
|
28
|
1092 (defun widget-move (arg)
|
|
1093 "Move point to the ARG next field or button.
|
|
1094 ARG may be negative to move backward."
|
155
|
1095 (or (bobp) (> arg 0) (backward-char))
|
|
1096 (let ((pos (point))
|
|
1097 (number arg)
|
161
|
1098 (old (widget-tabable-at))
|
155
|
1099 new)
|
|
1100 ;; Forward.
|
|
1101 (while (> arg 0)
|
173
|
1102 (cond ((eobp)
|
|
1103 (goto-char (point-min)))
|
|
1104 (widget-use-overlay-change
|
|
1105 (goto-char (next-overlay-change (point))))
|
|
1106 (t
|
|
1107 (forward-char 1)))
|
155
|
1108 (and (eq pos (point))
|
|
1109 (eq arg number)
|
|
1110 (error "No buttons or fields found"))
|
161
|
1111 (let ((new (widget-tabable-at)))
|
155
|
1112 (when new
|
|
1113 (unless (eq new old)
|
161
|
1114 (setq arg (1- arg))
|
155
|
1115 (setq old new)))))
|
|
1116 ;; Backward.
|
|
1117 (while (< arg 0)
|
173
|
1118 (cond ((bobp)
|
|
1119 (goto-char (point-max)))
|
|
1120 (widget-use-overlay-change
|
|
1121 (goto-char (previous-overlay-change (point))))
|
|
1122 (t
|
|
1123 (backward-char 1)))
|
155
|
1124 (and (eq pos (point))
|
|
1125 (eq arg number)
|
|
1126 (error "No buttons or fields found"))
|
161
|
1127 (let ((new (widget-tabable-at)))
|
155
|
1128 (when new
|
|
1129 (unless (eq new old)
|
161
|
1130 (setq arg (1+ arg))))))
|
|
1131 (let ((new (widget-tabable-at)))
|
|
1132 (while (eq (widget-tabable-at) new)
|
|
1133 (backward-char)))
|
155
|
1134 (forward-char))
|
28
|
1135 (widget-echo-help (point))
|
|
1136 (run-hooks 'widget-move-hook))
|
|
1137
|
|
1138 (defun widget-forward (arg)
|
|
1139 "Move point to the next field or button.
|
|
1140 With optional ARG, move across that many fields."
|
|
1141 (interactive "p")
|
|
1142 (run-hooks 'widget-forward-hook)
|
|
1143 (widget-move arg))
|
|
1144
|
|
1145 (defun widget-backward (arg)
|
|
1146 "Move point to the previous field or button.
|
|
1147 With optional ARG, move across that many fields."
|
|
1148 (interactive "p")
|
|
1149 (run-hooks 'widget-backward-hook)
|
|
1150 (widget-move (- arg)))
|
|
1151
|
|
1152 (defun widget-beginning-of-line ()
|
|
1153 "Go to beginning of field or beginning of line, whichever is first."
|
|
1154 (interactive)
|
155
|
1155 (let* ((field (widget-field-find (point)))
|
|
1156 (start (and field (widget-field-start field))))
|
|
1157 (if (and start (not (eq start (point))))
|
|
1158 (goto-char start)
|
173
|
1159 (call-interactively 'beginning-of-line)))
|
|
1160 ;; XEmacs: preserve the region
|
|
1161 (setq zmacs-region-stays t))
|
28
|
1162
|
|
1163 (defun widget-end-of-line ()
|
|
1164 "Go to end of field or end of line, whichever is first."
|
|
1165 (interactive)
|
155
|
1166 (let* ((field (widget-field-find (point)))
|
|
1167 (end (and field (widget-field-end field))))
|
|
1168 (if (and end (not (eq end (point))))
|
|
1169 (goto-char end)
|
173
|
1170 (call-interactively 'end-of-line)))
|
|
1171 ;; XEmacs: preserve the region
|
|
1172 (setq zmacs-region-stays t))
|
28
|
1173
|
|
1174 (defun widget-kill-line ()
|
|
1175 "Kill to end of field or end of line, whichever is first."
|
|
1176 (interactive)
|
157
|
1177 (let* ((field (widget-field-find (point)))
|
|
1178 (newline (save-excursion (forward-line 1) (point)))
|
|
1179 (end (and field (widget-field-end field))))
|
|
1180 (if (and field (> newline end))
|
|
1181 (kill-region (point) end)
|
28
|
1182 (call-interactively 'kill-line))))
|
|
1183
|
155
|
1184 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
|
|
1185 "Default function to call for completion inside fields."
|
|
1186 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
|
|
1187 :type 'function
|
|
1188 :group 'widgets)
|
|
1189
|
|
1190 (defun widget-complete ()
|
|
1191 "Complete content of editable field from point.
|
|
1192 When not inside a field, move to the previous button or field."
|
|
1193 (interactive)
|
|
1194 (let ((field (widget-field-find (point))))
|
|
1195 (if field
|
|
1196 (widget-apply field :complete)
|
|
1197 (error "Not in an editable field"))))
|
|
1198
|
28
|
1199 ;;; Setting up the buffer.
|
|
1200
|
|
1201 (defvar widget-field-new nil)
|
|
1202 ;; List of all newly created editable fields in the buffer.
|
|
1203 (make-variable-buffer-local 'widget-field-new)
|
|
1204
|
|
1205 (defvar widget-field-list nil)
|
|
1206 ;; List of all editable fields in the buffer.
|
|
1207 (make-variable-buffer-local 'widget-field-list)
|
|
1208
|
|
1209 (defun widget-setup ()
|
|
1210 "Setup current buffer so editing string widgets works."
|
|
1211 (let ((inhibit-read-only t)
|
|
1212 (after-change-functions nil)
|
163
|
1213 before-change-functions
|
28
|
1214 field)
|
|
1215 (while widget-field-new
|
|
1216 (setq field (car widget-field-new)
|
|
1217 widget-field-new (cdr widget-field-new)
|
|
1218 widget-field-list (cons field widget-field-list))
|
155
|
1219 (let ((from (car (widget-get field :field-overlay)))
|
|
1220 (to (cdr (widget-get field :field-overlay))))
|
161
|
1221 (widget-specify-field field
|
|
1222 (marker-position from) (marker-position to))
|
155
|
1223 (set-marker from nil)
|
|
1224 (set-marker to nil))))
|
28
|
1225 (widget-clear-undo)
|
173
|
1226 (widget-add-change))
|
28
|
1227
|
|
1228 (defvar widget-field-last nil)
|
|
1229 ;; Last field containing point.
|
|
1230 (make-variable-buffer-local 'widget-field-last)
|
|
1231
|
|
1232 (defvar widget-field-was nil)
|
|
1233 ;; The widget data before the change.
|
|
1234 (make-variable-buffer-local 'widget-field-was)
|
|
1235
|
155
|
1236 (defun widget-field-buffer (widget)
|
|
1237 "Return the start of WIDGET's editing field."
|
161
|
1238 (let ((overlay (widget-get widget :field-overlay)))
|
|
1239 (and overlay (overlay-buffer overlay))))
|
155
|
1240
|
|
1241 (defun widget-field-start (widget)
|
|
1242 "Return the start of WIDGET's editing field."
|
161
|
1243 (let ((overlay (widget-get widget :field-overlay)))
|
|
1244 (and overlay (overlay-start overlay))))
|
155
|
1245
|
|
1246 (defun widget-field-end (widget)
|
|
1247 "Return the end of WIDGET's editing field."
|
161
|
1248 (let ((overlay (widget-get widget :field-overlay)))
|
|
1249 ;; Don't subtract one if local-map works at the end of the overlay.
|
173
|
1250 (and overlay (if (or widget-field-add-space
|
|
1251 (null (widget-get widget :size)))
|
161
|
1252 (1- (overlay-end overlay))
|
|
1253 (overlay-end overlay)))))
|
155
|
1254
|
28
|
1255 (defun widget-field-find (pos)
|
155
|
1256 "Return the field at POS.
|
|
1257 Unlike (get-char-property POS 'field) this, works with empty fields too."
|
28
|
1258 (let ((fields widget-field-list)
|
|
1259 field found)
|
|
1260 (while fields
|
|
1261 (setq field (car fields)
|
|
1262 fields (cdr fields))
|
155
|
1263 (let ((start (widget-field-start field))
|
|
1264 (end (widget-field-end field)))
|
|
1265 (when (and (<= start pos) (<= pos end))
|
|
1266 (when found
|
|
1267 (debug "Overlapping fields"))
|
|
1268 (setq found field))))
|
28
|
1269 found))
|
|
1270
|
173
|
1271 (defun widget-before-change (from to)
|
163
|
1272 ;; This is how, for example, a variable changes its state to `modified'.
|
|
1273 ;; when it is being edited.
|
173
|
1274 (let ((from-field (widget-field-find from))
|
|
1275 (to-field (widget-field-find to)))
|
|
1276 (cond ((not (eq from-field to-field))
|
|
1277 (add-hook 'post-command-hook 'widget-add-change nil t)
|
|
1278 (error "Change should be restricted to a single field"))
|
|
1279 ((null from-field)
|
|
1280 (add-hook 'post-command-hook 'widget-add-change nil t)
|
|
1281 (error "Attempt to change text outside editable field"))
|
|
1282 (widget-field-use-before-change
|
|
1283 (condition-case nil
|
|
1284 (widget-apply from-field :notify from-field)
|
|
1285 (error (debug "Before Change")))))))
|
|
1286
|
|
1287 (defun widget-add-change ()
|
|
1288 (make-local-hook 'post-command-hook)
|
|
1289 (remove-hook 'post-command-hook 'widget-add-change t)
|
|
1290 (make-local-hook 'before-change-functions)
|
|
1291 (add-hook 'before-change-functions 'widget-before-change nil t)
|
|
1292 (make-local-hook 'after-change-functions)
|
|
1293 (add-hook 'after-change-functions 'widget-after-change nil t))
|
163
|
1294
|
28
|
1295 (defun widget-after-change (from to old)
|
|
1296 ;; Adjust field size and text properties.
|
|
1297 (condition-case nil
|
|
1298 (let ((field (widget-field-find from))
|
155
|
1299 (other (widget-field-find to)))
|
|
1300 (when field
|
|
1301 (unless (eq field other)
|
|
1302 (debug "Change in different fields"))
|
161
|
1303 (let ((size (widget-get field :size))
|
|
1304 (secret (widget-get field :secret)))
|
155
|
1305 (when size
|
|
1306 (let ((begin (widget-field-start field))
|
|
1307 (end (widget-field-end field)))
|
|
1308 (cond ((< (- end begin) size)
|
|
1309 ;; Field too small.
|
|
1310 (save-excursion
|
|
1311 (goto-char end)
|
|
1312 (insert-char ?\ (- (+ begin size) end))))
|
|
1313 ((> (- end begin) size)
|
|
1314 ;; Field too large and
|
|
1315 (if (or (< (point) (+ begin size))
|
|
1316 (> (point) end))
|
|
1317 ;; Point is outside extra space.
|
|
1318 (setq begin (+ begin size))
|
|
1319 ;; Point is within the extra space.
|
|
1320 (setq begin (point)))
|
|
1321 (save-excursion
|
|
1322 (goto-char end)
|
|
1323 (while (and (eq (preceding-char) ?\ )
|
|
1324 (> (point) begin))
|
161
|
1325 (delete-backward-char 1)))))))
|
|
1326 (when secret
|
|
1327 (let ((begin (widget-field-start field))
|
|
1328 (end (widget-field-end field)))
|
|
1329 (when size
|
|
1330 (while (and (> end begin)
|
|
1331 (eq (char-after (1- end)) ?\ ))
|
|
1332 (setq end (1- end))))
|
|
1333 (while (< begin end)
|
|
1334 (let ((old (char-after begin)))
|
|
1335 (unless (eq old secret)
|
|
1336 (subst-char-in-region begin (1+ begin) old secret)
|
|
1337 (put-text-property begin (1+ begin) 'secret old))
|
|
1338 (setq begin (1+ begin)))))))
|
155
|
1339 (widget-apply field :notify field)))
|
|
1340 (error (debug "After Change"))))
|
28
|
1341
|
|
1342 ;;; Widget Functions
|
|
1343 ;;
|
|
1344 ;; These functions are used in the definition of multiple widgets.
|
|
1345
|
149
|
1346 (defun widget-parent-action (widget &optional event)
|
|
1347 "Tell :parent of WIDGET to handle the :action.
|
|
1348 Optional EVENT is the event that triggered the action."
|
|
1349 (widget-apply (widget-get widget :parent) :action event))
|
|
1350
|
28
|
1351 (defun widget-children-value-delete (widget)
|
|
1352 "Delete all :children and :buttons in WIDGET."
|
|
1353 (mapcar 'widget-delete (widget-get widget :children))
|
|
1354 (widget-put widget :children nil)
|
|
1355 (mapcar 'widget-delete (widget-get widget :buttons))
|
|
1356 (widget-put widget :buttons nil))
|
|
1357
|
149
|
1358 (defun widget-children-validate (widget)
|
|
1359 "All the :children must be valid."
|
|
1360 (let ((children (widget-get widget :children))
|
|
1361 child found)
|
|
1362 (while (and children (not found))
|
|
1363 (setq child (car children)
|
|
1364 children (cdr children)
|
|
1365 found (widget-apply child :validate)))
|
|
1366 found))
|
|
1367
|
28
|
1368 (defun widget-types-convert-widget (widget)
|
|
1369 "Convert :args as widget types in WIDGET."
|
|
1370 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
|
|
1371 widget)
|
|
1372
|
149
|
1373 (defun widget-value-convert-widget (widget)
|
|
1374 "Initialize :value from :args in WIDGET."
|
|
1375 (let ((args (widget-get widget :args)))
|
|
1376 (when args
|
|
1377 (widget-put widget :value (car args))
|
|
1378 ;; Don't convert :value here, as this is done in `widget-convert'.
|
|
1379 ;; (widget-put widget :value (widget-apply widget
|
|
1380 ;; :value-to-internal (car args)))
|
|
1381 (widget-put widget :args nil)))
|
|
1382 widget)
|
|
1383
|
|
1384 (defun widget-value-value-get (widget)
|
|
1385 "Return the :value property of WIDGET."
|
|
1386 (widget-get widget :value))
|
|
1387
|
28
|
1388 ;;; The `default' Widget.
|
|
1389
|
|
1390 (define-widget 'default nil
|
|
1391 "Basic widget other widgets are derived from."
|
|
1392 :value-to-internal (lambda (widget value) value)
|
|
1393 :value-to-external (lambda (widget value) value)
|
153
|
1394 :button-prefix 'widget-button-prefix
|
|
1395 :button-suffix 'widget-button-suffix
|
155
|
1396 :complete 'widget-default-complete
|
28
|
1397 :create 'widget-default-create
|
|
1398 :indent nil
|
|
1399 :offset 0
|
|
1400 :format-handler 'widget-default-format-handler
|
|
1401 :button-face-get 'widget-default-button-face-get
|
|
1402 :sample-face-get 'widget-default-sample-face-get
|
|
1403 :delete 'widget-default-delete
|
|
1404 :value-set 'widget-default-value-set
|
|
1405 :value-inline 'widget-default-value-inline
|
|
1406 :menu-tag-get 'widget-default-menu-tag-get
|
|
1407 :validate (lambda (widget) nil)
|
116
|
1408 :active 'widget-default-active
|
|
1409 :activate 'widget-specify-active
|
|
1410 :deactivate 'widget-default-deactivate
|
149
|
1411 :mouse-down-action (lambda (widget event) nil)
|
28
|
1412 :action 'widget-default-action
|
149
|
1413 :notify 'widget-default-notify
|
|
1414 :prompt-value 'widget-default-prompt-value)
|
28
|
1415
|
155
|
1416 (defun widget-default-complete (widget)
|
|
1417 "Call the value of the :complete-function property of WIDGET.
|
|
1418 If that does not exists, call the value of `widget-complete-field'."
|
|
1419 (let ((fun (widget-get widget :complete-function)))
|
|
1420 (call-interactively (or fun widget-complete-field))))
|
|
1421
|
28
|
1422 (defun widget-default-create (widget)
|
|
1423 "Create WIDGET at point in the current buffer."
|
|
1424 (widget-specify-insert
|
|
1425 (let ((from (point))
|
|
1426 button-begin button-end
|
|
1427 sample-begin sample-end
|
|
1428 doc-begin doc-end
|
|
1429 value-pos)
|
|
1430 (insert (widget-get widget :format))
|
|
1431 (goto-char from)
|
|
1432 ;; Parse escapes in format.
|
|
1433 (while (re-search-forward "%\\(.\\)" nil t)
|
|
1434 (let ((escape (aref (match-string 1) 0)))
|
|
1435 (replace-match "" t t)
|
|
1436 (cond ((eq escape ?%)
|
|
1437 (insert "%"))
|
|
1438 ((eq escape ?\[)
|
153
|
1439 (setq button-begin (point))
|
163
|
1440 (insert (widget-get-indirect widget :button-prefix)))
|
28
|
1441 ((eq escape ?\])
|
163
|
1442 (insert (widget-get-indirect widget :button-suffix))
|
28
|
1443 (setq button-end (point)))
|
|
1444 ((eq escape ?\{)
|
|
1445 (setq sample-begin (point)))
|
|
1446 ((eq escape ?\})
|
|
1447 (setq sample-end (point)))
|
|
1448 ((eq escape ?n)
|
|
1449 (when (widget-get widget :indent)
|
|
1450 (insert "\n")
|
|
1451 (insert-char ? (widget-get widget :indent))))
|
|
1452 ((eq escape ?t)
|
153
|
1453 (let ((glyph (widget-get widget :tag-glyph))
|
|
1454 (tag (widget-get widget :tag)))
|
|
1455 (cond (glyph
|
|
1456 (widget-glyph-insert widget (or tag "image") glyph))
|
|
1457 (tag
|
|
1458 (insert tag))
|
|
1459 (t
|
|
1460 (let ((standard-output (current-buffer)))
|
|
1461 (princ (widget-get widget :value)))))))
|
28
|
1462 ((eq escape ?d)
|
153
|
1463 (let ((doc (widget-get widget :doc)))
|
|
1464 (when doc
|
|
1465 (setq doc-begin (point))
|
|
1466 (insert doc)
|
|
1467 (while (eq (preceding-char) ?\n)
|
|
1468 (delete-backward-char 1))
|
|
1469 (insert "\n")
|
|
1470 (setq doc-end (point)))))
|
28
|
1471 ((eq escape ?v)
|
|
1472 (if (and button-begin (not button-end))
|
|
1473 (widget-apply widget :value-create)
|
|
1474 (setq value-pos (point))))
|
|
1475 (t
|
|
1476 (widget-apply widget :format-handler escape)))))
|
|
1477 ;; Specify button, sample, and doc, and insert value.
|
|
1478 (and button-begin button-end
|
|
1479 (widget-specify-button widget button-begin button-end))
|
|
1480 (and sample-begin sample-end
|
|
1481 (widget-specify-sample widget sample-begin sample-end))
|
|
1482 (and doc-begin doc-end
|
|
1483 (widget-specify-doc widget doc-begin doc-end))
|
|
1484 (when value-pos
|
|
1485 (goto-char value-pos)
|
|
1486 (widget-apply widget :value-create)))
|
|
1487 (let ((from (copy-marker (point-min)))
|
|
1488 (to (copy-marker (point-max))))
|
|
1489 (set-marker-insertion-type from t)
|
|
1490 (set-marker-insertion-type to nil)
|
|
1491 (widget-put widget :from from)
|
149
|
1492 (widget-put widget :to to)))
|
|
1493 (widget-clear-undo))
|
28
|
1494
|
|
1495 (defun widget-default-format-handler (widget escape)
|
|
1496 ;; We recognize the %h escape by default.
|
161
|
1497 (let* ((buttons (widget-get widget :buttons)))
|
28
|
1498 (cond ((eq escape ?h)
|
161
|
1499 (let* ((doc-property (widget-get widget :documentation-property))
|
|
1500 (doc-try (cond ((widget-get widget :doc))
|
|
1501 ((symbolp doc-property)
|
|
1502 (documentation-property
|
|
1503 (widget-get widget :value)
|
|
1504 doc-property))
|
|
1505 (t
|
|
1506 (funcall doc-property
|
|
1507 (widget-get widget :value)))))
|
|
1508 (doc-text (and (stringp doc-try)
|
|
1509 (> (length doc-try) 1)
|
163
|
1510 doc-try))
|
|
1511 (doc-indent (widget-get widget :documentation-indent)))
|
161
|
1512 (when doc-text
|
|
1513 (and (eq (preceding-char) ?\n)
|
|
1514 (widget-get widget :indent)
|
|
1515 (insert-char ? (widget-get widget :indent)))
|
|
1516 ;; The `*' in the beginning is redundant.
|
|
1517 (when (eq (aref doc-text 0) ?*)
|
|
1518 (setq doc-text (substring doc-text 1)))
|
|
1519 ;; Get rid of trailing newlines.
|
|
1520 (when (string-match "\n+\\'" doc-text)
|
|
1521 (setq doc-text (substring doc-text 0 (match-beginning 0))))
|
|
1522 (push (widget-create-child-and-convert
|
|
1523 widget 'documentation-string
|
163
|
1524 :indent (cond ((numberp doc-indent )
|
|
1525 doc-indent)
|
|
1526 ((null doc-indent)
|
|
1527 nil)
|
|
1528 (t 0))
|
161
|
1529 doc-text)
|
|
1530 buttons))))
|
28
|
1531 (t
|
|
1532 (error "Unknown escape `%c'" escape)))
|
|
1533 (widget-put widget :buttons buttons)))
|
|
1534
|
|
1535 (defun widget-default-button-face-get (widget)
|
|
1536 ;; Use :button-face or widget-button-face
|
165
|
1537 (or (widget-get widget :button-face)
|
|
1538 (let ((parent (widget-get widget :parent)))
|
|
1539 (if parent
|
|
1540 (widget-apply parent :button-face-get)
|
173
|
1541 widget-button-face))))
|
28
|
1542
|
|
1543 (defun widget-default-sample-face-get (widget)
|
|
1544 ;; Use :sample-face.
|
|
1545 (widget-get widget :sample-face))
|
|
1546
|
|
1547 (defun widget-default-delete (widget)
|
|
1548 ;; Remove widget from the buffer.
|
|
1549 (let ((from (widget-get widget :from))
|
|
1550 (to (widget-get widget :to))
|
155
|
1551 (inactive-overlay (widget-get widget :inactive))
|
|
1552 (button-overlay (widget-get widget :button-overlay))
|
173
|
1553 (sample-overlay (widget-get widget :sample-overlay))
|
|
1554 (doc-overlay (widget-get widget :doc-overlay))
|
163
|
1555 before-change-functions
|
155
|
1556 after-change-functions
|
|
1557 (inhibit-read-only t))
|
28
|
1558 (widget-apply widget :value-delete)
|
155
|
1559 (when inactive-overlay
|
|
1560 (delete-overlay inactive-overlay))
|
|
1561 (when button-overlay
|
|
1562 (delete-overlay button-overlay))
|
173
|
1563 (when sample-overlay
|
|
1564 (delete-overlay sample-overlay))
|
|
1565 (when doc-overlay
|
|
1566 (delete-overlay doc-overlay))
|
116
|
1567 (when (< from to)
|
|
1568 ;; Kludge: this doesn't need to be true for empty formats.
|
|
1569 (delete-region from to))
|
28
|
1570 (set-marker from nil)
|
149
|
1571 (set-marker to nil))
|
|
1572 (widget-clear-undo))
|
28
|
1573
|
|
1574 (defun widget-default-value-set (widget value)
|
|
1575 ;; Recreate widget with new value.
|
165
|
1576 (let* ((old-pos (point))
|
|
1577 (from (copy-marker (widget-get widget :from)))
|
|
1578 (to (copy-marker (widget-get widget :to)))
|
|
1579 (offset (if (and (<= from old-pos) (<= old-pos to))
|
|
1580 (if (>= old-pos (1- to))
|
|
1581 (- old-pos to 1)
|
|
1582 (- old-pos from)))))
|
|
1583 ;;??? Bug: this ought to insert the new value before deleting the old one,
|
|
1584 ;; so that markers on either side of the value automatically
|
|
1585 ;; stay on the same side. -- rms.
|
|
1586 (save-excursion
|
|
1587 (goto-char (widget-get widget :from))
|
|
1588 (widget-apply widget :delete)
|
|
1589 (widget-put widget :value value)
|
|
1590 (widget-apply widget :create))
|
|
1591 (if offset
|
|
1592 (if (< offset 0)
|
|
1593 (goto-char (+ (widget-get widget :to) offset 1))
|
|
1594 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
|
28
|
1595
|
|
1596 (defun widget-default-value-inline (widget)
|
|
1597 ;; Wrap value in a list unless it is inline.
|
|
1598 (if (widget-get widget :inline)
|
|
1599 (widget-value widget)
|
|
1600 (list (widget-value widget))))
|
|
1601
|
|
1602 (defun widget-default-menu-tag-get (widget)
|
|
1603 ;; Use tag or value for menus.
|
|
1604 (or (widget-get widget :menu-tag)
|
|
1605 (widget-get widget :tag)
|
|
1606 (widget-princ-to-string (widget-get widget :value))))
|
|
1607
|
116
|
1608 (defun widget-default-active (widget)
|
|
1609 "Return t iff this widget active (user modifiable)."
|
|
1610 (and (not (widget-get widget :inactive))
|
|
1611 (let ((parent (widget-get widget :parent)))
|
|
1612 (or (null parent)
|
|
1613 (widget-apply parent :active)))))
|
|
1614
|
|
1615 (defun widget-default-deactivate (widget)
|
|
1616 "Make WIDGET inactive for user modifications."
|
|
1617 (widget-specify-inactive widget
|
|
1618 (widget-get widget :from)
|
|
1619 (widget-get widget :to)))
|
|
1620
|
28
|
1621 (defun widget-default-action (widget &optional event)
|
|
1622 ;; Notify the parent when a widget change
|
|
1623 (let ((parent (widget-get widget :parent)))
|
|
1624 (when parent
|
|
1625 (widget-apply parent :notify widget event))))
|
|
1626
|
|
1627 (defun widget-default-notify (widget child &optional event)
|
|
1628 ;; Pass notification to parent.
|
|
1629 (widget-default-action widget event))
|
|
1630
|
149
|
1631 (defun widget-default-prompt-value (widget prompt value unbound)
|
|
1632 ;; Read an arbitrary value. Stolen from `set-variable'.
|
|
1633 ;; (let ((initial (if unbound
|
|
1634 ;; nil
|
|
1635 ;; ;; It would be nice if we could do a `(cons val 1)' here.
|
|
1636 ;; (prin1-to-string (custom-quote value))))))
|
|
1637 (eval-minibuffer prompt ))
|
|
1638
|
28
|
1639 ;;; The `item' Widget.
|
|
1640
|
|
1641 (define-widget 'item 'default
|
|
1642 "Constant items for inclusion in other widgets."
|
149
|
1643 :convert-widget 'widget-value-convert-widget
|
28
|
1644 :value-create 'widget-item-value-create
|
|
1645 :value-delete 'ignore
|
149
|
1646 :value-get 'widget-value-value-get
|
28
|
1647 :match 'widget-item-match
|
|
1648 :match-inline 'widget-item-match-inline
|
|
1649 :action 'widget-item-action
|
|
1650 :format "%t\n")
|
|
1651
|
|
1652 (defun widget-item-value-create (widget)
|
|
1653 ;; Insert the printed representation of the value.
|
|
1654 (let ((standard-output (current-buffer)))
|
|
1655 (princ (widget-get widget :value))))
|
|
1656
|
|
1657 (defun widget-item-match (widget value)
|
|
1658 ;; Match if the value is the same.
|
|
1659 (equal (widget-get widget :value) value))
|
|
1660
|
|
1661 (defun widget-item-match-inline (widget values)
|
|
1662 ;; Match if the value is the same.
|
|
1663 (let ((value (widget-get widget :value)))
|
|
1664 (and (listp value)
|
|
1665 (<= (length value) (length values))
|
155
|
1666 (let ((head (widget-sublist values 0 (length value))))
|
28
|
1667 (and (equal head value)
|
155
|
1668 (cons head (widget-sublist values (length value))))))))
|
|
1669
|
|
1670 (defun widget-sublist (list start &optional end)
|
|
1671 "Return the sublist of LIST from START to END.
|
|
1672 If END is omitted, it defaults to the length of LIST."
|
|
1673 (if (> start 0) (setq list (nthcdr start list)))
|
|
1674 (if end
|
|
1675 (if (<= end start)
|
|
1676 nil
|
|
1677 (setq list (copy-sequence list))
|
|
1678 (setcdr (nthcdr (- end start 1) list) nil)
|
|
1679 list)
|
|
1680 (copy-sequence list)))
|
28
|
1681
|
|
1682 (defun widget-item-action (widget &optional event)
|
|
1683 ;; Just notify itself.
|
|
1684 (widget-apply widget :notify widget event))
|
|
1685
|
|
1686 ;;; The `push-button' Widget.
|
|
1687
|
|
1688 (defcustom widget-push-button-gui t
|
|
1689 "If non nil, use GUI push buttons when available."
|
|
1690 :group 'widgets
|
|
1691 :type 'boolean)
|
|
1692
|
|
1693 ;; Cache already created GUI objects.
|
|
1694 (defvar widget-push-button-cache nil)
|
|
1695
|
153
|
1696 (defcustom widget-push-button-prefix "["
|
|
1697 "String used as prefix for buttons."
|
|
1698 :type 'string
|
|
1699 :group 'widget-button)
|
|
1700
|
|
1701 (defcustom widget-push-button-suffix "]"
|
|
1702 "String used as suffix for buttons."
|
|
1703 :type 'string
|
|
1704 :group 'widget-button)
|
|
1705
|
28
|
1706 (define-widget 'push-button 'item
|
|
1707 "A pushable button."
|
153
|
1708 :button-prefix ""
|
|
1709 :button-suffix ""
|
28
|
1710 :value-create 'widget-push-button-value-create
|
|
1711 :format "%[%v%]")
|
|
1712
|
|
1713 (defun widget-push-button-value-create (widget)
|
|
1714 ;; Insert text representing the `on' and `off' states.
|
|
1715 (let* ((tag (or (widget-get widget :tag)
|
|
1716 (widget-get widget :value)))
|
163
|
1717 (tag-glyph (widget-get widget :tag-glyph))
|
153
|
1718 (text (concat widget-push-button-prefix
|
|
1719 tag widget-push-button-suffix))
|
28
|
1720 (gui (cdr (assoc tag widget-push-button-cache))))
|
163
|
1721 (cond (tag-glyph
|
|
1722 (widget-glyph-insert widget text tag-glyph))
|
|
1723 ((and (fboundp 'make-gui-button)
|
28
|
1724 (fboundp 'make-glyph)
|
|
1725 widget-push-button-gui
|
|
1726 (fboundp 'device-on-window-system-p)
|
|
1727 (device-on-window-system-p)
|
|
1728 (string-match "XEmacs" emacs-version))
|
163
|
1729 (unless gui
|
|
1730 (setq gui (make-gui-button tag 'widget-gui-action widget))
|
|
1731 (push (cons tag gui) widget-push-button-cache))
|
|
1732 (widget-glyph-insert-glyph widget
|
|
1733 (make-glyph
|
|
1734 (list (nth 0 (aref gui 1))
|
|
1735 (vector 'string ':data text)))
|
|
1736 (make-glyph
|
|
1737 (list (nth 1 (aref gui 1))
|
|
1738 (vector 'string ':data text)))
|
|
1739 (make-glyph
|
|
1740 (list (nth 2 (aref gui 1))
|
|
1741 (vector 'string ':data text)))))
|
|
1742 (t
|
|
1743 (insert text)))))
|
28
|
1744
|
|
1745 (defun widget-gui-action (widget)
|
|
1746 "Apply :action for WIDGET."
|
116
|
1747 (widget-apply-action widget (this-command-keys)))
|
28
|
1748
|
|
1749 ;;; The `link' Widget.
|
|
1750
|
155
|
1751 (defcustom widget-link-prefix "["
|
153
|
1752 "String used as prefix for links."
|
|
1753 :type 'string
|
|
1754 :group 'widget-button)
|
|
1755
|
155
|
1756 (defcustom widget-link-suffix "]"
|
153
|
1757 "String used as suffix for links."
|
|
1758 :type 'string
|
|
1759 :group 'widget-button)
|
|
1760
|
28
|
1761 (define-widget 'link 'item
|
|
1762 "An embedded link."
|
153
|
1763 :button-prefix 'widget-link-prefix
|
|
1764 :button-suffix 'widget-link-suffix
|
30
|
1765 :help-echo "Follow the link."
|
153
|
1766 :format "%[%t%]")
|
28
|
1767
|
|
1768 ;;; The `info-link' Widget.
|
|
1769
|
|
1770 (define-widget 'info-link 'link
|
|
1771 "A link to an info file."
|
|
1772 :action 'widget-info-link-action)
|
|
1773
|
|
1774 (defun widget-info-link-action (widget &optional event)
|
|
1775 "Open the info node specified by WIDGET."
|
136
|
1776 (Info-goto-node (widget-value widget)))
|
28
|
1777
|
|
1778 ;;; The `url-link' Widget.
|
|
1779
|
|
1780 (define-widget 'url-link 'link
|
|
1781 "A link to an www page."
|
|
1782 :action 'widget-url-link-action)
|
|
1783
|
|
1784 (defun widget-url-link-action (widget &optional event)
|
|
1785 "Open the url specified by WIDGET."
|
|
1786 (require 'browse-url)
|
|
1787 (funcall browse-url-browser-function (widget-value widget)))
|
|
1788
|
173
|
1789 ;;; The `file-link' Widget.
|
|
1790
|
|
1791 (define-widget 'file-link 'link
|
|
1792 "A link to a file."
|
|
1793 :action 'widget-file-link-action)
|
|
1794
|
|
1795 (defun widget-file-link-action (widget &optional event)
|
|
1796 "Find the file specified by WIDGET."
|
|
1797 (find-file (widget-value widget)))
|
|
1798
|
|
1799 ;;; The `emacs-library-link' Widget.
|
|
1800
|
|
1801 (define-widget 'emacs-library-link 'link
|
|
1802 "A link to an Emacs Lisp library file."
|
|
1803 :action 'widget-emacs-library-link-action)
|
|
1804
|
|
1805 (defun widget-emacs-library-link-action (widget &optional event)
|
|
1806 "Find the Emacs Library file specified by WIDGET."
|
|
1807 (find-file (locate-library (widget-value widget))))
|
|
1808
|
|
1809 ;;; The `emacs-commentary-link' Widget.
|
|
1810
|
|
1811 (define-widget 'emacs-commentary-link 'link
|
|
1812 "A link to Commentary in an Emacs Lisp library file."
|
|
1813 :action 'widget-emacs-commentary-link-action)
|
|
1814
|
|
1815 (defun widget-emacs-commentary-link-action (widget &optional event)
|
|
1816 "Find the Commentary section of the Emacs file specified by WIDGET."
|
|
1817 (finder-commentary (widget-value widget)))
|
|
1818
|
28
|
1819 ;;; The `editable-field' Widget.
|
|
1820
|
|
1821 (define-widget 'editable-field 'default
|
|
1822 "An editable text field."
|
149
|
1823 :convert-widget 'widget-value-convert-widget
|
28
|
1824 :keymap widget-field-keymap
|
|
1825 :format "%v"
|
|
1826 :value ""
|
149
|
1827 :prompt-internal 'widget-field-prompt-internal
|
|
1828 :prompt-history 'widget-field-history
|
|
1829 :prompt-value 'widget-field-prompt-value
|
28
|
1830 :action 'widget-field-action
|
|
1831 :validate 'widget-field-validate
|
|
1832 :valid-regexp ""
|
|
1833 :error "No match"
|
|
1834 :value-create 'widget-field-value-create
|
|
1835 :value-delete 'widget-field-value-delete
|
|
1836 :value-get 'widget-field-value-get
|
|
1837 :match 'widget-field-match)
|
|
1838
|
149
|
1839 (defvar widget-field-history nil
|
|
1840 "History of field minibuffer edits.")
|
|
1841
|
|
1842 (defun widget-field-prompt-internal (widget prompt initial history)
|
|
1843 ;; Read string for WIDGET promptinhg with PROMPT.
|
|
1844 ;; INITIAL is the initial input and HISTORY is a symbol containing
|
|
1845 ;; the earlier input.
|
|
1846 (read-string prompt initial history))
|
|
1847
|
|
1848 (defun widget-field-prompt-value (widget prompt value unbound)
|
|
1849 ;; Prompt for a string.
|
|
1850 (let ((initial (if unbound
|
|
1851 nil
|
|
1852 (cons (widget-apply widget :value-to-internal
|
|
1853 value) 0)))
|
|
1854 (history (widget-get widget :prompt-history)))
|
|
1855 (let ((answer (widget-apply widget
|
|
1856 :prompt-internal prompt initial history)))
|
|
1857 (widget-apply widget :value-to-external answer))))
|
28
|
1858
|
165
|
1859 (defvar widget-edit-functions nil)
|
|
1860
|
28
|
1861 (defun widget-field-action (widget &optional event)
|
165
|
1862 ;; Move to next field.
|
|
1863 (widget-forward 1)
|
|
1864 (run-hook-with-args 'widget-edit-functions widget))
|
28
|
1865
|
|
1866 (defun widget-field-validate (widget)
|
|
1867 ;; Valid if the content matches `:valid-regexp'.
|
|
1868 (save-excursion
|
|
1869 (let ((value (widget-apply widget :value-get))
|
|
1870 (regexp (widget-get widget :valid-regexp)))
|
|
1871 (if (string-match regexp value)
|
|
1872 nil
|
|
1873 widget))))
|
|
1874
|
|
1875 (defun widget-field-value-create (widget)
|
|
1876 ;; Create an editable text field.
|
|
1877 (let ((size (widget-get widget :size))
|
|
1878 (value (widget-get widget :value))
|
155
|
1879 (from (point))
|
167
|
1880 ;; This is changed to a real overlay in `widget-setup'. We
|
|
1881 ;; need the end points to behave differently until
|
|
1882 ;; `widget-setup' is called.
|
155
|
1883 (overlay (cons (make-marker) (make-marker))))
|
|
1884 (widget-put widget :field-overlay overlay)
|
28
|
1885 (insert value)
|
|
1886 (and size
|
|
1887 (< (length value) size)
|
|
1888 (insert-char ?\ (- size (length value))))
|
|
1889 (unless (memq widget widget-field-list)
|
|
1890 (setq widget-field-new (cons widget widget-field-new)))
|
155
|
1891 (move-marker (cdr overlay) (point))
|
|
1892 (set-marker-insertion-type (cdr overlay) nil)
|
|
1893 (when (null size)
|
|
1894 (insert ?\n))
|
|
1895 (move-marker (car overlay) from)
|
|
1896 (set-marker-insertion-type (car overlay) t)))
|
28
|
1897
|
|
1898 (defun widget-field-value-delete (widget)
|
|
1899 ;; Remove the widget from the list of active editing fields.
|
|
1900 (setq widget-field-list (delq widget widget-field-list))
|
|
1901 ;; These are nil if the :format string doesn't contain `%v'.
|
155
|
1902 (let ((overlay (widget-get widget :field-overlay)))
|
|
1903 (when overlay
|
|
1904 (delete-overlay overlay))))
|
28
|
1905
|
|
1906 (defun widget-field-value-get (widget)
|
|
1907 ;; Return current text in editing field.
|
155
|
1908 (let ((from (widget-field-start widget))
|
|
1909 (to (widget-field-end widget))
|
|
1910 (buffer (widget-field-buffer widget))
|
28
|
1911 (size (widget-get widget :size))
|
|
1912 (secret (widget-get widget :secret))
|
|
1913 (old (current-buffer)))
|
|
1914 (if (and from to)
|
|
1915 (progn
|
155
|
1916 (set-buffer buffer)
|
28
|
1917 (while (and size
|
|
1918 (not (zerop size))
|
|
1919 (> to from)
|
|
1920 (eq (char-after (1- to)) ?\ ))
|
|
1921 (setq to (1- to)))
|
|
1922 (let ((result (buffer-substring-no-properties from to)))
|
|
1923 (when secret
|
|
1924 (let ((index 0))
|
|
1925 (while (< (+ from index) to)
|
|
1926 (aset result index
|
155
|
1927 (get-char-property (+ from index) 'secret))
|
28
|
1928 (setq index (1+ index)))))
|
|
1929 (set-buffer old)
|
|
1930 result))
|
|
1931 (widget-get widget :value))))
|
|
1932
|
|
1933 (defun widget-field-match (widget value)
|
|
1934 ;; Match any string.
|
|
1935 (stringp value))
|
|
1936
|
|
1937 ;;; The `text' Widget.
|
|
1938
|
|
1939 (define-widget 'text 'editable-field
|
|
1940 :keymap widget-text-keymap
|
|
1941 "A multiline text area.")
|
|
1942
|
|
1943 ;;; The `menu-choice' Widget.
|
|
1944
|
|
1945 (define-widget 'menu-choice 'default
|
|
1946 "A menu of options."
|
|
1947 :convert-widget 'widget-types-convert-widget
|
|
1948 :format "%[%t%]: %v"
|
|
1949 :case-fold t
|
|
1950 :tag "choice"
|
|
1951 :void '(item :format "invalid (%t)\n")
|
|
1952 :value-create 'widget-choice-value-create
|
|
1953 :value-delete 'widget-children-value-delete
|
|
1954 :value-get 'widget-choice-value-get
|
|
1955 :value-inline 'widget-choice-value-inline
|
149
|
1956 :mouse-down-action 'widget-choice-mouse-down-action
|
28
|
1957 :action 'widget-choice-action
|
|
1958 :error "Make a choice"
|
|
1959 :validate 'widget-choice-validate
|
|
1960 :match 'widget-choice-match
|
|
1961 :match-inline 'widget-choice-match-inline)
|
|
1962
|
|
1963 (defun widget-choice-value-create (widget)
|
|
1964 ;; Insert the first choice that matches the value.
|
|
1965 (let ((value (widget-get widget :value))
|
|
1966 (args (widget-get widget :args))
|
|
1967 current)
|
|
1968 (while args
|
|
1969 (setq current (car args)
|
|
1970 args (cdr args))
|
|
1971 (when (widget-apply current :match value)
|
|
1972 (widget-put widget :children (list (widget-create-child-value
|
|
1973 widget current value)))
|
|
1974 (widget-put widget :choice current)
|
|
1975 (setq args nil
|
|
1976 current nil)))
|
|
1977 (when current
|
|
1978 (let ((void (widget-get widget :void)))
|
|
1979 (widget-put widget :children (list (widget-create-child-and-convert
|
|
1980 widget void :value value)))
|
|
1981 (widget-put widget :choice void)))))
|
|
1982
|
|
1983 (defun widget-choice-value-get (widget)
|
|
1984 ;; Get value of the child widget.
|
|
1985 (widget-value (car (widget-get widget :children))))
|
|
1986
|
|
1987 (defun widget-choice-value-inline (widget)
|
|
1988 ;; Get value of the child widget.
|
|
1989 (widget-apply (car (widget-get widget :children)) :value-inline))
|
|
1990
|
149
|
1991 (defcustom widget-choice-toggle nil
|
|
1992 "If non-nil, a binary choice will just toggle between the values.
|
|
1993 Otherwise, the user will explicitly have to choose between the values
|
155
|
1994 when he invoked the menu."
|
149
|
1995 :type 'boolean
|
|
1996 :group 'widgets)
|
|
1997
|
|
1998 (defun widget-choice-mouse-down-action (widget &optional event)
|
|
1999 ;; Return non-nil if we need a menu.
|
|
2000 (let ((args (widget-get widget :args))
|
|
2001 (old (widget-get widget :choice)))
|
|
2002 (cond ((not window-system)
|
|
2003 ;; No place to pop up a menu.
|
|
2004 nil)
|
|
2005 ((not (or (fboundp 'x-popup-menu) (fboundp 'popup-menu)))
|
|
2006 ;; No way to pop up a menu.
|
|
2007 nil)
|
|
2008 ((< (length args) 2)
|
|
2009 ;; Empty or singleton list, just return the value.
|
|
2010 nil)
|
|
2011 ((> (length args) widget-menu-max-size)
|
|
2012 ;; Too long, prompt.
|
|
2013 nil)
|
|
2014 ((> (length args) 2)
|
|
2015 ;; Reasonable sized list, use menu.
|
|
2016 t)
|
|
2017 ((and widget-choice-toggle (memq old args))
|
|
2018 ;; We toggle.
|
|
2019 nil)
|
|
2020 (t
|
|
2021 ;; Ask which of the two.
|
|
2022 t))))
|
|
2023
|
28
|
2024 (defun widget-choice-action (widget &optional event)
|
|
2025 ;; Make a choice.
|
|
2026 (let ((args (widget-get widget :args))
|
|
2027 (old (widget-get widget :choice))
|
|
2028 (tag (widget-apply widget :menu-tag-get))
|
|
2029 (completion-ignore-case (widget-get widget :case-fold))
|
|
2030 current choices)
|
|
2031 ;; Remember old value.
|
|
2032 (if (and old (not (widget-apply widget :validate)))
|
|
2033 (let* ((external (widget-value widget))
|
|
2034 (internal (widget-apply old :value-to-internal external)))
|
|
2035 (widget-put old :value internal)))
|
|
2036 ;; Find new choice.
|
|
2037 (setq current
|
|
2038 (cond ((= (length args) 0)
|
|
2039 nil)
|
|
2040 ((= (length args) 1)
|
|
2041 (nth 0 args))
|
149
|
2042 ((and widget-choice-toggle
|
|
2043 (= (length args) 2)
|
28
|
2044 (memq old args))
|
|
2045 (if (eq old (nth 0 args))
|
|
2046 (nth 1 args)
|
|
2047 (nth 0 args)))
|
|
2048 (t
|
|
2049 (while args
|
|
2050 (setq current (car args)
|
|
2051 args (cdr args))
|
|
2052 (setq choices
|
|
2053 (cons (cons (widget-apply current :menu-tag-get)
|
|
2054 current)
|
|
2055 choices)))
|
|
2056 (widget-choose tag (reverse choices) event))))
|
|
2057 (when current
|
|
2058 (widget-value-set widget
|
|
2059 (widget-apply current :value-to-external
|
|
2060 (widget-get current :value)))
|
155
|
2061 (widget-setup)
|
165
|
2062 (widget-apply widget :notify widget event)))
|
173
|
2063 (run-hook-with-args 'widget-edit-functions widget))
|
28
|
2064
|
|
2065 (defun widget-choice-validate (widget)
|
|
2066 ;; Valid if we have made a valid choice.
|
|
2067 (let ((void (widget-get widget :void))
|
|
2068 (choice (widget-get widget :choice))
|
|
2069 (child (car (widget-get widget :children))))
|
|
2070 (if (eq void choice)
|
|
2071 widget
|
|
2072 (widget-apply child :validate))))
|
|
2073
|
|
2074 (defun widget-choice-match (widget value)
|
|
2075 ;; Matches if one of the choices matches.
|
|
2076 (let ((args (widget-get widget :args))
|
|
2077 current found)
|
|
2078 (while (and args (not found))
|
|
2079 (setq current (car args)
|
|
2080 args (cdr args)
|
|
2081 found (widget-apply current :match value)))
|
|
2082 found))
|
|
2083
|
|
2084 (defun widget-choice-match-inline (widget values)
|
|
2085 ;; Matches if one of the choices matches.
|
|
2086 (let ((args (widget-get widget :args))
|
|
2087 current found)
|
|
2088 (while (and args (null found))
|
|
2089 (setq current (car args)
|
|
2090 args (cdr args)
|
|
2091 found (widget-match-inline current values)))
|
|
2092 found))
|
|
2093
|
|
2094 ;;; The `toggle' Widget.
|
|
2095
|
|
2096 (define-widget 'toggle 'item
|
|
2097 "Toggle between two states."
|
|
2098 :format "%[%v%]\n"
|
|
2099 :value-create 'widget-toggle-value-create
|
|
2100 :action 'widget-toggle-action
|
|
2101 :match (lambda (widget value) t)
|
|
2102 :on "on"
|
|
2103 :off "off")
|
|
2104
|
|
2105 (defun widget-toggle-value-create (widget)
|
|
2106 ;; Insert text representing the `on' and `off' states.
|
|
2107 (if (widget-value widget)
|
|
2108 (widget-glyph-insert widget
|
|
2109 (widget-get widget :on)
|
|
2110 (widget-get widget :on-glyph))
|
|
2111 (widget-glyph-insert widget
|
|
2112 (widget-get widget :off)
|
|
2113 (widget-get widget :off-glyph))))
|
|
2114
|
|
2115 (defun widget-toggle-action (widget &optional event)
|
|
2116 ;; Toggle value.
|
|
2117 (widget-value-set widget (not (widget-value widget)))
|
165
|
2118 (widget-apply widget :notify widget event)
|
173
|
2119 (run-hook-with-args 'widget-edit-functions widget))
|
149
|
2120
|
28
|
2121 ;;; The `checkbox' Widget.
|
|
2122
|
|
2123 (define-widget 'checkbox 'toggle
|
|
2124 "A checkbox toggle."
|
153
|
2125 :button-suffix ""
|
|
2126 :button-prefix ""
|
28
|
2127 :format "%[%v%]"
|
|
2128 :on "[X]"
|
|
2129 :on-glyph "check1"
|
|
2130 :off "[ ]"
|
116
|
2131 :off-glyph "check0"
|
|
2132 :action 'widget-checkbox-action)
|
|
2133
|
|
2134 (defun widget-checkbox-action (widget &optional event)
|
|
2135 "Toggle checkbox, notify parent, and set active state of sibling."
|
|
2136 (widget-toggle-action widget event)
|
|
2137 (let ((sibling (widget-get-sibling widget)))
|
|
2138 (when sibling
|
|
2139 (if (widget-value widget)
|
|
2140 (widget-apply sibling :activate)
|
|
2141 (widget-apply sibling :deactivate)))))
|
28
|
2142
|
|
2143 ;;; The `checklist' Widget.
|
|
2144
|
|
2145 (define-widget 'checklist 'default
|
|
2146 "A multiple choice widget."
|
|
2147 :convert-widget 'widget-types-convert-widget
|
|
2148 :format "%v"
|
|
2149 :offset 4
|
|
2150 :entry-format "%b %v"
|
|
2151 :menu-tag "checklist"
|
|
2152 :greedy nil
|
|
2153 :value-create 'widget-checklist-value-create
|
|
2154 :value-delete 'widget-children-value-delete
|
|
2155 :value-get 'widget-checklist-value-get
|
|
2156 :validate 'widget-checklist-validate
|
|
2157 :match 'widget-checklist-match
|
|
2158 :match-inline 'widget-checklist-match-inline)
|
|
2159
|
|
2160 (defun widget-checklist-value-create (widget)
|
|
2161 ;; Insert all values
|
|
2162 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
|
|
2163 (args (widget-get widget :args)))
|
|
2164 (while args
|
|
2165 (widget-checklist-add-item widget (car args) (assq (car args) alist))
|
|
2166 (setq args (cdr args)))
|
|
2167 (widget-put widget :children (nreverse (widget-get widget :children)))))
|
|
2168
|
|
2169 (defun widget-checklist-add-item (widget type chosen)
|
|
2170 ;; Create checklist item in WIDGET of type TYPE.
|
|
2171 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
|
|
2172 (and (eq (preceding-char) ?\n)
|
|
2173 (widget-get widget :indent)
|
|
2174 (insert-char ? (widget-get widget :indent)))
|
|
2175 (widget-specify-insert
|
|
2176 (let* ((children (widget-get widget :children))
|
|
2177 (buttons (widget-get widget :buttons))
|
30
|
2178 (button-args (or (widget-get type :sibling-args)
|
|
2179 (widget-get widget :button-args)))
|
28
|
2180 (from (point))
|
|
2181 child button)
|
|
2182 (insert (widget-get widget :entry-format))
|
|
2183 (goto-char from)
|
|
2184 ;; Parse % escapes in format.
|
|
2185 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
2186 (let ((escape (aref (match-string 1) 0)))
|
|
2187 (replace-match "" t t)
|
|
2188 (cond ((eq escape ?%)
|
|
2189 (insert "%"))
|
|
2190 ((eq escape ?b)
|
30
|
2191 (setq button (apply 'widget-create-child-and-convert
|
|
2192 widget 'checkbox
|
|
2193 :value (not (null chosen))
|
|
2194 button-args)))
|
28
|
2195 ((eq escape ?v)
|
|
2196 (setq child
|
|
2197 (cond ((not chosen)
|
116
|
2198 (let ((child (widget-create-child widget type)))
|
|
2199 (widget-apply child :deactivate)
|
|
2200 child))
|
28
|
2201 ((widget-get type :inline)
|
|
2202 (widget-create-child-value
|
|
2203 widget type (cdr chosen)))
|
|
2204 (t
|
|
2205 (widget-create-child-value
|
|
2206 widget type (car (cdr chosen)))))))
|
|
2207 (t
|
|
2208 (error "Unknown escape `%c'" escape)))))
|
|
2209 ;; Update properties.
|
|
2210 (and button child (widget-put child :button button))
|
|
2211 (and button (widget-put widget :buttons (cons button buttons)))
|
|
2212 (and child (widget-put widget :children (cons child children))))))
|
|
2213
|
|
2214 (defun widget-checklist-match (widget values)
|
|
2215 ;; All values must match a type in the checklist.
|
|
2216 (and (listp values)
|
|
2217 (null (cdr (widget-checklist-match-inline widget values)))))
|
|
2218
|
|
2219 (defun widget-checklist-match-inline (widget values)
|
|
2220 ;; Find the values which match a type in the checklist.
|
|
2221 (let ((greedy (widget-get widget :greedy))
|
149
|
2222 (args (copy-sequence (widget-get widget :args)))
|
28
|
2223 found rest)
|
|
2224 (while values
|
|
2225 (let ((answer (widget-checklist-match-up args values)))
|
|
2226 (cond (answer
|
|
2227 (let ((vals (widget-match-inline answer values)))
|
|
2228 (setq found (append found (car vals))
|
|
2229 values (cdr vals)
|
|
2230 args (delq answer args))))
|
|
2231 (greedy
|
|
2232 (setq rest (append rest (list (car values)))
|
|
2233 values (cdr values)))
|
|
2234 (t
|
|
2235 (setq rest (append rest values)
|
|
2236 values nil)))))
|
|
2237 (cons found rest)))
|
|
2238
|
|
2239 (defun widget-checklist-match-find (widget vals)
|
|
2240 ;; Find the vals which match a type in the checklist.
|
|
2241 ;; Return an alist of (TYPE MATCH).
|
|
2242 (let ((greedy (widget-get widget :greedy))
|
149
|
2243 (args (copy-sequence (widget-get widget :args)))
|
28
|
2244 found)
|
|
2245 (while vals
|
|
2246 (let ((answer (widget-checklist-match-up args vals)))
|
|
2247 (cond (answer
|
|
2248 (let ((match (widget-match-inline answer vals)))
|
|
2249 (setq found (cons (cons answer (car match)) found)
|
|
2250 vals (cdr match)
|
|
2251 args (delq answer args))))
|
|
2252 (greedy
|
|
2253 (setq vals (cdr vals)))
|
|
2254 (t
|
|
2255 (setq vals nil)))))
|
|
2256 found))
|
|
2257
|
|
2258 (defun widget-checklist-match-up (args vals)
|
|
2259 ;; Rerturn the first type from ARGS that matches VALS.
|
|
2260 (let (current found)
|
|
2261 (while (and args (null found))
|
|
2262 (setq current (car args)
|
|
2263 args (cdr args)
|
|
2264 found (widget-match-inline current vals)))
|
|
2265 (if found
|
|
2266 current
|
|
2267 nil)))
|
|
2268
|
|
2269 (defun widget-checklist-value-get (widget)
|
|
2270 ;; The values of all selected items.
|
|
2271 (let ((children (widget-get widget :children))
|
|
2272 child result)
|
|
2273 (while children
|
|
2274 (setq child (car children)
|
|
2275 children (cdr children))
|
|
2276 (if (widget-value (widget-get child :button))
|
|
2277 (setq result (append result (widget-apply child :value-inline)))))
|
|
2278 result))
|
|
2279
|
|
2280 (defun widget-checklist-validate (widget)
|
|
2281 ;; Ticked chilren must be valid.
|
|
2282 (let ((children (widget-get widget :children))
|
|
2283 child button found)
|
|
2284 (while (and children (not found))
|
|
2285 (setq child (car children)
|
|
2286 children (cdr children)
|
|
2287 button (widget-get child :button)
|
|
2288 found (and (widget-value button)
|
|
2289 (widget-apply child :validate))))
|
|
2290 found))
|
|
2291
|
|
2292 ;;; The `option' Widget
|
|
2293
|
|
2294 (define-widget 'option 'checklist
|
|
2295 "An widget with an optional item."
|
|
2296 :inline t)
|
|
2297
|
|
2298 ;;; The `choice-item' Widget.
|
|
2299
|
|
2300 (define-widget 'choice-item 'item
|
|
2301 "Button items that delegate action events to their parents."
|
149
|
2302 :action 'widget-parent-action
|
28
|
2303 :format "%[%t%] \n")
|
|
2304
|
|
2305 ;;; The `radio-button' Widget.
|
|
2306
|
|
2307 (define-widget 'radio-button 'toggle
|
|
2308 "A radio button for use in the `radio' widget."
|
|
2309 :notify 'widget-radio-button-notify
|
|
2310 :format "%[%v%]"
|
153
|
2311 :button-suffix ""
|
|
2312 :button-prefix ""
|
28
|
2313 :on "(*)"
|
|
2314 :on-glyph "radio1"
|
|
2315 :off "( )"
|
|
2316 :off-glyph "radio0")
|
|
2317
|
|
2318 (defun widget-radio-button-notify (widget child &optional event)
|
|
2319 ;; Tell daddy.
|
|
2320 (widget-apply (widget-get widget :parent) :action widget event))
|
|
2321
|
|
2322 ;;; The `radio-button-choice' Widget.
|
|
2323
|
|
2324 (define-widget 'radio-button-choice 'default
|
|
2325 "Select one of multiple options."
|
|
2326 :convert-widget 'widget-types-convert-widget
|
|
2327 :offset 4
|
|
2328 :format "%v"
|
|
2329 :entry-format "%b %v"
|
|
2330 :menu-tag "radio"
|
|
2331 :value-create 'widget-radio-value-create
|
|
2332 :value-delete 'widget-children-value-delete
|
|
2333 :value-get 'widget-radio-value-get
|
|
2334 :value-inline 'widget-radio-value-inline
|
|
2335 :value-set 'widget-radio-value-set
|
|
2336 :error "You must push one of the buttons"
|
|
2337 :validate 'widget-radio-validate
|
|
2338 :match 'widget-choice-match
|
|
2339 :match-inline 'widget-choice-match-inline
|
|
2340 :action 'widget-radio-action)
|
|
2341
|
|
2342 (defun widget-radio-value-create (widget)
|
|
2343 ;; Insert all values
|
|
2344 (let ((args (widget-get widget :args))
|
|
2345 arg)
|
|
2346 (while args
|
|
2347 (setq arg (car args)
|
|
2348 args (cdr args))
|
|
2349 (widget-radio-add-item widget arg))))
|
|
2350
|
|
2351 (defun widget-radio-add-item (widget type)
|
|
2352 "Add to radio widget WIDGET a new radio button item of type TYPE."
|
|
2353 ;; (setq type (widget-convert type))
|
|
2354 (and (eq (preceding-char) ?\n)
|
|
2355 (widget-get widget :indent)
|
|
2356 (insert-char ? (widget-get widget :indent)))
|
|
2357 (widget-specify-insert
|
|
2358 (let* ((value (widget-get widget :value))
|
|
2359 (children (widget-get widget :children))
|
|
2360 (buttons (widget-get widget :buttons))
|
30
|
2361 (button-args (or (widget-get type :sibling-args)
|
|
2362 (widget-get widget :button-args)))
|
28
|
2363 (from (point))
|
|
2364 (chosen (and (null (widget-get widget :choice))
|
|
2365 (widget-apply type :match value)))
|
|
2366 child button)
|
|
2367 (insert (widget-get widget :entry-format))
|
|
2368 (goto-char from)
|
|
2369 ;; Parse % escapes in format.
|
|
2370 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
2371 (let ((escape (aref (match-string 1) 0)))
|
|
2372 (replace-match "" t t)
|
|
2373 (cond ((eq escape ?%)
|
|
2374 (insert "%"))
|
|
2375 ((eq escape ?b)
|
30
|
2376 (setq button (apply 'widget-create-child-and-convert
|
|
2377 widget 'radio-button
|
|
2378 :value (not (null chosen))
|
|
2379 button-args)))
|
28
|
2380 ((eq escape ?v)
|
|
2381 (setq child (if chosen
|
|
2382 (widget-create-child-value
|
|
2383 widget type value)
|
116
|
2384 (widget-create-child widget type)))
|
|
2385 (unless chosen
|
|
2386 (widget-apply child :deactivate)))
|
28
|
2387 (t
|
|
2388 (error "Unknown escape `%c'" escape)))))
|
|
2389 ;; Update properties.
|
|
2390 (when chosen
|
|
2391 (widget-put widget :choice type))
|
|
2392 (when button
|
|
2393 (widget-put child :button button)
|
|
2394 (widget-put widget :buttons (nconc buttons (list button))))
|
|
2395 (when child
|
|
2396 (widget-put widget :children (nconc children (list child))))
|
|
2397 child)))
|
|
2398
|
|
2399 (defun widget-radio-value-get (widget)
|
|
2400 ;; Get value of the child widget.
|
|
2401 (let ((chosen (widget-radio-chosen widget)))
|
|
2402 (and chosen (widget-value chosen))))
|
|
2403
|
|
2404 (defun widget-radio-chosen (widget)
|
|
2405 "Return the widget representing the chosen radio button."
|
|
2406 (let ((children (widget-get widget :children))
|
|
2407 current found)
|
|
2408 (while children
|
|
2409 (setq current (car children)
|
|
2410 children (cdr children))
|
|
2411 (let* ((button (widget-get current :button))
|
|
2412 (value (widget-apply button :value-get)))
|
|
2413 (when value
|
|
2414 (setq found current
|
|
2415 children nil))))
|
|
2416 found))
|
|
2417
|
|
2418 (defun widget-radio-value-inline (widget)
|
|
2419 ;; Get value of the child widget.
|
|
2420 (let ((children (widget-get widget :children))
|
|
2421 current found)
|
|
2422 (while children
|
|
2423 (setq current (car children)
|
|
2424 children (cdr children))
|
|
2425 (let* ((button (widget-get current :button))
|
|
2426 (value (widget-apply button :value-get)))
|
|
2427 (when value
|
|
2428 (setq found (widget-apply current :value-inline)
|
|
2429 children nil))))
|
|
2430 found))
|
|
2431
|
|
2432 (defun widget-radio-value-set (widget value)
|
|
2433 ;; We can't just delete and recreate a radio widget, since children
|
|
2434 ;; can be added after the original creation and won't be recreated
|
|
2435 ;; by `:create'.
|
|
2436 (let ((children (widget-get widget :children))
|
|
2437 current found)
|
|
2438 (while children
|
|
2439 (setq current (car children)
|
|
2440 children (cdr children))
|
|
2441 (let* ((button (widget-get current :button))
|
|
2442 (match (and (not found)
|
|
2443 (widget-apply current :match value))))
|
|
2444 (widget-value-set button match)
|
|
2445 (if match
|
116
|
2446 (progn
|
|
2447 (widget-value-set current value)
|
|
2448 (widget-apply current :activate))
|
|
2449 (widget-apply current :deactivate))
|
28
|
2450 (setq found (or found match))))))
|
|
2451
|
|
2452 (defun widget-radio-validate (widget)
|
|
2453 ;; Valid if we have made a valid choice.
|
|
2454 (let ((children (widget-get widget :children))
|
|
2455 current found button)
|
|
2456 (while (and children (not found))
|
|
2457 (setq current (car children)
|
|
2458 children (cdr children)
|
|
2459 button (widget-get current :button)
|
|
2460 found (widget-apply button :value-get)))
|
|
2461 (if found
|
|
2462 (widget-apply current :validate)
|
|
2463 widget)))
|
|
2464
|
|
2465 (defun widget-radio-action (widget child event)
|
|
2466 ;; Check if a radio button was pressed.
|
|
2467 (let ((children (widget-get widget :children))
|
|
2468 (buttons (widget-get widget :buttons))
|
|
2469 current)
|
|
2470 (when (memq child buttons)
|
|
2471 (while children
|
|
2472 (setq current (car children)
|
|
2473 children (cdr children))
|
|
2474 (let* ((button (widget-get current :button)))
|
|
2475 (cond ((eq child button)
|
116
|
2476 (widget-value-set button t)
|
|
2477 (widget-apply current :activate))
|
28
|
2478 ((widget-value button)
|
116
|
2479 (widget-value-set button nil)
|
|
2480 (widget-apply current :deactivate)))))))
|
28
|
2481 ;; Pass notification to parent.
|
|
2482 (widget-apply widget :notify child event))
|
|
2483
|
|
2484 ;;; The `insert-button' Widget.
|
|
2485
|
|
2486 (define-widget 'insert-button 'push-button
|
|
2487 "An insert button for the `editable-list' widget."
|
|
2488 :tag "INS"
|
30
|
2489 :help-echo "Insert a new item into the list at this position."
|
28
|
2490 :action 'widget-insert-button-action)
|
|
2491
|
|
2492 (defun widget-insert-button-action (widget &optional event)
|
|
2493 ;; Ask the parent to insert a new item.
|
|
2494 (widget-apply (widget-get widget :parent)
|
|
2495 :insert-before (widget-get widget :widget)))
|
|
2496
|
|
2497 ;;; The `delete-button' Widget.
|
|
2498
|
|
2499 (define-widget 'delete-button 'push-button
|
|
2500 "A delete button for the `editable-list' widget."
|
|
2501 :tag "DEL"
|
30
|
2502 :help-echo "Delete this item from the list."
|
28
|
2503 :action 'widget-delete-button-action)
|
|
2504
|
|
2505 (defun widget-delete-button-action (widget &optional event)
|
|
2506 ;; Ask the parent to insert a new item.
|
|
2507 (widget-apply (widget-get widget :parent)
|
|
2508 :delete-at (widget-get widget :widget)))
|
|
2509
|
|
2510 ;;; The `editable-list' Widget.
|
|
2511
|
|
2512 (defcustom widget-editable-list-gui nil
|
|
2513 "If non nil, use GUI push-buttons in editable list when available."
|
|
2514 :type 'boolean
|
|
2515 :group 'widgets)
|
|
2516
|
|
2517 (define-widget 'editable-list 'default
|
|
2518 "A variable list of widgets of the same type."
|
|
2519 :convert-widget 'widget-types-convert-widget
|
|
2520 :offset 12
|
|
2521 :format "%v%i\n"
|
|
2522 :format-handler 'widget-editable-list-format-handler
|
|
2523 :entry-format "%i %d %v"
|
|
2524 :menu-tag "editable-list"
|
|
2525 :value-create 'widget-editable-list-value-create
|
|
2526 :value-delete 'widget-children-value-delete
|
|
2527 :value-get 'widget-editable-list-value-get
|
149
|
2528 :validate 'widget-children-validate
|
28
|
2529 :match 'widget-editable-list-match
|
|
2530 :match-inline 'widget-editable-list-match-inline
|
|
2531 :insert-before 'widget-editable-list-insert-before
|
|
2532 :delete-at 'widget-editable-list-delete-at)
|
|
2533
|
|
2534 (defun widget-editable-list-format-handler (widget escape)
|
|
2535 ;; We recognize the insert button.
|
|
2536 (let ((widget-push-button-gui widget-editable-list-gui))
|
|
2537 (cond ((eq escape ?i)
|
|
2538 (and (widget-get widget :indent)
|
|
2539 (insert-char ? (widget-get widget :indent)))
|
30
|
2540 (apply 'widget-create-child-and-convert
|
|
2541 widget 'insert-button
|
|
2542 (widget-get widget :append-button-args)))
|
28
|
2543 (t
|
|
2544 (widget-default-format-handler widget escape)))))
|
|
2545
|
|
2546 (defun widget-editable-list-value-create (widget)
|
|
2547 ;; Insert all values
|
|
2548 (let* ((value (widget-get widget :value))
|
|
2549 (type (nth 0 (widget-get widget :args)))
|
|
2550 (inlinep (widget-get type :inline))
|
|
2551 children)
|
|
2552 (widget-put widget :value-pos (copy-marker (point)))
|
|
2553 (set-marker-insertion-type (widget-get widget :value-pos) t)
|
|
2554 (while value
|
|
2555 (let ((answer (widget-match-inline type value)))
|
|
2556 (if answer
|
|
2557 (setq children (cons (widget-editable-list-entry-create
|
|
2558 widget
|
|
2559 (if inlinep
|
|
2560 (car answer)
|
|
2561 (car (car answer)))
|
|
2562 t)
|
|
2563 children)
|
|
2564 value (cdr answer))
|
|
2565 (setq value nil))))
|
|
2566 (widget-put widget :children (nreverse children))))
|
|
2567
|
|
2568 (defun widget-editable-list-value-get (widget)
|
|
2569 ;; Get value of the child widget.
|
|
2570 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
|
|
2571 (widget-get widget :children))))
|
|
2572
|
|
2573 (defun widget-editable-list-match (widget value)
|
|
2574 ;; Value must be a list and all the members must match the type.
|
|
2575 (and (listp value)
|
|
2576 (null (cdr (widget-editable-list-match-inline widget value)))))
|
|
2577
|
|
2578 (defun widget-editable-list-match-inline (widget value)
|
|
2579 (let ((type (nth 0 (widget-get widget :args)))
|
|
2580 (ok t)
|
|
2581 found)
|
|
2582 (while (and value ok)
|
|
2583 (let ((answer (widget-match-inline type value)))
|
|
2584 (if answer
|
|
2585 (setq found (append found (car answer))
|
|
2586 value (cdr answer))
|
|
2587 (setq ok nil))))
|
|
2588 (cons found value)))
|
|
2589
|
|
2590 (defun widget-editable-list-insert-before (widget before)
|
|
2591 ;; Insert a new child in the list of children.
|
|
2592 (save-excursion
|
|
2593 (let ((children (widget-get widget :children))
|
|
2594 (inhibit-read-only t)
|
163
|
2595 before-change-functions
|
28
|
2596 after-change-functions)
|
|
2597 (cond (before
|
|
2598 (goto-char (widget-get before :entry-from)))
|
|
2599 (t
|
|
2600 (goto-char (widget-get widget :value-pos))))
|
|
2601 (let ((child (widget-editable-list-entry-create
|
|
2602 widget nil nil)))
|
|
2603 (when (< (widget-get child :entry-from) (widget-get widget :from))
|
|
2604 (set-marker (widget-get widget :from)
|
|
2605 (widget-get child :entry-from)))
|
|
2606 (if (eq (car children) before)
|
|
2607 (widget-put widget :children (cons child children))
|
|
2608 (while (not (eq (car (cdr children)) before))
|
|
2609 (setq children (cdr children)))
|
|
2610 (setcdr children (cons child (cdr children)))))))
|
|
2611 (widget-setup)
|
155
|
2612 (widget-apply widget :notify widget))
|
28
|
2613
|
|
2614 (defun widget-editable-list-delete-at (widget child)
|
|
2615 ;; Delete child from list of children.
|
|
2616 (save-excursion
|
149
|
2617 (let ((buttons (copy-sequence (widget-get widget :buttons)))
|
28
|
2618 button
|
|
2619 (inhibit-read-only t)
|
163
|
2620 before-change-functions
|
28
|
2621 after-change-functions)
|
|
2622 (while buttons
|
|
2623 (setq button (car buttons)
|
|
2624 buttons (cdr buttons))
|
|
2625 (when (eq (widget-get button :widget) child)
|
|
2626 (widget-put widget
|
|
2627 :buttons (delq button (widget-get widget :buttons)))
|
|
2628 (widget-delete button))))
|
|
2629 (let ((entry-from (widget-get child :entry-from))
|
|
2630 (entry-to (widget-get child :entry-to))
|
|
2631 (inhibit-read-only t)
|
163
|
2632 before-change-functions
|
28
|
2633 after-change-functions)
|
|
2634 (widget-delete child)
|
|
2635 (delete-region entry-from entry-to)
|
|
2636 (set-marker entry-from nil)
|
|
2637 (set-marker entry-to nil))
|
|
2638 (widget-put widget :children (delq child (widget-get widget :children))))
|
|
2639 (widget-setup)
|
|
2640 (widget-apply widget :notify widget))
|
|
2641
|
|
2642 (defun widget-editable-list-entry-create (widget value conv)
|
|
2643 ;; Create a new entry to the list.
|
|
2644 (let ((type (nth 0 (widget-get widget :args)))
|
|
2645 (widget-push-button-gui widget-editable-list-gui)
|
|
2646 child delete insert)
|
|
2647 (widget-specify-insert
|
|
2648 (save-excursion
|
|
2649 (and (widget-get widget :indent)
|
|
2650 (insert-char ? (widget-get widget :indent)))
|
|
2651 (insert (widget-get widget :entry-format)))
|
|
2652 ;; Parse % escapes in format.
|
|
2653 (while (re-search-forward "%\\(.\\)" nil t)
|
|
2654 (let ((escape (aref (match-string 1) 0)))
|
|
2655 (replace-match "" t t)
|
|
2656 (cond ((eq escape ?%)
|
|
2657 (insert "%"))
|
|
2658 ((eq escape ?i)
|
30
|
2659 (setq insert (apply 'widget-create-child-and-convert
|
|
2660 widget 'insert-button
|
|
2661 (widget-get widget :insert-button-args))))
|
28
|
2662 ((eq escape ?d)
|
30
|
2663 (setq delete (apply 'widget-create-child-and-convert
|
|
2664 widget 'delete-button
|
|
2665 (widget-get widget :delete-button-args))))
|
28
|
2666 ((eq escape ?v)
|
|
2667 (if conv
|
|
2668 (setq child (widget-create-child-value
|
|
2669 widget type value))
|
|
2670 (setq child (widget-create-child widget type))))
|
|
2671 (t
|
|
2672 (error "Unknown escape `%c'" escape)))))
|
|
2673 (widget-put widget
|
|
2674 :buttons (cons delete
|
|
2675 (cons insert
|
|
2676 (widget-get widget :buttons))))
|
|
2677 (let ((entry-from (copy-marker (point-min)))
|
|
2678 (entry-to (copy-marker (point-max))))
|
|
2679 (set-marker-insertion-type entry-from t)
|
|
2680 (set-marker-insertion-type entry-to nil)
|
|
2681 (widget-put child :entry-from entry-from)
|
|
2682 (widget-put child :entry-to entry-to)))
|
|
2683 (widget-put insert :widget child)
|
|
2684 (widget-put delete :widget child)
|
|
2685 child))
|
|
2686
|
|
2687 ;;; The `group' Widget.
|
|
2688
|
|
2689 (define-widget 'group 'default
|
|
2690 "A widget which group other widgets inside."
|
|
2691 :convert-widget 'widget-types-convert-widget
|
|
2692 :format "%v"
|
|
2693 :value-create 'widget-group-value-create
|
|
2694 :value-delete 'widget-children-value-delete
|
|
2695 :value-get 'widget-editable-list-value-get
|
149
|
2696 :validate 'widget-children-validate
|
28
|
2697 :match 'widget-group-match
|
|
2698 :match-inline 'widget-group-match-inline)
|
|
2699
|
|
2700 (defun widget-group-value-create (widget)
|
|
2701 ;; Create each component.
|
|
2702 (let ((args (widget-get widget :args))
|
|
2703 (value (widget-get widget :value))
|
|
2704 arg answer children)
|
|
2705 (while args
|
|
2706 (setq arg (car args)
|
|
2707 args (cdr args)
|
|
2708 answer (widget-match-inline arg value)
|
|
2709 value (cdr answer))
|
|
2710 (and (eq (preceding-char) ?\n)
|
|
2711 (widget-get widget :indent)
|
|
2712 (insert-char ? (widget-get widget :indent)))
|
|
2713 (push (cond ((null answer)
|
|
2714 (widget-create-child widget arg))
|
|
2715 ((widget-get arg :inline)
|
|
2716 (widget-create-child-value widget arg (car answer)))
|
|
2717 (t
|
|
2718 (widget-create-child-value widget arg (car (car answer)))))
|
|
2719 children))
|
|
2720 (widget-put widget :children (nreverse children))))
|
|
2721
|
|
2722 (defun widget-group-match (widget values)
|
|
2723 ;; Match if the components match.
|
|
2724 (and (listp values)
|
|
2725 (let ((match (widget-group-match-inline widget values)))
|
|
2726 (and match (null (cdr match))))))
|
|
2727
|
|
2728 (defun widget-group-match-inline (widget vals)
|
|
2729 ;; Match if the components match.
|
|
2730 (let ((args (widget-get widget :args))
|
|
2731 argument answer found)
|
|
2732 (while args
|
|
2733 (setq argument (car args)
|
|
2734 args (cdr args)
|
|
2735 answer (widget-match-inline argument vals))
|
|
2736 (if answer
|
|
2737 (setq vals (cdr answer)
|
|
2738 found (append found (car answer)))
|
|
2739 (setq vals nil
|
|
2740 args nil)))
|
|
2741 (if answer
|
|
2742 (cons found vals)
|
|
2743 nil)))
|
|
2744
|
155
|
2745 ;;; The `visibility' Widget.
|
|
2746
|
|
2747 (define-widget 'visibility 'item
|
|
2748 "An indicator and manipulator for hidden items."
|
|
2749 :format "%[%v%]"
|
|
2750 :button-prefix ""
|
|
2751 :button-suffix ""
|
163
|
2752 :on "Hide"
|
|
2753 :off "Show"
|
155
|
2754 :value-create 'widget-visibility-value-create
|
|
2755 :action 'widget-toggle-action
|
|
2756 :match (lambda (widget value) t))
|
|
2757
|
|
2758 (defun widget-visibility-value-create (widget)
|
|
2759 ;; Insert text representing the `on' and `off' states.
|
|
2760 (let ((on (widget-get widget :on))
|
|
2761 (off (widget-get widget :off)))
|
|
2762 (if on
|
|
2763 (setq on (concat widget-push-button-prefix
|
|
2764 on
|
|
2765 widget-push-button-suffix))
|
|
2766 (setq on ""))
|
|
2767 (if off
|
|
2768 (setq off (concat widget-push-button-prefix
|
163
|
2769 off
|
|
2770 widget-push-button-suffix))
|
155
|
2771 (setq off ""))
|
|
2772 (if (widget-value widget)
|
|
2773 (widget-glyph-insert widget on "down" "down-pushed")
|
163
|
2774 (widget-glyph-insert widget off "right" "right-pushed"))))
|
155
|
2775
|
161
|
2776 ;;; The `documentation-link' Widget.
|
163
|
2777 ;;
|
|
2778 ;; This is a helper widget for `documentation-string'.
|
161
|
2779
|
|
2780 (define-widget 'documentation-link 'link
|
|
2781 "Link type used in documentation strings."
|
163
|
2782 :tab-order -1
|
|
2783 :help-echo 'widget-documentation-link-echo-help
|
161
|
2784 :action 'widget-documentation-link-action)
|
|
2785
|
163
|
2786 (defun widget-documentation-link-echo-help (widget)
|
|
2787 "Tell what this link will describe."
|
|
2788 (concat "Describe the `" (widget-get widget :value) "' symbol."))
|
|
2789
|
161
|
2790 (defun widget-documentation-link-action (widget &optional event)
|
165
|
2791 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
|
|
2792 (let* ((string (widget-get widget :value))
|
|
2793 (symbol (intern string)))
|
|
2794 (if (and (fboundp symbol) (boundp symbol))
|
|
2795 ;; If there are two doc strings, give the user a way to pick one.
|
|
2796 (apropos (concat "\\`" (regexp-quote string) "\\'"))
|
|
2797 (if (fboundp symbol)
|
|
2798 (describe-function symbol)
|
|
2799 (describe-variable symbol)))))
|
161
|
2800
|
|
2801 (defcustom widget-documentation-links t
|
|
2802 "Add hyperlinks to documentation strings when non-nil."
|
|
2803 :type 'boolean
|
|
2804 :group 'widget-documentation)
|
|
2805
|
|
2806 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
|
|
2807 "Regexp for matching potential links in documentation strings.
|
|
2808 The first group should be the link itself."
|
|
2809 :type 'regexp
|
|
2810 :group 'widget-documentation)
|
|
2811
|
|
2812 (defcustom widget-documentation-link-p 'intern-soft
|
|
2813 "Predicate used to test if a string is useful as a link.
|
|
2814 The value should be a function. The function will be called one
|
|
2815 argument, a string, and should return non-nil if there should be a
|
|
2816 link for that string."
|
|
2817 :type 'function
|
|
2818 :options '(widget-documentation-link-p)
|
|
2819 :group 'widget-documentation)
|
|
2820
|
|
2821 (defcustom widget-documentation-link-type 'documentation-link
|
|
2822 "Widget type used for links in documentation strings."
|
|
2823 :type 'symbol
|
|
2824 :group 'widget-documentation)
|
|
2825
|
|
2826 (defun widget-documentation-link-add (widget from to)
|
|
2827 (widget-specify-doc widget from to)
|
|
2828 (when widget-documentation-links
|
|
2829 (let ((regexp widget-documentation-link-regexp)
|
|
2830 (predicate widget-documentation-link-p)
|
|
2831 (type widget-documentation-link-type)
|
|
2832 (buttons (widget-get widget :buttons)))
|
|
2833 (save-excursion
|
163
|
2834 (goto-char from)
|
161
|
2835 (while (re-search-forward regexp to t)
|
|
2836 (let ((name (match-string 1))
|
163
|
2837 (begin (match-beginning 1))
|
|
2838 (end (match-end 1)))
|
161
|
2839 (when (funcall predicate name)
|
|
2840 (push (widget-convert-button type begin end :value name)
|
|
2841 buttons)))))
|
163
|
2842 (widget-put widget :buttons buttons)))
|
|
2843 (let ((indent (widget-get widget :indent)))
|
|
2844 (when (and indent (not (zerop indent)))
|
|
2845 (save-excursion
|
|
2846 (save-restriction
|
|
2847 (narrow-to-region from to)
|
|
2848 (goto-char (point-min))
|
|
2849 (while (search-forward "\n" nil t)
|
|
2850 (insert-char ?\ indent)))))))
|
161
|
2851
|
155
|
2852 ;;; The `documentation-string' Widget.
|
28
|
2853
|
155
|
2854 (define-widget 'documentation-string 'item
|
|
2855 "A documentation string."
|
|
2856 :format "%v"
|
|
2857 :action 'widget-documentation-string-action
|
|
2858 :value-delete 'widget-children-value-delete
|
|
2859 :value-create 'widget-documentation-string-value-create)
|
28
|
2860
|
155
|
2861 (defun widget-documentation-string-value-create (widget)
|
|
2862 ;; Insert documentation string.
|
|
2863 (let ((doc (widget-value widget))
|
163
|
2864 (indent (widget-get widget :indent))
|
161
|
2865 (shown (widget-get (widget-get widget :parent) :documentation-shown))
|
|
2866 (start (point)))
|
155
|
2867 (if (string-match "\n" doc)
|
|
2868 (let ((before (substring doc 0 (match-beginning 0)))
|
|
2869 (after (substring doc (match-beginning 0)))
|
|
2870 buttons)
|
|
2871 (insert before " ")
|
161
|
2872 (widget-documentation-link-add widget start (point))
|
155
|
2873 (push (widget-create-child-and-convert
|
|
2874 widget 'visibility
|
163
|
2875 :help-echo "Show or hide rest of the documentation."
|
|
2876 :off "More"
|
155
|
2877 :action 'widget-parent-action
|
|
2878 shown)
|
|
2879 buttons)
|
|
2880 (when shown
|
|
2881 (setq start (point))
|
163
|
2882 (when (and indent (not (zerop indent)))
|
|
2883 (insert-char ?\ indent))
|
155
|
2884 (insert after)
|
161
|
2885 (widget-documentation-link-add widget start (point)))
|
155
|
2886 (widget-put widget :buttons buttons))
|
161
|
2887 (insert doc)
|
|
2888 (widget-documentation-link-add widget start (point))))
|
155
|
2889 (insert "\n"))
|
|
2890
|
|
2891 (defun widget-documentation-string-action (widget &rest ignore)
|
|
2892 ;; Toggle documentation.
|
|
2893 (let ((parent (widget-get widget :parent)))
|
|
2894 (widget-put parent :documentation-shown
|
|
2895 (not (widget-get parent :documentation-shown))))
|
|
2896 ;; Redraw.
|
28
|
2897 (widget-value-set widget (widget-value widget)))
|
|
2898
|
|
2899 ;;; The Sexp Widgets.
|
|
2900
|
|
2901 (define-widget 'const 'item
|
|
2902 "An immutable sexp."
|
149
|
2903 :prompt-value 'widget-const-prompt-value
|
28
|
2904 :format "%t\n%d")
|
|
2905
|
149
|
2906 (defun widget-const-prompt-value (widget prompt value unbound)
|
|
2907 ;; Return the value of the const.
|
|
2908 (widget-value widget))
|
|
2909
|
|
2910 (define-widget 'function-item 'const
|
28
|
2911 "An immutable function name."
|
|
2912 :format "%v\n%h"
|
|
2913 :documentation-property (lambda (symbol)
|
|
2914 (condition-case nil
|
|
2915 (documentation symbol t)
|
|
2916 (error nil))))
|
|
2917
|
149
|
2918 (define-widget 'variable-item 'const
|
28
|
2919 "An immutable variable name."
|
|
2920 :format "%v\n%h"
|
|
2921 :documentation-property 'variable-documentation)
|
|
2922
|
149
|
2923 (defvar widget-string-prompt-value-history nil
|
|
2924 "History of input to `widget-string-prompt-value'.")
|
|
2925
|
28
|
2926 (define-widget 'string 'editable-field
|
|
2927 "A string"
|
|
2928 :tag "String"
|
149
|
2929 :format "%{%t%}: %v"
|
155
|
2930 :complete-function 'ispell-complete-word
|
149
|
2931 :prompt-history 'widget-string-prompt-value-history)
|
28
|
2932
|
|
2933 (define-widget 'regexp 'string
|
|
2934 "A regular expression."
|
149
|
2935 :match 'widget-regexp-match
|
|
2936 :validate 'widget-regexp-validate
|
173
|
2937 ;; Doesn't work well with terminating newline.
|
|
2938 ;; :value-face 'widget-single-line-field-face
|
28
|
2939 :tag "Regexp")
|
|
2940
|
149
|
2941 (defun widget-regexp-match (widget value)
|
|
2942 ;; Match valid regexps.
|
|
2943 (and (stringp value)
|
|
2944 (condition-case nil
|
|
2945 (prog1 t
|
|
2946 (string-match value ""))
|
|
2947 (error nil))))
|
|
2948
|
|
2949 (defun widget-regexp-validate (widget)
|
|
2950 "Check that the value of WIDGET is a valid regexp."
|
|
2951 (let ((val (widget-value widget)))
|
|
2952 (condition-case data
|
|
2953 (prog1 nil
|
|
2954 (string-match val ""))
|
|
2955 (error (widget-put widget :error (error-message-string data))
|
|
2956 widget))))
|
|
2957
|
28
|
2958 (define-widget 'file 'string
|
|
2959 "A file widget.
|
155
|
2960 It will read a file name from the minibuffer when invoked."
|
165
|
2961 :complete-function 'widget-file-complete
|
149
|
2962 :prompt-value 'widget-file-prompt-value
|
|
2963 :format "%{%t%}: %v"
|
173
|
2964 ;; Doesn't work well with terminating newline.
|
|
2965 ;; :value-face 'widget-single-line-field-face
|
165
|
2966 :tag "File")
|
|
2967
|
|
2968 (defun widget-file-complete ()
|
|
2969 "Perform completion on file name preceding point."
|
|
2970 (interactive)
|
|
2971 (let* ((end (point))
|
|
2972 (beg (save-excursion
|
|
2973 (skip-chars-backward "^ ")
|
|
2974 (point)))
|
|
2975 (pattern (buffer-substring beg end))
|
|
2976 (name-part (file-name-nondirectory pattern))
|
|
2977 (directory (file-name-directory pattern))
|
|
2978 (completion (file-name-completion name-part directory)))
|
|
2979 (cond ((eq completion t))
|
|
2980 ((null completion)
|
|
2981 (message "Can't find completion for \"%s\"" pattern)
|
|
2982 (ding))
|
|
2983 ((not (string= name-part completion))
|
|
2984 (delete-region beg end)
|
|
2985 (insert (expand-file-name completion directory)))
|
|
2986 (t
|
|
2987 (message "Making completion list...")
|
|
2988 (let ((list (file-name-all-completions name-part directory)))
|
|
2989 (setq list (sort list 'string<))
|
|
2990 (with-output-to-temp-buffer "*Completions*"
|
|
2991 (display-completion-list list)))
|
|
2992 (message "Making completion list...%s" "done")))))
|
28
|
2993
|
149
|
2994 (defun widget-file-prompt-value (widget prompt value unbound)
|
|
2995 ;; Read file from minibuffer.
|
|
2996 (abbreviate-file-name
|
|
2997 (if unbound
|
|
2998 (read-file-name prompt)
|
|
2999 (let ((prompt2 (format "%s (default %s) " prompt value))
|
|
3000 (dir (file-name-directory value))
|
|
3001 (file (file-name-nondirectory value))
|
|
3002 (must-match (widget-get widget :must-match)))
|
|
3003 (read-file-name prompt2 dir nil must-match file)))))
|
|
3004
|
165
|
3005 ;;;(defun widget-file-action (widget &optional event)
|
|
3006 ;;; ;; Read a file name from the minibuffer.
|
|
3007 ;;; (let* ((value (widget-value widget))
|
|
3008 ;;; (dir (file-name-directory value))
|
|
3009 ;;; (file (file-name-nondirectory value))
|
|
3010 ;;; (menu-tag (widget-apply widget :menu-tag-get))
|
|
3011 ;;; (must-match (widget-get widget :must-match))
|
|
3012 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
|
|
3013 ;;; dir nil must-match file)))
|
|
3014 ;;; (widget-value-set widget (abbreviate-file-name answer))
|
|
3015 ;;; (widget-setup)
|
|
3016 ;;; (widget-apply widget :notify widget event)))
|
28
|
3017
|
|
3018 (define-widget 'directory 'file
|
|
3019 "A directory widget.
|
155
|
3020 It will read a directory name from the minibuffer when invoked."
|
28
|
3021 :tag "Directory")
|
|
3022
|
149
|
3023 (defvar widget-symbol-prompt-value-history nil
|
|
3024 "History of input to `widget-symbol-prompt-value'.")
|
|
3025
|
|
3026 (define-widget 'symbol 'editable-field
|
28
|
3027 "A lisp symbol."
|
|
3028 :value nil
|
|
3029 :tag "Symbol"
|
149
|
3030 :format "%{%t%}: %v"
|
28
|
3031 :match (lambda (widget value) (symbolp value))
|
165
|
3032 :complete-function 'lisp-complete-symbol
|
149
|
3033 :prompt-internal 'widget-symbol-prompt-internal
|
|
3034 :prompt-match 'symbolp
|
|
3035 :prompt-history 'widget-symbol-prompt-value-history
|
28
|
3036 :value-to-internal (lambda (widget value)
|
|
3037 (if (symbolp value)
|
|
3038 (symbol-name value)
|
|
3039 value))
|
|
3040 :value-to-external (lambda (widget value)
|
|
3041 (if (stringp value)
|
|
3042 (intern value)
|
|
3043 value)))
|
|
3044
|
149
|
3045 (defun widget-symbol-prompt-internal (widget prompt initial history)
|
|
3046 ;; Read file from minibuffer.
|
|
3047 (let ((answer (completing-read prompt obarray
|
|
3048 (widget-get widget :prompt-match)
|
|
3049 nil initial history)))
|
|
3050 (if (and (stringp answer)
|
|
3051 (not (zerop (length answer))))
|
|
3052 answer
|
|
3053 (error "No value"))))
|
|
3054
|
|
3055 (defvar widget-function-prompt-value-history nil
|
|
3056 "History of input to `widget-function-prompt-value'.")
|
|
3057
|
28
|
3058 (define-widget 'function 'sexp
|
|
3059 "A lisp function."
|
155
|
3060 :complete-function 'lisp-complete-symbol
|
149
|
3061 :prompt-value 'widget-field-prompt-value
|
|
3062 :prompt-internal 'widget-symbol-prompt-internal
|
|
3063 :prompt-match 'fboundp
|
|
3064 :prompt-history 'widget-function-prompt-value-history
|
|
3065 :action 'widget-field-action
|
28
|
3066 :tag "Function")
|
|
3067
|
149
|
3068 (defvar widget-variable-prompt-value-history nil
|
|
3069 "History of input to `widget-variable-prompt-value'.")
|
|
3070
|
28
|
3071 (define-widget 'variable 'symbol
|
|
3072 ;; Should complete on variables.
|
|
3073 "A lisp variable."
|
149
|
3074 :prompt-match 'boundp
|
|
3075 :prompt-history 'widget-variable-prompt-value-history
|
28
|
3076 :tag "Variable")
|
|
3077
|
159
|
3078 (when (featurep 'mule)
|
|
3079 (defvar widget-coding-system-prompt-value-history nil
|
|
3080 "History of input to `widget-coding-system-prompt-value'.")
|
|
3081
|
|
3082 (define-widget 'coding-system 'symbol
|
|
3083 "A MULE coding-system."
|
|
3084 :format "%{%t%}: %v"
|
|
3085 :tag "Coding system"
|
|
3086 :prompt-history 'widget-coding-system-prompt-value-history
|
|
3087 :prompt-value 'widget-coding-system-prompt-value
|
|
3088 :action 'widget-coding-system-action)
|
|
3089
|
|
3090 (defun widget-coding-system-prompt-value (widget prompt value unbound)
|
|
3091 ;; Read coding-system from minibuffer.
|
|
3092 (intern
|
|
3093 (completing-read (format "%s (default %s) " prompt value)
|
|
3094 (mapcar (function
|
|
3095 (lambda (sym)
|
|
3096 (list (symbol-name sym))
|
|
3097 ))
|
|
3098 (coding-system-list)))))
|
|
3099
|
|
3100 (defun widget-coding-system-action (widget &optional event)
|
|
3101 ;; Read a file name from the minibuffer.
|
|
3102 (let ((answer
|
|
3103 (widget-coding-system-prompt-value
|
|
3104 widget
|
|
3105 (widget-apply widget :menu-tag-get)
|
|
3106 (widget-value widget)
|
|
3107 t)))
|
|
3108 (widget-value-set widget answer)
|
|
3109 (widget-apply widget :notify widget event)
|
|
3110 (widget-setup)))
|
|
3111 )
|
|
3112
|
149
|
3113 (define-widget 'sexp 'editable-field
|
28
|
3114 "An arbitrary lisp expression."
|
|
3115 :tag "Lisp expression"
|
149
|
3116 :format "%{%t%}: %v"
|
28
|
3117 :value nil
|
|
3118 :validate 'widget-sexp-validate
|
|
3119 :match (lambda (widget value) t)
|
|
3120 :value-to-internal 'widget-sexp-value-to-internal
|
149
|
3121 :value-to-external (lambda (widget value) (read value))
|
|
3122 :prompt-history 'widget-sexp-prompt-value-history
|
|
3123 :prompt-value 'widget-sexp-prompt-value)
|
28
|
3124
|
|
3125 (defun widget-sexp-value-to-internal (widget value)
|
|
3126 ;; Use pp for printer representation.
|
155
|
3127 (let ((pp (if (symbolp value)
|
|
3128 (prin1-to-string value)
|
|
3129 (pp-to-string value))))
|
28
|
3130 (while (string-match "\n\\'" pp)
|
|
3131 (setq pp (substring pp 0 -1)))
|
|
3132 (if (or (string-match "\n\\'" pp)
|
|
3133 (> (length pp) 40))
|
|
3134 (concat "\n" pp)
|
|
3135 pp)))
|
|
3136
|
|
3137 (defun widget-sexp-validate (widget)
|
|
3138 ;; Valid if we can read the string and there is no junk left after it.
|
|
3139 (save-excursion
|
|
3140 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
|
|
3141 (erase-buffer)
|
|
3142 (insert (widget-apply widget :value-get))
|
|
3143 (goto-char (point-min))
|
|
3144 (condition-case data
|
|
3145 (let ((value (read buffer)))
|
|
3146 (if (eobp)
|
|
3147 (if (widget-apply widget :match value)
|
|
3148 nil
|
|
3149 (widget-put widget :error (widget-get widget :type-error))
|
|
3150 widget)
|
|
3151 (widget-put widget
|
|
3152 :error (format "Junk at end of expression: %s"
|
|
3153 (buffer-substring (point)
|
|
3154 (point-max))))
|
|
3155 widget))
|
|
3156 (error (widget-put widget :error (error-message-string data))
|
|
3157 widget)))))
|
|
3158
|
149
|
3159 (defvar widget-sexp-prompt-value-history nil
|
|
3160 "History of input to `widget-sexp-prompt-value'.")
|
|
3161
|
|
3162 (defun widget-sexp-prompt-value (widget prompt value unbound)
|
|
3163 ;; Read an arbitrary sexp.
|
|
3164 (let ((found (read-string prompt
|
|
3165 (if unbound nil (cons (prin1-to-string value) 0))
|
|
3166 (widget-get widget :prompt-history))))
|
|
3167 (save-excursion
|
|
3168 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
|
|
3169 (erase-buffer)
|
|
3170 (insert found)
|
|
3171 (goto-char (point-min))
|
|
3172 (let ((answer (read buffer)))
|
|
3173 (unless (eobp)
|
|
3174 (error "Junk at end of expression: %s"
|
|
3175 (buffer-substring (point) (point-max))))
|
|
3176 answer)))))
|
|
3177
|
165
|
3178 (define-widget 'restricted-sexp 'sexp
|
|
3179 "A Lisp expression restricted to values that match.
|
|
3180 To use this type, you must define :match or :match-alternatives."
|
|
3181 :type-error "The specified value is not valid"
|
|
3182 :match 'widget-restricted-sexp-match
|
|
3183 :value-to-internal (lambda (widget value)
|
|
3184 (if (widget-apply widget :match value)
|
|
3185 (prin1-to-string value)
|
|
3186 value)))
|
|
3187
|
|
3188 (defun widget-restricted-sexp-match (widget value)
|
|
3189 (let ((alternatives (widget-get widget :match-alternatives))
|
|
3190 matched)
|
|
3191 (while (and alternatives (not matched))
|
|
3192 (if (cond ((functionp (car alternatives))
|
|
3193 (funcall (car alternatives) value))
|
|
3194 ((and (consp (car alternatives))
|
|
3195 (eq (car (car alternatives)) 'quote))
|
|
3196 (eq value (nth 1 (car alternatives)))))
|
|
3197 (setq matched t))
|
|
3198 (setq alternatives (cdr alternatives)))
|
|
3199 matched))
|
|
3200
|
|
3201 (define-widget 'integer 'restricted-sexp
|
28
|
3202 "An integer."
|
|
3203 :tag "Integer"
|
|
3204 :value 0
|
|
3205 :type-error "This field should contain an integer"
|
165
|
3206 :match-alternatives '(integerp))
|
|
3207
|
|
3208 (define-widget 'number 'restricted-sexp
|
|
3209 "A floating point number."
|
|
3210 :tag "Number"
|
|
3211 :value 0.0
|
|
3212 :type-error "This field should contain a number"
|
|
3213 :match-alternatives '(numberp))
|
28
|
3214
|
149
|
3215 (define-widget 'character 'editable-field
|
165
|
3216 "A character."
|
28
|
3217 :tag "Character"
|
|
3218 :value 0
|
|
3219 :size 1
|
|
3220 :format "%{%t%}: %v\n"
|
149
|
3221 :valid-regexp "\\`.\\'"
|
|
3222 :error "This field should contain a single character"
|
28
|
3223 :value-to-internal (lambda (widget value)
|
149
|
3224 (if (stringp value)
|
|
3225 value
|
|
3226 (char-to-string value)))
|
28
|
3227 :value-to-external (lambda (widget value)
|
|
3228 (if (stringp value)
|
|
3229 (aref value 0)
|
|
3230 value))
|
149
|
3231 :match (lambda (widget value)
|
|
3232 (if (fboundp 'characterp)
|
|
3233 (characterp value)
|
|
3234 (integerp value))))
|
28
|
3235
|
|
3236 (define-widget 'list 'group
|
|
3237 "A lisp list."
|
|
3238 :tag "List"
|
|
3239 :format "%{%t%}:\n%v")
|
|
3240
|
|
3241 (define-widget 'vector 'group
|
|
3242 "A lisp vector."
|
|
3243 :tag "Vector"
|
|
3244 :format "%{%t%}:\n%v"
|
|
3245 :match 'widget-vector-match
|
|
3246 :value-to-internal (lambda (widget value) (append value nil))
|
|
3247 :value-to-external (lambda (widget value) (apply 'vector value)))
|
|
3248
|
|
3249 (defun widget-vector-match (widget value)
|
|
3250 (and (vectorp value)
|
|
3251 (widget-group-match widget
|
120
|
3252 (widget-apply widget :value-to-internal value))))
|
28
|
3253
|
|
3254 (define-widget 'cons 'group
|
|
3255 "A cons-cell."
|
|
3256 :tag "Cons-cell"
|
|
3257 :format "%{%t%}:\n%v"
|
|
3258 :match 'widget-cons-match
|
|
3259 :value-to-internal (lambda (widget value)
|
|
3260 (list (car value) (cdr value)))
|
|
3261 :value-to-external (lambda (widget value)
|
|
3262 (cons (nth 0 value) (nth 1 value))))
|
|
3263
|
|
3264 (defun widget-cons-match (widget value)
|
|
3265 (and (consp value)
|
|
3266 (widget-group-match widget
|
|
3267 (widget-apply widget :value-to-internal value))))
|
|
3268
|
|
3269 (define-widget 'choice 'menu-choice
|
|
3270 "A union of several sexp types."
|
|
3271 :tag "Choice"
|
163
|
3272 :format "%{%t%}: %[Value Menu%] %v"
|
|
3273 :button-prefix 'widget-push-button-prefix
|
|
3274 :button-suffix 'widget-push-button-suffix
|
149
|
3275 :prompt-value 'widget-choice-prompt-value)
|
|
3276
|
|
3277 (defun widget-choice-prompt-value (widget prompt value unbound)
|
|
3278 "Make a choice."
|
|
3279 (let ((args (widget-get widget :args))
|
|
3280 (completion-ignore-case (widget-get widget :case-fold))
|
|
3281 current choices old)
|
|
3282 ;; Find the first arg that match VALUE.
|
|
3283 (let ((look args))
|
|
3284 (while look
|
|
3285 (if (widget-apply (car look) :match value)
|
|
3286 (setq old (car look)
|
|
3287 look nil)
|
|
3288 (setq look (cdr look)))))
|
|
3289 ;; Find new choice.
|
|
3290 (setq current
|
|
3291 (cond ((= (length args) 0)
|
|
3292 nil)
|
|
3293 ((= (length args) 1)
|
|
3294 (nth 0 args))
|
|
3295 ((and (= (length args) 2)
|
|
3296 (memq old args))
|
|
3297 (if (eq old (nth 0 args))
|
|
3298 (nth 1 args)
|
|
3299 (nth 0 args)))
|
|
3300 (t
|
|
3301 (while args
|
|
3302 (setq current (car args)
|
|
3303 args (cdr args))
|
|
3304 (setq choices
|
|
3305 (cons (cons (widget-apply current :menu-tag-get)
|
|
3306 current)
|
|
3307 choices)))
|
|
3308 (let ((val (completing-read prompt choices nil t)))
|
|
3309 (if (stringp val)
|
|
3310 (let ((try (try-completion val choices)))
|
|
3311 (when (stringp try)
|
|
3312 (setq val try))
|
|
3313 (cdr (assoc val choices)))
|
|
3314 nil)))))
|
|
3315 (if current
|
|
3316 (widget-prompt-value current prompt nil t)
|
|
3317 value)))
|
28
|
3318
|
|
3319 (define-widget 'radio 'radio-button-choice
|
|
3320 "A union of several sexp types."
|
|
3321 :tag "Choice"
|
149
|
3322 :format "%{%t%}:\n%v"
|
|
3323 :prompt-value 'widget-choice-prompt-value)
|
28
|
3324
|
|
3325 (define-widget 'repeat 'editable-list
|
|
3326 "A variable length homogeneous list."
|
|
3327 :tag "Repeat"
|
|
3328 :format "%{%t%}:\n%v%i\n")
|
|
3329
|
|
3330 (define-widget 'set 'checklist
|
|
3331 "A list of members from a fixed set."
|
|
3332 :tag "Set"
|
|
3333 :format "%{%t%}:\n%v")
|
|
3334
|
|
3335 (define-widget 'boolean 'toggle
|
|
3336 "To be nil or non-nil, that is the question."
|
|
3337 :tag "Boolean"
|
149
|
3338 :prompt-value 'widget-boolean-prompt-value
|
163
|
3339 :button-prefix 'widget-push-button-prefix
|
|
3340 :button-suffix 'widget-push-button-suffix
|
|
3341 :format "%{%t%}: %[Toggle%] %v\n"
|
|
3342 :on "on (non-nil)"
|
|
3343 :off "off (nil)")
|
149
|
3344
|
|
3345 (defun widget-boolean-prompt-value (widget prompt value unbound)
|
|
3346 ;; Toggle a boolean.
|
|
3347 (y-or-n-p prompt))
|
28
|
3348
|
|
3349 ;;; The `color' Widget.
|
|
3350
|
173
|
3351 (define-widget 'color 'editable-field
|
|
3352 "Choose a color name (with sample)."
|
|
3353 :format "%t: %v (%{sample%})\n"
|
|
3354 :size 10
|
|
3355 :tag "Color"
|
|
3356 :value "black"
|
|
3357 :complete 'widget-color-complete
|
|
3358 :sample-face-get 'widget-color-sample-face-get
|
|
3359 :notify 'widget-color-notify
|
|
3360 :action 'widget-color-action)
|
|
3361
|
|
3362 (defun widget-color-complete (widget)
|
|
3363 "Complete the color in WIDGET."
|
|
3364 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
|
|
3365 (point)))
|
|
3366 (list (widget-color-choice-list))
|
|
3367 (completion (try-completion prefix list)))
|
|
3368 (cond ((eq completion t)
|
|
3369 (message "Exact match."))
|
|
3370 ((null completion)
|
|
3371 (error "Can't find completion for \"%s\"" prefix))
|
|
3372 ((not (string-equal prefix completion))
|
|
3373 (insert-and-inherit (substring completion (length prefix))))
|
|
3374 (t
|
|
3375 (message "Making completion list...")
|
|
3376 (let ((list (all-completions prefix list nil)))
|
|
3377 (with-output-to-temp-buffer "*Completions*"
|
|
3378 (display-completion-list list)))
|
|
3379 (message "Making completion list...done")))))
|
|
3380
|
|
3381 (defun widget-color-sample-face-get (widget)
|
|
3382 (let* ((value (condition-case nil
|
|
3383 (widget-value widget)
|
|
3384 (error (widget-get widget :value))))
|
|
3385 (symbol (intern (concat "fg:" value))))
|
155
|
3386 (if (string-match "XEmacs" emacs-version)
|
|
3387 (prog1 symbol
|
|
3388 (or (find-face symbol)
|
173
|
3389 (set-face-foreground (make-face symbol) value)))
|
155
|
3390 (condition-case nil
|
|
3391 (facemenu-get-face symbol)
|
|
3392 (error 'default)))))
|
28
|
3393
|
|
3394 (defvar widget-color-choice-list nil)
|
|
3395 ;; Variable holding the possible colors.
|
|
3396
|
|
3397 (defun widget-color-choice-list ()
|
|
3398 (unless widget-color-choice-list
|
|
3399 (setq widget-color-choice-list
|
173
|
3400 (if (fboundp 'read-color-completion-table)
|
|
3401 (read-color-completion-table)
|
|
3402 (mapcar '(lambda (color) (list color))
|
|
3403 (x-defined-colors)))))
|
28
|
3404 widget-color-choice-list)
|
|
3405
|
|
3406 (defvar widget-color-history nil
|
|
3407 "History of entered colors")
|
|
3408
|
|
3409 (defun widget-color-action (widget &optional event)
|
|
3410 ;; Prompt for a color.
|
|
3411 (let* ((tag (widget-apply widget :menu-tag-get))
|
|
3412 (prompt (concat tag ": "))
|
173
|
3413 (value (widget-value widget))
|
|
3414 (start (widget-field-start widget))
|
|
3415 (pos (cond ((< (point) start)
|
|
3416 0)
|
|
3417 ((> (point) (+ start (length value)))
|
|
3418 (length value))
|
|
3419 (t
|
|
3420 (- (point) start))))
|
|
3421 (answer (if (commandp 'read-color)
|
|
3422 (read-color prompt)
|
|
3423 (completing-read (concat tag ": ")
|
|
3424 (widget-color-choice-list)
|
|
3425 nil nil
|
|
3426 (cons value pos)
|
|
3427 'widget-color-history))))
|
28
|
3428 (unless (zerop (length answer))
|
|
3429 (widget-value-set widget answer)
|
155
|
3430 (widget-setup)
|
|
3431 (widget-apply widget :notify widget event))))
|
28
|
3432
|
173
|
3433 (defun widget-color-notify (widget child &optional event)
|
|
3434 "Update the sample, and notofy the parent."
|
|
3435 (overlay-put (widget-get widget :sample-overlay)
|
|
3436 'face (widget-apply widget :sample-face-get))
|
|
3437 (widget-default-notify widget child event))
|
|
3438
|
28
|
3439 ;;; The Help Echo
|
|
3440
|
|
3441 (defun widget-echo-help-mouse ()
|
|
3442 "Display the help message for the widget under the mouse.
|
|
3443 Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)"
|
|
3444 (let* ((pos (mouse-position))
|
|
3445 (frame (car pos))
|
|
3446 (x (car (cdr pos)))
|
|
3447 (y (cdr (cdr pos)))
|
|
3448 (win (window-at x y frame))
|
|
3449 (where (coordinates-in-window-p (cons x y) win)))
|
|
3450 (when (consp where)
|
|
3451 (save-window-excursion
|
|
3452 (progn ; save-excursion
|
|
3453 (select-window win)
|
|
3454 (let* ((result (compute-motion (window-start win)
|
|
3455 '(0 . 0)
|
|
3456 (window-end win)
|
|
3457 where
|
|
3458 (window-width win)
|
|
3459 (cons (window-hscroll) 0)
|
|
3460 win)))
|
|
3461 (when (and (eq (nth 1 result) x)
|
|
3462 (eq (nth 2 result) y))
|
|
3463 (widget-echo-help (nth 0 result))))))))
|
|
3464 (unless track-mouse
|
|
3465 (setq track-mouse t)
|
|
3466 (add-hook 'post-command-hook 'widget-stop-mouse-tracking)))
|
|
3467
|
|
3468 (defun widget-stop-mouse-tracking (&rest args)
|
|
3469 "Stop the mouse tracking done while idle."
|
|
3470 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking)
|
|
3471 (setq track-mouse nil))
|
|
3472
|
|
3473 (defun widget-at (pos)
|
|
3474 "The button or field at POS."
|
155
|
3475 (or (get-char-property pos 'button)
|
|
3476 (get-char-property pos 'field)))
|
28
|
3477
|
|
3478 (defun widget-echo-help (pos)
|
|
3479 "Display the help echo for widget at POS."
|
|
3480 (let* ((widget (widget-at pos))
|
|
3481 (help-echo (and widget (widget-get widget :help-echo))))
|
|
3482 (cond ((stringp help-echo)
|
|
3483 (message "%s" help-echo))
|
|
3484 ((and (symbolp help-echo) (fboundp help-echo)
|
|
3485 (stringp (setq help-echo (funcall help-echo widget))))
|
|
3486 (message "%s" help-echo)))))
|
|
3487
|
|
3488 ;;; The End:
|
|
3489
|
|
3490 (provide 'wid-edit)
|
|
3491
|
|
3492 ;; wid-edit.el ends here
|