aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Moringen2016-02-28 17:27:23 +1100
committerLars Ingebrigtsen2016-02-28 17:27:23 +1100
commit8ed026d6176d02412b6c48d9dfbd9f3a345a86a6 (patch)
tree11fffc240489638e1e1705eb80d9090be651067d
parent3ac844be4ec66728f33b3651f7cc87c4601dcc49 (diff)
downloademacs-8ed026d6176d02412b6c48d9dfbd9f3a345a86a6.tar.gz
emacs-8ed026d6176d02412b6c48d9dfbd9f3a345a86a6.zip
Show the face colours when completing in `read-color'
* lisp/faces.el (defined-colors-with-face-attributes): New function. (readable-foreground-color, defined-colors-with-face-attributes) (readable-foreground-color): Ditto. (read-color): Use them (bug#5305).
-rw-r--r--lisp/faces.el62
1 files changed, 58 insertions, 4 deletions
diff --git a/lisp/faces.el b/lisp/faces.el
index bfb5d4c0f69..b5e9fdca08e 100644
--- a/lisp/faces.el
+++ b/lisp/faces.el
@@ -1792,6 +1792,58 @@ If FRAME is nil, that stands for the selected frame."
1792 (mapcar 'car (tty-color-alist frame)))) 1792 (mapcar 'car (tty-color-alist frame))))
1793(defalias 'x-defined-colors 'defined-colors) 1793(defalias 'x-defined-colors 'defined-colors)
1794 1794
1795(defun defined-colors-with-face-attributes (&optional frame)
1796 "Return a list of colors supported for a particular frame.
1797See `defined-colors' for arguments and return value. In contrast
1798to `define-colors' the elements of the returned list are color
1799strings with text properties, that make the color names render
1800with the color they represent as background color."
1801 (mapcar
1802 (lambda (color-name)
1803 (let ((foreground (readable-foreground-color color-name))
1804 (color (copy-sequence color-name)))
1805 (propertize color 'face (list :foreground foreground
1806 :background color))))
1807 (defined-colors frame)))
1808
1809(defun readable-foreground-color (color)
1810 "Return a readable foreground color for background COLOR."
1811 (let* ((rgb (color-values color))
1812 (max (apply #'max rgb))
1813 (black (car (color-values "black")))
1814 (white (car (color-values "white"))))
1815 ;; Select black or white depending on which one is less similar to
1816 ;; the brightest component.
1817 (if (> (abs (- max black)) (abs (- max white)))
1818 "black"
1819 "white")))
1820
1821(defun defined-colors-with-face-attributes (&optional frame)
1822 "Return a list of colors supported for a particular frame.
1823See `defined-colors' for arguments and return value. In contrast
1824to `define-colors' the elements of the returned list are color
1825strings with text properties, that make the color names render
1826with the color they represent as background color."
1827 (mapcar
1828 (lambda (color-name)
1829 (let ((foreground (readable-foreground-color color-name))
1830 (color (copy-sequence color-name)))
1831 (propertize color 'face (list :foreground foreground
1832 :background color))))
1833 (defined-colors frame)))
1834
1835(defun readable-foreground-color (color)
1836 "Return a readable foreground color for background COLOR."
1837 (let* ((rgb (color-values color))
1838 (max (apply #'max rgb))
1839 (black (car (color-values "black")))
1840 (white (car (color-values "white"))))
1841 ;; Select black or white depending on which one is less similar to
1842 ;; the brightest component.
1843 (if (> (abs (- max black)) (abs (- max white)))
1844 "black"
1845 "white")))
1846
1795(declare-function xw-color-defined-p "xfns.c" (color &optional frame)) 1847(declare-function xw-color-defined-p "xfns.c" (color &optional frame))
1796 1848
1797(defun color-defined-p (color &optional frame) 1849(defun color-defined-p (color &optional frame)
@@ -1896,22 +1948,24 @@ resulting color name in the echo area."
1896 (colors (or facemenu-color-alist 1948 (colors (or facemenu-color-alist
1897 (append '("foreground at point" "background at point") 1949 (append '("foreground at point" "background at point")
1898 (if allow-empty-name '("")) 1950 (if allow-empty-name '(""))
1899 (defined-colors)))) 1951 (if (display-color-p)
1952 (defined-colors-with-face-attributes)
1953 (defined-colors)))))
1900 (color (completing-read 1954 (color (completing-read
1901 (or prompt "Color (name or #RGB triplet): ") 1955 (or prompt "Color (name or #RGB triplet): ")
1902 ;; Completing function for reading colors, accepting 1956 ;; Completing function for reading colors, accepting
1903 ;; both color names and RGB triplets. 1957 ;; both color names and RGB triplets.
1904 (lambda (string pred flag) 1958 (lambda (string pred flag)
1905 (cond 1959 (cond
1906 ((null flag) ; Try completion. 1960 ((null flag) ; Try completion.
1907 (or (try-completion string colors pred) 1961 (or (try-completion string colors pred)
1908 (if (color-defined-p string) 1962 (if (color-defined-p string)
1909 string))) 1963 string)))
1910 ((eq flag t) ; List all completions. 1964 ((eq flag t) ; List all completions.
1911 (or (all-completions string colors pred) 1965 (or (all-completions string colors pred)
1912 (if (color-defined-p string) 1966 (if (color-defined-p string)
1913 (list string)))) 1967 (list string))))
1914 ((eq flag 'lambda) ; Test completion. 1968 ((eq flag 'lambda) ; Test completion.
1915 (or (member string colors) 1969 (or (member string colors)
1916 (color-defined-p string))))) 1970 (color-defined-p string)))))
1917 nil t))) 1971 nil t)))