]> code.delx.au - gnu-emacs/blobdiff - lisp/faces.el
*** empty log message ***
[gnu-emacs] / lisp / faces.el
index 75bd43cf89fb67a93eebdc62ba221014755fe2c1..d5a7aa6b4b565beb09ab52a1d58061c14a50c818 100644 (file)
@@ -4,6 +4,7 @@
 ;;   Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
+;; Keywords: internal
 
 ;; This file is part of GNU Emacs.
 
@@ -31,8 +32,6 @@
   ;; Warning suppression -- can't require x-win in batch:
   (autoload 'xw-defined-colors "x-win"))
 
-(require 'cus-face)
-
 \f
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;; Font selection.
@@ -53,7 +52,7 @@ those face attributes first that appear first in the list.  For
 example, if `:slant' appears before `:height', font selection first
 tries to find a font with a suitable slant, even if this results in
 a font height that isn't optimal."
-  :tag "Font selection order."
+  :tag "Font selection order"
   :type '(list symbol symbol symbol symbol)
   :group 'font-selection
   :set #'(lambda (symbol value)
@@ -69,7 +68,7 @@ a font height that isn't optimal."
 Each element has the form (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...).
 If fonts of family FAMILY can't be loaded, try ALTERNATIVE1, then
 ALTERNATIVE2 etc."
-  :tag "Alternative font families to try."
+  :tag "Alternative font families to try"
   :type '(repeat (repeat string))
   :group 'font-selection
   :set #'(lambda (symbol value)
@@ -94,7 +93,7 @@ Each element has the form (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...).
 If fonts of registry REGISTRY can be loaded, font selection
 tries to find a best matching font among all fonts of registry
 REGISTRY, ALTERNATIVE1, ALTERNATIVE2, and etc."
-  :tag "Alternative font registries to try."
+  :tag "Alternative font registries to try"
   :type '(repeat (repeat string))
   :version "21.1"
   :group 'font-selection
@@ -149,7 +148,7 @@ If the face already exists, it is left unmodified.  Value is FACE."
 If NEW-FACE already exists as a face, it is modified to be like
 OLD-FACE.  If it doesn't already exist, it is created.
 
-If the optional argument FRAME is given as a frame,  NEW-FACE is
+If the optional argument FRAME is given as a frame, NEW-FACE is
 changed on FRAME only.
 If FRAME is t, the frame-independent default specification for OLD-FACE
 is copied to NEW-FACE.
@@ -197,13 +196,10 @@ should not be used anymore."
 If the optional argument FRAME is given, this gets the face NAME for
 that frame; otherwise, it uses the selected frame.
 If FRAME is the symbol t, then the global, non-frame face is returned.
-If NAME is already a face, it is simply returned.
-
-This function is defined for compatibility with Emacs 20.2.  It
-should not be used anymore."
+If NAME is already a face, it is simply returned."
   (or (facep name)
       (check-face name)))
-(make-obsolete 'internal-get-face "See `facep' and `check-face'." "21.1")
+(make-obsolete 'internal-get-face "see `facep' and `check-face'." "21.1")
 
 \f
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -228,7 +224,7 @@ Value is FACE."
 ;; support faces in display table entries.
 
 (defun face-id (face &optional frame)
-  "Return the interNal ID of face with name FACE.
+  "Return the internal ID of face with name FACE.
 If optional argument FRAME is nil or omitted, use the selected frame."
   (check-face face)
   (get face 'face))
@@ -384,7 +380,7 @@ completely specified)."
     (when (and inherit
               (not (eq inherit t))
               (face-attribute-relative-p attribute value))
-       ;; We should merge with INHERIT as well
+      ;; We should merge with INHERIT as well
       (setq value (face-attribute-merged-with attribute value inherit frame)))
     value))
 
@@ -668,6 +664,9 @@ of face names.  Attributes from inherited faces are merged into the face
 like an underlying face would be, with higher priority than underlying faces."
   (let ((where (if (null frame) 0 frame)))
     (setq args (purecopy args))
+    ;; If we set the new-frame defaults, this face is modified outside Custom.
+    (if (memq where '(0 t))
+       (put face 'face-modified t))
     (while args
       (internal-set-lisp-face-attribute face (car args)
                                        (purecopy (cadr args))
@@ -846,21 +845,54 @@ of the default face.  Value is FACE."
 ;;; Interactively modifying faces.
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
-(defun read-face-name (prompt)
-  "Read and return a face symbol, prompting with PROMPT.
-PROMPT should not end with a blank, since this function appends one.
-Value is a symbol naming a known face."
-  (let ((face-list (mapcar #'(lambda (x) (cons (symbol-name x) x))
-                          (face-list)))
-       (def (thing-at-point 'symbol))
-       face)
-    (cond ((assoc def face-list)
-          (setq prompt (concat prompt " (default " def "): ")))
-         (t (setq def nil)
-            (setq prompt (concat prompt ": "))))
-    (while (equal "" (setq face (completing-read
-                                prompt face-list nil t nil nil def))))
-    (intern face)))
+(defun read-face-name (prompt &optional string-describing-default multiple)
+  "Read a face, defaulting to the face or faces on the char after point.
+If it has a `read-face-name' property, that overrides the `face' property.
+PROMPT describes what you will do with the face (don't end in a space).
+STRING-DESCRIBING-DEFAULT describes what default you will use
+if this function returns nil.
+If MULTIPLE is non-nil, return a list of faces (possibly only one).
+Otherwise, return a single face."
+  (let ((faceprop (or (get-char-property (point) 'read-face-name)
+                     (get-char-property (point) 'face)))
+       faces)
+    ;; Make a list of the named faces that the `face' property uses.
+    (if (listp faceprop)
+       (dolist (f faceprop)
+         (if (symbolp f)
+             (push f faces)))
+      (if (symbolp faceprop)
+         (setq faces (list faceprop))))
+    ;; If there are none, try to get a face name from the buffer.
+    (if (and (null faces)
+            (memq (intern-soft (thing-at-point 'symbol)) (face-list)))
+       (setq faces (list (intern-soft (thing-at-point 'symbol)))))
+
+    ;; If we only want one, and the default is more than one,
+    ;; discard the unwanted ones now.
+    (unless multiple
+      (if faces
+         (setq faces (list (car faces)))))
+    (let* ((input
+           ;; Read the input.
+           (completing-read
+            (if (or faces string-describing-default)
+                (format "%s (default %s): " prompt
+                        (if faces (mapconcat 'symbol-name faces ", ")
+                          string-describing-default))
+              (format "%s: " prompt))
+            obarray 'custom-facep t))
+          ;; Canonicalize the output.
+          (output
+           (if (equal input "")
+               faces
+             (if (stringp input)
+                 (list (intern input))
+               input))))
+      ;; Return either a list of faces or just one face.
+      (if multiple
+         output
+       (car output)))))
 
 
 (defun face-valid-attribute-values (attribute &optional frame)
@@ -1106,6 +1138,7 @@ Value is a list (FACE NEW-VALUE) where FACE is the face read
 ;; The name list-faces would be more consistent, but let's avoid a
 ;; conflict with Lucid, which uses that name differently.
 
+(defvar help-xref-stack)
 (defun list-faces-display ()
   "List all faces, using the same sample text in each.
 The sample text is a string that comes from the variable
@@ -1137,8 +1170,9 @@ The sample text is a string that comes from the variable
          (save-excursion
            (save-match-data
              (search-backward face-name)
-             (help-xref-button 0 'help-customize-face face-name)))
-         (let ((beg (point)))
+             (help-xref-button 0 'help-customize-face face)))
+         (let ((beg (point))
+               (line-beg (line-beginning-position)))
            (insert list-faces-sample-text)
            ;; Hyperlink to a help buffer for the face.
            (save-excursion
@@ -1147,6 +1181,9 @@ The sample text is a string that comes from the variable
                (help-xref-button 0 'help-face face)))
            (insert "\n")
            (put-text-property beg (1- (point)) 'face face)
+           ;; Make all face commands default to the proper face
+           ;; anywhere in the line.
+           (put-text-property line-beg (1- (point)) 'read-face-name face)
            ;; If the sample text has multiple lines, line up all of them.
            (goto-char beg)
            (forward-line 1)
@@ -1167,13 +1204,15 @@ The sample text is a string that comes from the variable
            (copy-face (car faces) (car faces) frame disp-frame)
            (setq faces (cdr faces)))))))
 
-
 (defun describe-face (face &optional frame)
   "Display the properties of face FACE on FRAME.
+Interactively, FACE defaults to the faces of the character after point
+and FRAME defaults to the selected frame.
+
 If the optional argument FRAME is given, report on face FACE in that frame.
 If FRAME is t, report on the defaults for face FACE (for new frames).
 If FRAME is omitted or nil, use the selected frame."
-  (interactive (list (read-face-name "Describe face")))
+  (interactive (list (read-face-name "Describe face" "= `default' face" t)))
   (let* ((attrs '((:family . "Family")
                  (:width . "Width")
                  (:height . "Height")
@@ -1193,25 +1232,33 @@ If FRAME is omitted or nil, use the selected frame."
        (max-width (apply #'max (mapcar #'(lambda (x) (length (cdr x)))
                                        attrs))))
     (help-setup-xref (list #'describe-face face) (interactive-p))
+    (unless face
+      (setq face 'default))
+    (if (not (listp face))
+       (setq face (list face)))
     (with-output-to-temp-buffer (help-buffer)
       (save-excursion
        (set-buffer standard-output)
-       (dolist (a attrs)
-         (let ((attr (face-attribute face (car a) frame)))
-           (insert (make-string (- max-width (length (cdr a))) ?\ )
-                   (cdr a) ": " (format "%s" attr) "\n")))
-       (insert "\nDocumentation:\n\n"
-               (or (face-documentation face)
-                   "not documented as a face."))
-       (let ((customize-label "customize"))
-         (terpri)
-         (terpri)
-         (princ (concat "You can " customize-label " this face."))
-         (with-current-buffer standard-output
-           (save-excursion
-             (re-search-backward
-              (concat "\\(" customize-label "\\)") nil t)
-             (help-xref-button 1 'help-customize-face face)))))
+       (dolist (f face)
+         (insert "Face: " (symbol-name f))
+         (if (not (facep f))
+             (insert "   undefined face.\n")
+           (let ((customize-label "customize this face"))
+             (princ (concat " (" customize-label ")\n"))
+             (insert "Documentation: "
+                     (or (face-documentation f)
+                         "Not documented as a face.")
+                     "\n\n")
+             (with-current-buffer standard-output
+               (save-excursion
+                 (re-search-backward
+                  (concat "\\(" customize-label "\\)") nil t)
+                 (help-xref-button 1 'help-customize-face f)))
+             (dolist (a attrs)
+               (let ((attr (face-attribute f (car a) frame)))
+                 (insert (make-string (- max-width (length (cdr a))) ?\ )
+                         (cdr a) ": " (format "%s" attr) "\n")))))
+         (terpri)))
       (print-help-return-message))))
 
 \f
@@ -1273,6 +1320,8 @@ If FRAME is nil, the current FRAME is used."
                        ((eq req 'background)
                         (memq (frame-parameter frame 'background-mode)
                               options))
+                       ((eq req 'supports)
+                        (display-supports-face-attributes-p options frame))
                        (t (error "Unknown req `%S' with options `%S'"
                                  req options)))))
     match))
@@ -1284,19 +1333,21 @@ If SPEC is nil, return nil."
   (unless frame
     (setq frame (selected-frame)))
   (let ((tail spec)
-       result)
+       result all)
     (while tail
       (let* ((entry (pop tail))
             (display (car entry))
             (attrs (cdr entry)))
        (when (face-spec-set-match-display display frame)
-         (setq result (if (listp (car attrs))
+         (setq result (if (null (cdr attrs)) ;; was (listp (car attrs))
                           ;; Old-style entry, the attribute list is the
                           ;; first element.
                           (car attrs)
-                        attrs)
-               tail nil))))
-    result))
+                        attrs))
+         (if (eq display t)
+             (setq all result result nil)
+           (setq tail nil)))))
+    (if all (append result all) result)))
 
 
 (defun face-spec-reset-face (face &optional frame)
@@ -1331,7 +1382,11 @@ If SPEC is nil, do nothing."
               (setq attribute nil))))
        (when attribute
          (set-face-attribute face frame attribute value)))
-      (setq attrs (cdr (cdr attrs))))))
+      (setq attrs (cdr (cdr attrs)))))
+  ;; When we reset the face based on its spec, then it is unmodified
+  ;; as far as Custom is concerned.
+  (if (null frame)
+      (put face 'face-modified nil)))
 
 
 (defun face-attr-match-p (face attrs &optional frame)
@@ -1430,6 +1485,33 @@ If omitted or nil, that stands for the selected frame's display."
      (t
       (> (tty-color-gray-shades display) 2)))))
 
+(defun display-supports-face-attributes-p (attributes &optional display)
+  "Return non-nil if all the face attributes in ATTRIBUTES are supported.
+The optional argument DISPLAY can be a display name, a frame, or
+nil (meaning the selected frame's display)
+
+The definition of `supported' is somewhat heuristic, but basically means
+that a face containing all the attributes in ATTRIBUTES, when merged
+with the default face for display, can be represented in a way that's
+
+ (1) different in appearance than the default face, and
+ (2) `close in spirit' to what the attributes specify, if not exact.
+
+Point (2) implies that a `:weight black' attribute will be satisfied by
+any display that can display bold, and a `:foreground \"yellow\"' as long
+as it can display a yellowish color, but `:slant italic' will _not_ be
+satisfied by the tty display code's automatic substitution of a `dim'
+face for italic."
+  (let ((frame
+        (if (framep display)
+            display
+          (car (frames-on-display-list display)))))
+    ;; For now, we assume that non-tty displays can support everything.
+    ;; Later, we should add the ability to query about specific fonts,
+    ;; colors, etc.
+    (or (memq (framep frame) '(x w32 mac))
+       (tty-supports-face-attributes-p attributes frame))))
+
 \f
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;; Background mode.
@@ -1457,7 +1539,7 @@ Display-dependent faces are those which have different definitions
 according to the `background-mode' and `display-type' frame parameters."
   (let* ((bg-resource
          (and window-system
-              (x-get-resource ".backgroundMode" "BackgroundMode")))
+              (x-get-resource "backgroundMode" "BackgroundMode")))
         (bg-color (frame-parameter frame 'background-color))
         (bg-mode
          (cond (frame-background-mode)
@@ -1596,15 +1678,30 @@ Value is the new frame created."
 (defun face-set-after-frame-default (frame)
   "Set frame-local faces of FRAME from face specs and resources.
 Initialize colors of certain faces from frame parameters."
+  (if (face-attribute 'default :font t)
+      (set-face-attribute 'default frame :font
+                         (face-attribute 'default :font t))
+    (set-face-attribute 'default frame :family
+                       (face-attribute 'default :family t))
+    (set-face-attribute 'default frame :height
+                       (face-attribute 'default :height t))
+    (set-face-attribute 'default frame :slant
+                       (face-attribute 'default :slant t))
+    (set-face-attribute 'default frame :weight
+                       (face-attribute 'default :weight t))
+    (set-face-attribute 'default frame :width
+                       (face-attribute 'default :width t)))
   (dolist (face (face-list))
-    (when (not (equal face 'default))
-      (face-spec-set face (face-user-default-spec face) frame)
-      (internal-merge-in-global-face face frame)
-      (when (and (memq window-system '(x w32 mac))
-                (or (not (boundp 'inhibit-default-face-x-resources))
-                    (not (eq face 'default))))
-       (make-face-x-resource-internal face frame))))
-
+    ;; Don't let frame creation fail because of an invalid face spec.
+    (condition-case ()
+       (when (not (equal face 'default))
+         (face-spec-set face (face-user-default-spec face) frame)
+         (internal-merge-in-global-face face frame)
+         (when (and (memq window-system '(x w32 mac))
+                    (or (not (boundp 'inhibit-default-face-x-resources))
+                        (not (eq face 'default))))
+           (make-face-x-resource-internal face frame)))
+      (error nil)))
   ;; Initialize attributes from frame parameters.
   (let ((params '((foreground-color default :foreground)
                  (background-color default :background)
@@ -1679,7 +1776,7 @@ created."
 ;; Update a frame's faces when we change its default font.
 
 (defalias 'frame-update-faces 'ignore)
-(make-obsolete 'frame-update-faces "No longer necessary" "21.1")
+(make-obsolete 'frame-update-faces "no longer necessary." "21.1")
 
 ;; Update the colors of FACE, after FRAME's own colors have been
 ;; changed.
@@ -1705,31 +1802,28 @@ created."
 
 (defface mode-line
   '((((type x w32 mac) (class color))
-     (:box (:line-width -1 :style released-button)
-          :background "grey75" :foreground "black"))
+     :box (:line-width -1 :style released-button)
+     :background "grey75" :foreground "black")
     (t
-     (:inverse-video t)))
+     :inverse-video t))
   "Basic mode line face for selected window."
   :version "21.1"
   :group 'modeline
   :group 'basic-faces)
 
 (defface mode-line-inactive
-  '((((type x w32 mac) (background light) (class color))
-     :inherit mode-line
+  '((t
+     :inherit mode-line)
+    (((type x w32 mac) (background light) (class color))
      :weight light
      :box (:line-width -1 :color "grey75" :style nil)
      :foreground "grey20" :background "grey90")
     (((type x w32 mac) (background dark) (class color))
-     :inherit mode-line
      :weight light
      :box (:line-width -1 :color "grey40" :style nil)
-     :foreground "grey80" :background "grey30")
-    (t
-     :inherit mode-line
-     :inverse-video t))
+     :foreground "grey80" :background "grey30"))
   "Basic mode line face for non-selected windows."
-  :version "21.2"
+  :version "21.4"
   :group 'modeline
   :group 'basic-faces)
 
@@ -1748,30 +1842,27 @@ created."
      ;; highlighting; this may be too confusing in general, although it
      ;; happens to look good with the only current use of header-lines,
      ;; the info browser. XXX
-     :inherit mode-line
      :underline t)
     (((class color grayscale) (background light))
-     :inherit mode-line
      :background "grey90" :foreground "grey20"
-     :box nil)
+     :box nil
+     :inherit mode-line)
     (((class color grayscale) (background dark))
-     :inherit mode-line
      :background "grey20" :foreground "grey90"
-     :box nil)
+     :box nil
+     :inherit mode-line)
     (((class mono) (background light))
-     :inherit mode-line
      :background "white" :foreground "black"
      :inverse-video nil
      :box nil
-     :underline t)
+     :underline t
+     :inherit mode-line)
     (((class mono) (background dark))
-     :inherit mode-line
      :background "black" :foreground "white"
      :inverse-video nil
      :box nil
-     :underline t)
-    (t
-     :inverse-video t))
+     :underline t
+     :inherit mode-line))
   "Basic header-line face."
   :version "21.1"
   :group 'basic-faces)
@@ -1779,11 +1870,11 @@ created."
 
 (defface tool-bar
   '((((type x w32 mac) (class color))
-     (:box (:line-width 1 :style released-button)
-          :background "grey75" :foreground "black"))
+     :box (:line-width 1 :style released-button)
+     :background "grey75" :foreground "black")
     (((type x) (class mono))
-     (:box (:line-width 1 :style released-button)
-          :background "grey" :foreground "black"))
+     :box (:line-width 1 :style released-button)
+     :background "grey" :foreground "black")
     (t
      ()))
   "Basic tool-bar face."
@@ -1791,11 +1882,11 @@ created."
   :group 'basic-faces)
 
 
-(defface minibuffer-prompt '((((background dark)) (:foreground "cyan"))
-                            (((type pc)) (:foreground "magenta"))
-                            (t (:foreground "dark blue")))
+(defface minibuffer-prompt '((((background dark)) :foreground "cyan")
+                            (((type pc)) :foreground "magenta")
+                            (t :foreground "dark blue"))
   "Face for minibuffer prompts."
-  :version "21.3"
+  :version "21.4"
   :group 'basic-faces)
 
 (setq minibuffer-prompt-properties
@@ -1803,14 +1894,14 @@ created."
 
 (defface region
   '((((type tty) (class color))
-     (:background "blue" :foreground "white"))
+     :background "blue" :foreground "white")
     (((type tty) (class mono))
-     (:inverse-video t))
+     :inverse-video t)
     (((class color) (background dark))
-     (:background "blue3"))
+     :background "blue3")
     (((class color) (background light))
-     (:background "lightgoldenrod2"))
-    (t (:background "gray")))
+     :background "lightgoldenrod2")
+    (t :background "gray"))
   "Basic face for highlighting the region."
   :version "21.1"
   :group 'basic-faces)
@@ -1818,11 +1909,11 @@ created."
 
 (defface fringe
   '((((class color) (background light))
-       (:background "grey95"))
-      (((class color) (background dark))
-       (:background "grey10"))
-      (t
-       (:background "gray")))
+     :background "grey95")
+    (((class color) (background dark))
+     :background "grey10")
+    (t
+     :background "gray"))
   "Basic face for the fringes to the left and right of windows under X."
   :version "21.1"
   :group 'frames
@@ -1870,66 +1961,75 @@ created."
   :group 'basic-faces)
 
 
-(defface bold '((t (:weight bold)))
+(defface bold '((t :weight bold))
   "Basic bold face."
   :group 'basic-faces)
 
 
-(defface italic '((t (:slant italic)))
+(defface italic
+  '((((supports :slant italic))
+     :slant italic)
+    (((supports :underline t))
+     :underline t)
+    (t
+     ;; default to italic, even it doesn't appear to be supported,
+     ;; because in some cases the display engine will do it's own
+     ;; workaround (to `dim' on ttys)
+     :slant italic))
   "Basic italic font."
   :group 'basic-faces)
 
 
-(defface bold-italic '((t (:weight bold :slant italic)))
+(defface bold-italic '((t :weight bold :slant italic))
   "Basic bold-italic face."
   :group 'basic-faces)
 
 
-(defface underline '((t (:underline t)))
+(defface underline '((t :underline t))
   "Basic underlined face."
   :group 'basic-faces)
 
 
 (defface highlight
   '((((type tty) (class color))
-     (:background "green"))
+     :background "green" :foreground "black")
     (((class color) (background light))
-     (:background "darkseagreen2"))
+     :background "darkseagreen2")
     (((class color) (background dark))
-     (:background "darkolivegreen"))
-    (t (:inverse-video t)))
+     :background "darkolivegreen")
+    (t :inverse-video t))
   "Basic face for highlighting."
   :group 'basic-faces)
 
 
 (defface secondary-selection
   '((((type tty) (class color))
-     (:background "cyan" :foreground "black"))
+     :background "cyan" :foreground "black")
     (((class color) (background light))
-     (:background "yellow"))
+     :background "yellow")
     (((class color) (background dark))
-     (:background "SkyBlue4"))
-    (t (:inverse-video t)))
+     :background "SkyBlue4")
+    (t :inverse-video t))
   "Basic face for displaying the secondary selection."
   :group 'basic-faces)
 
 
-(defface fixed-pitch '((t (:family "courier")))
+(defface fixed-pitch '((t :family "courier"))
   "The basic fixed-pitch face."
   :group 'basic-faces)
 
 
-(defface variable-pitch '((t (:family "helv")))
+(defface variable-pitch '((t :family "helv"))
   "The basic variable-pitch face."
   :group 'basic-faces)
 
 
 (defface trailing-whitespace
   '((((class color) (background light))
-     (:background "red"))
+     :background "red")
     (((class color) (background dark))
-     (:background "red"))
-    (t (:inverse-video t)))
+     :background "red")
+    (t :inverse-video t))
   "Basic face for highlighting trailing whitespace."
   :version "21.1"
   :group 'font-lock                    ; like `show-trailing-whitespace'