cus-face.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. ;;; cus-face.el --- customization support for faces
  2. ;;
  3. ;; Copyright (C) 1996-1997, 1999-2012 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 FACE is evaluated as a normal argument."
  27. (unless (get face 'face-defface-spec)
  28. (let ((facep (facep face)))
  29. (unless facep
  30. ;; If the user has already created the face, respect that.
  31. (let ((value (or (get face 'saved-face) spec))
  32. (have-window-system (memq initial-window-system '(x w32))))
  33. ;; Create global face.
  34. (make-empty-face face)
  35. ;; Create frame-local faces
  36. (dolist (frame (frame-list))
  37. (face-spec-set-2 face frame value)
  38. (when (memq (window-system frame) '(x w32 ns))
  39. (setq have-window-system t)))
  40. ;; When making a face after frames already exist
  41. (if have-window-system
  42. (make-face-x-resource-internal face))))
  43. ;; Don't record SPEC until we see it causes no errors.
  44. (put face 'face-defface-spec (purecopy spec))
  45. (push (cons 'defface face) current-load-list)
  46. (when (and doc (null (face-documentation face)))
  47. (set-face-documentation face (purecopy doc)))
  48. (custom-handle-all-keywords face args 'custom-face)
  49. (run-hooks 'custom-define-hook)
  50. ;; If the face had existing settings, recalculate it. For
  51. ;; example, the user might load a theme with a face setting, and
  52. ;; later load a library defining that face.
  53. (if facep
  54. (custom-theme-recalc-face face))))
  55. face)
  56. ;;; Face attributes.
  57. (defconst custom-face-attributes
  58. '((:family
  59. (string :tag "Font Family"
  60. :help-echo "Font family or fontset alias name."))
  61. (:foundry
  62. (string :tag "Font Foundry"
  63. :help-echo "Font foundry name."))
  64. (:width
  65. (choice :tag "Width"
  66. :help-echo "Font width."
  67. :value normal ; default
  68. (const :tag "compressed" condensed)
  69. (const :tag "condensed" condensed)
  70. (const :tag "demiexpanded" semi-expanded)
  71. (const :tag "expanded" expanded)
  72. (const :tag "extracondensed" extra-condensed)
  73. (const :tag "extraexpanded" extra-expanded)
  74. (const :tag "medium" normal)
  75. (const :tag "narrow" condensed)
  76. (const :tag "normal" normal)
  77. (const :tag "regular" normal)
  78. (const :tag "semicondensed" semi-condensed)
  79. (const :tag "semiexpanded" semi-expanded)
  80. (const :tag "ultracondensed" ultra-condensed)
  81. (const :tag "ultraexpanded" ultra-expanded)
  82. (const :tag "wide" extra-expanded)))
  83. (:height
  84. (choice :tag "Height"
  85. :help-echo "Face's font height."
  86. :value 1.0 ; default
  87. (integer :tag "Height in 1/10 pt")
  88. (number :tag "Scale" 1.0)))
  89. (:weight
  90. (choice :tag "Weight"
  91. :help-echo "Font weight."
  92. :value normal ; default
  93. (const :tag "black" ultra-bold)
  94. (const :tag "bold" bold)
  95. (const :tag "book" semi-light)
  96. (const :tag "demibold" semi-bold)
  97. (const :tag "extralight" extra-light)
  98. (const :tag "extrabold" extra-bold)
  99. (const :tag "heavy" extra-bold)
  100. (const :tag "light" light)
  101. (const :tag "medium" normal)
  102. (const :tag "normal" normal)
  103. (const :tag "regular" normal)
  104. (const :tag "semibold" semi-bold)
  105. (const :tag "semilight" semi-light)
  106. (const :tag "ultralight" ultra-light)
  107. (const :tag "ultrabold" ultra-bold)
  108. (const :tag "thin" thin)))
  109. (:slant
  110. (choice :tag "Slant"
  111. :help-echo "Font slant."
  112. :value normal ; default
  113. (const :tag "italic" italic)
  114. (const :tag "oblique" oblique)
  115. (const :tag "normal" normal)
  116. (const :tag "roman" roman)))
  117. (:underline
  118. (choice :tag "Underline"
  119. :help-echo "Control text underlining."
  120. (const :tag "Off" nil)
  121. (const :tag "On" t)
  122. (color :tag "Colored")))
  123. (:overline
  124. (choice :tag "Overline"
  125. :help-echo "Control text overlining."
  126. (const :tag "Off" nil)
  127. (const :tag "On" t)
  128. (color :tag "Colored")))
  129. (:strike-through
  130. (choice :tag "Strike-through"
  131. :help-echo "Control text strike-through."
  132. (const :tag "Off" nil)
  133. (const :tag "On" t)
  134. (color :tag "Colored")))
  135. (:box
  136. ;; Fixme: this can probably be done better.
  137. (choice :tag "Box around text"
  138. :help-echo "Control box around text."
  139. (const :tag "Off" nil)
  140. (list :tag "Box"
  141. :value (:line-width 2 :color "grey75" :style released-button)
  142. (const :format "" :value :line-width)
  143. (integer :tag "Width")
  144. (const :format "" :value :color)
  145. (choice :tag "Color" (const :tag "*" nil) color)
  146. (const :format "" :value :style)
  147. (choice :tag "Style"
  148. (const :tag "Raised" released-button)
  149. (const :tag "Sunken" pressed-button)
  150. (const :tag "None" nil))))
  151. ;; filter to make value suitable for customize
  152. (lambda (real-value)
  153. (and real-value
  154. (let ((lwidth
  155. (or (and (consp real-value)
  156. (plist-get real-value :line-width))
  157. (and (integerp real-value) real-value)
  158. 1))
  159. (color
  160. (or (and (consp real-value) (plist-get real-value :color))
  161. (and (stringp real-value) real-value)
  162. nil))
  163. (style
  164. (and (consp real-value) (plist-get real-value :style))))
  165. (list :line-width lwidth :color color :style style))))
  166. ;; filter to make customized-value suitable for storing
  167. (lambda (cus-value)
  168. (and cus-value
  169. (let ((lwidth (plist-get cus-value :line-width))
  170. (color (plist-get cus-value :color))
  171. (style (plist-get cus-value :style)))
  172. (cond ((and (null color) (null style))
  173. lwidth)
  174. ((and (null lwidth) (null style))
  175. ;; actually can't happen, because LWIDTH is always an int
  176. color)
  177. (t
  178. ;; Keep as a plist, but remove null entries
  179. (nconc (and lwidth `(:line-width ,lwidth))
  180. (and color `(:color ,color))
  181. (and style `(:style ,style)))))))))
  182. (:inverse-video
  183. (choice :tag "Inverse-video"
  184. :help-echo "Control whether text should be in inverse-video."
  185. (const :tag "Off" nil)
  186. (const :tag "On" t)))
  187. (:foreground
  188. (color :tag "Foreground"
  189. :help-echo "Set foreground color (name or #RRGGBB hex spec)."))
  190. (:background
  191. (color :tag "Background"
  192. :help-echo "Set background color (name or #RRGGBB hex spec)."))
  193. (:stipple
  194. (choice :tag "Stipple"
  195. :help-echo "Background bit-mask"
  196. (const :tag "None" nil)
  197. (file :tag "File"
  198. :help-echo "Name of bitmap file."
  199. :must-match t)))
  200. (:inherit
  201. (repeat :tag "Inherit"
  202. :help-echo "List of faces to inherit attributes from."
  203. (face :Tag "Face" default))
  204. ;; filter to make value suitable for customize
  205. (lambda (real-value)
  206. (cond ((or (null real-value) (eq real-value 'unspecified))
  207. nil)
  208. ((symbolp real-value)
  209. (list real-value))
  210. (t
  211. real-value)))
  212. ;; filter to make customized-value suitable for storing
  213. (lambda (cus-value)
  214. (if (and (consp cus-value) (null (cdr cus-value)))
  215. (car cus-value)
  216. cus-value))))
  217. "Alist of face attributes.
  218. The elements are of the form (KEY TYPE PRE-FILTER POST-FILTER),
  219. where KEY is the name of the attribute, TYPE is a widget type for
  220. editing the attribute, PRE-FILTER is a function to make the attribute's
  221. value suitable for the customization widget, and POST-FILTER is a
  222. function to make the customized value suitable for storing. PRE-FILTER
  223. and POST-FILTER are optional.
  224. The PRE-FILTER should take a single argument, the attribute value as
  225. stored, and should return a value for customization (using the
  226. customization type TYPE).
  227. The POST-FILTER should also take a single argument, the value after
  228. being customized, and should return a value suitable for setting the
  229. given face attribute.")
  230. (defun custom-face-attributes-get (face frame)
  231. "For FACE on FRAME, return an alternating list describing its attributes.
  232. The list has the form (KEYWORD VALUE KEYWORD VALUE...).
  233. Each keyword should be listed in `custom-face-attributes'.
  234. If FRAME is nil, use the global defaults for FACE."
  235. (let ((attrs custom-face-attributes)
  236. plist)
  237. (while attrs
  238. (let* ((attribute (car (car attrs)))
  239. (value (face-attribute face attribute frame)))
  240. (setq attrs (cdr attrs))
  241. (unless (or (eq value 'unspecified)
  242. (and (null value) (memq attribute '(:inherit))))
  243. (setq plist (cons attribute (cons value plist))))))
  244. plist))
  245. ;;; Initializing.
  246. (defun custom-set-faces (&rest args)
  247. "Initialize faces according to user preferences.
  248. This associates the settings with the `user' theme.
  249. The arguments should be a list where each entry has the form:
  250. (FACE SPEC [NOW [COMMENT]])
  251. SPEC is stored as the saved value for FACE, as well as the value for the
  252. `user' theme. The `user' theme is one of the default themes known to Emacs.
  253. See `custom-known-themes' for more information on the known themes.
  254. See `custom-theme-set-faces' for more information on the interplay
  255. between themes and faces.
  256. See `defface' for the format of SPEC.
  257. If NOW is present and non-nil, FACE is created now, according to SPEC.
  258. COMMENT is a string comment about FACE."
  259. (apply 'custom-theme-set-faces 'user args))
  260. (defun custom-theme-set-faces (theme &rest args)
  261. "Initialize faces for theme THEME.
  262. The arguments should be a list where each entry has the form:
  263. (FACE SPEC [NOW [COMMENT]])
  264. SPEC is stored as the saved value for FACE, as well as the value for the
  265. `user' theme. The `user' theme is one of the default themes known to Emacs.
  266. See `custom-known-themes' for more information on the known themes.
  267. See `custom-theme-set-faces' for more information on the interplay
  268. between themes and faces.
  269. See `defface' for the format of SPEC.
  270. If NOW is present and non-nil, FACE is created now, according to SPEC.
  271. COMMENT is a string comment about FACE.
  272. Several properties of THEME and FACE are used in the process:
  273. If THEME property `theme-immediate' is non-nil, this is equivalent of
  274. providing the NOW argument to all faces in the argument list: FACE is
  275. created now. The only difference is FACE property `force-face': if NOW
  276. is non-nil, FACE property `force-face' is set to the symbol `rogue', else
  277. if THEME property `theme-immediate' is non-nil, FACE property `force-face'
  278. is set to the symbol `immediate'.
  279. SPEC itself is saved in FACE property `saved-face' and it is stored in
  280. FACE's list property `theme-face' \(using `custom-push-theme')."
  281. (custom-check-theme theme)
  282. (let ((immediate (get theme 'theme-immediate)))
  283. (dolist (entry args)
  284. (unless (listp entry)
  285. (error "Incompatible Custom theme spec"))
  286. (let ((face (car entry))
  287. (spec (nth 1 entry)))
  288. ;; If FACE is actually an alias, customize the face it
  289. ;; is aliased to.
  290. (if (get face 'face-alias)
  291. (setq face (get face 'face-alias)))
  292. (if custom--inhibit-theme-enable
  293. ;; Just update theme settings.
  294. (custom-push-theme 'theme-face face theme 'set spec)
  295. ;; Update theme settings and set the face spec.
  296. (let ((now (nth 2 entry))
  297. (comment (nth 3 entry))
  298. (oldspec (get face 'theme-face)))
  299. (when (not (and oldspec (eq 'user (caar oldspec))))
  300. (put face 'saved-face spec)
  301. (put face 'saved-face-comment comment))
  302. ;; Do this AFTER checking the `theme-face' property.
  303. (custom-push-theme 'theme-face face theme 'set spec)
  304. (when (or now immediate)
  305. (put face 'force-face (if now 'rogue 'immediate)))
  306. (when (or now immediate (facep face))
  307. (unless (facep face)
  308. (make-empty-face face))
  309. (put face 'face-comment comment)
  310. (put face 'face-override-spec nil)
  311. (face-spec-set face spec t))))))))
  312. ;; XEmacs compatibility function. In XEmacs, when you reset a Custom
  313. ;; Theme, you have to specify the theme to reset it to. We just apply
  314. ;; the next theme.
  315. (defun custom-theme-reset-faces (theme &rest args)
  316. "Reset the specs in THEME of some faces to their specs in other themes.
  317. Each of the arguments ARGS has this form:
  318. (FACE IGNORED)
  319. This means reset FACE. The argument IGNORED is ignored."
  320. (custom-check-theme theme)
  321. (dolist (arg args)
  322. (custom-push-theme 'theme-face (car arg) theme 'reset)))
  323. (defun custom-reset-faces (&rest args)
  324. "Reset the specs of some faces to their specs in specified themes.
  325. This creates settings in the `user' theme.
  326. Each of the arguments ARGS has this form:
  327. (FACE FROM-THEME)
  328. This means reset FACE to its value in FROM-THEME."
  329. (apply 'custom-theme-reset-faces 'user args))
  330. ;;; The End.
  331. (provide 'cus-face)
  332. ;;; cus-face.el ends here