face-remap.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. ;;; face-remap.el --- Functions for managing `face-remapping-alist'
  2. ;;
  3. ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Miles Bader <miles@gnu.org>
  6. ;; Keywords: faces, face remapping, display, user commands
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;;
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;;
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; This file defines some simple operations that can be used for
  26. ;; maintaining the `face-remapping-alist' in a cooperative way. This is
  27. ;; especially important for the `default' face.
  28. ;;
  29. ;; Each face-remapping definition in `face-remapping-alist' added by
  30. ;; this code uses the form:
  31. ;;
  32. ;; (face RELATIVE_SPECS_1 RELATIVE_SPECS_2 ... BASE_SPECS)
  33. ;;
  34. ;; The "specs" values are a lists of face names or face attribute-value
  35. ;; pairs, and are merged together, with earlier values taking precedence.
  36. ;;
  37. ;; The RELATIVE_SPECS_* values are added by `face-remap-add-relative'
  38. ;; (and removed by `face-remap-remove-relative', and are intended for
  39. ;; face "modifications" (such as increasing the size). Typical users of
  40. ;; relative specs would be minor modes.
  41. ;;
  42. ;; BASE_SPECS is the lowest-priority value, and by default is just the
  43. ;; face name, which causes the global definition of that face to be used.
  44. ;;
  45. ;; A non-default value of BASE_SPECS may also be set using
  46. ;; `face-remap-set-base'. Because this _overwrites_ the default
  47. ;; value inheriting from the global face definition, it is up to the
  48. ;; caller of face-remap-set-base to add such inheritance if it is
  49. ;; desired. A typical use of face-remap-set-base would be a major
  50. ;; mode setting face remappings, e.g., of the default face.
  51. ;;
  52. ;; All modifications cause face-remapping-alist to be made buffer-local.
  53. ;;
  54. ;;; Code:
  55. ;; ----------------------------------------------------------------
  56. ;; Utility functions
  57. ;; Names of face attributes corresponding to lisp face-vector positions.
  58. ;; This variable should probably be defined in C code where the actual
  59. ;; definitions are available.
  60. ;;
  61. (defvar internal-lisp-face-attributes
  62. [nil
  63. :family :foundry :swidth :height :weight :slant :underline :inverse
  64. :foreground :background :stipple :overline :strike :box
  65. :font :inherit :fontset :vector])
  66. (defun face-attrs-more-relative-p (attrs1 attrs2)
  67. "Return true if ATTRS1 contains a greater number of relative
  68. face-attributes than ATTRS2. A face attribute is considered
  69. relative if `face-attribute-relative-p' returns non-nil.
  70. ATTRS1 and ATTRS2 may be any value suitable for a `face' text
  71. property, including face names, lists of face names,
  72. face-attribute plists, etc.
  73. This function can be used as a predicate with `sort', to sort
  74. face lists so that more specific faces are located near the end."
  75. (unless (vectorp attrs1)
  76. (setq attrs1 (face-attributes-as-vector attrs1)))
  77. (unless (vectorp attrs2)
  78. (setq attrs2 (face-attributes-as-vector attrs2)))
  79. (let ((rel1-count 0) (rel2-count 0))
  80. (dotimes (i (length attrs1))
  81. (let ((attr (aref internal-lisp-face-attributes i)))
  82. (when attr
  83. (when (face-attribute-relative-p attr (aref attrs1 i))
  84. (setq rel1-count (+ rel1-count 1)))
  85. (when (face-attribute-relative-p attr (aref attrs2 i))
  86. (setq rel2-count (+ rel2-count 1))))))
  87. (< rel1-count rel2-count)))
  88. (defun face-remap-order (entry)
  89. "Order ENTRY so that more relative face specs are near the beginning.
  90. The list structure of ENTRY may be destructively modified."
  91. (setq entry (nreverse entry))
  92. (setcdr entry (sort (cdr entry) 'face-attrs-more-relative-p))
  93. (nreverse entry))
  94. ;;;###autoload
  95. (defun face-remap-add-relative (face &rest specs)
  96. "Add a face remapping entry of FACE to SPECS in the current buffer.
  97. Return a cookie which can be used to delete this remapping with
  98. `face-remap-remove-relative'.
  99. The remaining arguments, SPECS, should be either a list of face
  100. names, or a property list of face attribute/value pairs. The
  101. remapping specified by SPECS takes effect alongside the
  102. remappings from other calls to `face-remap-add-relative', as well
  103. as the normal definition of FACE (at lowest priority). This
  104. function tries to sort multiple remappings for the same face, so
  105. that remappings specifying relative face attributes are applied
  106. after remappings specifying absolute face attributes.
  107. The base (lowest priority) remapping may be set to something
  108. other than the normal definition of FACE via `face-remap-set-base'."
  109. (while (and (consp specs) (null (cdr specs)))
  110. (setq specs (car specs)))
  111. (make-local-variable 'face-remapping-alist)
  112. (let ((entry (assq face face-remapping-alist)))
  113. (when (null entry)
  114. (setq entry (list face face)) ; explicitly merge with global def
  115. (push entry face-remapping-alist))
  116. (setcdr entry (face-remap-order (cons specs (cdr entry))))
  117. (cons face specs)))
  118. (defun face-remap-remove-relative (cookie)
  119. "Remove a face remapping previously added by `face-remap-add-relative'.
  120. COOKIE should be the return value from that function."
  121. (let ((remapping (assq (car cookie) face-remapping-alist)))
  122. (when remapping
  123. (let ((updated-entries (remq (cdr cookie) (cdr remapping))))
  124. (unless (eq updated-entries (cdr remapping))
  125. (setcdr remapping updated-entries)
  126. (when (or (null updated-entries)
  127. (and (eq (car-safe updated-entries) (car cookie))
  128. (null (cdr updated-entries))))
  129. (setq face-remapping-alist
  130. (remq remapping face-remapping-alist)))
  131. (cdr cookie))))))
  132. ;;;###autoload
  133. (defun face-remap-reset-base (face)
  134. "Set the base remapping of FACE to the normal definition of FACE.
  135. This causes the remappings specified by `face-remap-add-relative'
  136. to apply on top of the normal definition of FACE."
  137. (let ((entry (assq face face-remapping-alist)))
  138. (when entry
  139. ;; If there's nothing except a base remapping, we simply remove
  140. ;; the entire remapping entry, as setting the base to the default
  141. ;; would be the same as the global definition. Otherwise, we
  142. ;; modify the base remapping.
  143. (if (null (cddr entry)) ; nothing except base remapping
  144. (setq face-remapping-alist ; so remove entire entry
  145. (remq entry face-remapping-alist))
  146. (setcar (last entry) face))))) ; otherwise, just inherit global def
  147. ;;;###autoload
  148. (defun face-remap-set-base (face &rest specs)
  149. "Set the base remapping of FACE in the current buffer to SPECS.
  150. This causes the remappings specified by `face-remap-add-relative'
  151. to apply on top of the face specification given by SPECS. SPECS
  152. should be either a list of face names, or a property list of face
  153. attribute/value pairs.
  154. If SPECS is empty, call `face-remap-reset-base' to use the normal
  155. definition of FACE as the base remapping; note that this is
  156. different from SPECS containing a single value `nil', which means
  157. not to inherit from the global definition of FACE at all."
  158. (while (and (consp specs) (not (null (car specs))) (null (cdr specs)))
  159. (setq specs (car specs)))
  160. (if (or (null specs)
  161. (and (eq (car specs) face) (null (cdr specs)))) ; default
  162. ;; Set entry back to default
  163. (face-remap-reset-base face)
  164. ;; Set the base remapping
  165. (make-local-variable 'face-remapping-alist)
  166. (let ((entry (assq face face-remapping-alist)))
  167. (if entry
  168. (setcar (last entry) specs) ; overwrite existing base entry
  169. (push (list face specs) face-remapping-alist)))))
  170. ;; ----------------------------------------------------------------
  171. ;; text-scale-mode
  172. (defcustom text-scale-mode-step 1.2
  173. "Scale factor used by `text-scale-mode'.
  174. Each positive or negative step scales the default face height by this amount."
  175. :group 'display
  176. :type 'number
  177. :version "23.1")
  178. ;; current remapping cookie for text-scale-mode
  179. (defvar text-scale-mode-remapping nil)
  180. (make-variable-buffer-local 'text-scale-mode-remapping)
  181. ;; Lighter displayed for text-scale-mode in mode-line minor-mode list
  182. (defvar text-scale-mode-lighter "+0")
  183. (make-variable-buffer-local 'text-scale-mode-lighter)
  184. ;; Number of steps that text-scale-mode will increase/decrease text height
  185. (defvar text-scale-mode-amount 0)
  186. (make-variable-buffer-local 'text-scale-mode-amount)
  187. (define-minor-mode text-scale-mode
  188. "Minor mode for displaying buffer text in a larger/smaller font.
  189. With a prefix argument ARG, enable the mode if ARG is positive,
  190. and disable it otherwise. If called from Lisp, enable the mode
  191. if ARG is omitted or nil.
  192. The amount of scaling is determined by the variable
  193. `text-scale-mode-amount': one step scales the global default
  194. face size by the value of the variable `text-scale-mode-step'
  195. \(a negative amount shrinks the text).
  196. The `text-scale-increase', `text-scale-decrease', and
  197. `text-scale-set' functions may be used to interactively modify
  198. the variable `text-scale-mode-amount' (they also enable or
  199. disable `text-scale-mode' as necessary)."
  200. :lighter (" " text-scale-mode-lighter)
  201. (when text-scale-mode-remapping
  202. (face-remap-remove-relative text-scale-mode-remapping))
  203. (setq text-scale-mode-lighter
  204. (format (if (>= text-scale-mode-amount 0) "+%d" "%d")
  205. text-scale-mode-amount))
  206. (setq text-scale-mode-remapping
  207. (and text-scale-mode
  208. (face-remap-add-relative 'default
  209. :height
  210. (expt text-scale-mode-step
  211. text-scale-mode-amount))))
  212. (force-window-update (current-buffer)))
  213. ;;;###autoload
  214. (defun text-scale-set (level)
  215. "Set the scale factor of the default face in the current buffer to LEVEL.
  216. If LEVEL is non-zero, `text-scale-mode' is enabled, otherwise it is disabled.
  217. LEVEL is a number of steps, with 0 representing the default size.
  218. Each step scales the height of the default face by the variable
  219. `text-scale-mode-step' (a negative number decreases the height by
  220. the same amount)."
  221. (interactive "p")
  222. (setq text-scale-mode-amount level)
  223. (text-scale-mode (if (zerop text-scale-mode-amount) -1 1)))
  224. ;;;###autoload
  225. (defun text-scale-increase (inc)
  226. "Increase the height of the default face in the current buffer by INC steps.
  227. If the new height is other than the default, `text-scale-mode' is enabled.
  228. Each step scales the height of the default face by the variable
  229. `text-scale-mode-step' (a negative number of steps decreases the
  230. height by the same amount). As a special case, an argument of 0
  231. will remove any scaling currently active."
  232. (interactive "p")
  233. (setq text-scale-mode-amount
  234. (if (= inc 0) 0 (+ (if text-scale-mode text-scale-mode-amount 0) inc)))
  235. (text-scale-mode (if (zerop text-scale-mode-amount) -1 1)))
  236. ;;;###autoload
  237. (defun text-scale-decrease (dec)
  238. "Decrease the height of the default face in the current buffer by DEC steps.
  239. See `text-scale-increase' for more details."
  240. (interactive "p")
  241. (text-scale-increase (- dec)))
  242. ;;;###autoload (define-key ctl-x-map [(control ?+)] 'text-scale-adjust)
  243. ;;;###autoload (define-key ctl-x-map [(control ?-)] 'text-scale-adjust)
  244. ;;;###autoload (define-key ctl-x-map [(control ?=)] 'text-scale-adjust)
  245. ;;;###autoload (define-key ctl-x-map [(control ?0)] 'text-scale-adjust)
  246. ;;;###autoload
  247. (defun text-scale-adjust (inc)
  248. "Increase or decrease the height of the default face in the current buffer.
  249. The actual adjustment made depends on the final component of the
  250. key-binding used to invoke the command, with all modifiers removed:
  251. +, = Increase the default face height by one step
  252. - Decrease the default face height by one step
  253. 0 Reset the default face height to the global default
  254. Then, continue to read input events and further adjust the face
  255. height as long as the input event read (with all modifiers removed)
  256. is one of the above.
  257. Each step scales the height of the default face by the variable
  258. `text-scale-mode-step' (a negative number of steps decreases the
  259. height by the same amount). As a special case, an argument of 0
  260. will remove any scaling currently active.
  261. This command is a special-purpose wrapper around the
  262. `text-scale-increase' command which makes repetition convenient
  263. even when it is bound in a non-top-level keymap. For binding in
  264. a top-level keymap, `text-scale-increase' or
  265. `text-scale-decrease' may be more appropriate."
  266. (interactive "p")
  267. (let ((first t)
  268. (step t)
  269. (ev last-command-event)
  270. (echo-keystrokes nil))
  271. (while step
  272. (let ((base (event-basic-type ev)))
  273. (cond ((or (eq base ?+) (eq base ?=))
  274. (setq step inc))
  275. ((eq base ?-)
  276. (setq step (- inc)))
  277. ((eq base ?0)
  278. (setq step 0))
  279. (first
  280. (setq step inc))
  281. (t
  282. (setq step nil))))
  283. (when step
  284. (text-scale-increase step)
  285. (setq inc 1 first nil)
  286. (setq ev (read-event "+,-,0 for further adjustment: "))))
  287. (push ev unread-command-events)))
  288. ;; ----------------------------------------------------------------
  289. ;; buffer-face-mode
  290. (defcustom buffer-face-mode-face 'variable-pitch
  291. "The face specification used by `buffer-face-mode'.
  292. It may contain any value suitable for a `face' text property,
  293. including a face name, a list of face names, a face-attribute
  294. plist, etc."
  295. :group 'display
  296. :version "23.1")
  297. ;; current remapping cookie for buffer-face-mode
  298. (defvar buffer-face-mode-remapping nil)
  299. (make-variable-buffer-local 'buffer-face-mode-remapping)
  300. ;;;###autoload
  301. (define-minor-mode buffer-face-mode
  302. "Minor mode for a buffer-specific default face.
  303. With a prefix argument ARG, enable the mode if ARG is positive,
  304. and disable it otherwise. If called from Lisp, enable the mode
  305. if ARG is omitted or nil. When enabled, the face specified by the
  306. variable `buffer-face-mode-face' is used to display the buffer text."
  307. :lighter " BufFace"
  308. (when buffer-face-mode-remapping
  309. (face-remap-remove-relative buffer-face-mode-remapping))
  310. (setq buffer-face-mode-remapping
  311. (and buffer-face-mode
  312. (face-remap-add-relative 'default buffer-face-mode-face)))
  313. (force-window-update (current-buffer)))
  314. ;;;###autoload
  315. (defun buffer-face-set (&rest specs)
  316. "Enable `buffer-face-mode', using face specs SPECS.
  317. SPECS can be any value suitable for the `face' text property,
  318. including a face name, a list of face names, or a face-attribute
  319. If SPECS is nil, then `buffer-face-mode' is disabled.
  320. This function will make the variable `buffer-face-mode-face'
  321. buffer local, and set it to FACE."
  322. (interactive (list (read-face-name "Set buffer face")))
  323. (while (and (consp specs) (null (cdr specs)))
  324. (setq specs (car specs)))
  325. (if (null specs)
  326. (buffer-face-mode 0)
  327. (set (make-local-variable 'buffer-face-mode-face) specs)
  328. (buffer-face-mode t)))
  329. ;;;###autoload
  330. (defun buffer-face-toggle (&rest specs)
  331. "Toggle `buffer-face-mode', using face specs SPECS.
  332. SPECS can be any value suitable for the `face' text property,
  333. including a face name, a list of face names, or a face-attribute
  334. If `buffer-face-mode' is already enabled, and is currently using
  335. the face specs SPECS, then it is disabled; if buffer-face-mode is
  336. disabled, or is enabled and currently displaying some other face,
  337. then is left enabled, but the face changed to reflect SPECS.
  338. This function will make the variable `buffer-face-mode-face'
  339. buffer local, and set it to SPECS."
  340. (interactive (list buffer-face-mode-face))
  341. (while (and (consp specs) (null (cdr specs)))
  342. (setq specs (car specs)))
  343. (if (or (null specs)
  344. (and buffer-face-mode (equal buffer-face-mode-face specs)))
  345. (buffer-face-mode 0)
  346. (set (make-local-variable 'buffer-face-mode-face) specs)
  347. (buffer-face-mode t)))
  348. (defun buffer-face-mode-invoke (specs arg &optional interactive)
  349. "Enable or disable `buffer-face-mode' using face specs SPECS, and argument ARG.
  350. ARG controls whether the mode is enabled or disabled, and is
  351. interpreted in the usual manner for minor-mode commands.
  352. SPECS can be any value suitable for the `face' text property,
  353. including a face name, a list of face names, or a face-attribute
  354. If INTERACTIVE is non-nil, a message will be displayed describing the result.
  355. This is a wrapper function which calls `buffer-face-set' or
  356. `buffer-face-toggle' (depending on ARG), and prints a status
  357. message in the echo area. In many cases one of those functions
  358. may be more appropriate."
  359. (let ((last-message (current-message)))
  360. (if (or (eq arg 'toggle) (not arg))
  361. (buffer-face-toggle specs)
  362. (buffer-face-set (and (> (prefix-numeric-value arg) 0) specs)))
  363. (when interactive
  364. (unless (and (current-message)
  365. (not (equal last-message (current-message))))
  366. (message "Buffer-Face mode %sabled"
  367. (if buffer-face-mode "en" "dis"))))))
  368. ;; ----------------------------------------------------------------
  369. ;; variable-pitch-mode
  370. ;;;###autoload
  371. (defun variable-pitch-mode (&optional arg)
  372. "Variable-pitch default-face mode.
  373. An interface to `buffer-face-mode' which uses the `variable-pitch' face.
  374. Besides the choice of face, it is the same as `buffer-face-mode'."
  375. (interactive (list (or current-prefix-arg 'toggle)))
  376. (buffer-face-mode-invoke 'variable-pitch arg
  377. (called-interactively-p 'interactive)))
  378. (provide 'face-remap)
  379. ;;; face-remap.el ends here