comparison lisp/wid-edit.el @ 18372:5b5261ce8db9

(widget-file-complete): New function. (file): Use widget-file-complete for :completion. Delete :action. (symbol): Use lisp-complete-symbol for :completion. (widget-file-action): Function deleted. (widget-field-action): Just move to next field. (widget-choice-action, widget-toggle-action): Preserve point usefully if it is within the widget. (group-visibility): Inherit from visibility.
author Richard M. Stallman <rms@gnu.org>
date Sat, 21 Jun 1997 21:25:16 +0000
parents ffdf3d404802
children 201d766770fd
comparison
equal deleted inserted replaced
18371:a32f9b2c2e0c 18372:5b5261ce8db9
1701 (let ((answer (widget-apply widget 1701 (let ((answer (widget-apply widget
1702 :prompt-internal prompt initial history))) 1702 :prompt-internal prompt initial history)))
1703 (widget-apply widget :value-to-external answer)))) 1703 (widget-apply widget :value-to-external answer))))
1704 1704
1705 (defun widget-field-action (widget &optional event) 1705 (defun widget-field-action (widget &optional event)
1706 ;; Edit the value in the minibuffer. 1706 ;; Move to next field.
1707 (let ((invalid (widget-apply widget :validate))) 1707 (widget-forward 1)
1708 (let ((prompt (concat (widget-apply widget :menu-tag-get) ": ")) 1708 (message "To set this variable or face, invoke [State] and choose Set"))
1709 (value (unless invalid
1710 (widget-value widget))))
1711 (let ((answer (widget-apply widget :prompt-value prompt value invalid) ))
1712 (widget-value-set widget answer)))
1713 (widget-setup)
1714 (widget-apply widget :notify widget event)))
1715 1709
1716 (defun widget-field-validate (widget) 1710 (defun widget-field-validate (widget)
1717 ;; Valid if the content matches `:valid-regexp'. 1711 ;; Valid if the content matches `:valid-regexp'.
1718 (save-excursion 1712 (save-excursion
1719 (let ((value (widget-apply widget :value-get)) 1713 (let ((value (widget-apply widget :value-get))
1899 (setq choices 1893 (setq choices
1900 (cons (cons (widget-apply current :menu-tag-get) 1894 (cons (cons (widget-apply current :menu-tag-get)
1901 current) 1895 current)
1902 choices))) 1896 choices)))
1903 (widget-choose tag (reverse choices) event)))) 1897 (widget-choose tag (reverse choices) event))))
1904 (when current 1898 ;; Try to preserve point even if it is within the widget.
1905 (widget-value-set widget 1899 (let* ((old-pos (point))
1906 (widget-apply current :value-to-external 1900 (from (copy-marker (widget-get widget :from)))
1907 (widget-get current :value))) 1901 (to (copy-marker (widget-get widget :to)))
1908 (widget-setup) 1902 (offset (if (and (<= from old-pos) (<= old-pos to))
1909 (widget-apply widget :notify widget event)))) 1903 (- old-pos from))))
1904 (when current
1905 (widget-value-set widget
1906 (widget-apply current :value-to-external
1907 (widget-get current :value)))
1908 (widget-setup)
1909 (widget-apply widget :notify widget event))
1910 (if offset
1911 (goto-char (min (+ from offset) to))))))
1910 1912
1911 (defun widget-choice-validate (widget) 1913 (defun widget-choice-validate (widget)
1912 ;; Valid if we have made a valid choice. 1914 ;; Valid if we have made a valid choice.
1913 (let ((void (widget-get widget :void)) 1915 (let ((void (widget-get widget :void))
1914 (choice (widget-get widget :choice)) 1916 (choice (widget-get widget :choice))
1958 (widget-get widget :off) 1960 (widget-get widget :off)
1959 (widget-get widget :off-glyph)))) 1961 (widget-get widget :off-glyph))))
1960 1962
1961 (defun widget-toggle-action (widget &optional event) 1963 (defun widget-toggle-action (widget &optional event)
1962 ;; Toggle value. 1964 ;; Toggle value.
1963 (widget-value-set widget (not (widget-value widget))) 1965 ;; Try to preserve point even if it is within the widget.
1964 (widget-apply widget :notify widget event)) 1966 (let* ((old-pos (point))
1967 (from (copy-marker (widget-get widget :from)))
1968 (to (copy-marker (widget-get widget :to)))
1969 (offset (if (and (<= from old-pos) (<= old-pos to))
1970 (- old-pos from))))
1971 (widget-value-set widget (not (widget-value widget)))
1972 (widget-apply widget :notify widget event)
1973 (if offset
1974 (goto-char (min (+ from offset) to)))))
1965 1975
1966 ;;; The `checkbox' Widget. 1976 ;;; The `checkbox' Widget.
1967 1977
1968 (define-widget 'checkbox 'toggle 1978 (define-widget 'checkbox 'toggle
1969 "A checkbox toggle." 1979 "A checkbox toggle."
2619 (setq off "")) 2629 (setq off ""))
2620 (if (widget-value widget) 2630 (if (widget-value widget)
2621 (widget-glyph-insert widget on "down" "down-pushed") 2631 (widget-glyph-insert widget on "down" "down-pushed")
2622 (widget-glyph-insert widget off "right" "right-pushed")))) 2632 (widget-glyph-insert widget off "right" "right-pushed"))))
2623 2633
2624 (define-widget 'group-visibility 'item 2634 (define-widget 'group-visibility 'visibility
2625 "An indicator and manipulator for hidden group contents." 2635 "An indicator and manipulator for hidden group contents."
2626 :format "%[%v%]" 2636 :create 'widget-group-visibility-create)
2627 :create 'widget-group-visibility-create
2628 :button-prefix ""
2629 :button-suffix ""
2630 :on "Hide"
2631 :off "Show"
2632 :value-create 'widget-visibility-value-create
2633 :action 'widget-toggle-action
2634 :match (lambda (widget value) t))
2635 2637
2636 (defun widget-group-visibility-create (widget) 2638 (defun widget-group-visibility-create (widget)
2637 (let ((visible (widget-value widget))) 2639 (let ((visible (widget-value widget)))
2638 (if visible 2640 (if visible
2639 (insert "--------"))) 2641 (insert "--------")))
2820 widget)))) 2822 widget))))
2821 2823
2822 (define-widget 'file 'string 2824 (define-widget 'file 'string
2823 "A file widget. 2825 "A file widget.
2824 It will read a file name from the minibuffer when invoked." 2826 It will read a file name from the minibuffer when invoked."
2827 :complete-function 'widget-file-complete
2825 :prompt-value 'widget-file-prompt-value 2828 :prompt-value 'widget-file-prompt-value
2826 :format "%{%t%}: %v" 2829 :format "%{%t%}: %v"
2827 :tag "File" 2830 :tag "File")
2828 :action 'widget-file-action) 2831
2832 (defun widget-file-complete ()
2833 "Perform completion on file name preceding point."
2834 (interactive)
2835 (let* ((end (point))
2836 (beg (save-excursion
2837 (skip-chars-backward "^ ")
2838 (point)))
2839 (pattern (buffer-substring beg end))
2840 (name-part (file-name-nondirectory pattern))
2841 (directory (file-name-directory pattern))
2842 (completion (file-name-completion name-part directory)))
2843 (cond ((eq completion t))
2844 ((null completion)
2845 (message "Can't find completion for \"%s\"" pattern)
2846 (ding))
2847 ((not (string= name-part completion))
2848 (delete-region beg end)
2849 (insert (expand-file-name completion directory)))
2850 (t
2851 (message "Making completion list...")
2852 (let ((list (file-name-all-completions name-part directory)))
2853 (setq list (sort list 'string<))
2854 (with-output-to-temp-buffer "*Completions*"
2855 (display-completion-list list)))
2856 (message "Making completion list...%s" "done")))))
2829 2857
2830 (defun widget-file-prompt-value (widget prompt value unbound) 2858 (defun widget-file-prompt-value (widget prompt value unbound)
2831 ;; Read file from minibuffer. 2859 ;; Read file from minibuffer.
2832 (abbreviate-file-name 2860 (abbreviate-file-name
2833 (if unbound 2861 (if unbound
2836 (dir (file-name-directory value)) 2864 (dir (file-name-directory value))
2837 (file (file-name-nondirectory value)) 2865 (file (file-name-nondirectory value))
2838 (must-match (widget-get widget :must-match))) 2866 (must-match (widget-get widget :must-match)))
2839 (read-file-name prompt2 dir nil must-match file))))) 2867 (read-file-name prompt2 dir nil must-match file)))))
2840 2868
2841 (defun widget-file-action (widget &optional event) 2869 ;;;(defun widget-file-action (widget &optional event)
2842 ;; Read a file name from the minibuffer. 2870 ;;; ;; Read a file name from the minibuffer.
2843 (let* ((value (widget-value widget)) 2871 ;;; (let* ((value (widget-value widget))
2844 (dir (file-name-directory value)) 2872 ;;; (dir (file-name-directory value))
2845 (file (file-name-nondirectory value)) 2873 ;;; (file (file-name-nondirectory value))
2846 (menu-tag (widget-apply widget :menu-tag-get)) 2874 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2847 (must-match (widget-get widget :must-match)) 2875 ;;; (must-match (widget-get widget :must-match))
2848 (answer (read-file-name (concat menu-tag ": (default `" value "') ") 2876 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2849 dir nil must-match file))) 2877 ;;; dir nil must-match file)))
2850 (widget-value-set widget (abbreviate-file-name answer)) 2878 ;;; (widget-value-set widget (abbreviate-file-name answer))
2851 (widget-setup) 2879 ;;; (widget-setup)
2852 (widget-apply widget :notify widget event))) 2880 ;;; (widget-apply widget :notify widget event)))
2853 2881
2854 (define-widget 'directory 'file 2882 (define-widget 'directory 'file
2855 "A directory widget. 2883 "A directory widget.
2856 It will read a directory name from the minibuffer when invoked." 2884 It will read a directory name from the minibuffer when invoked."
2857 :tag "Directory") 2885 :tag "Directory")
2863 "A lisp symbol." 2891 "A lisp symbol."
2864 :value nil 2892 :value nil
2865 :tag "Symbol" 2893 :tag "Symbol"
2866 :format "%{%t%}: %v" 2894 :format "%{%t%}: %v"
2867 :match (lambda (widget value) (symbolp value)) 2895 :match (lambda (widget value) (symbolp value))
2896 :complete-function 'lisp-complete-symbol
2868 :prompt-internal 'widget-symbol-prompt-internal 2897 :prompt-internal 'widget-symbol-prompt-internal
2869 :prompt-match 'symbolp 2898 :prompt-match 'symbolp
2870 :prompt-history 'widget-symbol-prompt-value-history 2899 :prompt-history 'widget-symbol-prompt-value-history
2871 :value-to-internal (lambda (widget value) 2900 :value-to-internal (lambda (widget value)
2872 (if (symbolp value) 2901 (if (symbolp value)