declare smobs in alloc.c
[bpt/emacs.git] / lisp / face-remap.el
CommitLineData
c5772569 1;;; face-remap.el --- Functions for managing `face-remapping-alist' -*- lexical-binding: t -*-
9d3d42fb 2;;
ba318903 3;; Copyright (C) 2008-2014 Free Software Foundation, Inc.
9d3d42fb
MB
4;;
5;; Author: Miles Bader <miles@gnu.org>
88f4758e 6;; Keywords: faces, face remapping, display, user commands
9d3d42fb
MB
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
24;;; Commentary:
25
26;;
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.
30;;
31;; Each face-remapping definition in `face-remapping-alist' added by
32;; this code uses the form:
33;;
34;; (face RELATIVE_SPECS_1 RELATIVE_SPECS_2 ... BASE_SPECS)
35;;
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.
38;;
e40a85cd
MB
39;; The RELATIVE_SPECS_* values are added by `face-remap-add-relative'
40;; (and removed by `face-remap-remove-relative', and are intended for
9d3d42fb
MB
41;; face "modifications" (such as increasing the size). Typical users of
42;; relative specs would be minor modes.
43;;
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.
46;;
47;; A non-default value of BASE_SPECS may also be set using
e40a85cd 48;; `face-remap-set-base'. Because this _overwrites_ the default
9d3d42fb 49;; value inheriting from the global face definition, it is up to the
e40a85cd
MB
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
9d3d42fb
MB
52;; mode setting face remappings, e.g., of the default face.
53;;
54;; All modifications cause face-remapping-alist to be made buffer-local.
55;;
56
57
58;;; Code:
59
60\f
61;; ----------------------------------------------------------------
62;; Utility functions
63
d03d411d
MB
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.
67;;
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])
73
74(defun face-attrs-more-relative-p (attrs1 attrs2)
58aa805b 75 "Return true if ATTRS1 contains a greater number of relative
d03d411d
MB
76face-attributes than ATTRS2. A face attribute is considered
77relative if `face-attribute-relative-p' returns non-nil.
78
79ATTRS1 and ATTRS2 may be any value suitable for a `face' text
80property, including face names, lists of face names,
81face-attribute plists, etc.
82
83This function can be used as a predicate with `sort', to sort
84face 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)))
98
99(defun face-remap-order (entry)
100 "Order ENTRY so that more relative face specs are near the beginning.
101The 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))
105
23b77eee 106;;;###autoload
e40a85cd 107(defun face-remap-add-relative (face &rest specs)
9d3d42fb 108 "Add a face remapping entry of FACE to SPECS in the current buffer.
fb5b8aca 109Return a cookie which can be used to delete this remapping with
e40a85cd 110`face-remap-remove-relative'.
9d3d42fb 111
6175e34b
CY
112The remaining arguments, SPECS, should form a list of faces.
113Each list element should be either a face name or a property list
114of face attribute/value pairs. If more than one face is listed,
115that specifies an aggregate face, in the same way as in a `face'
116text property, except for possible priority changes noted below.
117
118The face remapping specified by SPECS takes effect alongside the
119remappings from other calls to `face-remap-add-relative' for the
120same FACE, as well as the normal definition of FACE (at lowest
121priority). This function tries to sort multiple remappings for
122the same face, so that remappings specifying relative face
123attributes are applied after remappings specifying absolute face
124attributes.
fb5b8aca
CY
125
126The base (lowest priority) remapping may be set to something
127other than the normal definition of FACE via `face-remap-set-base'."
cece37cf
MB
128 (while (and (consp specs) (null (cdr specs)))
129 (setq specs (car specs)))
9d3d42fb
MB
130 (make-local-variable 'face-remapping-alist)
131 (let ((entry (assq face face-remapping-alist)))
132 (when (null entry)
133 (setq entry (list face face)) ; explicitly merge with global def
134 (push entry face-remapping-alist))
894b9dd9
CY
135 (let ((faces (cdr entry)))
136 (if (symbolp faces)
137 (setq faces (list faces)))
881329dc
EZ
138 (setcdr entry (face-remap-order (cons specs faces)))
139 ;; Force redisplay of this buffer.
140 (force-mode-line-update))
9d3d42fb
MB
141 (cons face specs)))
142
e40a85cd
MB
143(defun face-remap-remove-relative (cookie)
144 "Remove a face remapping previously added by `face-remap-add-relative'.
9d3d42fb
MB
145COOKIE should be the return value from that function."
146 (let ((remapping (assq (car cookie) face-remapping-alist)))
147 (when remapping
148 (let ((updated-entries (remq (cdr cookie) (cdr remapping))))
149 (unless (eq updated-entries (cdr remapping))
150 (setcdr remapping updated-entries)
151 (when (or (null updated-entries)
152 (and (eq (car-safe updated-entries) (car cookie))
153 (null (cdr updated-entries))))
154 (setq face-remapping-alist
881329dc
EZ
155 (remq remapping face-remapping-alist))
156 ;; Force redisplay of this buffer.
157 (force-mode-line-update))
9d3d42fb
MB
158 (cdr cookie))))))
159
23b77eee 160;;;###autoload
e40a85cd 161(defun face-remap-reset-base (face)
fb5b8aca
CY
162 "Set the base remapping of FACE to the normal definition of FACE.
163This causes the remappings specified by `face-remap-add-relative'
164to apply on top of the normal definition of FACE."
9d3d42fb
MB
165 (let ((entry (assq face face-remapping-alist)))
166 (when entry
167 ;; If there's nothing except a base remapping, we simply remove
168 ;; the entire remapping entry, as setting the base to the default
169 ;; would be the same as the global definition. Otherwise, we
170 ;; modify the base remapping.
171 (if (null (cddr entry)) ; nothing except base remapping
172 (setq face-remapping-alist ; so remove entire entry
173 (remq entry face-remapping-alist))
881329dc
EZ
174 (setcar (last entry) face))
175 ;; Force redisplay of this buffer.
176 (force-mode-line-update)))) ; otherwise, just inherit global def
9d3d42fb 177
23b77eee 178;;;###autoload
e40a85cd 179(defun face-remap-set-base (face &rest specs)
9d3d42fb 180 "Set the base remapping of FACE in the current buffer to SPECS.
fb5b8aca 181This causes the remappings specified by `face-remap-add-relative'
6175e34b
CY
182to apply on top of the face specification given by SPECS.
183
184The remaining arguments, SPECS, should form a list of faces.
185Each list element should be either a face name or a property list
186of face attribute/value pairs, like in a `face' text property.
fb5b8aca
CY
187
188If SPECS is empty, call `face-remap-reset-base' to use the normal
189definition of FACE as the base remapping; note that this is
190different from SPECS containing a single value `nil', which means
191not to inherit from the global definition of FACE at all."
cece37cf
MB
192 (while (and (consp specs) (not (null (car specs))) (null (cdr specs)))
193 (setq specs (car specs)))
9d3d42fb
MB
194 (if (or (null specs)
195 (and (eq (car specs) face) (null (cdr specs)))) ; default
196 ;; Set entry back to default
e40a85cd 197 (face-remap-reset-base face)
9d3d42fb
MB
198 ;; Set the base remapping
199 (make-local-variable 'face-remapping-alist)
200 (let ((entry (assq face face-remapping-alist)))
201 (if entry
202 (setcar (last entry) specs) ; overwrite existing base entry
881329dc
EZ
203 (push (list face specs) face-remapping-alist)))
204 ;; Force redisplay of this buffer.
205 (force-mode-line-update)))
12de5099 206
9d3d42fb
MB
207\f
208;; ----------------------------------------------------------------
209;; text-scale-mode
210
211(defcustom text-scale-mode-step 1.2
212 "Scale factor used by `text-scale-mode'.
213Each positive or negative step scales the default face height by this amount."
214 :group 'display
af09cfd7
JB
215 :type 'number
216 :version "23.1")
9d3d42fb
MB
217
218;; current remapping cookie for text-scale-mode
219(defvar text-scale-mode-remapping nil)
220(make-variable-buffer-local 'text-scale-mode-remapping)
221
222;; Lighter displayed for text-scale-mode in mode-line minor-mode list
223(defvar text-scale-mode-lighter "+0")
224(make-variable-buffer-local 'text-scale-mode-lighter)
225
226;; Number of steps that text-scale-mode will increase/decrease text height
227(defvar text-scale-mode-amount 0)
228(make-variable-buffer-local 'text-scale-mode-amount)
229
230(define-minor-mode text-scale-mode
06e21633 231 "Minor mode for displaying buffer text in a larger/smaller font.
e1ac4066
GM
232With a prefix argument ARG, enable the mode if ARG is positive,
233and disable it otherwise. If called from Lisp, enable the mode
234if ARG is omitted or nil.
9d3d42fb
MB
235
236The amount of scaling is determined by the variable
12de5099
JB
237`text-scale-mode-amount': one step scales the global default
238face size by the value of the variable `text-scale-mode-step'
239\(a negative amount shrinks the text).
240
05fbc4a9
MB
241The `text-scale-increase', `text-scale-decrease', and
242`text-scale-set' functions may be used to interactively modify
243the variable `text-scale-mode-amount' (they also enable or
244disable `text-scale-mode' as necessary)."
9d3d42fb
MB
245 :lighter (" " text-scale-mode-lighter)
246 (when text-scale-mode-remapping
e40a85cd 247 (face-remap-remove-relative text-scale-mode-remapping))
9d3d42fb
MB
248 (setq text-scale-mode-lighter
249 (format (if (>= text-scale-mode-amount 0) "+%d" "%d")
250 text-scale-mode-amount))
251 (setq text-scale-mode-remapping
252 (and text-scale-mode
e40a85cd 253 (face-remap-add-relative 'default
9d3d42fb
MB
254 :height
255 (expt text-scale-mode-step
256 text-scale-mode-amount))))
257 (force-window-update (current-buffer)))
258
05fbc4a9
MB
259;;;###autoload
260(defun text-scale-set (level)
261 "Set the scale factor of the default face in the current buffer to LEVEL.
262If LEVEL is non-zero, `text-scale-mode' is enabled, otherwise it is disabled.
263
264LEVEL is a number of steps, with 0 representing the default size.
265Each step scales the height of the default face by the variable
266`text-scale-mode-step' (a negative number decreases the height by
267the same amount)."
268 (interactive "p")
269 (setq text-scale-mode-amount level)
270 (text-scale-mode (if (zerop text-scale-mode-amount) -1 1)))
271
9d3d42fb 272;;;###autoload
16c1ddc2 273(defun text-scale-increase (inc)
9d3d42fb
MB
274 "Increase the height of the default face in the current buffer by INC steps.
275If the new height is other than the default, `text-scale-mode' is enabled.
276
277Each step scales the height of the default face by the variable
278`text-scale-mode-step' (a negative number of steps decreases the
279height by the same amount). As a special case, an argument of 0
280will remove any scaling currently active."
56c73dec 281 (interactive "p")
dced1efd
MB
282 (setq text-scale-mode-amount
283 (if (= inc 0) 0 (+ (if text-scale-mode text-scale-mode-amount 0) inc)))
9d3d42fb
MB
284 (text-scale-mode (if (zerop text-scale-mode-amount) -1 1)))
285
9d3d42fb 286;;;###autoload
16c1ddc2 287(defun text-scale-decrease (dec)
9d3d42fb 288 "Decrease the height of the default face in the current buffer by DEC steps.
e40a85cd 289See `text-scale-increase' for more details."
56c73dec 290 (interactive "p")
e40a85cd 291 (text-scale-increase (- dec)))
9d3d42fb 292
e40a85cd
MB
293;;;###autoload (define-key ctl-x-map [(control ?+)] 'text-scale-adjust)
294;;;###autoload (define-key ctl-x-map [(control ?-)] 'text-scale-adjust)
295;;;###autoload (define-key ctl-x-map [(control ?=)] 'text-scale-adjust)
296;;;###autoload (define-key ctl-x-map [(control ?0)] 'text-scale-adjust)
56c73dec 297;;;###autoload
16c1ddc2 298(defun text-scale-adjust (inc)
c5772569
B
299 "Adjust the height of the default face by INC.
300
301INC may be passed as a numeric prefix argument.
56c73dec
MB
302
303The actual adjustment made depends on the final component of the
12de5099 304key-binding used to invoke the command, with all modifiers removed:
56c73dec
MB
305
306 +, = Increase the default face height by one step
307 - Decrease the default face height by one step
308 0 Reset the default face height to the global default
309
b22d0686
GM
310After adjusting, continue to read input events and further adjust
311the face height as long as the input event read
312\(with all modifiers removed) is one of the above characters.
56c73dec
MB
313
314Each step scales the height of the default face by the variable
315`text-scale-mode-step' (a negative number of steps decreases the
316height by the same amount). As a special case, an argument of 0
317will remove any scaling currently active.
318
319This command is a special-purpose wrapper around the
e40a85cd
MB
320`text-scale-increase' command which makes repetition convenient
321even when it is bound in a non-top-level keymap. For binding in
322a top-level keymap, `text-scale-increase' or
323`text-scale-decrease' may be more appropriate."
56c73dec 324 (interactive "p")
c5772569 325 (let ((ev last-command-event)
fed7c4f5 326 (echo-keystrokes nil))
df96ab1e
SM
327 (let* ((base (event-basic-type ev))
328 (step
329 (pcase base
f58e0fd5
SM
330 ((or ?+ ?=) inc)
331 (?- (- inc))
332 (?0 0)
df96ab1e
SM
333 (t inc))))
334 (text-scale-increase step)
c5772569
B
335 ;; (unless (zerop step)
336 (message "Use +,-,0 for further adjustment")
8cd22a08 337 (set-transient-map
df96ab1e
SM
338 (let ((map (make-sparse-keymap)))
339 (dolist (mods '(() (control)))
c5772569
B
340 (dolist (key '(?- ?+ ?= ?0)) ;; = is often unshifted +.
341 (define-key map (vector (append mods (list key)))
342 (lambda () (interactive) (text-scale-adjust (abs inc))))))
343 map))))) ;; )
56c73dec 344
9d3d42fb
MB
345\f
346;; ----------------------------------------------------------------
d7ed971d 347;; buffer-face-mode
9d3d42fb 348
d7ed971d
MB
349(defcustom buffer-face-mode-face 'variable-pitch
350 "The face specification used by `buffer-face-mode'.
351It may contain any value suitable for a `face' text property,
352including a face name, a list of face names, a face-attribute
353plist, etc."
9c5a5c77
GM
354 :type '(choice (face)
355 (repeat :tag "List of faces" face)
356 (plist :tag "Face property list"))
af09cfd7
JB
357 :group 'display
358 :version "23.1")
9d3d42fb 359
d7ed971d
MB
360;; current remapping cookie for buffer-face-mode
361(defvar buffer-face-mode-remapping nil)
362(make-variable-buffer-local 'buffer-face-mode-remapping)
9d3d42fb 363
15252ee9 364;;;###autoload
d7ed971d
MB
365(define-minor-mode buffer-face-mode
366 "Minor mode for a buffer-specific default face.
e1ac4066
GM
367With a prefix argument ARG, enable the mode if ARG is positive,
368and disable it otherwise. If called from Lisp, enable the mode
369if ARG is omitted or nil. When enabled, the face specified by the
370variable `buffer-face-mode-face' is used to display the buffer text."
d7ed971d
MB
371 :lighter " BufFace"
372 (when buffer-face-mode-remapping
373 (face-remap-remove-relative buffer-face-mode-remapping))
374 (setq buffer-face-mode-remapping
375 (and buffer-face-mode
376 (face-remap-add-relative 'default buffer-face-mode-face)))
9d3d42fb
MB
377 (force-window-update (current-buffer)))
378
d7ed971d 379;;;###autoload
cece37cf
MB
380(defun buffer-face-set (&rest specs)
381 "Enable `buffer-face-mode', using face specs SPECS.
6175e34b
CY
382Each argument in SPECS should be a face, i.e. either a face name
383or a property list of face attributes and values. If more than
384one face is listed, that specifies an aggregate face, like in a
385`face' text property. If SPECS is nil or omitted, disable
386`buffer-face-mode'.
387
388This function makes the variable `buffer-face-mode-face' buffer
389local, and sets it to FACE."
011cddd6 390 (interactive (list (read-face-name "Set buffer face" (face-at-point t))))
cece37cf
MB
391 (while (and (consp specs) (null (cdr specs)))
392 (setq specs (car specs)))
393 (if (null specs)
d7ed971d 394 (buffer-face-mode 0)
cece37cf 395 (set (make-local-variable 'buffer-face-mode-face) specs)
d7ed971d
MB
396 (buffer-face-mode t)))
397
398;;;###autoload
cece37cf
MB
399(defun buffer-face-toggle (&rest specs)
400 "Toggle `buffer-face-mode', using face specs SPECS.
6175e34b
CY
401Each argument in SPECS should be a face, i.e. either a face name
402or a property list of face attributes and values. If more than
403one face is listed, that specifies an aggregate face, like in a
404`face' text property.
d7ed971d
MB
405
406If `buffer-face-mode' is already enabled, and is currently using
58aa805b
JB
407the face specs SPECS, then it is disabled; if `buffer-face-mode'
408is disabled, or is enabled and currently displaying some other
409face, then is left enabled, but the face changed to reflect SPECS.
cece37cf
MB
410
411This function will make the variable `buffer-face-mode-face'
412buffer local, and set it to SPECS."
d7ed971d 413 (interactive (list buffer-face-mode-face))
cece37cf
MB
414 (while (and (consp specs) (null (cdr specs)))
415 (setq specs (car specs)))
416 (if (or (null specs)
417 (and buffer-face-mode (equal buffer-face-mode-face specs)))
d7ed971d 418 (buffer-face-mode 0)
cece37cf 419 (set (make-local-variable 'buffer-face-mode-face) specs)
d7ed971d
MB
420 (buffer-face-mode t)))
421
cece37cf 422(defun buffer-face-mode-invoke (specs arg &optional interactive)
58aa805b 423 "Enable or disable `buffer-face-mode' using face specs SPECS.
cece37cf
MB
424ARG controls whether the mode is enabled or disabled, and is
425interpreted in the usual manner for minor-mode commands.
426
6175e34b 427SPECS can be any value suitable for a `face' text property,
58aa805b
JB
428including a face name, a plist of face attributes and values,
429or a list of faces.
cece37cf 430
6175e34b
CY
431If INTERACTIVE is non-nil, display a message describing the
432result.
cece37cf 433
23b77eee 434This is a wrapper function which calls `buffer-face-set' or
cece37cf
MB
435`buffer-face-toggle' (depending on ARG), and prints a status
436message in the echo area. In many cases one of those functions
437may be more appropriate."
d7ed971d
MB
438 (let ((last-message (current-message)))
439 (if (or (eq arg 'toggle) (not arg))
2875c646
MB
440 (buffer-face-toggle specs)
441 (buffer-face-set (and (> (prefix-numeric-value arg) 0) specs)))
d7ed971d
MB
442 (when interactive
443 (unless (and (current-message)
444 (not (equal last-message (current-message))))
445 (message "Buffer-Face mode %sabled"
446 (if buffer-face-mode "en" "dis"))))))
447
448\f
449;; ----------------------------------------------------------------
450;; variable-pitch-mode
451
452;;;###autoload
453(defun variable-pitch-mode (&optional arg)
454 "Variable-pitch default-face mode.
455An interface to `buffer-face-mode' which uses the `variable-pitch' face.
456Besides the choice of face, it is the same as `buffer-face-mode'."
457 (interactive (list (or current-prefix-arg 'toggle)))
32226619
JB
458 (buffer-face-mode-invoke 'variable-pitch arg
459 (called-interactively-p 'interactive)))
d7ed971d 460
9d3d42fb
MB
461
462(provide 'face-remap)
463
9d3d42fb 464;;; face-remap.el ends here