diff options
| author | Richard M. Stallman | 1994-09-14 19:32:18 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1994-09-14 19:32:18 +0000 |
| commit | 4e8aa578533b6f2b9f8e7704d90888485e03a22b (patch) | |
| tree | cbc25e35824b0f33189599a574093e9ef39d36c7 | |
| parent | c814718b2ea9ebcbee6773e7b378cdeb6f3163f6 (diff) | |
| download | emacs-4e8aa578533b6f2b9f8e7704d90888485e03a22b.tar.gz emacs-4e8aa578533b6f2b9f8e7704d90888485e03a22b.zip | |
Initial revision
| -rw-r--r-- | lisp/facemenu.el | 288 |
1 files changed, 288 insertions, 0 deletions
diff --git a/lisp/facemenu.el b/lisp/facemenu.el new file mode 100644 index 00000000000..1a2d670fac2 --- /dev/null +++ b/lisp/facemenu.el | |||
| @@ -0,0 +1,288 @@ | |||
| 1 | ;;; facemenu.el -- Create a face menu for interactively adding fonts to text | ||
| 2 | ;; Copyright (c) 1994 Free Software Foundation, Inc. | ||
| 3 | |||
| 4 | ;; Author: Boris Goldowsky <boris@cs.rochester.edu> | ||
| 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: | ||
| 24 | ;; This file defines a menu of faces (bold, italic, etc) which | ||
| 25 | ;; allows you to set the face used for a region of the buffer. | ||
| 26 | ;; Some faces also have keybindings, which are shown in the menu. | ||
| 27 | |||
| 28 | ;;; Installation: | ||
| 29 | ;; Put this file somewhere on emacs's load-path, and put | ||
| 30 | ;; (require 'facemenu) | ||
| 31 | ;; in your .emacs file. | ||
| 32 | |||
| 33 | ;;; Usage: | ||
| 34 | ;; Selecting a face from the menu or typing the keyboard equivalent | ||
| 35 | ;; will change the region to use that face. | ||
| 36 | ;; If you use transient-mark-mode and the region is not active, the | ||
| 37 | ;; face will be remembered and used for the next insertion. It will | ||
| 38 | ;; be forgotten if you move point or make other modifications before | ||
| 39 | ;; inserting or typing anything. | ||
| 40 | ;; | ||
| 41 | ;; Faces can be selected from the keyboard as well. | ||
| 42 | ;; The standard keybindings are M-s (or ESC s) + letter: | ||
| 43 | ;; M-s i = "set italic", M-s b = "set bold", etc. | ||
| 44 | |||
| 45 | ;;; Customization: | ||
| 46 | ;; An alternative set of keybindings that may be easier to type can be set up | ||
| 47 | ;; using "Hyper" keys. This requires that you set up a hyper-key on your | ||
| 48 | ;; keyboard. On my system, putting the following command in my .xinitrc: | ||
| 49 | ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L" | ||
| 50 | ;; makes the key labelled "Alt" act as a hyper key, but check with local | ||
| 51 | ;; X-perts for how to do it on your system. If you do this, then put the | ||
| 52 | ;; following in your .emacs before the (require 'facemenu): | ||
| 53 | ;; (setq facemenu-keybindings | ||
| 54 | ;; '((default . [?\H-d]) | ||
| 55 | ;; (bold . [?\H-b]) | ||
| 56 | ;; (italic . [?\H-i]) | ||
| 57 | ;; (bold-italic . [?\H-o]) | ||
| 58 | ;; (underline . [?\H-u]))) | ||
| 59 | ;; (setq facemenu-keymap global-map) | ||
| 60 | ;; (setq facemenu-key nil) | ||
| 61 | ;; | ||
| 62 | ;; In general, the order of the faces that appear in the menu and their | ||
| 63 | ;; keybindings can be controlled by setting the variable | ||
| 64 | ;; `facemenu-keybindings'. Faces that you never want to add to your | ||
| 65 | ;; document (e.g., `region') are listed in `facemenu-unlisted-faces'. | ||
| 66 | |||
| 67 | ;;; Known Problems: | ||
| 68 | ;; Only works with Emacs 19.23 and later. | ||
| 69 | ;; | ||
| 70 | ;; There is at present no way to display what the faces look like in | ||
| 71 | ;; the menu itself. | ||
| 72 | ;; | ||
| 73 | ;; `list-faces-display' shows the faces in a different order than | ||
| 74 | ;; this menu, which could be confusing. I do /not/ sort the list | ||
| 75 | ;; alphabetically, because I like the default order: it puts the most | ||
| 76 | ;; basic, common fonts first. | ||
| 77 | ;; | ||
| 78 | ;; Please send me any other problems, comments or ideas. | ||
| 79 | |||
| 80 | ;;; Code: | ||
| 81 | |||
| 82 | (provide 'facemenu) | ||
| 83 | |||
| 84 | (defvar facemenu-key "\M-s" | ||
| 85 | "Prefix to use for facemenu commands.") | ||
| 86 | |||
| 87 | (defvar facemenu-keymap nil | ||
| 88 | "Map for keybindings of face commands. | ||
| 89 | If nil, `facemenu-update' will create one. | ||
| 90 | `Facemenu-update' also fills in the keymap according to the bindings | ||
| 91 | requested in facemenu-keybindings.") | ||
| 92 | |||
| 93 | (defvar facemenu-keybindings | ||
| 94 | '((default . "d") | ||
| 95 | (bold . "b") | ||
| 96 | (italic . "i") | ||
| 97 | (bold-italic . "o") ; O for "Oblique" or "bOld"... | ||
| 98 | (underline . "u")) | ||
| 99 | "Alist of interesting faces and keybindings. | ||
| 100 | Each element is itself a list: the car is the name of the face, | ||
| 101 | the next element is the key to use as a keyboard equivalent of the menu item; | ||
| 102 | the binding is made in facemenu-keymap. | ||
| 103 | |||
| 104 | The faces specifically mentioned in this list are put at the top of | ||
| 105 | the menu, in the order specified. All other faces which are defined, | ||
| 106 | except for those in `facemenu-unlisted-faces', are listed after them, | ||
| 107 | but get no keyboard equivalents. | ||
| 108 | |||
| 109 | If you change this variable after loading facemenu.el, you will need to call | ||
| 110 | `facemenu-update' to make it take effect.") | ||
| 111 | |||
| 112 | (defvar facemenu-unlisted-faces | ||
| 113 | '(modeline region secondary-selection highlight scratch-face) | ||
| 114 | "Faces that are not included in the Face menu. | ||
| 115 | Set this before loading facemenu.el, or call `facemenu-update' after | ||
| 116 | changing it.") | ||
| 117 | |||
| 118 | (defvar facemenu-next nil) ; set when we are going to set a face on next char. | ||
| 119 | (defvar facemenu-loc nil) | ||
| 120 | |||
| 121 | (defun facemenu-update () | ||
| 122 | "Add or update the \"Face\" menu in the menu bar." | ||
| 123 | (interactive) | ||
| 124 | |||
| 125 | ;; Set up keymaps | ||
| 126 | (fset 'facemenu-menu (setq facemenu-menu (make-sparse-keymap "Face"))) | ||
| 127 | (if (null facemenu-keymap) | ||
| 128 | (fset 'facemenu-keymap | ||
| 129 | (setq facemenu-keymap (make-sparse-keymap "Set face")))) | ||
| 130 | (if facemenu-key | ||
| 131 | (define-key global-map facemenu-key facemenu-keymap)) | ||
| 132 | |||
| 133 | ;; Define basic keys | ||
| 134 | (define-key facemenu-menu [update] '("Update Menu" . facemenu-update)) | ||
| 135 | (define-key facemenu-menu [display] '("Display" . list-faces-display)) | ||
| 136 | (define-key facemenu-menu [sep1] '("-------------")) | ||
| 137 | (define-key facemenu-menu [remove] '("Remove Properties" . | ||
| 138 | facemenu-remove-all)) | ||
| 139 | (define-key facemenu-menu [read-only] '("Read-Only". facemenu-set-read-only)) | ||
| 140 | (define-key facemenu-menu [invisible] '("Invisible" | ||
| 141 | . facemenu-set-invisible)) | ||
| 142 | (define-key facemenu-menu [sep2] '("---Special---")) | ||
| 143 | (define-key facemenu-menu [other] '("Other..." . facemenu-set-face)) | ||
| 144 | |||
| 145 | ;; Define commands for face-changing | ||
| 146 | (facemenu-iterate | ||
| 147 | (function | ||
| 148 | (lambda (f) | ||
| 149 | (let ((face (car f)) | ||
| 150 | (name (symbol-name (car f))) | ||
| 151 | (key (cdr f))) | ||
| 152 | (cond ((memq face facemenu-unlisted-faces) | ||
| 153 | nil) | ||
| 154 | ((null key) (define-key facemenu-menu (vector face) | ||
| 155 | (cons name 'facemenu-set-face-from-menu))) | ||
| 156 | (t (let ((function (intern (concat "facemenu-set-" name)))) | ||
| 157 | (fset function | ||
| 158 | (` (lambda () (interactive) | ||
| 159 | (facemenu-set-face (quote (, face)))))) | ||
| 160 | (define-key facemenu-keymap key (cons name function)) | ||
| 161 | (define-key facemenu-menu key (cons name function)))))) | ||
| 162 | nil)) | ||
| 163 | (facemenu-complete-face-list facemenu-keybindings)) | ||
| 164 | |||
| 165 | (define-key global-map (vector 'menu-bar 'Face) | ||
| 166 | (cons "Face" facemenu-menu))) | ||
| 167 | |||
| 168 | ; We'd really like to name the menu items as follows, | ||
| 169 | ; but we can't since menu entries don't display text properties (yet?) | ||
| 170 | ; (let ((s (copy-sequence (symbol-name face)))) | ||
| 171 | ; (put-text-property 0 (1- (length s)) | ||
| 172 | ; 'face face s) | ||
| 173 | ; s) | ||
| 174 | |||
| 175 | ;;;###autoload | ||
| 176 | (defun facemenu-set-face (face &optional start end) | ||
| 177 | "Set the face of the region or next character typed. | ||
| 178 | The face to be used is prompted for. | ||
| 179 | If the region is active, it will be set to the requested face. If | ||
| 180 | it is inactive \(even if mark-even-if-inactive is set) the next | ||
| 181 | character that is typed \(via `self-insert-command') will be set to | ||
| 182 | the the selected face. Moving point or switching buffers before | ||
| 183 | typing a character cancels the request." | ||
| 184 | (interactive (list (read-face-name "Use face: "))) | ||
| 185 | (if mark-active | ||
| 186 | (put-text-property (or start (region-beginning)) | ||
| 187 | (or end (region-end)) | ||
| 188 | 'face face) | ||
| 189 | (setq facemenu-next face facemenu-loc (point)))) | ||
| 190 | |||
| 191 | (defun facemenu-set-face-from-menu (face start end) | ||
| 192 | "Set the face of the region or next character typed. | ||
| 193 | This function is designed to be called from a menu; the face to use | ||
| 194 | is the menu item's name. | ||
| 195 | If the region is active, it will be set to the requested face. If | ||
| 196 | it is inactive \(even if mark-even-if-inactive is set) the next | ||
| 197 | character that is typed \(via `self-insert-command') will be set to | ||
| 198 | the the selected face. Moving point or switching buffers before | ||
| 199 | typing a character cancels the request." | ||
| 200 | (interactive (let ((keys (this-command-keys))) | ||
| 201 | (list (elt keys (1- (length keys))) | ||
| 202 | (if mark-active (region-beginning)) | ||
| 203 | (if mark-active (region-end))))) | ||
| 204 | (if start | ||
| 205 | (put-text-property start end 'face face) | ||
| 206 | (setq facemenu-next face facemenu-loc (point)))) | ||
| 207 | |||
| 208 | (defun facemenu-set-invisible (start end) | ||
| 209 | "Make the region invisible. | ||
| 210 | This sets the `invisible' text property; it can be undone with | ||
| 211 | `facemenu-remove-all'." | ||
| 212 | (interactive "r") | ||
| 213 | (put-text-property start end 'invisible t)) | ||
| 214 | |||
| 215 | (defun facemenu-set-intangible (start end) | ||
| 216 | "Make the region intangible: disallow moving into it. | ||
| 217 | This sets the `intangible' text property; it can be undone with | ||
| 218 | `facemenu-remove-all'." | ||
| 219 | (interactive "r") | ||
| 220 | (put-text-property start end 'intangible t)) | ||
| 221 | |||
| 222 | (defun facemenu-set-read-only (start end) | ||
| 223 | "Make the region unmodifiable. | ||
| 224 | This sets the `read-only' text property; it can be undone with | ||
| 225 | `facemenu-remove-all'." | ||
| 226 | (interactive "r") | ||
| 227 | (put-text-property start end 'read-only t)) | ||
| 228 | |||
| 229 | (defun facemenu-remove-all (start end) | ||
| 230 | "Remove all text properties that facemenu added to region." | ||
| 231 | (interactive "*r") ; error if buffer is read-only despite the next line. | ||
| 232 | (let ((inhibit-read-only t)) | ||
| 233 | (remove-text-properties | ||
| 234 | start end '(face nil invisible nil intangible nil | ||
| 235 | read-only nil category nil)))) | ||
| 236 | |||
| 237 | (defun facemenu-after-change (begin end old-length) | ||
| 238 | "May set the face of just-inserted text to user's request. | ||
| 239 | This only happens if the change is an insertion, and | ||
| 240 | `facemenu-set-face[-from-menu]' was called with point at the | ||
| 241 | beginning of the insertion." | ||
| 242 | (if (null facemenu-next) ; exit immediately if no work | ||
| 243 | nil | ||
| 244 | (if (and (= 0 old-length) ; insertion | ||
| 245 | (= facemenu-loc begin)) ; point wasn't moved in between | ||
| 246 | (put-text-property begin end 'face facemenu-next)) | ||
| 247 | (setq facemenu-next nil))) | ||
| 248 | |||
| 249 | |||
| 250 | (defun facemenu-complete-face-list (&optional oldlist) | ||
| 251 | "Return alist of all faces that are look different. | ||
| 252 | Starts with given LIST of faces, and adds elements only if they display | ||
| 253 | differently from any face already on the list. | ||
| 254 | The original LIST will end up at the end of the returned list, in reverse | ||
| 255 | order. The elements added will have null cdrs." | ||
| 256 | (let ((list nil)) | ||
| 257 | (facemenu-iterate | ||
| 258 | (function | ||
| 259 | (lambda (item) | ||
| 260 | (if (internal-find-face (car item)) | ||
| 261 | (setq list (cons item list))) | ||
| 262 | nil)) | ||
| 263 | oldlist) | ||
| 264 | (facemenu-iterate | ||
| 265 | (function | ||
| 266 | (lambda (new-face) | ||
| 267 | (if (not (facemenu-iterate | ||
| 268 | (function | ||
| 269 | (lambda (item) (face-equal (car item) new-face t))) | ||
| 270 | list)) | ||
| 271 | (setq list (cons (cons new-face nil) list))) | ||
| 272 | nil)) | ||
| 273 | (nreverse (face-list))) | ||
| 274 | list)) | ||
| 275 | |||
| 276 | (defun facemenu-iterate (func iterate-list) | ||
| 277 | "Apply FUNC to each element of LIST until one returns non-nil. | ||
| 278 | Returns the non-nil value it found, or nil if all were nil." | ||
| 279 | (while (and iterate-list (not (funcall func (car iterate-list)))) | ||
| 280 | (setq iterate-list (cdr iterate-list))) | ||
| 281 | (car iterate-list)) | ||
| 282 | |||
| 283 | (facemenu-update) | ||
| 284 | (add-hook 'menu-bar-final-items 'Face) | ||
| 285 | (add-hook 'after-change-functions 'facemenu-after-change) | ||
| 286 | |||
| 287 | ;;; facemenu.el ends here | ||
| 288 | |||