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