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

• In shell mode on Mac OS X, Emacs says, "Warning: no access to tty (Bad file descriptor). Thus no job control in this shell." This happens with the graphical version of Emacs, not with the version run from the Mac OS X Terminal application. If you change to a different shell using the instructions under "Which shell?" earlier in this chapter, the error goes away.

• Some commands don't work on Mac OS X. The graphical version of Mac OS X fails to find some operating system commands, especially when invoking them through M-! (for shell-command). Change to a different shell; see "Which shell?" earlier in this chapter for details. Another problem is that some Unix commands are not available by default on Mac OS X. Try them in the Mac Terminal application to see if they work at all before trying them in shell mode. To increase Mac OS X's Unix functionality, use Fink (http://fink.sourceforge.net) to download a wide variety of Unix commands and software for Mac OS X.

• Some commands don't work on Windows. This chapter describes many commands that have no Windows equivalent. The Windows port of Emacs works well for most Dired functions, the calendar, and the diary. To get Unix command functionality under Windows, install Cygwin (http://cygwin.com).

• Printing does not work from Windows on USB printers. Many USB printers do not support printing from the command line. This problem is not specific to Emacs.

Chapter 6. Writing Macros

What is a macro? In Emacs, a macro is simply a group of recorded keystrokes you can play back over and over again. Macros are a great way to save yourself repetitive work. For example, let's say you want to delete the third column of a table. Normally, you would go to the first line; move over to the third column; delete it; then go to the second line; give the same set of commands; and so on, until you finish, your fingers wear out, or you get too bored. Emacs lets you record the keystrokes you used to work on the first line of the table, and then "play these back" repeatedly until the job is done.[32]

Any command or action you do within Emacs, from typing text to editing to switching buffers, can be done within a macro. The key to using macros well is, not too surprisingly, recognizing when you're doing repetitive work: sensing that you have pressed more or less the same sequence of keys several times in a row. Once you learn to recognize repetitious work, you have a good feel for when to use macros. The next talent that you'll need is, given that you've recognized a cycle of "almost identical" keystrokes, figuring out how to make that cycle precisely identical—that is, figuring out a set of keystrokes that, if repeated, will do exactly what you want. Neither of these skills is particularly difficult; with a little practice, you'll be using macros all the time.

If this sounds like lazy man's programming, it is: macros give you a simple way to do very complicated things without learning Lisp and without learning any customization tricks. If the task you build the macro for is something you have to do frequently, you can save macros and load them when you want to use them. In this way, you can build up a set of convenient macros that become your own editing commands. Even if you don't write Lisp, you're not limited to the commands Emacs gives you; you can make your own!

What you use macros for will depend on the kind of work you do in Emacs. We've used macros to:

• Mark up text for formatting.

• Copy headings from one buffer to another to create an outline.

• Perform complex search-and-replace type operations that query-replace can't quite handle.

• Create index entries.

• Reformat files that were imported from another application.

• Edit tables.

• Compile, run, and test the output from a program with a single command.

• Manipulate and clean large datasets.

You'll be able to think of many more things to do with macros after you learn the few basic commands you need to use them.

A Macro Revolution

In this book, we almost never emphasize which version of Emacs we're talking about. Macros, specifically changes to macros in Emacs 21.3.5, have forced our hand. Macros underwent a major overhaul in 21.3.5. Although some of the core key bindings still work the same way, the keyboard macro functionality was radically expanded. If you are running an earlier version of Emacs, we encourage you to install the latest version (see Chapter 13) or go to the web site for this book, http://www.oreilly.com/catalog/gnu3/, which includes a link to an earlier version of this chapter.

6.1 Defining a Macro

To start defining a macro, press F3 or C-x (.[33] The abbreviation Def appears on the mode line, showing that you are in macro definition mode. In this mode, Emacs records all the keystrokes that you type, whether they are commands or literal text, so that you can replay them later. To end the macro, press F4 or C-x ); you leave macro definition mode, and Emacs stops recording your keystrokes. Emacs also stops recording your keystrokes automatically if an error occurs or if you press C-g.

While you're defining a macro, Emacs acts on your keystrokes as well as recording them: that is, anything you type while in macro definition mode is treated as a regular command and executed. While you're defining a macro, you're doing completely normal editing. That way you can see that the macro does exactly what you want it to, and you can cancel it (with C-g) if you notice that the macro isn't really quite what you want.

To execute your macro, press F4 or C-x e. Emacs then replays your keystrokes exactly. (You can see that F4 has two different functions relating to macros: to end a macro definition and, after it's defined, to execute the macro.)

This macro is referred to as the "last" keyboard macro, with last here meaning most recent. Only one macro is the last keyboard macro. A macro ring, much like the kill ring, allows you to access a number of macros during an Emacs session.

Table 6-1 shows the steps required to define and execute a macro. This macro takes a list of names in the ordinary First Name Last Name order and changes it to the frequently needed Last Name, First Name order.

Table 6-1. Steps for creating name transposition macro

Keystrokes Action
F3 or C-x ( Start the macro; Def appears on the mode line.
C-a Move to the beginning of the current line.
M-f Move forward a word.
, Type a comma.
M-t Transpose first and last.
C-n Move to the next line.
F4 or C-x ) End the macro definition.
вернуться

32

You could delete the third column of a table by marking it as a rectangle, as described in Chapter 7. But bear with us for the sake of making this point: when you find yourself doing repetitive work, macros are the tool to remember.

вернуться

33

Mac OS X users may have bound F3 and F4, used in defining and executing macros, to another key. These users should press Option-F3 and Option-F4 to get the same functionality.