Выбрать главу

7.6 Making Simple Drawings

Emacs is not, by any means, a graphics package, but it does provide some limited drawing capabilities. Emacs includes a picture mode that allows you to draw simple pictures using keyboard characters; it also includes artist mode, which enables you to draw quickly using the mouse.

Why would you want to draw with Emacs? Well, Emacs is useful for inserting a quick drawing or diagram in a mail message, something that most graphics packages can't do. It's also good for making block diagrams, timing diagrams (for electrical engineers), timelines, and other simple drawings.

Don't overlook this simple facility! We have seen many papers that were carefully formatted with a simple star-and-bar diagram dropped in the middle. Sure, you can use a graphics package to create a much nicer drawing, but if that's not your area of expertise, an Emacs ASCII drawing might be just the ticket.[40] We discuss picture mode first and then artist mode.

Picture mode turns the area being edited into a kind of drawing board consisting of columns and rows. In picture mode, you can create simple pictures (such as the one in Figure 7-3) using keyboard characters without having them "rearranged" by the word-wrap capabilities of auto-fill mode, for example.

Figure 7-3. Drawing in picture mode

To enter picture mode, type M-x edit-picture. The word Picture appears on the mode line, followed by the default drawing direction (more on that shortly). Typing C-c C-c exits picture mode and returns you to whatever major mode you were in before.

7.6.1 Drawing in Picture Mode

In picture mode, you can "draw" with any character in any of eight directions. Although you can draw in eight directions, only one direction is available at a time; this direction is referred to as the default direction. When you first enter picture mode, the default direction is right, meaning that if you press the hyphen key four times, you would draw a line to the right, as follows: ——. The default direction is displayed on the mode line, like this:

(Picture: right)

By typing special commands that change the default direction, you can draw in seven other directions as well. For example, C-c \ makes the default direction "southeast;" the mode line would then read (Picture: se). If you typed four hyphens in this direction, they would look like stair steps:

-

 -

  -

   -

Figure 7-4 illustrates the commands for setting various directions as the default in picture mode.

Figure 7-4. Moving around in picture mode

Picture mode tries to make these commands easy to remember, and it doesn't do too badly: for example, C-c ^ points upward, C-c-` arguably points to the northwest, and so on. If you can come up with a good mnemonic device for C-c . let us know! Maybe you can think of it as "dot for down."

After you set a default direction, pressing any character repeatedly draws a line of characters in that direction. Give it a try in a scratch buffer, using the commands in the figure to change the default direction. Try drawing a box.[41]

Type: M-x picture-mode

Putting the buffer into picture mode, default direction "right."

Type: Tab M-20 -

Emacs draws a line to the right. Next, we'll change the default direction to down, and use | for the right side of the square.

Type: C-c . M-5 |

Emacs draws a line down. Now we'll set the default direction to "left," then draw the bottom of the square.

Type: C-c < M-20 -

Emacs draws a line to the left. Next, use C-c ^ to set the default direction to "up," and then draw vertical bars back to the starting point.

Type: C-c ^ M-5 |

Emacs draws a line up that completes the box.

7.6.2 Editing in Picture Mode

By now, you should have a basic understanding of what picture mode can do for you. It's one of the more complicated minor modes because it redefines what many of the major editing keys do—and with good reason. The editing techniques you use for most ASCII files just won't work well for pictures. You don't really want to insert characters; the standard insert mode would prevent you from editing effectively, because any character you type distorts the rest of the line. Therefore, picture mode implicitly changes to overwrite mode. Many other features are redefined—some in insignificant ways, others in more substantial ways.

Therefore, to do justice to picture mode, we have to revisit most of the basic editing concepts. Please bear with us, or skip this section if you aren't interested in pictures. Let's start at the beginning: basic cursor motion.

7.6.2.1 Cursor motion in picture mode

Picture mode makes some small but important changes in the basic cursor commands. There's an easy way to summarize these changes: in picture mode the buffer becomes a grid of rows and columns. For example, consider what C-f does in most other modes: it moves forward through the file, one character at a time. Typing C-f repeatedly moves the cursor to the left, then at the end of the line, it jumps to the first character on the next line. picture mode, C-f means "move to the right." When you reach the end of the line in picture mode, C-f doesn't wrap to the next line; it continues adding characters to the current line.

C-p and C-n become vertical "up" and "down" commands, respectively. Try editing some sample text, moving to the end of a line, and typing C-p. Normally, as you type C-p, the cursor stays at the end of the line; if the previous line is short, the cursor moves to the left when it goes up. In picture mode, C-p and C-n always move up (or down) in a straight line.

You can get to every place you need to go with C-f, C-b, C-p, and C-n. The arrow keys work too, but you may want to know the cursor movement commands for moving in the default direction as well, so you can also go sideways when it's faster. C-c C-f moves you forward in the default direction (so "forward" here could mean to the left, right, up, or down, as well as all directions in between). C-c C-b moves you backward in the default direction. (Moving "up" or "down" relative to the default direction isn't defined.)

For example, let's say you had drawn the house shown in Figure 7-1 and you wanted to move the cursor down the left side of the roof. You would set the default direction to "southwest" by typing C-c /. If the cursor were on the top shingle on the left side of the roof, typing C-c C-f would move you down the left side of the roof and typing C-f would move you to the top-right shingle, as shown in Figure 7-5.

вернуться

40

A number of online groups are dedicated to ASCII art. Of course, all such art requires that you use a monospace font for proper viewing. Newsgroups such as alt.ascii.art and web sites such as the Ascii Art Dictionary (http://www.ascii-art.de/) provide a good introduction.

вернуться

41

Other commands can accomplish this task more quickly, but bear with us for the sake of a simple example. For example, this little exercise could be accomplished with a single mouse drag in artist mode. Picture mode also offers a quick command for drawing a rectangle, C-c C-r.