* lispref/modes.texi (Region to Refontify): Rename from "Region to Fontify".
[emacs.git] / lisp / face-remap.el
blob6bb84acb16c9b802d6d6359bd296d4095d94255a
1 ;;; face-remap.el --- Functions for managing `face-remapping-alist'
2 ;;
3 ;; Copyright (C) 2008, 2009, 2010, 2011 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.
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.
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/>.
24 ;;; Commentary:
27 ;; This file defines some simple operations that can be used for
28 ;; maintaining the `face-remapping-alist' in a cooperative way. This is
29 ;; especially important for the `default' face.
31 ;; Each face-remapping definition in `face-remapping-alist' added by
32 ;; this code uses the form:
34 ;; (face RELATIVE_SPECS_1 RELATIVE_SPECS_2 ... BASE_SPECS)
36 ;; The "specs" values are a lists of face names or face attribute-value
37 ;; pairs, and are merged together, with earlier values taking precedence.
39 ;; The RELATIVE_SPECS_* values are added by `face-remap-add-relative'
40 ;; (and removed by `face-remap-remove-relative', and are intended for
41 ;; face "modifications" (such as increasing the size). Typical users of
42 ;; relative specs would be minor modes.
44 ;; BASE_SPECS is the lowest-priority value, and by default is just the
45 ;; face name, which causes the global definition of that face to be used.
47 ;; A non-default value of BASE_SPECS may also be set using
48 ;; `face-remap-set-base'. Because this _overwrites_ the default
49 ;; value inheriting from the global face definition, it is up to the
50 ;; caller of face-remap-set-base to add such inheritance if it is
51 ;; desired. A typical use of face-remap-set-base would be a major
52 ;; mode setting face remappings, e.g., of the default face.
54 ;; All modifications cause face-remapping-alist to be made buffer-local.
58 ;;; Code:
61 ;; ----------------------------------------------------------------
62 ;; Utility functions
64 ;; Names of face attributes corresponding to lisp face-vector positions.
65 ;; This variable should probably be defined in C code where the actual
66 ;; definitions are available.
68 (defvar internal-lisp-face-attributes
69 [nil
70 :family :foundry :swidth :height :weight :slant :underline :inverse
71 :foreground :background :stipple :overline :strike :box
72 :font :inherit :fontset :vector])
74 (defun face-attrs-more-relative-p (attrs1 attrs2)
75 "Return true if ATTRS1 contains a greater number of relative
76 face-attributes than ATTRS2. A face attribute is considered
77 relative if `face-attribute-relative-p' returns non-nil.
79 ATTRS1 and ATTRS2 may be any value suitable for a `face' text
80 property, including face names, lists of face names,
81 face-attribute plists, etc.
83 This function can be used as a predicate with `sort', to sort
84 face lists so that more specific faces are located near the end."
85 (unless (vectorp attrs1)
86 (setq attrs1 (face-attributes-as-vector attrs1)))
87 (unless (vectorp attrs2)
88 (setq attrs2 (face-attributes-as-vector attrs2)))
89 (let ((rel1-count 0) (rel2-count 0))
90 (dotimes (i (length attrs1))
91 (let ((attr (aref internal-lisp-face-attributes i)))
92 (when attr
93 (when (face-attribute-relative-p attr (aref attrs1 i))
94 (setq rel1-count (+ rel1-count 1)))
95 (when (face-attribute-relative-p attr (aref attrs2 i))
96 (setq rel2-count (+ rel2-count 1))))))
97 (< rel1-count rel2-count)))
99 (defun face-remap-order (entry)
100 "Order ENTRY so that more relative face specs are near the beginning.
101 The list structure of ENTRY may be destructively modified."
102 (setq entry (nreverse entry))
103 (setcdr entry (sort (cdr entry) 'face-attrs-more-relative-p))
104 (nreverse entry))
106 ;;;###autoload
107 (defun face-remap-add-relative (face &rest specs)
108 "Add a face remapping entry of FACE to SPECS in the current buffer.
110 Return a cookie which can be used to delete the remapping with
111 `face-remap-remove-relative'.
113 SPECS can be any value suitable for the `face' text property,
114 including a face name, a list of face names, or a face-attribute
115 property list. The attributes given by SPECS will be merged with
116 any other currently active face remappings of FACE, and with the
117 global definition of FACE. An attempt is made to sort multiple
118 entries so that entries with relative face-attributes are applied
119 after entries with absolute face-attributes.
121 The base (lowest priority) remapping may be set to a specific
122 value, instead of the default of the global face definition,
123 using `face-remap-set-base'."
124 (while (and (consp specs) (null (cdr specs)))
125 (setq specs (car specs)))
126 (make-local-variable 'face-remapping-alist)
127 (let ((entry (assq face face-remapping-alist)))
128 (when (null entry)
129 (setq entry (list face face)) ; explicitly merge with global def
130 (push entry face-remapping-alist))
131 (setcdr entry (face-remap-order (cons specs (cdr entry))))
132 (cons face specs)))
134 (defun face-remap-remove-relative (cookie)
135 "Remove a face remapping previously added by `face-remap-add-relative'.
136 COOKIE should be the return value from that function."
137 (let ((remapping (assq (car cookie) face-remapping-alist)))
138 (when remapping
139 (let ((updated-entries (remq (cdr cookie) (cdr remapping))))
140 (unless (eq updated-entries (cdr remapping))
141 (setcdr remapping updated-entries)
142 (when (or (null updated-entries)
143 (and (eq (car-safe updated-entries) (car cookie))
144 (null (cdr updated-entries))))
145 (setq face-remapping-alist
146 (remq remapping face-remapping-alist)))
147 (cdr cookie))))))
149 ;;;###autoload
150 (defun face-remap-reset-base (face)
151 "Set the base remapping of FACE to inherit from FACE's global definition."
152 (let ((entry (assq face face-remapping-alist)))
153 (when entry
154 ;; If there's nothing except a base remapping, we simply remove
155 ;; the entire remapping entry, as setting the base to the default
156 ;; would be the same as the global definition. Otherwise, we
157 ;; modify the base remapping.
158 (if (null (cddr entry)) ; nothing except base remapping
159 (setq face-remapping-alist ; so remove entire entry
160 (remq entry face-remapping-alist))
161 (setcar (last entry) face))))) ; otherwise, just inherit global def
163 ;;;###autoload
164 (defun face-remap-set-base (face &rest specs)
165 "Set the base remapping of FACE in the current buffer to SPECS.
166 If SPECS is empty, the default base remapping is restored, which
167 inherits from the global definition of FACE; note that this is
168 different from SPECS containing a single value `nil', which does
169 not inherit from the global definition of FACE."
170 (while (and (consp specs) (not (null (car specs))) (null (cdr specs)))
171 (setq specs (car specs)))
172 (if (or (null specs)
173 (and (eq (car specs) face) (null (cdr specs)))) ; default
174 ;; Set entry back to default
175 (face-remap-reset-base face)
176 ;; Set the base remapping
177 (make-local-variable 'face-remapping-alist)
178 (let ((entry (assq face face-remapping-alist)))
179 (if entry
180 (setcar (last entry) specs) ; overwrite existing base entry
181 (push (list face specs) face-remapping-alist)))))
184 ;; ----------------------------------------------------------------
185 ;; text-scale-mode
187 (defcustom text-scale-mode-step 1.2
188 "Scale factor used by `text-scale-mode'.
189 Each positive or negative step scales the default face height by this amount."
190 :group 'display
191 :type 'number
192 :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)
198 ;; Lighter displayed for text-scale-mode in mode-line minor-mode list
199 (defvar text-scale-mode-lighter "+0")
200 (make-variable-buffer-local 'text-scale-mode-lighter)
202 ;; Number of steps that text-scale-mode will increase/decrease text height
203 (defvar text-scale-mode-amount 0)
204 (make-variable-buffer-local 'text-scale-mode-amount)
206 (define-minor-mode text-scale-mode
207 "Minor mode for displaying buffer text in a larger/smaller font than usual.
209 The amount of scaling is determined by the variable
210 `text-scale-mode-amount': one step scales the global default
211 face size by the value of the variable `text-scale-mode-step'
212 \(a negative amount shrinks the text).
214 The `text-scale-increase', `text-scale-decrease', and
215 `text-scale-set' functions may be used to interactively modify
216 the variable `text-scale-mode-amount' (they also enable or
217 disable `text-scale-mode' as necessary)."
218 :lighter (" " text-scale-mode-lighter)
219 (when text-scale-mode-remapping
220 (face-remap-remove-relative text-scale-mode-remapping))
221 (setq text-scale-mode-lighter
222 (format (if (>= text-scale-mode-amount 0) "+%d" "%d")
223 text-scale-mode-amount))
224 (setq text-scale-mode-remapping
225 (and text-scale-mode
226 (face-remap-add-relative 'default
227 :height
228 (expt text-scale-mode-step
229 text-scale-mode-amount))))
230 (force-window-update (current-buffer)))
232 ;;;###autoload
233 (defun text-scale-set (level)
234 "Set the scale factor of the default face in the current buffer to LEVEL.
235 If LEVEL is non-zero, `text-scale-mode' is enabled, otherwise it is disabled.
237 LEVEL is a number of steps, with 0 representing the default size.
238 Each step scales the height of the default face by the variable
239 `text-scale-mode-step' (a negative number decreases the height by
240 the same amount)."
241 (interactive "p")
242 (setq text-scale-mode-amount level)
243 (text-scale-mode (if (zerop text-scale-mode-amount) -1 1)))
245 ;;;###autoload
246 (defun text-scale-increase (inc)
247 "Increase the height of the default face in the current buffer by INC steps.
248 If the new height is other than the default, `text-scale-mode' is enabled.
250 Each step scales the height of the default face by the variable
251 `text-scale-mode-step' (a negative number of steps decreases the
252 height by the same amount). As a special case, an argument of 0
253 will remove any scaling currently active."
254 (interactive "p")
255 (setq text-scale-mode-amount
256 (if (= inc 0) 0 (+ (if text-scale-mode text-scale-mode-amount 0) inc)))
257 (text-scale-mode (if (zerop text-scale-mode-amount) -1 1)))
259 ;;;###autoload
260 (defun text-scale-decrease (dec)
261 "Decrease the height of the default face in the current buffer by DEC steps.
262 See `text-scale-increase' for more details."
263 (interactive "p")
264 (text-scale-increase (- dec)))
266 ;;;###autoload (define-key ctl-x-map [(control ?+)] 'text-scale-adjust)
267 ;;;###autoload (define-key ctl-x-map [(control ?-)] 'text-scale-adjust)
268 ;;;###autoload (define-key ctl-x-map [(control ?=)] 'text-scale-adjust)
269 ;;;###autoload (define-key ctl-x-map [(control ?0)] 'text-scale-adjust)
270 ;;;###autoload
271 (defun text-scale-adjust (inc)
272 "Increase or decrease the height of the default face in the current buffer.
274 The actual adjustment made depends on the final component of the
275 key-binding used to invoke the command, with all modifiers removed:
277 +, = Increase the default face height by one step
278 - Decrease the default face height by one step
279 0 Reset the default face height to the global default
281 Then, continue to read input events and further adjust the face
282 height as long as the input event read (with all modifiers removed)
283 is one of the above.
285 Each step scales the height of the default face by the variable
286 `text-scale-mode-step' (a negative number of steps decreases the
287 height by the same amount). As a special case, an argument of 0
288 will remove any scaling currently active.
290 This command is a special-purpose wrapper around the
291 `text-scale-increase' command which makes repetition convenient
292 even when it is bound in a non-top-level keymap. For binding in
293 a top-level keymap, `text-scale-increase' or
294 `text-scale-decrease' may be more appropriate."
295 (interactive "p")
296 (let ((first t)
297 (step t)
298 (ev last-command-event)
299 (echo-keystrokes nil))
300 (while step
301 (let ((base (event-basic-type ev)))
302 (cond ((or (eq base ?+) (eq base ?=))
303 (setq step inc))
304 ((eq base ?-)
305 (setq step (- inc)))
306 ((eq base ?0)
307 (setq step 0))
308 (first
309 (setq step inc))
311 (setq step nil))))
312 (when step
313 (text-scale-increase step)
314 (setq inc 1 first nil)
315 (setq ev (read-event "+,-,0 for further adjustment: "))))
316 (push ev unread-command-events)))
319 ;; ----------------------------------------------------------------
320 ;; buffer-face-mode
322 (defcustom buffer-face-mode-face 'variable-pitch
323 "The face specification used by `buffer-face-mode'.
324 It may contain any value suitable for a `face' text property,
325 including a face name, a list of face names, a face-attribute
326 plist, etc."
327 :group 'display
328 :version "23.1")
330 ;; current remapping cookie for buffer-face-mode
331 (defvar buffer-face-mode-remapping nil)
332 (make-variable-buffer-local 'buffer-face-mode-remapping)
334 ;;;###autoload
335 (define-minor-mode buffer-face-mode
336 "Minor mode for a buffer-specific default face.
337 When enabled, the face specified by the variable
338 `buffer-face-mode-face' is used to display the buffer text."
339 :lighter " BufFace"
340 (when buffer-face-mode-remapping
341 (face-remap-remove-relative buffer-face-mode-remapping))
342 (setq buffer-face-mode-remapping
343 (and buffer-face-mode
344 (face-remap-add-relative 'default buffer-face-mode-face)))
345 (force-window-update (current-buffer)))
347 ;;;###autoload
348 (defun buffer-face-set (&rest specs)
349 "Enable `buffer-face-mode', using face specs SPECS.
350 SPECS can be any value suitable for the `face' text property,
351 including a face name, a list of face names, or a face-attribute
352 If SPECS is nil, then `buffer-face-mode' is disabled.
354 This function will make the variable `buffer-face-mode-face'
355 buffer local, and set it to FACE."
356 (interactive (list (read-face-name "Set buffer face")))
357 (while (and (consp specs) (null (cdr specs)))
358 (setq specs (car specs)))
359 (if (null specs)
360 (buffer-face-mode 0)
361 (set (make-local-variable 'buffer-face-mode-face) specs)
362 (buffer-face-mode t)))
364 ;;;###autoload
365 (defun buffer-face-toggle (&rest specs)
366 "Toggle `buffer-face-mode', using face specs SPECS.
367 SPECS can be any value suitable for the `face' text property,
368 including a face name, a list of face names, or a face-attribute
370 If `buffer-face-mode' is already enabled, and is currently using
371 the face specs SPECS, then it is disabled; if buffer-face-mode is
372 disabled, or is enabled and currently displaying some other face,
373 then is left enabled, but the face changed to reflect SPECS.
375 This function will make the variable `buffer-face-mode-face'
376 buffer local, and set it to SPECS."
377 (interactive (list buffer-face-mode-face))
378 (while (and (consp specs) (null (cdr specs)))
379 (setq specs (car specs)))
380 (if (or (null specs)
381 (and buffer-face-mode (equal buffer-face-mode-face specs)))
382 (buffer-face-mode 0)
383 (set (make-local-variable 'buffer-face-mode-face) specs)
384 (buffer-face-mode t)))
386 (defun buffer-face-mode-invoke (specs arg &optional interactive)
387 "Enable or disable `buffer-face-mode' using face specs SPECS, and argument ARG.
388 ARG controls whether the mode is enabled or disabled, and is
389 interpreted in the usual manner for minor-mode commands.
391 SPECS can be any value suitable for the `face' text property,
392 including a face name, a list of face names, or a face-attribute
394 If INTERACTIVE is non-nil, a message will be displayed describing the result.
396 This is a wrapper function which calls `buffer-face-set' or
397 `buffer-face-toggle' (depending on ARG), and prints a status
398 message in the echo area. In many cases one of those functions
399 may be more appropriate."
400 (let ((last-message (current-message)))
401 (if (or (eq arg 'toggle) (not arg))
402 (buffer-face-toggle specs)
403 (buffer-face-set (and (> (prefix-numeric-value arg) 0) specs)))
404 (when interactive
405 (unless (and (current-message)
406 (not (equal last-message (current-message))))
407 (message "Buffer-Face mode %sabled"
408 (if buffer-face-mode "en" "dis"))))))
411 ;; ----------------------------------------------------------------
412 ;; variable-pitch-mode
414 ;;;###autoload
415 (defun variable-pitch-mode (&optional arg)
416 "Variable-pitch default-face mode.
417 An interface to `buffer-face-mode' which uses the `variable-pitch' face.
418 Besides the choice of face, it is the same as `buffer-face-mode'."
419 (interactive (list (or current-prefix-arg 'toggle)))
420 (buffer-face-mode-invoke 'variable-pitch arg
421 (called-interactively-p 'interactive)))
424 (provide 'face-remap)
426 ;; arch-tag: 5c5f034b-8d58-4967-82bd-d61fd364e686
427 ;;; face-remap.el ends here