diff options
| author | Artur Malabarba | 2015-10-30 15:00:37 +0000 |
|---|---|---|
| committer | Artur Malabarba | 2015-10-30 18:15:52 +0000 |
| commit | 7ccedcb486ee4e37da54dd82a8557c80616d9467 (patch) | |
| tree | 0cfe822c5e8881d03c847e0bc2ce19468233c36a | |
| parent | 5260ea68e02b1c1578330d1eeafdb8ff9079c6c9 (diff) | |
| download | emacs-7ccedcb486ee4e37da54dd82a8557c80616d9467.tar.gz emacs-7ccedcb486ee4e37da54dd82a8557c80616d9467.zip | |
* lisp/faces.el: Refactor common code and fix a bug
(faces--attribute-at-point): New function. Fix a bug when the
face at point is a list of faces and the desired attribute is not
on the first one.
(foreground-color-at-point, background-color-at-point): Use it.
| -rw-r--r-- | lisp/faces.el | 58 |
1 files changed, 30 insertions, 28 deletions
diff --git a/lisp/faces.el b/lisp/faces.el index de8a0b5bcb1..8c5480905a1 100644 --- a/lisp/faces.el +++ b/lisp/faces.el | |||
| @@ -1958,39 +1958,41 @@ Return nil if there is no face." | |||
| 1958 | (delete-dups (nreverse faces)) | 1958 | (delete-dups (nreverse faces)) |
| 1959 | (car (last faces))))) | 1959 | (car (last faces))))) |
| 1960 | 1960 | ||
| 1961 | (defun foreground-color-at-point () | 1961 | (defun faces--attribute-at-point (attribute &optional attribute-unnamed) |
| 1962 | "Return the foreground color of the character after point." | 1962 | "Return the face ATTRIBUTE at point. |
| 1963 | ATTRIBUTE is a keyword. | ||
| 1964 | If ATTRIBUTE-UNNAMED is non-nil, it is a symbol to look for in | ||
| 1965 | unnamed faces (e.g, `foreground-color')." | ||
| 1963 | ;; `face-at-point' alone is not sufficient. It only gets named faces. | 1966 | ;; `face-at-point' alone is not sufficient. It only gets named faces. |
| 1964 | ;; Need also pick up any face properties that are not associated with named faces. | 1967 | ;; Need also pick up any face properties that are not associated with named faces. |
| 1965 | (let ((face (or (face-at-point) | 1968 | (let (found) |
| 1966 | (get-char-property (point) 'read-face-name) | 1969 | (dolist (face (or (get-char-property (point) 'read-face-name) |
| 1967 | (get-char-property (point) 'face)))) | 1970 | ;; If `font-lock-mode' is on, `font-lock-face' takes precedence. |
| 1968 | (cond ((and face (symbolp face)) | 1971 | (and font-lock-mode |
| 1969 | (let ((value (face-foreground face nil 'default))) | 1972 | (get-char-property (point) 'font-lock-face)) |
| 1970 | (if (member value '("unspecified-fg" "unspecified-bg")) | 1973 | (get-char-property (point) 'face))) |
| 1971 | nil | 1974 | (cond (found) |
| 1972 | value))) | 1975 | ((and face (symbolp face)) |
| 1973 | ((consp face) | 1976 | (let ((value (face-attribute-specified-or |
| 1974 | (cond ((memq 'foreground-color face) (cdr (memq 'foreground-color face))) | 1977 | (face-attribute face attribute nil t) |
| 1975 | ((memq ':foreground face) (cadr (memq ':foreground face))))) | 1978 | nil))) |
| 1976 | (t nil)))) ; Invalid face value. | 1979 | (unless (member value '(nil "unspecified-fg" "unspecified-bg")) |
| 1980 | (setq found value)))) | ||
| 1981 | ((consp face) | ||
| 1982 | (setq found (cond ((and attribute-unnamed | ||
| 1983 | (memq attribute-unnamed face)) | ||
| 1984 | (cdr (memq attribute-unnamed face))) | ||
| 1985 | ((memq attribute face) (cadr (memq attribute face)))))))) | ||
| 1986 | (or found | ||
| 1987 | (face-attribute 'default attribute)))) | ||
| 1988 | |||
| 1989 | (defun foreground-color-at-point () | ||
| 1990 | "Return the foreground color of the character after point." | ||
| 1991 | (faces--attribute-at-point :foreground 'foreground-color)) | ||
| 1977 | 1992 | ||
| 1978 | (defun background-color-at-point () | 1993 | (defun background-color-at-point () |
| 1979 | "Return the background color of the character after point." | 1994 | "Return the background color of the character after point." |
| 1980 | ;; `face-at-point' alone is not sufficient. It only gets named faces. | 1995 | (faces--attribute-at-point :background 'background-color)) |
| 1981 | ;; Need also pick up any face properties that are not associated with named faces. | ||
| 1982 | (let ((face (or (face-at-point) | ||
| 1983 | (get-char-property (point) 'read-face-name) | ||
| 1984 | (get-char-property (point) 'face)))) | ||
| 1985 | (cond ((and face (symbolp face)) | ||
| 1986 | (let ((value (face-background face nil 'default))) | ||
| 1987 | (if (member value '("unspecified-fg" "unspecified-bg")) | ||
| 1988 | nil | ||
| 1989 | value))) | ||
| 1990 | ((consp face) | ||
| 1991 | (cond ((memq 'background-color face) (cdr (memq 'background-color face))) | ||
| 1992 | ((memq ':background face) (cadr (memq ':background face))))) | ||
| 1993 | (t nil)))) ; Invalid face value. | ||
| 1994 | 1996 | ||
| 1995 | 1997 | ||
| 1996 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 1998 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |