Improve detection libtool version detection
[sxemacs] / lisp / cus-face.el
1 ;;; cus-face.el -- Support for Custom faces.
2 ;;
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
7 ;; Keywords: help, faces
8 ;; Version: 1.9960-x
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
10
11 ;;; Synched with: Not synched.
12
13 ;;; Commentary:
14 ;;
15 ;; See `custom.el'.
16
17 ;;; Code:
18
19 ;; it is now safe to put the `provide' anywhere.  if an error occurs while
20 ;; loading, all provides (and fsets) will be undone.  put it first to
21 ;; prevent require/provide loop with custom and cus-face.
22 (provide 'cus-face)
23 (require 'custom)
24
25 ;; To elude the warnings for font functions.
26 (eval-when-compile
27   (require 'font))
28
29 ;;; Declaring a face.
30
31 ;;;###autoload
32 (defun custom-declare-face (face spec doc &rest args)
33   "Like `defface', but FACE is evaluated as a normal argument."
34   ;; (when (fboundp 'pureload)
35     ;; (error "Attempt to declare a face during dump"))
36   ;; #### should we possibly reset force-face here?
37   (unless (get face 'face-defface-spec)
38     (put face 'face-defface-spec spec)
39     (unless (find-face face)
40       ;; If the user has already created the face, respect that.
41       (let ((value (or (get face 'saved-face) spec))
42             (frames (relevant-custom-frames))
43             frame)
44         ;; Create global face.
45         (make-empty-face face)
46         (face-display-set face value nil '(custom))
47         ;; Create frame local faces
48         (while frames
49           (setq frame (car frames)
50                 frames (cdr frames))
51           (face-display-set face value frame '(custom)))
52         (init-face-from-resources face)))
53     ;; Don't record SPEC until we see it causes no errors.
54     (put face 'face-defface-spec spec)
55     (push (cons 'defface face) current-load-list)
56     (when (and doc (null (face-doc-string face)))
57       (set-face-doc-string face doc))
58     (custom-handle-all-keywords face args 'custom-face)
59     (run-hooks 'custom-define-hook))
60   face)
61
62 ;;; Font Attributes.
63
64 ;; Consider adding the stuff in the XML font model here.
65 (defconst custom-face-attributes
66   '((:foreground (color :tag "Foreground"
67                         :value ""
68                         :help-echo "Set foreground color.")
69                  set-face-foreground face-foreground-name)
70     (:background (color :tag "Background"
71                         :value ""
72                         :help-echo "Set background color.")
73                  set-face-background face-background-name)
74     (:size (editable-field :format "Size: %v"
75                            :help-echo "Text size (e.g. 9pt or 2mm).")
76            custom-set-face-font-size custom-face-font-size)
77     (:family (editable-field :format "Font Family: %v"
78                              :help-echo "Name of font family to use (e.g. times).")
79              custom-set-face-font-family custom-face-font-family)
80     (:background-pixmap (editable-field :format "Background pixmap: %v"
81                                         :help-echo "Name of background pixmap file.")
82               set-face-background-pixmap custom-face-background-pixmap)
83     (:dim (toggle :format "%[Dim%]: %v\n"
84                   :help-echo "Control whether the text should be dimmed.")
85           set-face-dim-p face-dim-p)
86     (:bold (toggle :format "%[Bold%]: %v\n"
87                    :help-echo "Control whether a bold font should be used.")
88            custom-set-face-bold custom-face-bold)
89     (:italic (toggle :format "%[Italic%]: %v\n"
90                      :help-echo "Control whether an italic font should be used.")
91              custom-set-face-italic custom-face-italic)
92     (:underline (toggle :format "%[Underline%]: %v\n"
93                         :help-echo "Control whether the text should be underlined.")
94                 set-face-underline-p face-underline-p)
95     (:strikethru (toggle :format "%[Strikethru%]: %v\n"
96                          :help-echo "Control whether the text should be strikethru.")
97                  set-face-strikethru-p face-strikethru-p)
98     (:inverse-video (toggle :format "%[Inverse Video%]: %v\n"
99                             :help-echo "\
100 Control whether the text should be inverted.  Works only on TTY-s")
101                     set-face-reverse-p face-reverse-p)
102     (:inherit
103      (repeat :tag "Inherit"
104              :help-echo "List of faces to inherit attributes from."
105              (face :Tag "Face" default))
106      ;; FSF 21.3
107 ;      ;; filter to make value suitable for customize
108 ;      (lambda (real-value)
109 ;        (cond ((or (null real-value) (eq real-value 'unspecified))
110 ;             nil)
111 ;            ((symbolp real-value)
112 ;             (list real-value))
113 ;            (t
114 ;             real-value)))
115 ;      ;; filter to make customized-value suitable for storing
116 ;      (lambda (cus-value)
117 ;        (if (and (consp cus-value) (null (cdr cus-value)))
118 ;          (car cus-value)
119 ;        cus-value))
120      custom-set-face-inherit custom-face-inherit))
121   "Alist of face attributes.
122
123 The elements are lists of the form (KEY TYPE SET GET) where:
124  KEY is a symbol identifying the attribute.
125  TYPE is a widget type for editing the attribute.
126  SET is a function for setting the attribute value.
127  GET is a function for getting the attribute value.
128
129 The SET function should take three arguments: the face to modify, the
130 value of the attribute, and optionally the frame where the face should
131 be changed.
132
133 The GET function should take two arguments, the face to examine, and
134 optionally the frame where the face should be examined.")
135
136 (defun face-custom-attributes-set (face frame tags &rest atts)
137   "For FACE on FRAME set the attributes [KEYWORD VALUE]....
138 Each keyword should be listed in `custom-face-attributes'.
139
140 If FRAME is nil, set the default face."
141   (while atts
142     (let* ((name (nth 0 atts))
143            (value (nth 1 atts))
144            (fun (nth 2 (assq name custom-face-attributes))))
145       (setq atts (cdr (cdr atts)))
146       (condition-case nil
147           (funcall fun face value frame tags)
148         (error nil)))))
149
150 (defun face-custom-attributes-get (face frame)
151   "For FACE on FRAME get the attributes [KEYWORD VALUE]....
152 Each keyword should be listed in `custom-face-attributes'.
153
154 If FRAME is nil, use the default face."
155   (condition-case nil
156       ;; Attempt to get `font.el' from w3.
157       (require 'font)
158     (error nil))
159   (let ((atts custom-face-attributes)
160         att result get)
161     (while atts
162       (setq att (car atts)
163             atts (cdr atts)
164             get (nth 3 att))
165       (condition-case nil
166           ;; This may fail if w3 doesn't exist.
167           (when get
168             (let ((answer (funcall get face frame)))
169               (unless (equal answer (funcall get 'default frame))
170                 (when (widget-apply (nth 1 att) :match answer)
171                   (setq result (cons (nth 0 att) (cons answer result)))))))
172         (error nil)))
173     result))
174
175 (defsubst custom-face-get-spec (symbol)
176   (or (get symbol 'customized-face)
177       (get symbol 'saved-face)
178       (get symbol 'face-defface-spec)
179       ;; Attempt to construct it.
180       (list (list t (face-custom-attributes-get
181                      symbol (selected-frame))))))
182
183 (defun custom-set-face-bold (face value &optional frame tags)
184   "Set the bold property of FACE to VALUE."
185   (if value
186       (make-face-bold face frame tags)
187     (make-face-unbold face frame tags)))
188
189 ;; Really, we should get rid of these font.el dependencies...  They
190 ;; are still presenting a problem with dumping the faces (font.el is
191 ;; too bloated for us to dump).  I am thinking about hacking up
192 ;; font-like functionality myself for the sake of this file.  It will
193 ;; probably be to-the-point and more efficient.
194
195 (defun custom-face-bold (face &rest args)
196   "Return non-nil if the font of FACE is bold."
197   (let* ((font (apply 'face-font-name face args))
198          ;; Gag
199          (fontobj (font-create-object font)))
200     (font-bold-p fontobj)))
201
202 (defun custom-set-face-italic (face value &optional frame tags)
203   "Set the italic property of FACE to VALUE."
204   (if value
205       (make-face-italic face frame tags)
206     (make-face-unitalic face frame tags)))
207
208 (defun custom-face-italic (face &rest args)
209   "Return non-nil if the font of FACE is italic."
210   (let* ((font (apply 'face-font-name face args))
211          ;; Gag
212          (fontobj (font-create-object font)))
213     (font-italic-p fontobj)))
214
215 (defun custom-face-background-pixmap (face &rest args)
216   "Return the name of the background pixmap file used for FACE."
217   (let ((image  (apply 'specifier-instance
218                        (face-background-pixmap face) args)))
219     (and image
220          (image-instance-file-name image))))
221
222 (defun custom-set-face-inherit (face value &optional frame tags)
223   "Set FACE to inherit its properties from another face."
224   (if (listp value) (setq value (car value))) ;; #### Temporary hack!
225   (if (find-face value)
226       (set-face-parent face value frame tags)))
227
228 (defun custom-face-inherit (face &rest args)
229   "Return the value (instance) of the `inherit' property for FACE."
230   ;; #### Major, temporary hack!
231   (let ((spec (apply 'specifier-instantiator
232                      (face-font face) args)))
233     (and spec (vector spec) (aref spec 0))))
234
235 ;; This consistently fails to dtrt
236 ;;(defun custom-set-face-font-size (face size &optional locale tags)
237 ;;  "Set the font of FACE to SIZE."
238 ;;  ;; #### should this call have tags in it?
239 ;;  (let* ((font (apply 'face-font-name face (list locale)))
240 ;;       ;; Gag
241 ;;       (fontobj (font-create-object font)))
242 ;;    (set-font-size fontobj size)
243 ;;    (apply 'font-set-face-font face fontobj locale tags)))
244
245 ;; From Jan Vroonhof -- see faces.el
246 (defun custom-set-face-font-size (face size &optional locale tags)
247   "Set the font of FACE to SIZE."
248   (make-face-size face size locale tags))
249
250 (defun custom-face-font-size (face &rest args)
251   "Return the size of the font of FACE as a string."
252   (let* ((font (apply 'face-font-name face args))
253          ;; Gag
254          (fontobj (font-create-object font)))
255     (format "%s" (font-size fontobj))))
256
257 ;; Jan suggests this may not dtrt
258 ;;(defun custom-set-face-font-family (face family &optional locale tags)
259 ;;  "Set the font of FACE to FAMILY."
260 ;;  ;; #### should this call have tags in it?
261 ;;  (let* ((font (apply 'face-font-name face (list locale)))
262 ;;       ;; Gag
263 ;;       (fontobj (font-create-object font)))
264 ;;    (set-font-family fontobj family)
265 ;;    (apply 'font-set-face-font face fontobj locale tags)))
266
267 ;; From Jan Vroonhof -- see faces.el
268 (defun custom-set-face-font-family (face family &optional locale tags)
269   "Set the font of FACE to FAMILY."
270   (make-face-family face family locale tags))
271
272 (defun custom-face-font-family (face &rest args)
273   "Return the name of the font family of FACE."
274   (let* ((font (apply 'face-font-name face args))
275          ;; Gag
276          (fontobj (font-create-object font)))
277     (font-family fontobj)))
278
279 ;;;###autoload
280 (defun custom-set-face-update-spec (face display plist)
281   "Customize the FACE for display types matching DISPLAY, merging
282   in the new items from PLIST."
283   (let ((spec (face-spec-update-all-matching (custom-face-get-spec face)
284                                              display plist)))
285     (put face 'customized-face spec)
286     (face-spec-set face spec nil '(custom))))
287
288 ;;; Initializing.
289
290 ;;;###autoload
291 (defun custom-set-faces (&rest args)
292   "Initialize faces according to user preferences.
293 This asociates the setting with the USER theme.
294 The arguments should be a list where each entry has the form:
295
296   (FACE SPEC [NOW [COMMENT]])
297
298 SPEC will be stored as the saved value for FACE.  If NOW is present
299 and non-nil, FACE will also be created according to SPEC.
300 COMMENT is a string comment about FACE.
301
302 See `defface' for the format of SPEC."
303   (apply #'custom-theme-set-faces 'user args))
304
305 ;;;###autoload
306 (defun custom-theme-set-faces (theme &rest args)
307   "Initialize faces according to settings specified by args.
308 Records the settings as belonging to THEME.
309
310 See `custom-set-faces' for a description of the arguments ARGS."
311   (custom-check-theme theme)
312   (let ((immediate (get theme 'theme-immediate)))
313     (while args
314       (let ((entry (car args)))
315         (if (listp entry)
316             (let ((face (nth 0 entry))
317                   (spec (nth 1 entry))
318                   (now (nth 2 entry))
319                   (comment (nth 3 entry)))
320               (put face 'saved-face spec)
321               (custom-push-theme 'theme-face face theme 'set spec)
322               (put face 'saved-face-comment comment)
323               (when (or now immediate)
324                 (put face 'force-face (if now 'rogue 'immediate)))
325               (when (or now immediate (find-face face))
326                 (put face 'face-comment comment)
327                 (unless (find-face face)
328                   (make-empty-face face))
329                 (face-spec-set face spec nil '(custom)))
330               (setq args (cdr args)))
331           ;; Old format, a plist of FACE SPEC pairs.
332           (let ((face (nth 0 args))
333                 (spec (nth 1 args)))
334             (put face 'saved-face spec)
335             (custom-push-theme 'theme-face face theme 'set spec))
336           (setq args (cdr (cdr args))))))))
337
338 ;;;###autoload
339 (defun custom-theme-face-value (face theme)
340   "Return spec of FACE in THEME if the THEME modifies the
341 FACE.  Nil otherwise."
342   (car-safe (custom-theme-value theme (get face 'theme-face))))
343
344 (defun custom-theme-reset-internal-face (face to-theme)
345   (let ((spec (custom-theme-face-value face to-theme))
346         was-in-theme)
347     (setq was-in-theme spec)
348     (setq spec (or spec (get face 'standard-value)))
349     (when spec
350       (put face 'save-face was-in-theme)
351       (when (or (get face 'force-face) (find-face face))
352               (unless (find-face face)
353                 (make-empty-face face))
354               (face-spec-set face spec)))
355     spec))
356
357 ;;;###autoload
358 (defun custom-theme-reset-faces (theme &rest args)
359   "Reset the value of the face to values previously defined.
360 Associate this setting with THEME.
361
362 ARGS is a list of lists of the form
363
364     (face to-theme)
365
366 This means reset face to its value in to-theme."
367   (custom-check-theme theme)
368   (mapc #'(lambda (arg)
369             (apply #'custom-theme-reset-internal-face arg)
370             (custom-push-theme (car arg) 'theme-face theme 'reset (cadr arg)))
371         args))
372
373 ;;;###autoload
374 (defun custom-reset-faces (&rest args)
375   "Reset the value of the face to values previously defined.
376 Associate this setting with the 'user' theme.
377
378 ARGS is defined as for `custom-theme-reset-faces'."
379   (apply #'custom-theme-reset-faces 'user args))
380
381
382 ;;; The End.
383
384 ;; cus-face.el ends here