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