* lisp/wid-edit.el (widget-complete-field): Remove.
[bpt/emacs.git] / lisp / face-remap.el
CommitLineData
9d3d42fb
MB
1;;; face-remap.el --- Functions for managing `face-remapping-alist'
2;;
acaf905b 3;; Copyright (C) 2008-2012 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)
75"Return true if ATTRS1 contains a greater number of relative
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
MB
108 "Add a face remapping entry of FACE to SPECS in the current buffer.
109
110Return a cookie which can be used to delete the remapping with
e40a85cd 111`face-remap-remove-relative'.
9d3d42fb
MB
112
113SPECS can be any value suitable for the `face' text property,
114including a face name, a list of face names, or a face-attribute
115property list. The attributes given by SPECS will be merged with
116any other currently active face remappings of FACE, and with the
d03d411d
MB
117global definition of FACE. An attempt is made to sort multiple
118entries so that entries with relative face-attributes are applied
119after entries with absolute face-attributes.
9d3d42fb
MB
120
121The base (lowest priority) remapping may be set to a specific
122value, instead of the default of the global face definition,
e40a85cd 123using `face-remap-set-base'."
cece37cf
MB
124 (while (and (consp specs) (null (cdr specs)))
125 (setq specs (car specs)))
9d3d42fb
MB
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))
d03d411d 131 (setcdr entry (face-remap-order (cons specs (cdr entry))))
9d3d42fb
MB
132 (cons face specs)))
133
e40a85cd
MB
134(defun face-remap-remove-relative (cookie)
135 "Remove a face remapping previously added by `face-remap-add-relative'.
9d3d42fb
MB
136COOKIE 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))))))
148
23b77eee 149;;;###autoload
e40a85cd 150(defun face-remap-reset-base (face)
9d3d42fb
MB
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
162
23b77eee 163;;;###autoload
e40a85cd 164(defun face-remap-set-base (face &rest specs)
9d3d42fb
MB
165 "Set the base remapping of FACE in the current buffer to SPECS.
166If SPECS is empty, the default base remapping is restored, which
167inherits from the global definition of FACE; note that this is
168different from SPECS containing a single value `nil', which does
169not inherit from the global definition of FACE."
cece37cf
MB
170 (while (and (consp specs) (not (null (car specs))) (null (cdr specs)))
171 (setq specs (car specs)))
9d3d42fb
MB
172 (if (or (null specs)
173 (and (eq (car specs) face) (null (cdr specs)))) ; default
174 ;; Set entry back to default
e40a85cd 175 (face-remap-reset-base face)
9d3d42fb
MB
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)))))
12de5099 182
9d3d42fb
MB
183\f
184;; ----------------------------------------------------------------
185;; text-scale-mode
186
187(defcustom text-scale-mode-step 1.2
188 "Scale factor used by `text-scale-mode'.
189Each positive or negative step scales the default face height by this amount."
190 :group 'display
af09cfd7
JB
191 :type 'number
192 :version "23.1")
9d3d42fb
MB
193
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
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)
201
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)
205
206(define-minor-mode text-scale-mode
06e21633 207 "Minor mode for displaying buffer text in a larger/smaller font.
e1ac4066
GM
208With a prefix argument ARG, enable the mode if ARG is positive,
209and disable it otherwise. If called from Lisp, enable the mode
210if ARG is omitted or nil.
9d3d42fb
MB
211
212The amount of scaling is determined by the variable
12de5099
JB
213`text-scale-mode-amount': one step scales the global default
214face size by the value of the variable `text-scale-mode-step'
215\(a negative amount shrinks the text).
216
05fbc4a9
MB
217The `text-scale-increase', `text-scale-decrease', and
218`text-scale-set' functions may be used to interactively modify
219the variable `text-scale-mode-amount' (they also enable or
220disable `text-scale-mode' as necessary)."
9d3d42fb
MB
221 :lighter (" " text-scale-mode-lighter)
222 (when text-scale-mode-remapping
e40a85cd 223 (face-remap-remove-relative text-scale-mode-remapping))
9d3d42fb
MB
224 (setq text-scale-mode-lighter
225 (format (if (>= text-scale-mode-amount 0) "+%d" "%d")
226 text-scale-mode-amount))
227 (setq text-scale-mode-remapping
228 (and text-scale-mode
e40a85cd 229 (face-remap-add-relative 'default
9d3d42fb
MB
230 :height
231 (expt text-scale-mode-step
232 text-scale-mode-amount))))
233 (force-window-update (current-buffer)))
234
05fbc4a9
MB
235;;;###autoload
236(defun text-scale-set (level)
237 "Set the scale factor of the default face in the current buffer to LEVEL.
238If LEVEL is non-zero, `text-scale-mode' is enabled, otherwise it is disabled.
239
240LEVEL is a number of steps, with 0 representing the default size.
241Each step scales the height of the default face by the variable
242`text-scale-mode-step' (a negative number decreases the height by
243the same amount)."
244 (interactive "p")
245 (setq text-scale-mode-amount level)
246 (text-scale-mode (if (zerop text-scale-mode-amount) -1 1)))
247
9d3d42fb 248;;;###autoload
16c1ddc2 249(defun text-scale-increase (inc)
9d3d42fb
MB
250 "Increase the height of the default face in the current buffer by INC steps.
251If the new height is other than the default, `text-scale-mode' is enabled.
252
253Each step scales the height of the default face by the variable
254`text-scale-mode-step' (a negative number of steps decreases the
255height by the same amount). As a special case, an argument of 0
256will remove any scaling currently active."
56c73dec 257 (interactive "p")
dced1efd
MB
258 (setq text-scale-mode-amount
259 (if (= inc 0) 0 (+ (if text-scale-mode text-scale-mode-amount 0) inc)))
9d3d42fb
MB
260 (text-scale-mode (if (zerop text-scale-mode-amount) -1 1)))
261
9d3d42fb 262;;;###autoload
16c1ddc2 263(defun text-scale-decrease (dec)
9d3d42fb 264 "Decrease the height of the default face in the current buffer by DEC steps.
e40a85cd 265See `text-scale-increase' for more details."
56c73dec 266 (interactive "p")
e40a85cd 267 (text-scale-increase (- dec)))
9d3d42fb 268
e40a85cd
MB
269;;;###autoload (define-key ctl-x-map [(control ?+)] 'text-scale-adjust)
270;;;###autoload (define-key ctl-x-map [(control ?-)] 'text-scale-adjust)
271;;;###autoload (define-key ctl-x-map [(control ?=)] 'text-scale-adjust)
272;;;###autoload (define-key ctl-x-map [(control ?0)] 'text-scale-adjust)
56c73dec 273;;;###autoload
16c1ddc2 274(defun text-scale-adjust (inc)
56c73dec
MB
275 "Increase or decrease the height of the default face in the current buffer.
276
277The actual adjustment made depends on the final component of the
12de5099 278key-binding used to invoke the command, with all modifiers removed:
56c73dec
MB
279
280 +, = Increase the default face height by one step
281 - Decrease the default face height by one step
282 0 Reset the default face height to the global default
283
284Then, continue to read input events and further adjust the face
12de5099
JB
285height as long as the input event read (with all modifiers removed)
286is one of the above.
56c73dec
MB
287
288Each step scales the height of the default face by the variable
289`text-scale-mode-step' (a negative number of steps decreases the
290height by the same amount). As a special case, an argument of 0
291will remove any scaling currently active.
292
293This command is a special-purpose wrapper around the
e40a85cd
MB
294`text-scale-increase' command which makes repetition convenient
295even when it is bound in a non-top-level keymap. For binding in
296a top-level keymap, `text-scale-increase' or
297`text-scale-decrease' may be more appropriate."
56c73dec 298 (interactive "p")
12de5099 299 (let ((first t)
56c73dec 300 (step t)
fed7c4f5
MB
301 (ev last-command-event)
302 (echo-keystrokes nil))
56c73dec
MB
303 (while step
304 (let ((base (event-basic-type ev)))
305 (cond ((or (eq base ?+) (eq base ?=))
306 (setq step inc))
307 ((eq base ?-)
308 (setq step (- inc)))
309 ((eq base ?0)
310 (setq step 0))
12de5099 311 (first
56c73dec
MB
312 (setq step inc))
313 (t
314 (setq step nil))))
315 (when step
e40a85cd 316 (text-scale-increase step)
56c73dec 317 (setq inc 1 first nil)
52ce2890 318 (setq ev (read-event "+,-,0 for further adjustment: "))))
56c73dec
MB
319 (push ev unread-command-events)))
320
9d3d42fb
MB
321\f
322;; ----------------------------------------------------------------
d7ed971d 323;; buffer-face-mode
9d3d42fb 324
d7ed971d
MB
325(defcustom buffer-face-mode-face 'variable-pitch
326 "The face specification used by `buffer-face-mode'.
327It may contain any value suitable for a `face' text property,
328including a face name, a list of face names, a face-attribute
329plist, etc."
af09cfd7
JB
330 :group 'display
331 :version "23.1")
9d3d42fb 332
d7ed971d
MB
333;; current remapping cookie for buffer-face-mode
334(defvar buffer-face-mode-remapping nil)
335(make-variable-buffer-local 'buffer-face-mode-remapping)
9d3d42fb 336
15252ee9 337;;;###autoload
d7ed971d
MB
338(define-minor-mode buffer-face-mode
339 "Minor mode for a buffer-specific default face.
e1ac4066
GM
340With a prefix argument ARG, enable the mode if ARG is positive,
341and disable it otherwise. If called from Lisp, enable the mode
342if ARG is omitted or nil. When enabled, the face specified by the
343variable `buffer-face-mode-face' is used to display the buffer text."
d7ed971d
MB
344 :lighter " BufFace"
345 (when buffer-face-mode-remapping
346 (face-remap-remove-relative buffer-face-mode-remapping))
347 (setq buffer-face-mode-remapping
348 (and buffer-face-mode
349 (face-remap-add-relative 'default buffer-face-mode-face)))
9d3d42fb
MB
350 (force-window-update (current-buffer)))
351
d7ed971d 352;;;###autoload
cece37cf
MB
353(defun buffer-face-set (&rest specs)
354 "Enable `buffer-face-mode', using face specs SPECS.
355SPECS can be any value suitable for the `face' text property,
356including a face name, a list of face names, or a face-attribute
357If SPECS is nil, then `buffer-face-mode' is disabled.
358
359This function will make the variable `buffer-face-mode-face'
360buffer local, and set it to FACE."
d7ed971d 361 (interactive (list (read-face-name "Set buffer face")))
cece37cf
MB
362 (while (and (consp specs) (null (cdr specs)))
363 (setq specs (car specs)))
364 (if (null specs)
d7ed971d 365 (buffer-face-mode 0)
cece37cf 366 (set (make-local-variable 'buffer-face-mode-face) specs)
d7ed971d
MB
367 (buffer-face-mode t)))
368
369;;;###autoload
cece37cf
MB
370(defun buffer-face-toggle (&rest specs)
371 "Toggle `buffer-face-mode', using face specs SPECS.
372SPECS can be any value suitable for the `face' text property,
373including a face name, a list of face names, or a face-attribute
d7ed971d
MB
374
375If `buffer-face-mode' is already enabled, and is currently using
cece37cf 376the face specs SPECS, then it is disabled; if buffer-face-mode is
d7ed971d 377disabled, or is enabled and currently displaying some other face,
cece37cf
MB
378then is left enabled, but the face changed to reflect SPECS.
379
380This function will make the variable `buffer-face-mode-face'
381buffer local, and set it to SPECS."
d7ed971d 382 (interactive (list buffer-face-mode-face))
cece37cf
MB
383 (while (and (consp specs) (null (cdr specs)))
384 (setq specs (car specs)))
385 (if (or (null specs)
386 (and buffer-face-mode (equal buffer-face-mode-face specs)))
d7ed971d 387 (buffer-face-mode 0)
cece37cf 388 (set (make-local-variable 'buffer-face-mode-face) specs)
d7ed971d
MB
389 (buffer-face-mode t)))
390
cece37cf
MB
391(defun buffer-face-mode-invoke (specs arg &optional interactive)
392 "Enable or disable `buffer-face-mode' using face specs SPECS, and argument ARG.
393ARG controls whether the mode is enabled or disabled, and is
394interpreted in the usual manner for minor-mode commands.
395
396SPECS can be any value suitable for the `face' text property,
397including a face name, a list of face names, or a face-attribute
398
399If INTERACTIVE is non-nil, a message will be displayed describing the result.
400
23b77eee 401This is a wrapper function which calls `buffer-face-set' or
cece37cf
MB
402`buffer-face-toggle' (depending on ARG), and prints a status
403message in the echo area. In many cases one of those functions
404may be more appropriate."
d7ed971d
MB
405 (let ((last-message (current-message)))
406 (if (or (eq arg 'toggle) (not arg))
2875c646
MB
407 (buffer-face-toggle specs)
408 (buffer-face-set (and (> (prefix-numeric-value arg) 0) specs)))
d7ed971d
MB
409 (when interactive
410 (unless (and (current-message)
411 (not (equal last-message (current-message))))
412 (message "Buffer-Face mode %sabled"
413 (if buffer-face-mode "en" "dis"))))))
414
415\f
416;; ----------------------------------------------------------------
417;; variable-pitch-mode
418
419;;;###autoload
420(defun variable-pitch-mode (&optional arg)
421 "Variable-pitch default-face mode.
422An interface to `buffer-face-mode' which uses the `variable-pitch' face.
423Besides the choice of face, it is the same as `buffer-face-mode'."
424 (interactive (list (or current-prefix-arg 'toggle)))
32226619
JB
425 (buffer-face-mode-invoke 'variable-pitch arg
426 (called-interactively-p 'interactive)))
d7ed971d 427
9d3d42fb
MB
428
429(provide 'face-remap)
430
9d3d42fb 431;;; face-remap.el ends here