Commit | Line | Data |
---|---|---|
4e8aa578 | 1 | ;;; facemenu.el -- Create a face menu for interactively adding fonts to text |
732be465 | 2 | ;; Copyright (c) 1994, 1995 Free Software Foundation, Inc. |
4e8aa578 | 3 | |
7865eac6 | 4 | ;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu> |
4e8aa578 RS |
5 | ;; Keywords: faces |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
9 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 | ;; it under the terms of the GNU General Public License as published by | |
11 | ;; the Free Software Foundation; either version 2, or (at your option) | |
12 | ;; any later version. | |
13 | ||
14 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
21 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | ||
23 | ;;; Commentary: | |
bf7d4561 BG |
24 | ;; This file defines a menu of faces (bold, italic, etc) which allows you to |
25 | ;; set the face used for a region of the buffer. Some faces also have | |
26 | ;; keybindings, which are shown in the menu. Faces with names beginning with | |
88d690a9 | 27 | ;; "fg:" or "bg:", as in "fg:red", are treated specially. |
bf7d4561 BG |
28 | ;; Such faces are assumed to consist only of a foreground (if "fg:") or |
29 | ;; background (if "bg:") color. They are thus put into the color submenus | |
88d690a9 RS |
30 | ;; rather than the general Face submenu. These faces can also be |
31 | ;; automatically created by selecting the "Other..." menu items in the | |
32 | ;; "Foreground" and "Background" submenus. | |
33 | ;; | |
34 | ;; The menu also contains submenus for indentation and justification-changing | |
35 | ;; commands. | |
4e8aa578 | 36 | |
4e8aa578 | 37 | ;;; Usage: |
bf7d4561 BG |
38 | ;; Selecting a face from the menu or typing the keyboard equivalent will |
39 | ;; change the region to use that face. If you use transient-mark-mode and the | |
40 | ;; region is not active, the face will be remembered and used for the next | |
41 | ;; insertion. It will be forgotten if you move point or make other | |
42 | ;; modifications before inserting or typing anything. | |
4e8aa578 RS |
43 | ;; |
44 | ;; Faces can be selected from the keyboard as well. | |
88d690a9 RS |
45 | ;; The standard keybindings are M-g (or ESC g) + letter: |
46 | ;; M-g i = "set italic", M-g b = "set bold", etc. | |
4e8aa578 RS |
47 | |
48 | ;;; Customization: | |
49 | ;; An alternative set of keybindings that may be easier to type can be set up | |
88d690a9 RS |
50 | ;; using "Alt" or "Hyper" keys. This requires that you either have or create |
51 | ;; an Alt or Hyper key on your keyboard. On my keyboard, there is a key | |
52 | ;; labeled "Alt", but to make it act as an Alt key I have to put this command | |
53 | ;; into my .xinitrc: | |
54 | ;; xmodmap -e "add Mod3 = Alt_L" | |
55 | ;; Or, I can make it into a Hyper key with this: | |
4e8aa578 | 56 | ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L" |
88d690a9 RS |
57 | ;; Check with local X-perts for how to do it on your system. |
58 | ;; Then you can define your keybindings with code like this in your .emacs: | |
4e8aa578 RS |
59 | ;; (setq facemenu-keybindings |
60 | ;; '((default . [?\H-d]) | |
61 | ;; (bold . [?\H-b]) | |
62 | ;; (italic . [?\H-i]) | |
88d690a9 | 63 | ;; (bold-italic . [?\H-l]) |
4e8aa578 RS |
64 | ;; (underline . [?\H-u]))) |
65 | ;; (setq facemenu-keymap global-map) | |
66 | ;; (setq facemenu-key nil) | |
88d690a9 RS |
67 | ;; (define-key global-map [?\H-c] 'facemenu-set-foreground) ; set fg color |
68 | ;; (define-key global-map [?\H-C] 'facemenu-set-background) ; set bg color | |
69 | ;; (require 'facemenu) | |
4e8aa578 | 70 | ;; |
88d690a9 RS |
71 | ;; The order of the faces that appear in the menu and their keybindings can be |
72 | ;; controlled by setting the variables `facemenu-keybindings' and | |
73 | ;; `facemenu-new-faces-at-end'. List faces that you don't use in documents | |
74 | ;; (eg, `region') in `facemenu-unlisted-faces'. | |
4e8aa578 RS |
75 | |
76 | ;;; Known Problems: | |
88d690a9 RS |
77 | ;; Bold and Italic do not combine to create bold-italic if you select them |
78 | ;; both, although most other combinations (eg bold + underline + some color) | |
79 | ;; do the intuitive thing. | |
80 | ;; | |
4e8aa578 RS |
81 | ;; There is at present no way to display what the faces look like in |
82 | ;; the menu itself. | |
83 | ;; | |
84 | ;; `list-faces-display' shows the faces in a different order than | |
85 | ;; this menu, which could be confusing. I do /not/ sort the list | |
86 | ;; alphabetically, because I like the default order: it puts the most | |
87 | ;; basic, common fonts first. | |
88 | ;; | |
89 | ;; Please send me any other problems, comments or ideas. | |
90 | ||
91 | ;;; Code: | |
92 | ||
93 | (provide 'facemenu) | |
94 | ||
9dc90430 BG |
95 | ;;; Provide some binding for startup: |
96 | ;;;###autoload (define-key global-map "\M-g" 'facemenu-keymap) | |
97 | ;;;###autoload (autoload 'facemenu-keymap "facemenu" "Keymap for face-changing commands." t 'keymap) | |
98 | ||
d2eafd88 | 99 | (defvar facemenu-key "\M-g" |
9dc90430 | 100 | "Prefix key to use for facemenu commands.") |
4e8aa578 | 101 | |
4e8aa578 RS |
102 | (defvar facemenu-keybindings |
103 | '((default . "d") | |
104 | (bold . "b") | |
105 | (italic . "i") | |
88d690a9 | 106 | (bold-italic . "l") ; {bold} intersect {italic} = {l} |
4e8aa578 RS |
107 | (underline . "u")) |
108 | "Alist of interesting faces and keybindings. | |
109 | Each element is itself a list: the car is the name of the face, | |
110 | the next element is the key to use as a keyboard equivalent of the menu item; | |
111 | the binding is made in facemenu-keymap. | |
112 | ||
113 | The faces specifically mentioned in this list are put at the top of | |
114 | the menu, in the order specified. All other faces which are defined, | |
115 | except for those in `facemenu-unlisted-faces', are listed after them, | |
116 | but get no keyboard equivalents. | |
117 | ||
118 | If you change this variable after loading facemenu.el, you will need to call | |
119 | `facemenu-update' to make it take effect.") | |
120 | ||
88d690a9 RS |
121 | (defvar facemenu-new-faces-at-end t |
122 | "Where in the menu to insert newly-created faces. | |
123 | This should be nil to put them at the top of the menu, or t to put them | |
124 | just before \"Other\" at the end.") | |
125 | ||
4e8aa578 | 126 | (defvar facemenu-unlisted-faces |
da627a71 BG |
127 | '(modeline region secondary-selection highlight scratch-face |
128 | font-lock-comment-face font-lock-string-face font-lock-keyword-face | |
129 | font-lock-function-name-face font-lock-variable-name-face | |
130 | font-lock-type-face font-lock-reference-face) | |
88d690a9 | 131 | "List of faces not to include in the Face menu. |
4e8aa578 | 132 | Set this before loading facemenu.el, or call `facemenu-update' after |
88d690a9 | 133 | changing it. |
4e8aa578 | 134 | |
88d690a9 RS |
135 | If this variable is t, no faces will be added to the menu. This is useful for |
136 | temporarily turning off the feature that automatically adds faces to the menu | |
137 | when they are created.") | |
138 | ||
9dc90430 | 139 | ;;;###autoload |
88d690a9 | 140 | (defvar facemenu-face-menu |
bf7d4561 | 141 | (let ((map (make-sparse-keymap "Face"))) |
88d690a9 | 142 | (define-key map "o" (cons "Other..." 'facemenu-set-face)) |
bf7d4561 BG |
143 | map) |
144 | "Menu keymap for faces.") | |
9dc90430 | 145 | ;;;###autoload |
88d690a9 | 146 | (defalias 'facemenu-face-menu facemenu-face-menu) |
bf7d4561 | 147 | |
9dc90430 | 148 | ;;;###autoload |
bf7d4561 BG |
149 | (defvar facemenu-foreground-menu |
150 | (let ((map (make-sparse-keymap "Foreground Color"))) | |
f34eaa2c | 151 | (define-key map "o" (cons "Other..." 'facemenu-set-foreground)) |
bf7d4561 BG |
152 | map) |
153 | "Menu keymap for foreground colors.") | |
9dc90430 | 154 | ;;;###autoload |
88d690a9 | 155 | (defalias 'facemenu-foreground-menu facemenu-foreground-menu) |
bf7d4561 | 156 | |
9dc90430 | 157 | ;;;###autoload |
bf7d4561 BG |
158 | (defvar facemenu-background-menu |
159 | (let ((map (make-sparse-keymap "Background Color"))) | |
f34eaa2c | 160 | (define-key map "o" (cons "Other..." 'facemenu-set-background)) |
bf7d4561 BG |
161 | map) |
162 | "Menu keymap for background colors") | |
9dc90430 | 163 | ;;;###autoload |
88d690a9 | 164 | (defalias 'facemenu-background-menu facemenu-background-menu) |
bf7d4561 | 165 | |
9dc90430 | 166 | ;;;###autoload |
bf7d4561 BG |
167 | (defvar facemenu-special-menu |
168 | (let ((map (make-sparse-keymap "Special"))) | |
f34eaa2c KH |
169 | (define-key map [?s] (cons "Remove Special" 'facemenu-remove-special)) |
170 | (define-key map [?t] (cons "Intangible" 'facemenu-set-intangible)) | |
171 | (define-key map [?v] (cons "Invisible" 'facemenu-set-invisible)) | |
172 | (define-key map [?r] (cons "Read-Only" 'facemenu-set-read-only)) | |
bf7d4561 BG |
173 | map) |
174 | "Menu keymap for non-face text-properties.") | |
9dc90430 | 175 | ;;;###autoload |
88d690a9 RS |
176 | (defalias 'facemenu-special-menu facemenu-special-menu) |
177 | ||
9dc90430 | 178 | ;;;###autoload |
88d690a9 RS |
179 | (defvar facemenu-justification-menu |
180 | (let ((map (make-sparse-keymap "Justification"))) | |
9dc90430 BG |
181 | (define-key map [?c] (cons "Center" 'set-justification-center)) |
182 | (define-key map [?b] (cons "Full" 'set-justification-full)) | |
183 | (define-key map [?r] (cons "Right" 'set-justification-right)) | |
184 | (define-key map [?l] (cons "Left" 'set-justification-left)) | |
185 | (define-key map [?u] (cons "Unfilled" 'set-justification-none)) | |
88d690a9 RS |
186 | map) |
187 | "Submenu for text justification commands.") | |
9dc90430 | 188 | ;;;###autoload |
88d690a9 RS |
189 | (defalias 'facemenu-justification-menu facemenu-justification-menu) |
190 | ||
9dc90430 | 191 | ;;;###autoload |
88d690a9 RS |
192 | (defvar facemenu-indentation-menu |
193 | (let ((map (make-sparse-keymap "Indentation"))) | |
f34eaa2c KH |
194 | (define-key map [decrease-right-margin] |
195 | (cons "Indent Right Less" 'decrease-right-margin)) | |
196 | (define-key map [increase-right-margin] | |
197 | (cons "Indent Right More" 'increase-right-margin)) | |
198 | (define-key map [decrease-left-margin] | |
199 | (cons "Indent Less" 'decrease-left-margin)) | |
200 | (define-key map [increase-left-margin] | |
201 | (cons "Indent More" 'increase-left-margin)) | |
88d690a9 RS |
202 | map) |
203 | "Submenu for indentation commands.") | |
9dc90430 | 204 | ;;;###autoload |
88d690a9 | 205 | (defalias 'facemenu-indentation-menu facemenu-indentation-menu) |
bf7d4561 | 206 | |
f34eaa2c | 207 | ;; This is split up to avoid an overlong line in loaddefs.el. |
9dc90430 | 208 | ;;;###autoload |
f34eaa2c | 209 | (defvar facemenu-menu nil |
535d2617 | 210 | "Facemenu top-level menu keymap.") |
9dc90430 | 211 | ;;;###autoload |
f34eaa2c KH |
212 | (setq facemenu-menu (make-sparse-keymap "Text Properties")) |
213 | ;;;###autoload | |
214 | (let ((map facemenu-menu)) | |
215 | (define-key map [dc] (cons "Display Colors" 'list-colors-display)) | |
216 | (define-key map [df] (cons "Display Faces" 'list-faces-display)) | |
217 | (define-key map [dp] (cons "List Properties" 'list-text-properties-at)) | |
218 | (define-key map [ra] (cons "Remove All" 'facemenu-remove-all)) | |
219 | (define-key map [rm] (cons "Remove Properties" 'facemenu-remove-props)) | |
220 | (define-key map [s1] (list "-----------------"))) | |
221 | ;;;###autoload | |
222 | (let ((map facemenu-menu)) | |
223 | (define-key map [in] (cons "Indentation" 'facemenu-indentation-menu)) | |
224 | (define-key map [ju] (cons "Justification" 'facemenu-justification-menu)) | |
225 | (define-key map [s2] (list "-----------------")) | |
29008daa | 226 | (define-key map [sp] (cons "Special Properties" 'facemenu-special-menu)) |
f34eaa2c KH |
227 | (define-key map [bg] (cons "Background Color" 'facemenu-background-menu)) |
228 | (define-key map [fg] (cons "Foreground Color" 'facemenu-foreground-menu)) | |
229 | (define-key map [fc] (cons "Face" 'facemenu-face-menu))) | |
230 | ;;;###autoload | |
88d690a9 | 231 | (defalias 'facemenu-menu facemenu-menu) |
bf7d4561 | 232 | |
88d690a9 RS |
233 | (defvar facemenu-keymap |
234 | (let ((map (make-sparse-keymap "Set face"))) | |
f34eaa2c | 235 | (define-key map "o" (cons "Other..." 'facemenu-set-face)) |
88d690a9 | 236 | map) |
9dc90430 | 237 | "Keymap for face-changing commands. |
bf7d4561 | 238 | `Facemenu-update' fills in the keymap according to the bindings |
535d2617 | 239 | requested in `facemenu-keybindings'.") |
88d690a9 | 240 | (defalias 'facemenu-keymap facemenu-keymap) |
bf7d4561 BG |
241 | |
242 | ;;; Internal Variables | |
243 | ||
244 | (defvar facemenu-color-alist nil | |
245 | ;; Don't initialize here; that doesn't work if preloaded. | |
246 | "Alist of colors, used for completion. | |
247 | If null, `facemenu-read-color' will set it.") | |
4a24b314 | 248 | |
4e8aa578 | 249 | (defun facemenu-update () |
bf7d4561 BG |
250 | "Add or update the \"Face\" menu in the menu bar. |
251 | You can call this to update things if you change any of the menu configuration | |
252 | variables." | |
4e8aa578 RS |
253 | (interactive) |
254 | ||
bf7d4561 | 255 | ;; Global bindings: |
88d690a9 RS |
256 | (define-key global-map [C-down-mouse-2] 'facemenu-menu) |
257 | (if facemenu-key (define-key global-map facemenu-key 'facemenu-keymap)) | |
4e8aa578 | 258 | |
bf7d4561 BG |
259 | ;; Add each defined face to the menu. |
260 | (facemenu-iterate 'facemenu-add-new-face | |
261 | (facemenu-complete-face-list facemenu-keybindings))) | |
4a24b314 | 262 | |
4e8aa578 RS |
263 | ;;;###autoload |
264 | (defun facemenu-set-face (face &optional start end) | |
4a24b314 RS |
265 | "Add FACE to the region or next character typed. |
266 | It will be added to the top of the face list; any faces lower on the list that | |
267 | will not show through at all will be removed. | |
268 | ||
f34eaa2c KH |
269 | Interactively, the face to be used is read with the minibuffer. |
270 | ||
271 | If the region is active and there is no prefix argument, | |
272 | this command sets the region to the requested face. | |
273 | ||
274 | Otherwise, this command specifies the face for the next character | |
275 | inserted. Moving point or switching buffers before | |
276 | typing a character to insert cancels the specification." | |
4e8aa578 | 277 | (interactive (list (read-face-name "Use face: "))) |
88d690a9 RS |
278 | (barf-if-buffer-read-only) |
279 | (facemenu-add-new-face face) | |
f34eaa2c | 280 | (if (and mark-active (not current-prefix-arg)) |
4a24b314 RS |
281 | (let ((start (or start (region-beginning))) |
282 | (end (or end (region-end)))) | |
283 | (facemenu-add-face face start end)) | |
7fce8c91 | 284 | (facemenu-self-insert-face face))) |
4a24b314 | 285 | |
bf7d4561 | 286 | ;;;###autoload |
4a24b314 RS |
287 | (defun facemenu-set-foreground (color &optional start end) |
288 | "Set the foreground color of the region or next character typed. | |
289 | The color is prompted for. A face named `fg:color' is used \(or created). | |
290 | If the region is active, it will be set to the requested face. If | |
291 | it is inactive \(even if mark-even-if-inactive is set) the next | |
292 | character that is typed \(via `self-insert-command') will be set to | |
f61deddc | 293 | the selected face. Moving point or switching buffers before |
4a24b314 RS |
294 | typing a character cancels the request." |
295 | (interactive (list (facemenu-read-color "Foreground color: "))) | |
296 | (let ((face (intern (concat "fg:" color)))) | |
297 | (or (facemenu-get-face face) | |
298 | (error "Unknown color: %s" color)) | |
299 | (facemenu-set-face face start end))) | |
300 | ||
bf7d4561 | 301 | ;;;###autoload |
4a24b314 RS |
302 | (defun facemenu-set-background (color &optional start end) |
303 | "Set the background color of the region or next character typed. | |
304 | The color is prompted for. A face named `bg:color' is used \(or created). | |
305 | If the region is active, it will be set to the requested face. If | |
306 | it is inactive \(even if mark-even-if-inactive is set) the next | |
307 | character that is typed \(via `self-insert-command') will be set to | |
f61deddc | 308 | the selected face. Moving point or switching buffers before |
4a24b314 RS |
309 | typing a character cancels the request." |
310 | (interactive (list (facemenu-read-color "Background color: "))) | |
311 | (let ((face (intern (concat "bg:" color)))) | |
312 | (or (facemenu-get-face face) | |
313 | (error "Unknown color: %s" color)) | |
314 | (facemenu-set-face face start end))) | |
4e8aa578 | 315 | |
9dc90430 | 316 | ;;;###autoload |
4e8aa578 RS |
317 | (defun facemenu-set-face-from-menu (face start end) |
318 | "Set the face of the region or next character typed. | |
319 | This function is designed to be called from a menu; the face to use | |
320 | is the menu item's name. | |
f34eaa2c KH |
321 | |
322 | If the region is active and there is no prefix argument, | |
323 | this command sets the region to the requested face. | |
324 | ||
325 | Otherwise, this command specifies the face for the next character | |
326 | inserted. Moving point or switching buffers before | |
327 | typing a character to insert cancels the specification." | |
4a24b314 | 328 | (interactive (list last-command-event |
f34eaa2c KH |
329 | (if (and mark-active (not current-prefix-arg)) |
330 | (region-beginning)) | |
331 | (if (and mark-active (not current-prefix-arg)) | |
332 | (region-end)))) | |
88d690a9 | 333 | (barf-if-buffer-read-only) |
4a24b314 | 334 | (facemenu-get-face face) |
4e8aa578 | 335 | (if start |
4a24b314 | 336 | (facemenu-add-face face start end) |
7fce8c91 RS |
337 | (facemenu-self-insert-face face))) |
338 | ||
339 | (defun facemenu-self-insert-face (face) | |
41e5bf66 BG |
340 | (setq self-insert-face (if (eq last-command self-insert-face-command) |
341 | (cons face (if (listp self-insert-face) | |
342 | self-insert-face | |
343 | (list self-insert-face))) | |
344 | face) | |
7fce8c91 | 345 | self-insert-face-command this-command)) |
4e8aa578 | 346 | |
9dc90430 | 347 | ;;;###autoload |
4e8aa578 RS |
348 | (defun facemenu-set-invisible (start end) |
349 | "Make the region invisible. | |
350 | This sets the `invisible' text property; it can be undone with | |
f34eaa2c | 351 | `facemenu-remove-special'." |
4e8aa578 RS |
352 | (interactive "r") |
353 | (put-text-property start end 'invisible t)) | |
354 | ||
9dc90430 | 355 | ;;;###autoload |
4e8aa578 RS |
356 | (defun facemenu-set-intangible (start end) |
357 | "Make the region intangible: disallow moving into it. | |
358 | This sets the `intangible' text property; it can be undone with | |
f34eaa2c | 359 | `facemenu-remove-special'." |
4e8aa578 RS |
360 | (interactive "r") |
361 | (put-text-property start end 'intangible t)) | |
362 | ||
9dc90430 | 363 | ;;;###autoload |
4e8aa578 RS |
364 | (defun facemenu-set-read-only (start end) |
365 | "Make the region unmodifiable. | |
366 | This sets the `read-only' text property; it can be undone with | |
f34eaa2c | 367 | `facemenu-remove-special'." |
4e8aa578 RS |
368 | (interactive "r") |
369 | (put-text-property start end 'read-only t)) | |
370 | ||
9dc90430 | 371 | ;;;###autoload |
f34eaa2c | 372 | (defun facemenu-remove-props (start end) |
4e8aa578 RS |
373 | "Remove all text properties that facemenu added to region." |
374 | (interactive "*r") ; error if buffer is read-only despite the next line. | |
375 | (let ((inhibit-read-only t)) | |
376 | (remove-text-properties | |
377 | start end '(face nil invisible nil intangible nil | |
378 | read-only nil category nil)))) | |
379 | ||
f34eaa2c KH |
380 | ;;;###autoload |
381 | (defun facemenu-remove-all (start end) | |
382 | "Remove all text properties from the region." | |
383 | (interactive "*r") ; error if buffer is read-only despite the next line. | |
384 | (let ((inhibit-read-only t)) | |
385 | (set-text-properties start end nil))) | |
386 | ||
387 | ;;;###autoload | |
388 | (defun facemenu-remove-special (start end) | |
389 | "Remove all the \"special\" text properties from the region. | |
390 | These special properties include `invisible', `intangible' and `read-only'." | |
391 | (interactive "*r") ; error if buffer is read-only despite the next line. | |
392 | (let ((inhibit-read-only t)) | |
393 | (remove-text-properties | |
394 | start end '(invisible nil intangible nil read-only nil)))) | |
395 | ||
c0a7db84 BG |
396 | ;;;###autoload |
397 | (defun list-text-properties-at (p) | |
398 | "Pop up a buffer listing text-properties at LOCATION." | |
399 | (interactive "d") | |
400 | (let ((props (text-properties-at p))) | |
401 | (if (null props) | |
402 | (message "None") | |
403 | (with-output-to-temp-buffer "*Text Properties*" | |
404 | (princ (format "Text properties at %d:\n\n" p)) | |
405 | (while props | |
406 | (princ (format "%-20s %S\n" | |
407 | (car props) (car (cdr props)))) | |
408 | (setq props (cdr (cdr props)))))))) | |
409 | ||
bf7d4561 | 410 | ;;;###autoload |
da627a71 | 411 | (defun facemenu-read-color (&optional prompt) |
bf7d4561 | 412 | "Read a color using the minibuffer." |
da627a71 | 413 | (let ((col (completing-read (or prompt "Color: ") |
bf7d4561 BG |
414 | (or facemenu-color-alist |
415 | (if (eq 'x window-system) | |
416 | (mapcar 'list (x-defined-colors)))) | |
417 | nil t))) | |
418 | (if (equal "" col) | |
419 | nil | |
420 | col))) | |
4e8aa578 | 421 | |
88d690a9 RS |
422 | ;;;###autoload |
423 | (defun list-colors-display (&optional list) | |
7c49006b RS |
424 | "Display names of defined colors, and show what they look like. |
425 | If the optional argument LIST is non-nil, it should be a list of | |
426 | colors to display. Otherwise, this command computes a list | |
427 | of colors that the current display can handle." | |
88d690a9 RS |
428 | (interactive) |
429 | (if (and (null list) (eq 'x window-system)) | |
7c49006b RS |
430 | (progn |
431 | (setq list (x-defined-colors)) | |
432 | ;; Delete duplicate colors. | |
433 | (let ((l list)) | |
434 | (while (cdr l) | |
435 | (if (facemenu-color-equal (car l) (car (cdr l))) | |
436 | (setcdr l (cdr (cdr l))) | |
437 | (setq l (cdr l))))))) | |
88d690a9 RS |
438 | (with-output-to-temp-buffer "*Colors*" |
439 | (save-excursion | |
440 | (set-buffer standard-output) | |
441 | (let ((facemenu-unlisted-faces t) | |
442 | s) | |
443 | (while list | |
444 | (setq s (point)) | |
445 | (insert (car list)) | |
446 | (indent-to 20) | |
447 | (put-text-property s (point) 'face | |
448 | (facemenu-get-face | |
449 | (intern (concat "bg:" (car list))))) | |
450 | (setq s (point)) | |
451 | (insert " " (car list) "\n") | |
452 | (put-text-property s (point) 'face | |
453 | (facemenu-get-face | |
454 | (intern (concat "fg:" (car list))))) | |
455 | (setq list (cdr list))))))) | |
456 | ||
457 | (defun facemenu-color-equal (a b) | |
458 | "Return t if colors A and B are the same color. | |
7c49006b RS |
459 | A and B should be strings naming colors. |
460 | This function queries the window-system server to find out what the | |
461 | color names mean. It returns nil if the colors differ or if it can't | |
462 | determine the correct answer." | |
88d690a9 RS |
463 | (cond ((equal a b) t) |
464 | ((and (eq 'x window-system) | |
465 | (equal (x-color-values a) (x-color-values b)))))) | |
466 | ||
4a24b314 RS |
467 | (defun facemenu-add-face (face start end) |
468 | "Add FACE to text between START and END. | |
469 | For each section of that region that has a different face property, FACE will | |
470 | be consed onto it, and other faces that are completely hidden by that will be | |
bf7d4561 BG |
471 | removed from the list. |
472 | ||
473 | As a special case, if FACE is `default', then the region is left with NO face | |
474 | text property. Otherwise, selecting the default face would not have any | |
475 | effect." | |
4a24b314 | 476 | (interactive "*xFace:\nr") |
bf7d4561 BG |
477 | (if (eq face 'default) |
478 | (remove-text-properties start end '(face default)) | |
479 | (let ((part-start start) part-end) | |
480 | (while (not (= part-start end)) | |
481 | (setq part-end (next-single-property-change part-start 'face nil end)) | |
482 | (let ((prev (get-text-property part-start 'face))) | |
483 | (put-text-property part-start part-end 'face | |
484 | (if (null prev) | |
485 | face | |
486 | (facemenu-discard-redundant-faces | |
487 | (cons face | |
488 | (if (listp prev) prev (list prev))))))) | |
489 | (setq part-start part-end))))) | |
4a24b314 RS |
490 | |
491 | (defun facemenu-discard-redundant-faces (face-list &optional mask) | |
492 | "Remove from FACE-LIST any faces that won't show at all. | |
493 | This means they have no non-nil elements that aren't also non-nil in an | |
494 | earlier face." | |
495 | (let ((useful nil)) | |
496 | (cond ((null face-list) nil) | |
497 | ((null mask) | |
498 | (cons (car face-list) | |
499 | (facemenu-discard-redundant-faces | |
500 | (cdr face-list) | |
501 | (copy-sequence (internal-get-face (car face-list)))))) | |
502 | ((let ((i (length mask)) | |
503 | (face (internal-get-face (car face-list)))) | |
504 | (while (>= (setq i (1- i)) 0) | |
505 | (if (and (aref face i) | |
506 | (not (aref mask i))) | |
507 | (progn (setq useful t) | |
508 | (aset mask i t)))) | |
509 | useful) | |
510 | (cons (car face-list) | |
511 | (facemenu-discard-redundant-faces (cdr face-list) mask))) | |
512 | (t (facemenu-discard-redundant-faces (cdr face-list) mask))))) | |
513 | ||
bf7d4561 BG |
514 | (defun facemenu-get-face (symbol) |
515 | "Make sure FACE exists. | |
516 | If not, it is created. If it is created and is of the form `fg:color', then | |
517 | set the foreground to that color. If of the form `bg:color', set the | |
88d690a9 RS |
518 | background. In any case, add it to the appropriate menu. Returns the face, |
519 | or nil if given a bad color." | |
520 | (if (or (internal-find-face symbol) | |
521 | (let* ((face (make-face symbol)) | |
522 | (name (symbol-name symbol)) | |
523 | (color (substring name 3))) | |
524 | (cond ((string-match "^fg:" name) | |
525 | (set-face-foreground face color) | |
526 | (and (eq 'x window-system) (x-color-defined-p color))) | |
527 | ((string-match "^bg:" name) | |
528 | (set-face-background face color) | |
529 | (and (eq 'x window-system) (x-color-defined-p color))) | |
530 | (t)))) | |
531 | symbol)) | |
bf7d4561 BG |
532 | |
533 | (defun facemenu-add-new-face (face) | |
534 | "Add a FACE to the appropriate Face menu. | |
535 | Automatically called when a new face is created." | |
536 | (let* ((name (symbol-name face)) | |
537 | (menu (cond ((string-match "^fg:" name) | |
538 | (setq name (substring name 3)) | |
88d690a9 | 539 | 'facemenu-foreground-menu) |
bf7d4561 BG |
540 | ((string-match "^bg:" name) |
541 | (setq name (substring name 3)) | |
88d690a9 RS |
542 | 'facemenu-background-menu) |
543 | (t 'facemenu-face-menu))) | |
544 | (key (cdr (assoc face facemenu-keybindings))) | |
545 | function menu-val) | |
546 | (cond ((eq t facemenu-unlisted-faces)) | |
547 | ((memq face facemenu-unlisted-faces)) | |
548 | (key ; has a keyboard equivalent. These go at the front. | |
549 | (setq function (intern (concat "facemenu-set-" name))) | |
550 | (fset function | |
551 | (` (lambda () (interactive) | |
552 | (facemenu-set-face (quote (, face)))))) | |
553 | (define-key 'facemenu-keymap key (cons name function)) | |
554 | (define-key menu key (cons name function))) | |
555 | ((facemenu-iterate ; check if equivalent face is already in the menu | |
556 | (lambda (m) (and (listp m) | |
557 | (symbolp (car m)) | |
558 | (face-equal (car m) face))) | |
559 | (cdr (symbol-function menu)))) | |
560 | (t ; No keyboard equivalent. Figure out where to put it: | |
561 | (setq key (vector face) | |
562 | function 'facemenu-set-face-from-menu | |
563 | menu-val (symbol-function menu)) | |
564 | (if (and facemenu-new-faces-at-end | |
565 | (> (length menu-val) 3)) | |
566 | (define-key-after menu-val key (cons name function) | |
567 | (car (nth (- (length menu-val) 3) menu-val))) | |
568 | (define-key menu key (cons name function)))))) | |
569 | nil) ; Return nil for facemenu-iterate | |
bf7d4561 | 570 | |
bf7d4561 BG |
571 | (defun facemenu-complete-face-list (&optional oldlist) |
572 | "Return list of all faces that are look different. | |
573 | Starts with given ALIST of faces, and adds elements only if they display | |
574 | differently from any face already on the list. | |
575 | The faces on ALIST will end up at the end of the returned list, in reverse | |
576 | order." | |
577 | (let ((list (nreverse (mapcar 'car oldlist)))) | |
578 | (facemenu-iterate | |
579 | (lambda (new-face) | |
580 | (if (not (memq new-face list)) | |
581 | (setq list (cons new-face list))) | |
582 | nil) | |
583 | (nreverse (face-list))) | |
584 | list)) | |
585 | ||
4e8aa578 RS |
586 | (defun facemenu-iterate (func iterate-list) |
587 | "Apply FUNC to each element of LIST until one returns non-nil. | |
588 | Returns the non-nil value it found, or nil if all were nil." | |
589 | (while (and iterate-list (not (funcall func (car iterate-list)))) | |
590 | (setq iterate-list (cdr iterate-list))) | |
591 | (car iterate-list)) | |
592 | ||
593 | (facemenu-update) | |
4e8aa578 RS |
594 | |
595 | ;;; facemenu.el ends here |