What's needed to remedy this situation is a way for text properties to be saved in an expanded text-markup form and restored into text properties when the file is next edited.
At the time of this writing, experimental code to support this is included with Emacs. A library called enriched-mode supports saving text properties into the MIME enriched-text format specified by the Internet standards document RFC 1896, and can parse files in that format into Emacs buffers with equivalent text and text properties.
Although this mode is quite usable as is, much design and development still needs to be done before the capabilities enriched mode supports are mature and well integrated with other Emacs modes. By the time you read this, there may be several such libraries, each supporting a different enriched format such as HTML. Eventually modes like these should enable Emacs to support WYSIWYG and even multimedia editing.
To enter enriched mode, type M-x enriched-mode. Enriched
appears on the mode line. Emacs may ask if you want to make newlines between paragraphs hard. (This is because Emacs reformats the paragraphs when you change margin settings.) Type y.
You can use several font commands to decorate your text. Most begin with the M-gprefix. Table 10-1lists some of the more common options. If you like using the menus, you can also select the options in Table 10-1using the Edit → Text Properties → Face menu.
Table 10-1. Enriched mode font commands
Command |
Font selected |
M-g d |
default |
M-g b |
bold |
M-g i |
italic |
M-g l |
bold-italic |
M-g u |
underline |
M-g o |
other (allows you to pick a font face by name) |
The commands listed in Table 10-1apply to the currently marked text. We used a number of these commands to produce the simple text example shown in Figure 10-12.
Figure 10-12. An enriched text example (Mac OS X)
10.3.5.1 Saving enriched text
When you save enriched text, Emacs marks up the document with XML-like tags. Emacs will happily read the document back in, although not many other applications will know what to do with the tags. Still, as you can see below, the tags are straightforward and would allow custom applications such as CGI scripts for the Web to parse them quickly.
Content-Type: text/enriched
Text-Width: 70
blueTesting
This is a quick test of the
redenriched mode in Emacs.
Not sure what's gonna happen.
Looks good from here.
But, you can't rely too much on enriched mode yet. For example note the Testing
title line. It doesn't appear to contain any information about the size of the font—which is definitely larger if you look at Figure 10-11. Sure enough, killing the buffer and reloading the file loses the size value. The text is still blue and the content is available, but some of the formatting has been lost.
The moral is a classic one: be careful. If you have serious enriched text needs, Emacs is probably not the tool to use (at least not yet). Many of the various word processors out there will do a much better job. But if you just need some basic enhancements to documents that only you or other Emacs users will view, enriched mode is just the ticket.
10.4 Customizing Your Key Bindings
Perhaps the most common things that Emacs users want to customize are the keystrokes that cause commands to run. Keystrokes are associated with commands via key bindings .
Actually, every keystroke runs a command in Emacs. Printable character keys (letters, numerals, punctuation, and spaces) run the self-insert-command, which merely causes the key just pressed to be inserted at the cursor in the current buffer. (You could play a nasty April Fool's joke on a naïve Emacs user by changing the bindings of their printable characters.)
The default set of key bindings is adequate for most purposes, of course, but there are various cases in which you may want to add or change key bindings. Emacs contains literally hundreds of commands, only some of which have key bindings. As you know, you can access those that don't have bindings by typing M-x command-name Enter.
If, however, you intend to use an unbound command often, you may want to bind it to a keystroke sequence for convenience. You may want to set special keys, such as arrow, numeric keypad, or function keys, to perform commands you use often.
The other important concept you need to know now is that of a keymap , which is a collection of key bindings. The most basic default key bindings in Emacs are kept in a keymap called global-map. There is also the concept of a local keymap, which is specific to a single buffer. Local keymaps are used to implement commands in modes (like C mode, text mode, shell mode, etc.), and each such mode has its own keymap it installs as the local map when invoked. When you type a key, Emacs first looks it up in the current buffer's local map (if any). If it doesn't find an entry there, it looks in global-map. If an entry for the key is found, its associated command is run.
What happens with commands that are bound to multiple keystrokes, as in C-x kfor kill-buffer? The answer is that the keys C-x, Esc, and C-care actually bound to special internal functions that cause Emacs to wait for another key to be pressed and then to look up that key's binding in another map; they also cause messages like C-x-to appear in the minibuffer if more than a second passes before the next key is pressed. The additional keymaps for C-xand Escare called ctl-x-mapand esc-map, [70]respectively; C-cis reserved for local keymaps associated with modes like C mode and shell mode.
For example, when you type Esc dor M-d, Emacs looks it up in the buffer's local keymap. We will assume it doesn't find an entry there. Then Emacs searches global-map; there it finds an entry for Escwith a special function (called ESC-prefix) that waits for the next keystroke and uses esc-mapto determine which command to execute. When you type d, ESC-prefixlooks up the entry for din esc-map, finds kill-word, and runs it.
You can create your own key bindings by adding entries in keymaps (or overriding existing ones). Three functions are available for doing this: define-key, global-set-key, and local-set-key. Their forms are:
(define-key keymap " keystroke " ' command-name )
(global-set-key " keystroke " ' command-name )
(local-set-key " keystroke " ' command-name )
Notice the double quotes around keystroke and the single quote preceding command-name . This is Lisp syntax; for more details, see Chapter 11. The keystroke is one or more characters, either printable or special characters. For the latter, use the conventions in Table 10-2.
Table 10-2. Special character conventions
Читать дальше