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.[6] We discuss picture mode first and then artist mode.

[6] 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.

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.[7]

[7] 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.

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.

Figure 7-5. Using the default direction versus typical cursor movement commands


7.6.2.2 Inserting blank lines

As you continue to work in picture mode, you'll find a few more surprises. Pressing Enter in picture mode moves you to the beginning of the next line, without inserting a blank line on the assumption that you probably don't want to change the relationship between lines. If you want to insert a new line, type C-o; an empty line appears beneath the current line, and the cursor does not move. For example, the cursor is initially on the 0 in the first line. If we want to open another line between the two, we type C-o.

Initial state:

Initial text; the cursor is on the 0 in the first line.


Type: C-o

C-o opens a new line but doesn't move the cursor.


One of the more difficult things to do in picture mode is to type a standard carriage return that breaks a line in the middle. You can move to a point in the middle of a line, type C-k to kill the right-hand portion, type C-o to insert a blank line; type Enter to move to the beginning of this blank line, and type C-y to yank the right-hand part of the line back. Or you can use the split-line command (C-M-o), and then delete the blank space at the beginning of the new line.

Deletion isn't quite the same, either. In picture mode C-c C-d is the delete character command that you're used to: it deletes the character under the cursor and moves the rest of the line to the left. An unadorned C-d deletes the character under the cursor, replacing it with a space. Del deletes the character to the left of the cursor, replacing it with a space.

Table 7-6 contrasts the picture mode commands with their normal text mode behavior.

Table 7-6. Picture mode v. text mode

Keystrokes

In text mode

In picture mode

Picture mode alternative

Enter

Insert a blank line.

Move the cursor to the beginning of the next line.

C-o inserts blank lines.

C-d

Delete the character and move the text to left.

Replace the character with Space and don't move.

C-c C-d is like C-d in text mode.

Space

Move the text to the right and insert a space.

Move the cursor to the right and delete any character you space over.

None; go back to text mode to insert blank spaces.

C-k

Erase the text on the current line; pressing C-k twice deletes a line.

Erase the text on the current line; it doesn't delete the line.

To delete a line, go back to text mode or use delete-rectangle.

Tab

Insert tabs and move the remaining text to the right.

Move the cursor across the screen but don't affect the underlying text.

To insert a tab's worth of space, go back to text mode.

C-n

Move to the next line.

Move down, staying in the same column.

(none)

C-p

Move to the previous line.

Move up, staying in the same column.

(none)

C-f

Move one character forward in the file.

Move one character to the right.

(none)

C-b

Move one character backward in the file.

Move one character to the left; stop at the beginning of the line.

(none)


If you want to insert a block of blank space, you can use a rectangle command such as open-rectangle. See the discussion of this command earlier in this chapter for more information. Also, if you want to insert blank space at the end of a line, you can use C-f.

To perform some tasks, you may find it easier to switch back temporarily to the mode you're used to. C-c C-c moves you back to the mode you were in before you entered picture mode. Make any necessary changes, then enter picture mode again by typing M-x picture-mode.

If you want to move something you've drawn, the easiest way is to use rectangles, as described earlier in this chapter.

Tabs are also different in picture mode. By default, picture mode interprets the following characters as tab stops if they appear by themselves on a line: exclamation point (!), hyphen (-) and tilde (~). If these characters appear on a line and the user presses tab on the next line, these characters are presumed to denote tab stops. You can change this behavior by setting the variable picture-tab-chars to other characters. If the characters appear with normal text, they are not interpreted as tab stops. To use these characters as tab stops, press Esc-Tab (for picture-tab-search).

Table 7-7 summarizes the commands for editing in picture mode.

Table 7-7. Picture mode commands

Keystrokes

Command name

Action

(none)

picture-modeoredit-picture

Enter picture mode.

C-c C-c

picture-mode-exit

Exit picture mode and return to the previous mode.

C-c ^

picture-movement-up

Set the default drawing direction to up.

C-c .

picture-movement-down

Set the default drawing direction to down.

C-c >

picture-movement-right

Set the default drawing direction to right.

C-c <

picture-movement-left

Set the default drawing direction to left.

C-c `

picture-movement-nw

Set the default drawing direction to northwest.

C-c `

picture-movement-ne

Set the default drawing direction to northeast.

C-c /

picture-movement-sw

Set the default drawing direction to southwest.

C-c \

picture-movement-se

Set the default drawing direction to southeast.

C-c C-f

picture-motion

Move the cursor forward in the default drawing direction.

C-c C-b

picture-motion-reverse

Move the cursor backward in the default drawing direction.

C-f

picture-forward-column

Move the cursor to the right one character.

C-b

picture-backward-column

Move the cursor to the left one character.

C-n

picture-move-down

Move the cursor down one character.

C-p

picture-move-up

Move the cursor up one character.

C-d

picture-clear-column

Blank out the character under the cursor; doesn't move remaining text to the left.

C-c C-d

delete-char

Delete the character under the cursor and move the remaining text to the left.

C-k

picture-clear-line

Delete the text on the current line; the line is not deleted if used twice.

C-o

picture-open-line

Insert a blank line.

C-c C-w r

picture-clear-rectangle-to-register

Clear the rectangle and save it in register r.

C-u C-c C-w r

picture-clear-rectangle-to-register

Delete the rectangle and save it in register r.

C-c C-x r

picture-yank-rectangle-from-register

Insert the rectangle saved in register r at the cursor position.

C-c C-r

picture-draw-rectangle

Draw a rectangle around current region.

C-c C-y

picture-yank-rectangle

Paste rectangle.

C-c C-k

picture-clear-rectangle

Erase rectangle.

C-c Tab

picture-set-tab-stops

Set tab stops applicable only in picture mode (!, -, and ~ denote tab stops by default).

M-Tab

picture-tab-search

Move to the next picture mode tab.


7.6.3 Drawing with the Mouse Using Artist

We would be remiss if we didn't introduce you to artist mode, an easy way to create ASCII art using the mouse. (You can also use keyboard commands, but trust us you won't want to.)

Artist mode is a minor mode related to picture mode, so you use them together. For example, you might draw using artist mode, then edit the picture in picture mode. Or you might choose to use artist mode alone for your creations.

We're going to give you a taste of artist mode; you can perfect your skills in your spare time. When you start artist mode, picture mode starts automatically.

Type: M-x artist-mode

Artist appears on the mode line, as does Picture.


When you start artist mode, pen drawing is selected by default.

Hold down the left mouse button and move around to scribble.

A random scribble.


With the pen, you can draw freestyle. Hold down the middle mouse button and a menu appears, with Drawing, Edit, and Settings submenus. The Drawing menu offers a variety of shapes from which to choose. Now that we've scribbled, let's create some graffiti using the spray can.

Select Spray Can from the Drawing menu, then spray the screen by holding down the left mouse button and moving the mouse.

A random spray.


We aren't going to go deep into artist mode, but we would like to give you a flavor of the basic drawing choices. You can draw rectangles (our personal favorite), ellipses, lines (which strive to be straight), and poly-lines (which strive to be polygon-angular). Figure 7-5 shows a representative sample of shapes. With practice, you can create complex drawings and edit them, either using the mouse or using standard picture mode commands.

Figure 7-6. A representative sample of artist shapes


For rectangles, lines, and ellipses, hold down the left mouse and pull them to the size and, in the case of lines, angle you prefer. (Ellipses are made of straight lines, so use your imagination; this is ASCII art after all.) For poly-lines, draw a line by holding down the left mouse button, then release it. Move the mouse away from that line to the next corner of the polygon and click. Emacs draws a line connecting the two points. Poly-lines allow you to create polygons quickly.

Table 7-8 provides an overview of artist commands. Artist works very well with the mouse and the middle-button mouse menu; if you're mouse-averse, you'll prefer picture mode.

Table 7-8. Artist mode commands

Keystrokes

Command name

Action

(none)

artist-mode

Enter artist mode.

C-c C-c

artist-mode-off

Exit artist mode.

C-f

artist-forward-char

Move to the right one character (at end of line, keep adding characters to current line).

C-b

artist-backward-char

Move to the left one character (at beginning of line, does nothing).

C-n

artist-next-line

Move down a column (at end of buffer, keep adding lines to the buffer).

C-p

artist-previous-line

Move up a column (at first line of buffer moves to first position in file, then does nothing).

C-c C-a C-oorMouse-2

artist-select-operation

Select an operation (press Tab to see a list).

C-c C-a f`Artist menu Flood-fill

artist-select-op-flood-fill

Select flood fill as the operation.

C-c C-a C-kArtist menu Cut

artist-select-op-cut-rectangle

Draw a rectangle around an area, then cut.

C-c C-a M-w Artist menu Copy

artist-select-op-copy-rectangle

Draw a rectangle around an area, then copy.

C-c C-a C-yArtist menu Paste

artist-select-op-paste

Paste what you copied wherever you click the mouse.

C-c C-a v Artist menu Vaporize

artist-select-op-vaporize-line

Erase a line you select (literal line; not a line in the file).

C-c C-a C-dArtist menu Erase

artist-select-op-erase-char

Set operation to erase (use the mouse as your eraser).

C-c C-a S Artist menu Spray-can

artist-select-op-spray-can

Set operation to spray can.

C-c C-a e Artist menu Ellipse

artist-select-op-ellipse

Draw ellipses.

C-c C-a p Artist menu Poly-line

artist-select-op-poly-line

Draws poly-lines

C-c C-a r Artist menu Rectangle

artist-select-op-rectangle

Draw rectangles.

C-c C-a l Artist menu Line

artist-select-op-line

Draw lines.

C-c C-a C-rArtist menu Rubber banding

artist-toggle-rubber-banding

If on (the default), show shape while stretching; if not, mark end-points.

C-c C-a C-lArtist menu Set Line

artist-select-line-char

Select character to use when drawing lines (- is the default).

C-c C-a C-f Artist menu Set Fill

artist-select-fill-char

Select character to fill shapes with (Space is the default).


7.6.4 Problems You May Encounter

  • Artist mode says you can't change to another shape while drawing. Exit artist mode and then reenter. Before drawing anything, click the mouse's middle button to display the pop-up menu and select the desired shape from the Drawing menu.



Learning GNU Emacs
Learning GNU Emacs, Third Edition
ISBN: 0596006489
EAN: 2147483647
Year: 2003
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net