face-remap.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. ;;; face-remap.el --- Functions for managing `face-remapping-alist' -*- lexical-binding: t -*-
  2. ;;
  3. ;; Copyright (C) 2008-2017 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 form a list of faces.
  100. Each list element should be either a face name or a property list
  101. of face attribute/value pairs. If more than one face is listed,
  102. that specifies an aggregate face, in the same way as in a `face'
  103. text property, except for possible priority changes noted below.
  104. The face remapping specified by SPECS takes effect alongside the
  105. remappings from other calls to `face-remap-add-relative' for the
  106. same FACE, as well as the normal definition of FACE (at lowest
  107. priority). This function tries to sort multiple remappings for
  108. the same face, so that remappings specifying relative face
  109. attributes are applied after remappings specifying absolute face
  110. attributes.
  111. The base (lowest priority) remapping may be set to something
  112. other than the normal definition of FACE via `face-remap-set-base'."
  113. (while (and (consp specs) (null (cdr specs)))
  114. (setq specs (car specs)))
  115. (make-local-variable 'face-remapping-alist)
  116. (let ((entry (assq face face-remapping-alist)))
  117. (when (null entry)
  118. (setq entry (list face face)) ; explicitly merge with global def
  119. (push entry face-remapping-alist))
  120. (let ((faces (cdr entry)))
  121. (if (symbolp faces)
  122. (setq faces (list faces)))
  123. (setcdr entry (face-remap-order (cons specs faces)))
  124. ;; Force redisplay of this buffer.
  125. (force-mode-line-update))
  126. (cons face specs)))
  127. (defun face-remap-remove-relative (cookie)
  128. "Remove a face remapping previously added by `face-remap-add-relative'.
  129. COOKIE should be the return value from that function."
  130. (let ((remapping (assq (car cookie) face-remapping-alist)))
  131. (when remapping
  132. (let ((updated-entries (remq (cdr cookie) (cdr remapping))))
  133. (unless (eq updated-entries (cdr remapping))
  134. (setcdr remapping updated-entries)
  135. (when (or (null updated-entries)
  136. (and (eq (car-safe updated-entries) (car cookie))
  137. (null (cdr updated-entries))))
  138. (setq face-remapping-alist
  139. (remq remapping face-remapping-alist))
  140. ;; Force redisplay of this buffer.
  141. (force-mode-line-update))
  142. (cdr cookie))))))
  143. ;;;###autoload
  144. (defun face-remap-reset-base (face)
  145. "Set the base remapping of FACE to the normal definition of FACE.
  146. This causes the remappings specified by `face-remap-add-relative'
  147. to apply on top of the normal definition of FACE."
  148. (let ((entry (assq face face-remapping-alist)))
  149. (when entry
  150. ;; If there's nothing except a base remapping, we simply remove
  151. ;; the entire remapping entry, as setting the base to the default
  152. ;; would be the same as the global definition. Otherwise, we
  153. ;; modify the base remapping.
  154. (if (null (cddr entry)) ; nothing except base remapping
  155. (setq face-remapping-alist ; so remove entire entry
  156. (remq entry face-remapping-alist))
  157. (setcar (last entry) face))
  158. ;; Force redisplay of this buffer.
  159. (force-mode-line-update)))) ; otherwise, just inherit global def
  160. ;;;###autoload
  161. (defun face-remap-set-base (face &rest specs)
  162. "Set the base remapping of FACE in the current buffer to SPECS.
  163. This causes the remappings specified by `face-remap-add-relative'
  164. to apply on top of the face specification given by SPECS.
  165. The remaining arguments, SPECS, should form a list of faces.
  166. Each list element should be either a face name or a property list
  167. of face attribute/value pairs, like in a `face' text property.
  168. If SPECS is empty, call `face-remap-reset-base' to use the normal
  169. definition of FACE as the base remapping; note that this is
  170. different from SPECS containing a single value nil, which means
  171. not to inherit from the global definition of FACE at all."
  172. (while (and (consp specs) (not (null (car specs))) (null (cdr specs)))
  173. (setq specs (car specs)))
  174. (if (or (null specs)
  175. (and (eq (car specs) face) (null (cdr specs)))) ; default
  176. ;; Set entry back to default
  177. (face-remap-reset-base face)
  178. ;; Set the base remapping
  179. (make-local-variable 'face-remapping-alist)
  180. (let ((entry (assq face face-remapping-alist)))
  181. (if entry
  182. (setcar (last entry) specs) ; overwrite existing base entry
  183. (push (list face specs) face-remapping-alist)))
  184. ;; Force redisplay of this buffer.
  185. (force-mode-line-update)))
  186. ;; ----------------------------------------------------------------
  187. ;; text-scale-mode
  188. (defcustom text-scale-mode-step 1.2
  189. "Scale factor used by `text-scale-mode'.
  190. Each positive or negative step scales the default face height by this amount."
  191. :group 'display
  192. :type 'number
  193. :version "23.1")
  194. ;; current remapping cookie for text-scale-mode
  195. (defvar text-scale-mode-remapping nil)
  196. (make-variable-buffer-local 'text-scale-mode-remapping)
  197. ;; Lighter displayed for text-scale-mode in mode-line minor-mode list
  198. (defvar text-scale-mode-lighter "+0")
  199. (make-variable-buffer-local 'text-scale-mode-lighter)
  200. ;; Number of steps that text-scale-mode will increase/decrease text height
  201. (defvar text-scale-mode-amount 0)
  202. (make-variable-buffer-local 'text-scale-mode-amount)
  203. (define-minor-mode text-scale-mode
  204. "Minor mode for displaying buffer text in a larger/smaller font.
  205. With a prefix argument ARG, enable the mode if ARG is positive,
  206. and disable it otherwise. If called from Lisp, enable the mode
  207. if ARG is omitted or nil.
  208. The amount of scaling is determined by the variable
  209. `text-scale-mode-amount': one step scales the global default
  210. face size by the value of the variable `text-scale-mode-step'
  211. \(a negative amount shrinks the text).
  212. The `text-scale-increase', `text-scale-decrease', and
  213. `text-scale-set' functions may be used to interactively modify
  214. the variable `text-scale-mode-amount' (they also enable or
  215. disable `text-scale-mode' as necessary)."
  216. :lighter (" " text-scale-mode-lighter)
  217. (when text-scale-mode-remapping
  218. (face-remap-remove-relative text-scale-mode-remapping))
  219. (setq text-scale-mode-lighter
  220. (format (if (>= text-scale-mode-amount 0) "+%d" "%d")
  221. text-scale-mode-amount))
  222. (setq text-scale-mode-remapping
  223. (and text-scale-mode
  224. (face-remap-add-relative 'default
  225. :height
  226. (expt text-scale-mode-step
  227. text-scale-mode-amount))))
  228. (force-window-update (current-buffer)))
  229. (defun text-scale-min-amount ()
  230. "Return the minimum amount of text-scaling we allow."
  231. ;; When the resulting pixel-height of characters will become smaller
  232. ;; than 1 pixel, we can expect trouble from the display engine.
  233. ;; E.g., it requires that the character glyph's ascent is
  234. ;; non-negative.
  235. (log (/ 1.0 (frame-char-height)) text-scale-mode-step))
  236. (defun text-scale-max-amount ()
  237. "Return the maximum amount of text-scaling we allow."
  238. ;; The display engine uses a 16-bit short for pixel-width of
  239. ;; characters, thus the 0xffff limitation. It also makes no sense
  240. ;; to have characters wider than the display.
  241. (log (/ (min (display-pixel-width) #xffff)
  242. (frame-char-width))
  243. text-scale-mode-step))
  244. ;;;###autoload
  245. (defun text-scale-set (level)
  246. "Set the scale factor of the default face in the current buffer to LEVEL.
  247. If LEVEL is non-zero, `text-scale-mode' is enabled, otherwise it is disabled.
  248. LEVEL is a number of steps, with 0 representing the default size.
  249. Each step scales the height of the default face by the variable
  250. `text-scale-mode-step' (a negative number decreases the height by
  251. the same amount)."
  252. (interactive "p")
  253. (setq text-scale-mode-amount
  254. (max (min level (text-scale-max-amount)) (text-scale-min-amount)))
  255. (text-scale-mode (if (zerop text-scale-mode-amount) -1 1)))
  256. ;;;###autoload
  257. (defun text-scale-increase (inc)
  258. "Increase the height of the default face in the current buffer by INC steps.
  259. If the new height is other than the default, `text-scale-mode' is enabled.
  260. Each step scales the height of the default face by the variable
  261. `text-scale-mode-step' (a negative number of steps decreases the
  262. height by the same amount). As a special case, an argument of 0
  263. will remove any scaling currently active."
  264. (interactive "p")
  265. (let* ((current-value (if text-scale-mode text-scale-mode-amount 0))
  266. (new-value (if (= inc 0) 0 (+ current-value inc))))
  267. (if (or (> new-value (text-scale-max-amount))
  268. (< new-value (text-scale-min-amount)))
  269. (user-error "Cannot %s the default face height more than it already is"
  270. (if (> inc 0) "increase" "decrease")))
  271. (setq text-scale-mode-amount new-value))
  272. (text-scale-mode (if (zerop text-scale-mode-amount) -1 1)))
  273. ;;;###autoload
  274. (defun text-scale-decrease (dec)
  275. "Decrease the height of the default face in the current buffer by DEC steps.
  276. See `text-scale-increase' for more details."
  277. (interactive "p")
  278. (text-scale-increase (- dec)))
  279. ;;;###autoload (define-key ctl-x-map [(control ?+)] 'text-scale-adjust)
  280. ;;;###autoload (define-key ctl-x-map [(control ?-)] 'text-scale-adjust)
  281. ;;;###autoload (define-key ctl-x-map [(control ?=)] 'text-scale-adjust)
  282. ;;;###autoload (define-key ctl-x-map [(control ?0)] 'text-scale-adjust)
  283. ;;;###autoload
  284. (defun text-scale-adjust (inc)
  285. "Adjust the height of the default face by INC.
  286. INC may be passed as a numeric prefix argument.
  287. The actual adjustment made depends on the final component of the
  288. key-binding used to invoke the command, with all modifiers removed:
  289. +, = Increase the default face height by one step
  290. - Decrease the default face height by one step
  291. 0 Reset the default face height to the global default
  292. After adjusting, continue to read input events and further adjust
  293. the face height as long as the input event read
  294. \(with all modifiers removed) is one of the above characters.
  295. Each step scales the height of the default face by the variable
  296. `text-scale-mode-step' (a negative number of steps decreases the
  297. height by the same amount). As a special case, an argument of 0
  298. will remove any scaling currently active.
  299. This command is a special-purpose wrapper around the
  300. `text-scale-increase' command which makes repetition convenient
  301. even when it is bound in a non-top-level keymap. For binding in
  302. a top-level keymap, `text-scale-increase' or
  303. `text-scale-decrease' may be more appropriate."
  304. (interactive "p")
  305. (let ((ev last-command-event)
  306. (echo-keystrokes nil))
  307. (let* ((base (event-basic-type ev))
  308. (step
  309. (pcase base
  310. ((or ?+ ?=) inc)
  311. (?- (- inc))
  312. (?0 0)
  313. (_ inc))))
  314. (text-scale-increase step)
  315. ;; (unless (zerop step)
  316. (message "Use +,-,0 for further adjustment")
  317. (set-transient-map
  318. (let ((map (make-sparse-keymap)))
  319. (dolist (mods '(() (control)))
  320. (dolist (key '(?- ?+ ?= ?0)) ;; = is often unshifted +.
  321. (define-key map (vector (append mods (list key)))
  322. (lambda () (interactive) (text-scale-adjust (abs inc))))))
  323. map))))) ;; )
  324. ;; ----------------------------------------------------------------
  325. ;; buffer-face-mode
  326. (defcustom buffer-face-mode-face 'variable-pitch
  327. "The face specification used by `buffer-face-mode'.
  328. It may contain any value suitable for a `face' text property,
  329. including a face name, a list of face names, a face-attribute
  330. plist, etc."
  331. :type '(choice (face)
  332. (repeat :tag "List of faces" face)
  333. (plist :tag "Face property list"))
  334. :group 'display
  335. :version "23.1")
  336. ;; current remapping cookie for buffer-face-mode
  337. (defvar buffer-face-mode-remapping nil)
  338. (make-variable-buffer-local 'buffer-face-mode-remapping)
  339. ;;;###autoload
  340. (define-minor-mode buffer-face-mode
  341. "Minor mode for a buffer-specific default face.
  342. With a prefix argument ARG, enable the mode if ARG is positive,
  343. and disable it otherwise. If called from Lisp, enable the mode
  344. if ARG is omitted or nil. When enabled, the face specified by the
  345. variable `buffer-face-mode-face' is used to display the buffer text."
  346. :lighter " BufFace"
  347. (when buffer-face-mode-remapping
  348. (face-remap-remove-relative buffer-face-mode-remapping))
  349. (setq buffer-face-mode-remapping
  350. (and buffer-face-mode
  351. (face-remap-add-relative 'default buffer-face-mode-face)))
  352. (force-window-update (current-buffer)))
  353. ;;;###autoload
  354. (defun buffer-face-set (&rest specs)
  355. "Enable `buffer-face-mode', using face specs SPECS.
  356. Each argument in SPECS should be a face, i.e. either a face name
  357. or a property list of face attributes and values. If more than
  358. one face is listed, that specifies an aggregate face, like in a
  359. `face' text property. If SPECS is nil or omitted, disable
  360. `buffer-face-mode'.
  361. This function makes the variable `buffer-face-mode-face' buffer
  362. local, and sets it to FACE."
  363. (interactive (list (read-face-name "Set buffer face" (face-at-point t))))
  364. (while (and (consp specs) (null (cdr specs)))
  365. (setq specs (car specs)))
  366. (if (null specs)
  367. (buffer-face-mode 0)
  368. (set (make-local-variable 'buffer-face-mode-face) specs)
  369. (buffer-face-mode t)))
  370. ;;;###autoload
  371. (defun buffer-face-toggle (&rest specs)
  372. "Toggle `buffer-face-mode', using face specs SPECS.
  373. Each argument in SPECS should be a face, i.e. either a face name
  374. or a property list of face attributes and values. If more than
  375. one face is listed, that specifies an aggregate face, like in a
  376. `face' text property.
  377. If `buffer-face-mode' is already enabled, and is currently using
  378. the face specs SPECS, then it is disabled; if `buffer-face-mode'
  379. is disabled, or is enabled and currently displaying some other
  380. face, then is left enabled, but the face changed to reflect SPECS.
  381. This function will make the variable `buffer-face-mode-face'
  382. buffer local, and set it to SPECS."
  383. (interactive (list buffer-face-mode-face))
  384. (while (and (consp specs) (null (cdr specs)))
  385. (setq specs (car specs)))
  386. (if (or (null specs)
  387. (and buffer-face-mode (equal buffer-face-mode-face specs)))
  388. (buffer-face-mode 0)
  389. (set (make-local-variable 'buffer-face-mode-face) specs)
  390. (buffer-face-mode t)))
  391. (defun buffer-face-mode-invoke (specs arg &optional interactive)
  392. "Enable or disable `buffer-face-mode' using face specs SPECS.
  393. ARG controls whether the mode is enabled or disabled, and is
  394. interpreted in the usual manner for minor-mode commands.
  395. SPECS can be any value suitable for a `face' text property,
  396. including a face name, a plist of face attributes and values,
  397. or a list of faces.
  398. If INTERACTIVE is non-nil, display a message describing the
  399. result.
  400. This is a wrapper function which calls `buffer-face-set' or
  401. `buffer-face-toggle' (depending on ARG), and prints a status
  402. message in the echo area. In many cases one of those functions
  403. may be more appropriate."
  404. (let ((last-message (current-message)))
  405. (if (or (eq arg 'toggle) (not arg))
  406. (buffer-face-toggle specs)
  407. (buffer-face-set (and (> (prefix-numeric-value arg) 0) specs)))
  408. (when interactive
  409. (unless (and (current-message)
  410. (not (equal last-message (current-message))))
  411. (message "Buffer-Face mode %sabled"
  412. (if buffer-face-mode "en" "dis"))))))
  413. ;; ----------------------------------------------------------------
  414. ;; variable-pitch-mode
  415. ;;;###autoload
  416. (defun variable-pitch-mode (&optional arg)
  417. "Variable-pitch default-face mode.
  418. An interface to `buffer-face-mode' which uses the `variable-pitch' face.
  419. Besides the choice of face, it is the same as `buffer-face-mode'."
  420. (interactive (list (or current-prefix-arg 'toggle)))
  421. (buffer-face-mode-invoke 'variable-pitch arg
  422. (called-interactively-p 'interactive)))
  423. (provide 'face-remap)
  424. ;;; face-remap.el ends here