cus-face.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. ;;; cus-face.el --- customization support for faces
  2. ;;
  3. ;; Copyright (C) 1996-1997, 1999-2015 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
  6. ;; Keywords: help, faces
  7. ;; Package: emacs
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;
  21. ;; See `custom.el'.
  22. ;;; Code:
  23. (defalias 'custom-facep 'facep)
  24. ;;; Declaring a face.
  25. (defun custom-declare-face (face spec doc &rest args)
  26. "Like `defface', but with FACE evaluated as a normal argument."
  27. (unless (get face 'face-defface-spec)
  28. (face-spec-set face (purecopy spec) 'face-defface-spec)
  29. (push (cons 'defface face) current-load-list)
  30. (when doc
  31. (set-face-documentation face (purecopy doc)))
  32. (custom-handle-all-keywords face args 'custom-face)
  33. (run-hooks 'custom-define-hook))
  34. face)
  35. ;;; Face attributes.
  36. (defconst custom-face-attributes
  37. '((:family
  38. (string :tag "Font Family"
  39. :help-echo "Font family or fontset alias name."))
  40. (:foundry
  41. (string :tag "Font Foundry"
  42. :help-echo "Font foundry name."))
  43. (:width
  44. (choice :tag "Width"
  45. :help-echo "Font width."
  46. :value normal ; default
  47. (const :tag "compressed" condensed)
  48. (const :tag "condensed" condensed)
  49. (const :tag "demiexpanded" semi-expanded)
  50. (const :tag "expanded" expanded)
  51. (const :tag "extracondensed" extra-condensed)
  52. (const :tag "extraexpanded" extra-expanded)
  53. (const :tag "medium" normal)
  54. (const :tag "narrow" condensed)
  55. (const :tag "normal" normal)
  56. (const :tag "regular" normal)
  57. (const :tag "semicondensed" semi-condensed)
  58. (const :tag "semiexpanded" semi-expanded)
  59. (const :tag "ultracondensed" ultra-condensed)
  60. (const :tag "ultraexpanded" ultra-expanded)
  61. (const :tag "wide" extra-expanded)))
  62. (:height
  63. (choice :tag "Height"
  64. :help-echo "Face's font height."
  65. :value 1.0 ; default
  66. (integer :tag "Height in 1/10 pt")
  67. (number :tag "Scale" 1.0)))
  68. (:weight
  69. (choice :tag "Weight"
  70. :help-echo "Font weight."
  71. :value normal ; default
  72. (const :tag "black" ultra-bold)
  73. (const :tag "bold" bold)
  74. (const :tag "book" semi-light)
  75. (const :tag "demibold" semi-bold)
  76. (const :tag "extralight" extra-light)
  77. (const :tag "extrabold" extra-bold)
  78. (const :tag "heavy" extra-bold)
  79. (const :tag "light" light)
  80. (const :tag "medium" normal)
  81. (const :tag "normal" normal)
  82. (const :tag "regular" normal)
  83. (const :tag "semibold" semi-bold)
  84. (const :tag "semilight" semi-light)
  85. (const :tag "ultralight" ultra-light)
  86. (const :tag "ultrabold" ultra-bold)
  87. (const :tag "thin" thin)))
  88. (:slant
  89. (choice :tag "Slant"
  90. :help-echo "Font slant."
  91. :value normal ; default
  92. (const :tag "italic" italic)
  93. (const :tag "oblique" oblique)
  94. (const :tag "normal" normal)
  95. (const :tag "roman" roman)))
  96. (:underline
  97. (choice :tag "Underline"
  98. :help-echo "Control text underlining."
  99. (const :tag "Off" nil)
  100. (list :tag "On"
  101. :value (:color foreground-color :style line)
  102. (const :format "" :value :color)
  103. (choice :tag "Color"
  104. (const :tag "Foreground Color" foreground-color)
  105. color)
  106. (const :format "" :value :style)
  107. (choice :tag "Style"
  108. (const :tag "Line" line)
  109. (const :tag "Wave" wave))))
  110. ;; filter to make value suitable for customize
  111. (lambda (real-value)
  112. (and real-value
  113. (let ((color
  114. (or (and (consp real-value) (plist-get real-value :color))
  115. (and (stringp real-value) real-value)
  116. 'foreground-color))
  117. (style
  118. (or (and (consp real-value) (plist-get real-value :style))
  119. 'line)))
  120. (list :color color :style style))))
  121. ;; filter to make customized-value suitable for storing
  122. (lambda (cus-value)
  123. (and cus-value
  124. (let ((color (plist-get cus-value :color))
  125. (style (plist-get cus-value :style)))
  126. (cond ((eq style 'line)
  127. ;; Use simple value for default style
  128. (if (eq color 'foreground-color) t color))
  129. (t
  130. `(:color ,color :style ,style)))))))
  131. (:overline
  132. (choice :tag "Overline"
  133. :help-echo "Control text overlining."
  134. (const :tag "Off" nil)
  135. (const :tag "On" t)
  136. (color :tag "Colored")))
  137. (:strike-through
  138. (choice :tag "Strike-through"
  139. :help-echo "Control text strike-through."
  140. (const :tag "Off" nil)
  141. (const :tag "On" t)
  142. (color :tag "Colored")))
  143. (:box
  144. ;; Fixme: this can probably be done better.
  145. (choice :tag "Box around text"
  146. :help-echo "Control box around text."
  147. (const :tag "Off" nil)
  148. (list :tag "Box"
  149. :value (:line-width 2 :color "grey75" :style released-button)
  150. (const :format "" :value :line-width)
  151. (integer :tag "Width")
  152. (const :format "" :value :color)
  153. (choice :tag "Color" (const :tag "*" nil) color)
  154. (const :format "" :value :style)
  155. (choice :tag "Style"
  156. (const :tag "Raised" released-button)
  157. (const :tag "Sunken" pressed-button)
  158. (const :tag "None" nil))))
  159. ;; filter to make value suitable for customize
  160. (lambda (real-value)
  161. (and real-value
  162. (let ((lwidth
  163. (or (and (consp real-value)
  164. (plist-get real-value :line-width))
  165. (and (integerp real-value) real-value)
  166. 1))
  167. (color
  168. (or (and (consp real-value) (plist-get real-value :color))
  169. (and (stringp real-value) real-value)
  170. nil))
  171. (style
  172. (and (consp real-value) (plist-get real-value :style))))
  173. (list :line-width lwidth :color color :style style))))
  174. ;; filter to make customized-value suitable for storing
  175. (lambda (cus-value)
  176. (and cus-value
  177. (let ((lwidth (plist-get cus-value :line-width))
  178. (color (plist-get cus-value :color))
  179. (style (plist-get cus-value :style)))
  180. (cond ((and (null color) (null style))
  181. lwidth)
  182. ((and (null lwidth) (null style))
  183. ;; actually can't happen, because LWIDTH is always an int
  184. color)
  185. (t
  186. ;; Keep as a plist, but remove null entries
  187. (nconc (and lwidth `(:line-width ,lwidth))
  188. (and color `(:color ,color))
  189. (and style `(:style ,style)))))))))
  190. (:inverse-video
  191. (choice :tag "Inverse-video"
  192. :help-echo "Control whether text should be in inverse-video."
  193. (const :tag "Off" nil)
  194. (const :tag "On" t)))
  195. (:foreground
  196. (color :tag "Foreground"
  197. :help-echo "Set foreground color (name or #RRGGBB hex spec)."))
  198. (:distant-foreground
  199. (color :tag "Distant Foreground"
  200. :help-echo "Set distant foreground color (name or #RRGGBB hex spec)."))
  201. (:background
  202. (color :tag "Background"
  203. :help-echo "Set background color (name or #RRGGBB hex spec)."))
  204. (:stipple
  205. (choice :tag "Stipple"
  206. :help-echo "Background bit-mask"
  207. (const :tag "None" nil)
  208. (file :tag "File"
  209. :help-echo "Name of bitmap file."
  210. :must-match t)))
  211. (:inherit
  212. (repeat :tag "Inherit"
  213. :help-echo "List of faces to inherit attributes from."
  214. (face :Tag "Face" default))
  215. ;; filter to make value suitable for customize
  216. (lambda (real-value)
  217. (cond ((or (null real-value) (eq real-value 'unspecified))
  218. nil)
  219. ((symbolp real-value)
  220. (list real-value))
  221. (t
  222. real-value)))
  223. ;; filter to make customized-value suitable for storing
  224. (lambda (cus-value)
  225. (if (and (consp cus-value) (null (cdr cus-value)))
  226. (car cus-value)
  227. cus-value))))
  228. "Alist of face attributes.
  229. The elements are of the form (KEY TYPE PRE-FILTER POST-FILTER),
  230. where KEY is the name of the attribute, TYPE is a widget type for
  231. editing the attribute, PRE-FILTER is a function to make the attribute's
  232. value suitable for the customization widget, and POST-FILTER is a
  233. function to make the customized value suitable for storing. PRE-FILTER
  234. and POST-FILTER are optional.
  235. The PRE-FILTER should take a single argument, the attribute value as
  236. stored, and should return a value for customization (using the
  237. customization type TYPE).
  238. The POST-FILTER should also take a single argument, the value after
  239. being customized, and should return a value suitable for setting the
  240. given face attribute.")
  241. (defun custom-face-attributes-get (face frame)
  242. "For FACE on FRAME, return an alternating list describing its attributes.
  243. The list has the form (KEYWORD VALUE KEYWORD VALUE...).
  244. Each keyword should be listed in `custom-face-attributes'.
  245. If FRAME is nil, use the global defaults for FACE."
  246. (let ((attrs custom-face-attributes)
  247. plist)
  248. (while attrs
  249. (let* ((attribute (car (car attrs)))
  250. (value (face-attribute face attribute frame)))
  251. (setq attrs (cdr attrs))
  252. (unless (or (eq value 'unspecified)
  253. (and (null value) (memq attribute '(:inherit))))
  254. (setq plist (cons attribute (cons value plist))))))
  255. plist))
  256. ;;; Initializing.
  257. (defun custom-set-faces (&rest args)
  258. "Apply a list of face specs for user customizations.
  259. This works by calling `custom-theme-set-faces' for the `user'
  260. theme, a special theme referring to settings made via Customize.
  261. The arguments should be a list where each entry has the form:
  262. (FACE SPEC [NOW [COMMENT]])
  263. See the documentation of `custom-theme-set-faces' for details."
  264. (apply 'custom-theme-set-faces 'user args))
  265. (defun custom-theme-set-faces (theme &rest args)
  266. "Apply a list of face specs associated with theme THEME.
  267. THEME should be a theme name (a symbol). The special theme named
  268. `user' refers to user settings applied via Customize.
  269. The remaining ARGS should be a list where each entry is a list of
  270. the form:
  271. (FACE SPEC [NOW [COMMENT]])
  272. FACE should be a face name (a symbol). If FACE is a face alias,
  273. the setting refers to the parent face.
  274. SPEC should be a face spec. For details, see `defface'.
  275. NOW, if present and non-nil, forces the face settings to take
  276. immediate effect in the Emacs display; in particular, FACE is
  277. initialized as a face if it is not yet one. If NOW is omitted or
  278. nil, the caller is responsible for making the settings take
  279. effect later, by calling `custom-theme-recalc-face' or
  280. `face-spec-recalc'.
  281. COMMENT is a string comment about FACE.
  282. This function works by calling `custom-push-theme' to record each
  283. SPEC in each FACE's `theme-face' property, and in THEME's
  284. `theme-settings' property. If FACE has not already been
  285. customized, it also stores SPEC in the `saved-face' property.
  286. If THEME has a non-nil `theme-immediate' property, this is
  287. equivalent to providing the NOW argument to all faces in the
  288. argument list."
  289. (custom-check-theme theme)
  290. (let ((immediate (get theme 'theme-immediate)))
  291. (dolist (entry args)
  292. (unless (listp entry)
  293. (error "Incompatible Custom theme spec"))
  294. (let ((face (car entry))
  295. (spec (nth 1 entry)))
  296. ;; If FACE is actually an alias, customize the face it
  297. ;; is aliased to.
  298. (if (get face 'face-alias)
  299. (setq face (get face 'face-alias)))
  300. (if custom--inhibit-theme-enable
  301. ;; Just update theme settings.
  302. (custom-push-theme 'theme-face face theme 'set spec)
  303. ;; Update theme settings and set the face spec.
  304. (let ((now (nth 2 entry))
  305. (comment (nth 3 entry))
  306. (oldspec (get face 'theme-face)))
  307. (when (not (and oldspec (eq 'user (caar oldspec))))
  308. (put face 'saved-face spec)
  309. (put face 'saved-face-comment comment))
  310. (custom-push-theme 'theme-face face theme 'set spec)
  311. (when (or now immediate)
  312. (put face 'force-face (if now 'rogue 'immediate)))
  313. (when (or now immediate (facep face))
  314. (put face 'face-comment comment)
  315. (face-spec-set face spec t))))))))
  316. ;; XEmacs compatibility function. In XEmacs, when you reset a Custom
  317. ;; Theme, you have to specify the theme to reset it to. We just apply
  318. ;; the next theme.
  319. (defun custom-theme-reset-faces (theme &rest args)
  320. "Reset the specs in THEME of some faces to their specs in other themes.
  321. Each of the arguments ARGS has this form:
  322. (FACE IGNORED)
  323. This means reset FACE. The argument IGNORED is ignored."
  324. (custom-check-theme theme)
  325. (dolist (arg args)
  326. (custom-push-theme 'theme-face (car arg) theme 'reset)))
  327. (defun custom-reset-faces (&rest args)
  328. "Reset the specs of some faces to their specs in specified themes.
  329. This creates settings in the `user' theme.
  330. Each of the arguments ARGS has this form:
  331. (FACE FROM-THEME)
  332. This means reset FACE to its value in FROM-THEME."
  333. (apply 'custom-theme-reset-faces 'user args))
  334. ;;; The End.
  335. (provide 'cus-face)
  336. ;;; cus-face.el ends here