Commit | Line | Data |
---|---|---|
be010748 | 1 | ;;; facemenu.el --- create a face menu for interactively adding fonts to text |
b578f267 | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1994, 1995, 1996, 2001, 2002, 2003, 2004, |
409cc4a3 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
4e8aa578 | 5 | |
5762abec | 6 | ;; Author: Boris Goldowsky <boris@gnu.org> |
4e8aa578 RS |
7 | ;; Keywords: faces |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
b4aa6026 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
4e8aa578 RS |
14 | ;; any later version. |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
4e8aa578 RS |
25 | |
26 | ;;; Commentary: | |
b578f267 | 27 | |
bf7d4561 BG |
28 | ;; This file defines a menu of faces (bold, italic, etc) which allows you to |
29 | ;; set the face used for a region of the buffer. Some faces also have | |
af1eab21 | 30 | ;; keybindings, which are shown in the menu. |
88d690a9 RS |
31 | ;; |
32 | ;; The menu also contains submenus for indentation and justification-changing | |
33 | ;; commands. | |
4e8aa578 | 34 | |
4e8aa578 | 35 | ;;; Usage: |
bf7d4561 BG |
36 | ;; Selecting a face from the menu or typing the keyboard equivalent will |
37 | ;; change the region to use that face. If you use transient-mark-mode and the | |
38 | ;; region is not active, the face will be remembered and used for the next | |
39 | ;; insertion. It will be forgotten if you move point or make other | |
40 | ;; modifications before inserting or typing anything. | |
4e8aa578 | 41 | ;; |
71296446 | 42 | ;; Faces can be selected from the keyboard as well. |
6be7d8db RS |
43 | ;; The standard keybindings are M-o (or ESC o) + letter: |
44 | ;; M-o i = "set italic", M-o b = "set bold", etc. | |
4e8aa578 RS |
45 | |
46 | ;;; Customization: | |
47 | ;; An alternative set of keybindings that may be easier to type can be set up | |
88d690a9 RS |
48 | ;; using "Alt" or "Hyper" keys. This requires that you either have or create |
49 | ;; an Alt or Hyper key on your keyboard. On my keyboard, there is a key | |
50 | ;; labeled "Alt", but to make it act as an Alt key I have to put this command | |
51 | ;; into my .xinitrc: | |
52 | ;; xmodmap -e "add Mod3 = Alt_L" | |
53 | ;; Or, I can make it into a Hyper key with this: | |
4e8aa578 | 54 | ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L" |
88d690a9 RS |
55 | ;; Check with local X-perts for how to do it on your system. |
56 | ;; Then you can define your keybindings with code like this in your .emacs: | |
4e8aa578 RS |
57 | ;; (setq facemenu-keybindings |
58 | ;; '((default . [?\H-d]) | |
59 | ;; (bold . [?\H-b]) | |
60 | ;; (italic . [?\H-i]) | |
88d690a9 | 61 | ;; (bold-italic . [?\H-l]) |
4e8aa578 | 62 | ;; (underline . [?\H-u]))) |
9086c730 | 63 | ;; (facemenu-update) |
4e8aa578 | 64 | ;; (setq facemenu-keymap global-map) |
88d690a9 RS |
65 | ;; (define-key global-map [?\H-c] 'facemenu-set-foreground) ; set fg color |
66 | ;; (define-key global-map [?\H-C] 'facemenu-set-background) ; set bg color | |
4e8aa578 | 67 | ;; |
88d690a9 RS |
68 | ;; The order of the faces that appear in the menu and their keybindings can be |
69 | ;; controlled by setting the variables `facemenu-keybindings' and | |
b6a67507 CY |
70 | ;; `facemenu-new-faces-at-end'. List faces that you want to use in documents |
71 | ;; in `facemenu-listed-faces'. | |
4e8aa578 RS |
72 | |
73 | ;;; Known Problems: | |
88d690a9 RS |
74 | ;; Bold and Italic do not combine to create bold-italic if you select them |
75 | ;; both, although most other combinations (eg bold + underline + some color) | |
76 | ;; do the intuitive thing. | |
77 | ;; | |
4e8aa578 RS |
78 | ;; There is at present no way to display what the faces look like in |
79 | ;; the menu itself. | |
80 | ;; | |
81 | ;; `list-faces-display' shows the faces in a different order than | |
82 | ;; this menu, which could be confusing. I do /not/ sort the list | |
83 | ;; alphabetically, because I like the default order: it puts the most | |
84 | ;; basic, common fonts first. | |
85 | ;; | |
86 | ;; Please send me any other problems, comments or ideas. | |
87 | ||
88 | ;;; Code: | |
89 | ||
71296446 | 90 | (eval-when-compile |
0e520006 PA |
91 | (require 'help) |
92 | (require 'button)) | |
93 | ||
9086c730 RS |
94 | ;; Global bindings: |
95 | (define-key global-map [C-down-mouse-2] 'facemenu-menu) | |
6be7d8db | 96 | (define-key global-map "\M-o" 'facemenu-keymap) |
4e8aa578 | 97 | |
487e6fcb | 98 | (defgroup facemenu nil |
8e51619c | 99 | "Create a face menu for interactively adding fonts to text." |
487e6fcb RS |
100 | :group 'faces |
101 | :prefix "facemenu-") | |
102 | ||
103 | (defcustom facemenu-keybindings | |
4e8aa578 RS |
104 | '((default . "d") |
105 | (bold . "b") | |
106 | (italic . "i") | |
88d690a9 | 107 | (bold-italic . "l") ; {bold} intersect {italic} = {l} |
4e8aa578 | 108 | (underline . "u")) |
220c969f | 109 | "Alist of interesting faces and keybindings. |
4e8aa578 RS |
110 | Each element is itself a list: the car is the name of the face, |
111 | the next element is the key to use as a keyboard equivalent of the menu item; | |
9086c730 | 112 | the binding is made in `facemenu-keymap'. |
4e8aa578 RS |
113 | |
114 | The faces specifically mentioned in this list are put at the top of | |
b6a67507 CY |
115 | the menu, in the order specified. All other faces which are defined |
116 | in `facemenu-listed-faces' are listed after them, but get no | |
117 | keyboard equivalents. | |
4e8aa578 RS |
118 | |
119 | If you change this variable after loading facemenu.el, you will need to call | |
487e6fcb RS |
120 | `facemenu-update' to make it take effect." |
121 | :type '(repeat (cons face string)) | |
122 | :group 'facemenu) | |
4e8aa578 | 123 | |
487e6fcb | 124 | (defcustom facemenu-new-faces-at-end t |
9086c730 | 125 | "*Where in the menu to insert newly-created faces. |
88d690a9 | 126 | This should be nil to put them at the top of the menu, or t to put them |
487e6fcb RS |
127 | just before \"Other\" at the end." |
128 | :type 'boolean | |
129 | :group 'facemenu) | |
88d690a9 | 130 | |
ed50f0d2 CY |
131 | (defvar facemenu-unlisted-faces |
132 | `(modeline region secondary-selection highlight scratch-face | |
133 | ,(purecopy "^font-lock-") ,(purecopy "^gnus-") ,(purecopy "^message-") | |
134 | ,(purecopy "^ediff-") ,(purecopy "^term-") ,(purecopy "^vc-") | |
135 | ,(purecopy "^widget-") ,(purecopy "^custom-") ,(purecopy "^vm-")) | |
d9f2959e JB |
136 | "*List of faces that are of no interest to the user.") |
137 | (make-obsolete-variable 'facemenu-unlisted-faces 'facemenu-listed-faces | |
b8a2c14a | 138 | "22.1,\n and has no effect on the Face menu") |
ed50f0d2 | 139 | |
b6a67507 CY |
140 | (defcustom facemenu-listed-faces nil |
141 | "*List of faces to include in the Face menu. | |
d7beaf53 | 142 | Each element should be a symbol, the name of a face. |
b6a67507 | 143 | The \"basic \" faces in `facemenu-keybindings' are automatically |
d7beaf53 RS |
144 | added to the Face menu, and need not be in this list. |
145 | ||
146 | This value takes effect when you load facemenu.el. If the | |
147 | list includes symbols which are not defined as faces, they | |
148 | are ignored; however, subsequently defining or creating | |
149 | those faces adds them to the menu then. You can call | |
150 | `facemenu-update' to recalculate the menu contents, such as | |
151 | if you change the value of this variable, | |
152 | ||
153 | If this variable is t, all faces that you apply to text | |
154 | using the face menu commands (even by name), and all faces | |
155 | that you define or create, are added to the menu. You may | |
156 | find it useful to set this variable to t temporarily while | |
157 | you define some faces, so that they will be added. However, | |
158 | if the value is no longer t and you call `facemenu-update', | |
159 | it will remove any faces not explicitly in the list." | |
b6a67507 CY |
160 | :type '(choice (const :tag "List all faces" t) |
161 | (const :tag "None" nil) | |
162 | (repeat symbol)) | |
163 | :group 'facemenu | |
164 | :version "22.1") | |
88d690a9 RS |
165 | |
166 | (defvar facemenu-face-menu | |
bf7d4561 | 167 | (let ((map (make-sparse-keymap "Face"))) |
88d690a9 | 168 | (define-key map "o" (cons "Other..." 'facemenu-set-face)) |
bf7d4561 BG |
169 | map) |
170 | "Menu keymap for faces.") | |
88d690a9 | 171 | (defalias 'facemenu-face-menu facemenu-face-menu) |
6c763f36 | 172 | (put 'facemenu-face-menu 'menu-enable '(facemenu-enable-faces-p)) |
bf7d4561 | 173 | |
71296446 | 174 | (defvar facemenu-foreground-menu |
bf7d4561 | 175 | (let ((map (make-sparse-keymap "Foreground Color"))) |
f34eaa2c | 176 | (define-key map "o" (cons "Other..." 'facemenu-set-foreground)) |
bf7d4561 BG |
177 | map) |
178 | "Menu keymap for foreground colors.") | |
88d690a9 | 179 | (defalias 'facemenu-foreground-menu facemenu-foreground-menu) |
6c763f36 | 180 | (put 'facemenu-foreground-menu 'menu-enable '(facemenu-enable-faces-p)) |
bf7d4561 BG |
181 | |
182 | (defvar facemenu-background-menu | |
183 | (let ((map (make-sparse-keymap "Background Color"))) | |
f34eaa2c | 184 | (define-key map "o" (cons "Other..." 'facemenu-set-background)) |
bf7d4561 | 185 | map) |
7e6cb513 | 186 | "Menu keymap for background colors.") |
88d690a9 | 187 | (defalias 'facemenu-background-menu facemenu-background-menu) |
6c763f36 RS |
188 | (put 'facemenu-background-menu 'menu-enable '(facemenu-enable-faces-p)) |
189 | ||
190 | ;;; Condition for enabling menu items that set faces. | |
191 | (defun facemenu-enable-faces-p () | |
192 | (not (and font-lock-mode font-lock-defaults))) | |
bf7d4561 | 193 | |
71296446 | 194 | (defvar facemenu-special-menu |
bf7d4561 | 195 | (let ((map (make-sparse-keymap "Special"))) |
2d07ff84 DL |
196 | (define-key map [?s] (cons (purecopy "Remove Special") |
197 | 'facemenu-remove-special)) | |
198 | (define-key map [?t] (cons (purecopy "Intangible") | |
199 | 'facemenu-set-intangible)) | |
200 | (define-key map [?v] (cons (purecopy "Invisible") | |
201 | 'facemenu-set-invisible)) | |
202 | (define-key map [?r] (cons (purecopy "Read-Only") | |
203 | 'facemenu-set-read-only)) | |
bf7d4561 BG |
204 | map) |
205 | "Menu keymap for non-face text-properties.") | |
88d690a9 RS |
206 | (defalias 'facemenu-special-menu facemenu-special-menu) |
207 | ||
208 | (defvar facemenu-justification-menu | |
209 | (let ((map (make-sparse-keymap "Justification"))) | |
2d07ff84 DL |
210 | (define-key map [?c] (cons (purecopy "Center") 'set-justification-center)) |
211 | (define-key map [?b] (cons (purecopy "Full") 'set-justification-full)) | |
212 | (define-key map [?r] (cons (purecopy "Right") 'set-justification-right)) | |
213 | (define-key map [?l] (cons (purecopy "Left") 'set-justification-left)) | |
214 | (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none)) | |
88d690a9 RS |
215 | map) |
216 | "Submenu for text justification commands.") | |
217 | (defalias 'facemenu-justification-menu facemenu-justification-menu) | |
218 | ||
219 | (defvar facemenu-indentation-menu | |
220 | (let ((map (make-sparse-keymap "Indentation"))) | |
71296446 | 221 | (define-key map [decrease-right-margin] |
2d07ff84 | 222 | (cons (purecopy "Indent Right Less") 'decrease-right-margin)) |
f34eaa2c | 223 | (define-key map [increase-right-margin] |
2d07ff84 | 224 | (cons (purecopy "Indent Right More") 'increase-right-margin)) |
f34eaa2c | 225 | (define-key map [decrease-left-margin] |
2d07ff84 | 226 | (cons (purecopy "Indent Less") 'decrease-left-margin)) |
f34eaa2c | 227 | (define-key map [increase-left-margin] |
2d07ff84 | 228 | (cons (purecopy "Indent More") 'increase-left-margin)) |
88d690a9 RS |
229 | map) |
230 | "Submenu for indentation commands.") | |
231 | (defalias 'facemenu-indentation-menu facemenu-indentation-menu) | |
bf7d4561 | 232 | |
f34eaa2c | 233 | ;; This is split up to avoid an overlong line in loaddefs.el. |
f34eaa2c | 234 | (defvar facemenu-menu nil |
535d2617 | 235 | "Facemenu top-level menu keymap.") |
f34eaa2c | 236 | (setq facemenu-menu (make-sparse-keymap "Text Properties")) |
f34eaa2c | 237 | (let ((map facemenu-menu)) |
2d07ff84 DL |
238 | (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display)) |
239 | (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display)) | |
cea5ec30 RS |
240 | (define-key map [dp] (cons (purecopy "Describe Properties") |
241 | 'describe-text-properties)) | |
2d07ff84 DL |
242 | (define-key map [ra] (cons (purecopy "Remove Text Properties") |
243 | 'facemenu-remove-all)) | |
244 | (define-key map [rm] (cons (purecopy "Remove Face Properties") | |
245 | 'facemenu-remove-face-props)) | |
246 | (define-key map [s1] (list (purecopy "--")))) | |
f34eaa2c | 247 | (let ((map facemenu-menu)) |
71296446 | 248 | (define-key map [in] (cons (purecopy "Indentation") |
2d07ff84 DL |
249 | 'facemenu-indentation-menu)) |
250 | (define-key map [ju] (cons (purecopy "Justification") | |
251 | 'facemenu-justification-menu)) | |
252 | (define-key map [s2] (list (purecopy "--"))) | |
71296446 | 253 | (define-key map [sp] (cons (purecopy "Special Properties") |
2d07ff84 | 254 | 'facemenu-special-menu)) |
71296446 | 255 | (define-key map [bg] (cons (purecopy "Background Color") |
2d07ff84 | 256 | 'facemenu-background-menu)) |
71296446 | 257 | (define-key map [fg] (cons (purecopy "Foreground Color") |
2d07ff84 | 258 | 'facemenu-foreground-menu)) |
71296446 | 259 | (define-key map [fc] (cons (purecopy "Face") |
2d07ff84 | 260 | 'facemenu-face-menu))) |
88d690a9 | 261 | (defalias 'facemenu-menu facemenu-menu) |
bf7d4561 | 262 | |
71296446 | 263 | (defvar facemenu-keymap |
88d690a9 | 264 | (let ((map (make-sparse-keymap "Set face"))) |
2d07ff84 | 265 | (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face)) |
ef49d20f | 266 | (define-key map "\M-o" 'font-lock-fontify-block) |
88d690a9 | 267 | map) |
9dc90430 | 268 | "Keymap for face-changing commands. |
bf7d4561 | 269 | `Facemenu-update' fills in the keymap according to the bindings |
535d2617 | 270 | requested in `facemenu-keybindings'.") |
88d690a9 | 271 | (defalias 'facemenu-keymap facemenu-keymap) |
bf7d4561 | 272 | |
cb5bec6e | 273 | |
487e6fcb | 274 | (defcustom facemenu-add-face-function nil |
7e6cb513 | 275 | "Function called at beginning of text to change or nil. |
cb5bec6e | 276 | This function is passed the FACE to set and END of text to change, and must |
487e6fcb RS |
277 | return a string which is inserted. It may set `facemenu-end-add-face'." |
278 | :type '(choice (const :tag "None" nil) | |
279 | function) | |
280 | :group 'facemenu) | |
cb5bec6e | 281 | |
487e6fcb | 282 | (defcustom facemenu-end-add-face nil |
7e6cb513 | 283 | "String to insert or function called at end of text to change or nil. |
cb5bec6e | 284 | This function is passed the FACE to set, and must return a string which is |
487e6fcb RS |
285 | inserted." |
286 | :type '(choice (const :tag "None" nil) | |
287 | string | |
288 | function) | |
289 | :group 'facemenu) | |
cb5bec6e | 290 | |
487e6fcb | 291 | (defcustom facemenu-remove-face-function nil |
9086c730 | 292 | "When non-nil, this is a function called to remove faces. |
cb5bec6e | 293 | This function is passed the START and END of text to change. |
7e6cb513 | 294 | May also be t meaning to use `facemenu-add-face-function'." |
487e6fcb RS |
295 | :type '(choice (const :tag "None" nil) |
296 | (const :tag "Use add-face" t) | |
297 | function) | |
298 | :group 'facemenu) | |
cb5bec6e | 299 | |
bf7d4561 BG |
300 | ;;; Internal Variables |
301 | ||
302 | (defvar facemenu-color-alist nil | |
bf7d4561 | 303 | "Alist of colors, used for completion. |
a926c9ce | 304 | If this is nil, then the value of (defined-colors) is used.") |
4a24b314 | 305 | |
4e8aa578 | 306 | (defun facemenu-update () |
bf7d4561 BG |
307 | "Add or update the \"Face\" menu in the menu bar. |
308 | You can call this to update things if you change any of the menu configuration | |
309 | variables." | |
4e8aa578 | 310 | (interactive) |
4e8aa578 | 311 | |
bf7d4561 BG |
312 | ;; Add each defined face to the menu. |
313 | (facemenu-iterate 'facemenu-add-new-face | |
314 | (facemenu-complete-face-list facemenu-keybindings))) | |
4a24b314 | 315 | |
4e8aa578 | 316 | (defun facemenu-set-face (face &optional start end) |
cd7890bd RS |
317 | "Apply FACE to the region or next character typed. |
318 | ||
319 | If the region is active (normally true except in Transient | |
320 | Mark mode) and nonempty, and there is no prefix argument, | |
321 | this command applies FACE to the region. Otherwise, it applies FACE | |
322 | to the faces to use for the next character | |
323 | inserted. (Moving point or switching buffers before typing | |
324 | a character to insert cancels the specification.) | |
325 | ||
326 | If FACE is `default', to \"apply\" it means clearing | |
327 | the list of faces to be used. For any other value of FACE, | |
328 | to \"apply\" it means putting FACE at the front of the list | |
329 | of faces to be used, and removing any faces further | |
330 | along in the list that would be completely overridden by | |
331 | preceding faces (including FACE). | |
332 | ||
333 | This command can also add FACE to the menu of faces, | |
334 | if `facemenu-listed-faces' says to do that." | |
7d8177cf RS |
335 | (interactive (list (progn |
336 | (barf-if-buffer-read-only) | |
337 | (read-face-name "Use face")) | |
338 | (if (and mark-active (not current-prefix-arg)) | |
339 | (region-beginning)) | |
340 | (if (and mark-active (not current-prefix-arg)) | |
341 | (region-end)))) | |
88d690a9 | 342 | (facemenu-add-new-face face) |
7d8177cf | 343 | (facemenu-add-face face start end)) |
4a24b314 RS |
344 | |
345 | (defun facemenu-set-foreground (color &optional start end) | |
7e6cb513 | 346 | "Set the foreground COLOR of the region or next character typed. |
af1eab21 | 347 | This command reads the color in the minibuffer. |
7d8177cf RS |
348 | |
349 | If the region is active (normally true except in Transient Mark mode) | |
350 | and there is no prefix argument, this command sets the region to the | |
351 | requested face. | |
352 | ||
353 | Otherwise, this command specifies the face for the next character | |
354 | inserted. Moving point or switching buffers before | |
71296446 | 355 | typing a character to insert cancels the specification." |
7d8177cf RS |
356 | (interactive (list (progn |
357 | (barf-if-buffer-read-only) | |
358 | (facemenu-read-color "Foreground color: ")) | |
359 | (if (and mark-active (not current-prefix-arg)) | |
360 | (region-beginning)) | |
361 | (if (and mark-active (not current-prefix-arg)) | |
362 | (region-end)))) | |
b97c98ad LK |
363 | (facemenu-set-face-from-menu |
364 | (facemenu-add-new-color color 'facemenu-foreground-menu) | |
365 | start end)) | |
4a24b314 RS |
366 | |
367 | (defun facemenu-set-background (color &optional start end) | |
7e6cb513 | 368 | "Set the background COLOR of the region or next character typed. |
af1eab21 | 369 | This command reads the color in the minibuffer. |
7d8177cf RS |
370 | |
371 | If the region is active (normally true except in Transient Mark mode) | |
372 | and there is no prefix argument, this command sets the region to the | |
373 | requested face. | |
374 | ||
375 | Otherwise, this command specifies the face for the next character | |
376 | inserted. Moving point or switching buffers before | |
71296446 | 377 | typing a character to insert cancels the specification." |
7d8177cf RS |
378 | (interactive (list (progn |
379 | (barf-if-buffer-read-only) | |
380 | (facemenu-read-color "Background color: ")) | |
381 | (if (and mark-active (not current-prefix-arg)) | |
382 | (region-beginning)) | |
383 | (if (and mark-active (not current-prefix-arg)) | |
384 | (region-end)))) | |
b97c98ad LK |
385 | (facemenu-set-face-from-menu |
386 | (facemenu-add-new-color color 'facemenu-background-menu) | |
387 | start end)) | |
4e8aa578 RS |
388 | |
389 | (defun facemenu-set-face-from-menu (face start end) | |
7e6cb513 | 390 | "Set the FACE of the region or next character typed. |
b97c98ad LK |
391 | This function is designed to be called from a menu; FACE is determined |
392 | using the event type of the menu entry. If FACE is a symbol whose | |
393 | name starts with \"fg:\" or \"bg:\", then this functions sets the | |
394 | foreground or background to the color specified by the rest of the | |
395 | symbol's name. Any other symbol is considered the name of a face. | |
f34eaa2c | 396 | |
7d8177cf RS |
397 | If the region is active (normally true except in Transient Mark mode) |
398 | and there is no prefix argument, this command sets the region to the | |
399 | requested face. | |
f34eaa2c KH |
400 | |
401 | Otherwise, this command specifies the face for the next character | |
b97c98ad LK |
402 | inserted. Moving point or switching buffers before typing a character |
403 | to insert cancels the specification." | |
4a24b314 | 404 | (interactive (list last-command-event |
f34eaa2c KH |
405 | (if (and mark-active (not current-prefix-arg)) |
406 | (region-beginning)) | |
407 | (if (and mark-active (not current-prefix-arg)) | |
408 | (region-end)))) | |
88d690a9 | 409 | (barf-if-buffer-read-only) |
b97c98ad LK |
410 | (facemenu-add-face |
411 | (let ((fn (symbol-name face))) | |
412 | (if (string-match "\\`\\([fb]\\)g:\\(.+\\)" fn) | |
413 | (list (list (if (string= (match-string 1 fn) "f") | |
414 | :foreground | |
415 | :background) | |
416 | (match-string 2 fn))) | |
417 | face)) | |
418 | start end)) | |
4e8aa578 RS |
419 | |
420 | (defun facemenu-set-invisible (start end) | |
421 | "Make the region invisible. | |
422 | This sets the `invisible' text property; it can be undone with | |
f34eaa2c | 423 | `facemenu-remove-special'." |
4e8aa578 | 424 | (interactive "r") |
0e3edd7b | 425 | (add-text-properties start end '(invisible t))) |
4e8aa578 RS |
426 | |
427 | (defun facemenu-set-intangible (start end) | |
428 | "Make the region intangible: disallow moving into it. | |
429 | This sets the `intangible' text property; it can be undone with | |
f34eaa2c | 430 | `facemenu-remove-special'." |
4e8aa578 | 431 | (interactive "r") |
0e3edd7b | 432 | (add-text-properties start end '(intangible t))) |
4e8aa578 RS |
433 | |
434 | (defun facemenu-set-read-only (start end) | |
435 | "Make the region unmodifiable. | |
436 | This sets the `read-only' text property; it can be undone with | |
f34eaa2c | 437 | `facemenu-remove-special'." |
4e8aa578 | 438 | (interactive "r") |
0e3edd7b | 439 | (add-text-properties start end '(read-only t))) |
4e8aa578 | 440 | |
a32d7856 KH |
441 | (defun facemenu-remove-face-props (start end) |
442 | "Remove `face' and `mouse-face' text properties." | |
4e8aa578 RS |
443 | (interactive "*r") ; error if buffer is read-only despite the next line. |
444 | (let ((inhibit-read-only t)) | |
71296446 | 445 | (remove-text-properties |
a32d7856 | 446 | start end '(face nil mouse-face nil)))) |
4e8aa578 | 447 | |
f34eaa2c KH |
448 | (defun facemenu-remove-all (start end) |
449 | "Remove all text properties from the region." | |
450 | (interactive "*r") ; error if buffer is read-only despite the next line. | |
451 | (let ((inhibit-read-only t)) | |
452 | (set-text-properties start end nil))) | |
453 | ||
f34eaa2c KH |
454 | (defun facemenu-remove-special (start end) |
455 | "Remove all the \"special\" text properties from the region. | |
456 | These special properties include `invisible', `intangible' and `read-only'." | |
457 | (interactive "*r") ; error if buffer is read-only despite the next line. | |
458 | (let ((inhibit-read-only t)) | |
71296446 | 459 | (remove-text-properties |
f34eaa2c | 460 | start end '(invisible nil intangible nil read-only nil)))) |
0af1db42 | 461 | \f |
da627a71 | 462 | (defun facemenu-read-color (&optional prompt) |
bf7d4561 | 463 | "Read a color using the minibuffer." |
5bcc074b RS |
464 | (let* ((completion-ignore-case t) |
465 | (col (completing-read (or prompt "Color: ") | |
466 | (or facemenu-color-alist | |
467 | (defined-colors)) | |
468 | nil t))) | |
bf7d4561 BG |
469 | (if (equal "" col) |
470 | nil | |
471 | col))) | |
4e8aa578 | 472 | |
066a23af | 473 | (defun list-colors-display (&optional list buffer-name) |
7c49006b RS |
474 | "Display names of defined colors, and show what they look like. |
475 | If the optional argument LIST is non-nil, it should be a list of | |
066a23af JL |
476 | colors to display. Otherwise, this command computes a list of |
477 | colors that the current display can handle. If the optional | |
478 | argument BUFFER-NAME is nil, it defaults to *Colors*." | |
88d690a9 | 479 | (interactive) |
6062889d | 480 | (when (and (null list) (> (display-color-cells) 0)) |
066a23af | 481 | (setq list (list-colors-duplicates (defined-colors))) |
d2596700 MB |
482 | (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color)) |
483 | ;; Don't show more than what the display can handle. | |
484 | (let ((lc (nthcdr (1- (display-color-cells)) list))) | |
485 | (if lc | |
486 | (setcdr lc nil))))) | |
4644fd35 | 487 | (with-help-window (or buffer-name "*Colors*") |
88d690a9 RS |
488 | (save-excursion |
489 | (set-buffer standard-output) | |
066a23af | 490 | (setq truncate-lines t) |
987d1819 JL |
491 | (if temp-buffer-show-function |
492 | (list-colors-print list) | |
493 | ;; Call list-colors-print from temp-buffer-show-hook | |
494 | ;; to get the right value of window-width in list-colors-print | |
495 | ;; after the buffer is displayed. | |
496 | (add-hook 'temp-buffer-show-hook | |
497 | (lambda () (list-colors-print list)) nil t))))) | |
498 | ||
499 | (defun list-colors-print (list) | |
500 | (dolist (color list) | |
501 | (if (consp color) | |
502 | (if (cdr color) | |
503 | (setq color (sort color (lambda (a b) | |
504 | (string< (downcase a) | |
505 | (downcase b)))))) | |
506 | (setq color (list color))) | |
507 | (put-text-property | |
508 | (prog1 (point) | |
509 | (insert (car color)) | |
510 | (indent-to 22)) | |
511 | (point) | |
78056104 | 512 | 'face (list ':background (car color))) |
987d1819 JL |
513 | (put-text-property |
514 | (prog1 (point) | |
51d23bd0 JL |
515 | (insert " " (if (cdr color) |
516 | (mapconcat 'identity (cdr color) ", ") | |
517 | (car color)))) | |
987d1819 | 518 | (point) |
78056104 | 519 | 'face (list ':foreground (car color))) |
51d23bd0 JL |
520 | (indent-to (max (- (window-width) 8) 44)) |
521 | (insert (apply 'format "#%02x%02x%02x" | |
522 | (mapcar (lambda (c) (lsh c -8)) | |
523 | (color-values (car color))))) | |
524 | ||
525 | (insert "\n")) | |
987d1819 | 526 | (goto-char (point-min))) |
066a23af JL |
527 | |
528 | (defun list-colors-duplicates (&optional list) | |
529 | "Return a list of colors with grouped duplicate colors. | |
530 | If a color has no duplicates, then the element of the returned list | |
531 | has the form '(COLOR-NAME). The element of the returned list with | |
532 | duplicate colors has the form '(COLOR-NAME DUPLICATE-COLOR-NAME ...). | |
533 | This function uses the predicate `facemenu-color-equal' to compare | |
534 | color names. If the optional argument LIST is non-nil, it should | |
535 | be a list of colors to display. Otherwise, this function uses | |
536 | a list of colors that the current display can handle." | |
537 | (let* ((list (mapcar 'list (or list (defined-colors)))) | |
538 | (l list)) | |
539 | (while (cdr l) | |
540 | (if (and (facemenu-color-equal (car (car l)) (car (car (cdr l)))) | |
51d23bd0 JL |
541 | (not (if (boundp 'w32-default-color-map) |
542 | (not (assoc (car (car l)) w32-default-color-map))))) | |
066a23af JL |
543 | (progn |
544 | (setcdr (car l) (cons (car (car (cdr l))) (cdr (car l)))) | |
545 | (setcdr l (cdr (cdr l)))) | |
546 | (setq l (cdr l)))) | |
547 | list)) | |
88d690a9 RS |
548 | |
549 | (defun facemenu-color-equal (a b) | |
550 | "Return t if colors A and B are the same color. | |
7c49006b | 551 | A and B should be strings naming colors. |
f795f633 EZ |
552 | This function queries the display system to find out what the color |
553 | names mean. It returns nil if the colors differ or if it can't | |
7c49006b | 554 | determine the correct answer." |
88d690a9 | 555 | (cond ((equal a b) t) |
f795f633 | 556 | ((equal (color-values a) (color-values b))))) |
88d690a9 | 557 | |
cb5bec6e | 558 | (defun facemenu-add-face (face &optional start end) |
4a24b314 | 559 | "Add FACE to text between START and END. |
7e6cb513 | 560 | If START is nil or START to END is empty, add FACE to next typed character |
cb5bec6e RS |
561 | instead. For each section of that region that has a different face property, |
562 | FACE will be consed onto it, and other faces that are completely hidden by | |
563 | that will be removed from the list. | |
af1eab21 | 564 | If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil, |
cb5bec6e | 565 | they are used to set the face information. |
bf7d4561 BG |
566 | |
567 | As a special case, if FACE is `default', then the region is left with NO face | |
568 | text property. Otherwise, selecting the default face would not have any | |
cb5bec6e RS |
569 | effect. See `facemenu-remove-face-function'." |
570 | (interactive "*xFace: \nr") | |
571 | (if (and (eq face 'default) | |
572 | (not (eq facemenu-remove-face-function t))) | |
573 | (if facemenu-remove-face-function | |
574 | (funcall facemenu-remove-face-function start end) | |
682e437e RS |
575 | (if (and start (< start end)) |
576 | (remove-text-properties start end '(face default)) | |
577 | (setq self-insert-face 'default | |
578 | self-insert-face-command this-command))) | |
cb5bec6e RS |
579 | (if facemenu-add-face-function |
580 | (save-excursion | |
581 | (if end (goto-char end)) | |
582 | (save-excursion | |
583 | (if start (goto-char start)) | |
584 | (insert-before-markers | |
585 | (funcall facemenu-add-face-function face end))) | |
586 | (if facemenu-end-add-face | |
587 | (insert (if (stringp facemenu-end-add-face) | |
588 | facemenu-end-add-face | |
589 | (funcall facemenu-end-add-face face))))) | |
590 | (if (and start (< start end)) | |
591 | (let ((part-start start) part-end) | |
592 | (while (not (= part-start end)) | |
593 | (setq part-end (next-single-property-change part-start 'face | |
594 | nil end)) | |
595 | (let ((prev (get-text-property part-start 'face))) | |
596 | (put-text-property part-start part-end 'face | |
597 | (if (null prev) | |
598 | face | |
599 | (facemenu-active-faces | |
600 | (cons face | |
601 | (if (listp prev) | |
602 | prev | |
cd7890bd RS |
603 | (list prev))) |
604 | ;; Specify the selected frame | |
605 | ;; because nil would mean to use | |
606 | ;; the new-frame default settings, | |
607 | ;; and those are usually nil. | |
608 | (selected-frame))))) | |
cb5bec6e RS |
609 | (setq part-start part-end))) |
610 | (setq self-insert-face (if (eq last-command self-insert-face-command) | |
611 | (cons face (if (listp self-insert-face) | |
612 | self-insert-face | |
613 | (list self-insert-face))) | |
614 | face) | |
97a7aa7b RS |
615 | self-insert-face-command this-command)))) |
616 | (unless (facemenu-enable-faces-p) | |
617 | (message "Font-lock mode will override any faces you set in this buffer"))) | |
4a24b314 | 618 | |
5a79ed26 KH |
619 | (defun facemenu-active-faces (face-list &optional frame) |
620 | "Return from FACE-LIST those faces that would be used for display. | |
621 | This means each face attribute is not specified in a face earlier in FACE-LIST | |
622 | and such a face is therefore active when used to display text. | |
623 | If the optional argument FRAME is given, use the faces in that frame; otherwise | |
624 | use the selected frame. If t, then the global, non-frame faces are used." | |
7d8177cf RS |
625 | (let* ((mask-atts (copy-sequence |
626 | (if (consp (car face-list)) | |
f790dddf | 627 | (face-attributes-as-vector (car face-list)) |
7d8177cf RS |
628 | (or (internal-lisp-face-p (car face-list) frame) |
629 | (check-face (car face-list)))))) | |
5a79ed26 KH |
630 | (active-list (list (car face-list))) |
631 | (face-list (cdr face-list)) | |
632 | (mask-len (length mask-atts))) | |
633 | (while face-list | |
7d8177cf RS |
634 | (if (let ((face-atts |
635 | (if (consp (car face-list)) | |
f790dddf | 636 | (face-attributes-as-vector (car face-list)) |
7d8177cf RS |
637 | (or (internal-lisp-face-p (car face-list) frame) |
638 | (check-face (car face-list))))) | |
639 | (i mask-len) | |
640 | (useful nil)) | |
240c0c90 | 641 | (while (>= (setq i (1- i)) 0) |
7d8177cf RS |
642 | (and (not (memq (aref face-atts i) '(nil unspecified))) |
643 | (memq (aref mask-atts i) '(nil unspecified)) | |
5a79ed26 KH |
644 | (aset mask-atts i (setq useful t)))) |
645 | useful) | |
646 | (setq active-list (cons (car face-list) active-list))) | |
647 | (setq face-list (cdr face-list))) | |
648 | (nreverse active-list))) | |
4a24b314 | 649 | |
9bf4c4e5 | 650 | (defun facemenu-add-new-face (face) |
cd7890bd RS |
651 | "Add FACE (a face) to the Face menu if `facemenu-listed-faces' says so. |
652 | This is called whenever you create a new face, and at other times." | |
7d8177cf RS |
653 | (let* (name |
654 | symbol | |
9bf4c4e5 RS |
655 | menu docstring |
656 | (key (cdr (assoc face facemenu-keybindings))) | |
88d690a9 | 657 | function menu-val) |
9bf4c4e5 RS |
658 | (if (symbolp face) |
659 | (setq name (symbol-name face) | |
660 | symbol face) | |
661 | (setq name face | |
1d792b18 | 662 | symbol (intern name))) |
9bf4c4e5 RS |
663 | (setq menu 'facemenu-face-menu) |
664 | (setq docstring | |
3187841b RS |
665 | (format "Select face `%s' for subsequent insertion. |
666 | If the mark is active and there is no prefix argument, | |
667 | apply face `%s' to the region instead. | |
668 | This command was defined by `facemenu-add-new-face'." | |
669 | name name)) | |
b6a67507 CY |
670 | (cond ((facemenu-iterate ; check if equivalent face is already in the menu |
671 | (lambda (m) (and (listp m) | |
672 | (symbolp (car m)) | |
f3359de1 RS |
673 | ;; Avoid error in face-equal |
674 | ;; when a non-face is erroneously present. | |
675 | (facep (car m)) | |
b6a67507 CY |
676 | (face-equal (car m) symbol))) |
677 | (cdr (symbol-function menu)))) | |
678 | ;; Faces with a keyboard equivalent. These go at the front. | |
679 | (key | |
88d690a9 RS |
680 | (setq function (intern (concat "facemenu-set-" name))) |
681 | (fset function | |
536f1a10 RS |
682 | `(lambda () |
683 | ,docstring | |
684 | (interactive) | |
af1eab21 RS |
685 | (facemenu-set-face |
686 | (quote ,symbol) | |
687 | (if (and mark-active (not current-prefix-arg)) | |
688 | (region-beginning)) | |
689 | (if (and mark-active (not current-prefix-arg)) | |
690 | (region-end))))) | |
88d690a9 RS |
691 | (define-key 'facemenu-keymap key (cons name function)) |
692 | (define-key menu key (cons name function))) | |
b6a67507 CY |
693 | ;; Faces with no keyboard equivalent. Figure out where to put it: |
694 | ((or (eq t facemenu-listed-faces) | |
695 | (memq symbol facemenu-listed-faces)) | |
7d8177cf | 696 | (setq key (vector symbol) |
88d690a9 RS |
697 | function 'facemenu-set-face-from-menu |
698 | menu-val (symbol-function menu)) | |
699 | (if (and facemenu-new-faces-at-end | |
b6a67507 | 700 | (> (length menu-val) 3)) |
88d690a9 | 701 | (define-key-after menu-val key (cons name function) |
9bf4c4e5 RS |
702 | (car (nth (- (length menu-val) 3) menu-val))) |
703 | (define-key menu key (cons name function)))))) | |
704 | nil) ; Return nil for facemenu-iterate | |
705 | ||
019b1899 | 706 | (defun facemenu-add-new-color (color menu) |
9bf4c4e5 | 707 | "Add COLOR (a color name string) to the appropriate Face menu. |
019b1899 | 708 | MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'. |
b97c98ad | 709 | Return the event type (a symbol) of the added menu entry. |
9bf4c4e5 RS |
710 | |
711 | This is called whenever you use a new color." | |
019b1899 LK |
712 | (let (symbol docstring) |
713 | (unless (color-defined-p color) | |
714 | (error "Color `%s' undefined" color)) | |
9bf4c4e5 RS |
715 | (cond ((eq menu 'facemenu-foreground-menu) |
716 | (setq docstring | |
717 | (format "Select foreground color %s for subsequent insertion." | |
019b1899 | 718 | color) |
b97c98ad | 719 | symbol (intern (concat "fg:" color)))) |
9bf4c4e5 RS |
720 | ((eq menu 'facemenu-background-menu) |
721 | (setq docstring | |
722 | (format "Select background color %s for subsequent insertion." | |
019b1899 | 723 | color) |
b97c98ad | 724 | symbol (intern (concat "bg:" color)))) |
019b1899 | 725 | (t (error "MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'"))) |
b97c98ad LK |
726 | (unless (facemenu-iterate ; Check if color is already in the menu. |
727 | (lambda (m) (and (listp m) | |
728 | (eq (car m) symbol))) | |
729 | (cdr (symbol-function menu))) | |
730 | ;; Color is not in the menu. Figure out where to put it. | |
731 | (let ((key (vector symbol)) | |
732 | (function 'facemenu-set-face-from-menu) | |
733 | (menu-val (symbol-function menu))) | |
734 | (if (and facemenu-new-faces-at-end | |
735 | (> (length menu-val) 3)) | |
736 | (define-key-after menu-val key (cons color function) | |
737 | (car (nth (- (length menu-val) 3) menu-val))) | |
738 | (define-key menu key (cons color function))))) | |
019b1899 | 739 | symbol)) |
bf7d4561 | 740 | |
bf7d4561 | 741 | (defun facemenu-complete-face-list (&optional oldlist) |
7cd49450 | 742 | "Return list of all faces that look different. |
71296446 | 743 | Starts with given ALIST of faces, and adds elements only if they display |
bf7d4561 | 744 | differently from any face already on the list. |
71296446 | 745 | The faces on ALIST will end up at the end of the returned list, in reverse |
bf7d4561 BG |
746 | order." |
747 | (let ((list (nreverse (mapcar 'car oldlist)))) | |
71296446 JB |
748 | (facemenu-iterate |
749 | (lambda (new-face) | |
bf7d4561 BG |
750 | (if (not (memq new-face list)) |
751 | (setq list (cons new-face list))) | |
752 | nil) | |
753 | (nreverse (face-list))) | |
754 | list)) | |
755 | ||
7e6cb513 | 756 | (defun facemenu-iterate (func list) |
4e8aa578 RS |
757 | "Apply FUNC to each element of LIST until one returns non-nil. |
758 | Returns the non-nil value it found, or nil if all were nil." | |
7e6cb513 SM |
759 | (while (and list (not (funcall func (car list)))) |
760 | (setq list (cdr list))) | |
761 | (car list)) | |
4e8aa578 RS |
762 | |
763 | (facemenu-update) | |
4e8aa578 | 764 | |
1ff4ace5 | 765 | (provide 'facemenu) |
ab5796a9 | 766 | |
cbee283d | 767 | ;; arch-tag: 85f6d02b-9085-420e-b651-0678f0e9c7eb |
4e8aa578 | 768 | ;;; facemenu.el ends here |