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