Commit | Line | Data |
---|---|---|
9d3d42fb MB |
1 | ;;; face-remap.el --- Functions for managing `face-remapping-alist' |
2 | ;; | |
3 | ;; Copyright (C) 2008 Free Software Foundation, Inc. | |
4 | ;; | |
5 | ;; Author: Miles Bader <miles@gnu.org> | |
6 | ;; Keywords: faces face 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 | ||
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) | |
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. | |
78 | ||
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. | |
82 | ||
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))) | |
98 | ||
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)) | |
105 | ||
9d3d42fb | 106 | ;;;### autoload |
e40a85cd | 107 | (defun face-remap-add-relative (face &rest specs) |
9d3d42fb MB |
108 | "Add a face remapping entry of FACE to SPECS in the current buffer. |
109 | ||
110 | Return a cookie which can be used to delete the remapping with | |
e40a85cd | 111 | `face-remap-remove-relative'. |
9d3d42fb MB |
112 | |
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 | |
d03d411d MB |
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. | |
9d3d42fb MB |
120 | |
121 | The base (lowest priority) remapping may be set to a specific | |
122 | value, instead of the default of the global face definition, | |
e40a85cd | 123 | using `face-remap-set-base'." |
9d3d42fb MB |
124 | (make-local-variable 'face-remapping-alist) |
125 | (let ((entry (assq face face-remapping-alist))) | |
126 | (when (null entry) | |
127 | (setq entry (list face face)) ; explicitly merge with global def | |
128 | (push entry face-remapping-alist)) | |
d03d411d | 129 | (setcdr entry (face-remap-order (cons specs (cdr entry)))) |
9d3d42fb MB |
130 | (cons face specs))) |
131 | ||
e40a85cd MB |
132 | (defun face-remap-remove-relative (cookie) |
133 | "Remove a face remapping previously added by `face-remap-add-relative'. | |
9d3d42fb MB |
134 | COOKIE should be the return value from that function." |
135 | (let ((remapping (assq (car cookie) face-remapping-alist))) | |
136 | (when remapping | |
137 | (let ((updated-entries (remq (cdr cookie) (cdr remapping)))) | |
138 | (unless (eq updated-entries (cdr remapping)) | |
139 | (setcdr remapping updated-entries) | |
140 | (when (or (null updated-entries) | |
141 | (and (eq (car-safe updated-entries) (car cookie)) | |
142 | (null (cdr updated-entries)))) | |
143 | (setq face-remapping-alist | |
144 | (remq remapping face-remapping-alist))) | |
145 | (cdr cookie)))))) | |
146 | ||
147 | ;;;### autoload | |
e40a85cd | 148 | (defun face-remap-reset-base (face) |
9d3d42fb MB |
149 | "Set the base remapping of FACE to inherit from FACE's global definition." |
150 | (let ((entry (assq face face-remapping-alist))) | |
151 | (when entry | |
152 | ;; If there's nothing except a base remapping, we simply remove | |
153 | ;; the entire remapping entry, as setting the base to the default | |
154 | ;; would be the same as the global definition. Otherwise, we | |
155 | ;; modify the base remapping. | |
156 | (if (null (cddr entry)) ; nothing except base remapping | |
157 | (setq face-remapping-alist ; so remove entire entry | |
158 | (remq entry face-remapping-alist)) | |
159 | (setcar (last entry) face))))) ; otherwise, just inherit global def | |
160 | ||
161 | ;;;### autoload | |
e40a85cd | 162 | (defun face-remap-set-base (face &rest specs) |
9d3d42fb MB |
163 | "Set the base remapping of FACE in the current buffer to SPECS. |
164 | If SPECS is empty, the default base remapping is restored, which | |
165 | inherits from the global definition of FACE; note that this is | |
166 | different from SPECS containing a single value `nil', which does | |
167 | not inherit from the global definition of FACE." | |
168 | (if (or (null specs) | |
169 | (and (eq (car specs) face) (null (cdr specs)))) ; default | |
170 | ;; Set entry back to default | |
e40a85cd | 171 | (face-remap-reset-base face) |
9d3d42fb MB |
172 | ;; Set the base remapping |
173 | (make-local-variable 'face-remapping-alist) | |
174 | (let ((entry (assq face face-remapping-alist))) | |
175 | (if entry | |
176 | (setcar (last entry) specs) ; overwrite existing base entry | |
177 | (push (list face specs) face-remapping-alist))))) | |
12de5099 | 178 | |
9d3d42fb MB |
179 | \f |
180 | ;; ---------------------------------------------------------------- | |
181 | ;; text-scale-mode | |
182 | ||
183 | (defcustom text-scale-mode-step 1.2 | |
184 | "Scale factor used by `text-scale-mode'. | |
185 | Each positive or negative step scales the default face height by this amount." | |
186 | :group 'display | |
187 | :type 'number) | |
188 | ||
189 | ;; current remapping cookie for text-scale-mode | |
190 | (defvar text-scale-mode-remapping nil) | |
191 | (make-variable-buffer-local 'text-scale-mode-remapping) | |
192 | ||
193 | ;; Lighter displayed for text-scale-mode in mode-line minor-mode list | |
194 | (defvar text-scale-mode-lighter "+0") | |
195 | (make-variable-buffer-local 'text-scale-mode-lighter) | |
196 | ||
197 | ;; Number of steps that text-scale-mode will increase/decrease text height | |
198 | (defvar text-scale-mode-amount 0) | |
199 | (make-variable-buffer-local 'text-scale-mode-amount) | |
200 | ||
201 | (define-minor-mode text-scale-mode | |
202 | "Minor mode for displaying buffer text in a larger/smaller font than usual. | |
203 | ||
204 | The amount of scaling is determined by the variable | |
12de5099 JB |
205 | `text-scale-mode-amount': one step scales the global default |
206 | face size by the value of the variable `text-scale-mode-step' | |
207 | \(a negative amount shrinks the text). | |
208 | ||
e40a85cd MB |
209 | The `text-scale-increase' and `text-scale-decrease' functions may |
210 | be used to interactively modify the variable | |
211 | `text-scale-mode-amount' (they also enable or disable | |
212 | `text-scale-mode' as necessary)." | |
9d3d42fb MB |
213 | :lighter (" " text-scale-mode-lighter) |
214 | (when text-scale-mode-remapping | |
e40a85cd | 215 | (face-remap-remove-relative text-scale-mode-remapping)) |
9d3d42fb MB |
216 | (setq text-scale-mode-lighter |
217 | (format (if (>= text-scale-mode-amount 0) "+%d" "%d") | |
218 | text-scale-mode-amount)) | |
219 | (setq text-scale-mode-remapping | |
220 | (and text-scale-mode | |
e40a85cd | 221 | (face-remap-add-relative 'default |
9d3d42fb MB |
222 | :height |
223 | (expt text-scale-mode-step | |
224 | text-scale-mode-amount)))) | |
225 | (force-window-update (current-buffer))) | |
226 | ||
9d3d42fb | 227 | ;;;###autoload |
e40a85cd | 228 | (defun text-scale-increase (&optional inc) |
9d3d42fb MB |
229 | "Increase the height of the default face in the current buffer by INC steps. |
230 | If the new height is other than the default, `text-scale-mode' is enabled. | |
231 | ||
232 | Each step scales the height of the default face by the variable | |
233 | `text-scale-mode-step' (a negative number of steps decreases the | |
234 | height by the same amount). As a special case, an argument of 0 | |
235 | will remove any scaling currently active." | |
56c73dec | 236 | (interactive "p") |
9d3d42fb MB |
237 | (setq text-scale-mode-amount (if (= inc 0) 0 (+ text-scale-mode-amount inc))) |
238 | (text-scale-mode (if (zerop text-scale-mode-amount) -1 1))) | |
239 | ||
9d3d42fb | 240 | ;;;###autoload |
e40a85cd | 241 | (defun text-scale-decrease (&optional dec) |
9d3d42fb | 242 | "Decrease the height of the default face in the current buffer by DEC steps. |
e40a85cd | 243 | See `text-scale-increase' for more details." |
56c73dec | 244 | (interactive "p") |
e40a85cd | 245 | (text-scale-increase (- dec))) |
9d3d42fb | 246 | |
e40a85cd MB |
247 | ;;;###autoload (define-key ctl-x-map [(control ?+)] 'text-scale-adjust) |
248 | ;;;###autoload (define-key ctl-x-map [(control ?-)] 'text-scale-adjust) | |
249 | ;;;###autoload (define-key ctl-x-map [(control ?=)] 'text-scale-adjust) | |
250 | ;;;###autoload (define-key ctl-x-map [(control ?0)] 'text-scale-adjust) | |
56c73dec | 251 | ;;;###autoload |
e40a85cd | 252 | (defun text-scale-adjust (&optional inc) |
56c73dec MB |
253 | "Increase or decrease the height of the default face in the current buffer. |
254 | ||
255 | The actual adjustment made depends on the final component of the | |
12de5099 | 256 | key-binding used to invoke the command, with all modifiers removed: |
56c73dec MB |
257 | |
258 | +, = Increase the default face height by one step | |
259 | - Decrease the default face height by one step | |
260 | 0 Reset the default face height to the global default | |
261 | ||
262 | Then, continue to read input events and further adjust the face | |
12de5099 JB |
263 | height as long as the input event read (with all modifiers removed) |
264 | is one of the above. | |
56c73dec MB |
265 | |
266 | Each step scales the height of the default face by the variable | |
267 | `text-scale-mode-step' (a negative number of steps decreases the | |
268 | height by the same amount). As a special case, an argument of 0 | |
269 | will remove any scaling currently active. | |
270 | ||
271 | This command is a special-purpose wrapper around the | |
e40a85cd MB |
272 | `text-scale-increase' command which makes repetition convenient |
273 | even when it is bound in a non-top-level keymap. For binding in | |
274 | a top-level keymap, `text-scale-increase' or | |
275 | `text-scale-decrease' may be more appropriate." | |
56c73dec | 276 | (interactive "p") |
12de5099 | 277 | (let ((first t) |
56c73dec MB |
278 | (step t) |
279 | (ev last-command-event)) | |
280 | (while step | |
281 | (let ((base (event-basic-type ev))) | |
282 | (cond ((or (eq base ?+) (eq base ?=)) | |
283 | (setq step inc)) | |
284 | ((eq base ?-) | |
285 | (setq step (- inc))) | |
286 | ((eq base ?0) | |
287 | (setq step 0)) | |
12de5099 | 288 | (first |
56c73dec MB |
289 | (setq step inc)) |
290 | (t | |
291 | (setq step nil)))) | |
292 | (when step | |
e40a85cd | 293 | (text-scale-increase step) |
56c73dec MB |
294 | (setq inc 1 first nil) |
295 | (setq ev (read-event)))) | |
296 | (push ev unread-command-events))) | |
297 | ||
9d3d42fb MB |
298 | \f |
299 | ;; ---------------------------------------------------------------- | |
d7ed971d | 300 | ;; buffer-face-mode |
9d3d42fb | 301 | |
d7ed971d MB |
302 | (defcustom buffer-face-mode-face 'variable-pitch |
303 | "The face specification used by `buffer-face-mode'. | |
304 | It may contain any value suitable for a `face' text property, | |
305 | including a face name, a list of face names, a face-attribute | |
306 | plist, etc." | |
307 | :group 'display) | |
9d3d42fb | 308 | |
d7ed971d MB |
309 | ;; current remapping cookie for buffer-face-mode |
310 | (defvar buffer-face-mode-remapping nil) | |
311 | (make-variable-buffer-local 'buffer-face-mode-remapping) | |
9d3d42fb | 312 | |
15252ee9 | 313 | ;;;###autoload |
d7ed971d MB |
314 | (define-minor-mode buffer-face-mode |
315 | "Minor mode for a buffer-specific default face. | |
316 | When enabled, the face specified by the variable | |
317 | `buffer-face-mode-face' is used to display the buffer text." | |
318 | :lighter " BufFace" | |
319 | (when buffer-face-mode-remapping | |
320 | (face-remap-remove-relative buffer-face-mode-remapping)) | |
321 | (setq buffer-face-mode-remapping | |
322 | (and buffer-face-mode | |
323 | (face-remap-add-relative 'default buffer-face-mode-face))) | |
9d3d42fb MB |
324 | (force-window-update (current-buffer))) |
325 | ||
d7ed971d MB |
326 | ;;;###autoload |
327 | (defun buffer-face-set (face) | |
328 | "Enable `buffer-face-mode', using the face FACE. | |
329 | If FACE is nil, then `buffer-face-mode' is disabled. This | |
330 | function will make the variable `buffer-face-mode-face' buffer | |
331 | local, and set it to FACE." | |
332 | (interactive (list (read-face-name "Set buffer face"))) | |
333 | (if (null face) | |
334 | (buffer-face-mode 0) | |
335 | (set (make-local-variable 'buffer-face-mode-face) face) | |
336 | (buffer-face-mode t))) | |
337 | ||
338 | ;;;###autoload | |
339 | (defun buffer-face-toggle (face) | |
340 | "Toggle `buffer-face-mode', using the face FACE. | |
341 | ||
342 | If `buffer-face-mode' is already enabled, and is currently using | |
343 | the face FACE, then it is disabled; if buffer-face-mode is | |
344 | disabled, or is enabled and currently displaying some other face, | |
345 | then is left enabled, but the face changed to FACE. This | |
346 | function will make the variable `buffer-face-mode-face' buffer | |
347 | local, and set it to FACE." | |
348 | (interactive (list buffer-face-mode-face)) | |
349 | (if (or (null face) | |
350 | (and buffer-face-mode (equal buffer-face-mode-face face))) | |
351 | (buffer-face-mode 0) | |
352 | (set (make-local-variable 'buffer-face-mode-face) face) | |
353 | (buffer-face-mode t))) | |
354 | ||
355 | (defun buffer-face-mode-invoke (face arg &optional interactive) | |
356 | "Enable or disable `buffer-face-mode' using the face FACE, and argument ARG. | |
357 | ARG is interpreted in the usual manner for minor-mode commands. | |
358 | Besides the choice of face, this is the same as the `buffer-face-mode' command. | |
359 | If INTERACTIVE is non-nil, a message will be displayed describing the result." | |
360 | (let ((last-message (current-message))) | |
361 | (if (or (eq arg 'toggle) (not arg)) | |
362 | (buffer-face-toggle face) | |
363 | (buffer-face-set (and (> (prefix-numeric-value arg) 0) face))) | |
364 | (when interactive | |
365 | (unless (and (current-message) | |
366 | (not (equal last-message (current-message)))) | |
367 | (message "Buffer-Face mode %sabled" | |
368 | (if buffer-face-mode "en" "dis")))))) | |
369 | ||
370 | \f | |
371 | ;; ---------------------------------------------------------------- | |
372 | ;; variable-pitch-mode | |
373 | ||
374 | ;;;###autoload | |
375 | (defun variable-pitch-mode (&optional arg) | |
376 | "Variable-pitch default-face mode. | |
377 | An interface to `buffer-face-mode' which uses the `variable-pitch' face. | |
378 | Besides the choice of face, it is the same as `buffer-face-mode'." | |
379 | (interactive (list (or current-prefix-arg 'toggle))) | |
380 | (buffer-face-mode-invoke 'variable-pitch arg (interactive-p))) | |
381 | ||
9d3d42fb MB |
382 | |
383 | (provide 'face-remap) | |
384 | ||
385 | ;; arch-tag: 5c5f034b-8d58-4967-82bd-d61fd364e686 | |
386 | ;;; face-remap.el ends here |