⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx27.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>

<HEAD>

<TITLE>UNIX Unleashed unx27.htm</TITLE>

<LINK REL="ToC" HREF="index.htm">

<LINK REL="Next" HREF="unx28.htm">

<LINK REL="Previous" HREF="unx26.htm"></HEAD>

<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>

<P><A HREF="unx26.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>

<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>

<A HREF="unx28.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>

<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>

</P><UL>

<LI>

<A HREF="#I1">27 &#151; Writing Your Own Macros</A></LI>

<UL>

<UL>

<UL>

<UL>

<LI>

<A HREF="#I3">By Susan Peppard</A></LI></UL></UL>

<LI>

<A HREF="#I4">Why Would Anyone Write a Macro?</A></LI>

<UL>

<LI>

<A HREF="#I5">Macro Review and Overview</A></LI>

<LI>

<A HREF="#I6">A Heading Macro, Dissected and Explained</A></LI>

<LI>

<A HREF="#I7">Number Registers</A></LI>

<LI>

<A HREF="#I8">Defined Strings</A></LI></UL>

<LI>

<A HREF="#I9">Getting Started</A></LI>

<UL>

<LI>

<A HREF="#I10">Defining a Macro</A></LI>

<LI>

<A HREF="#I11">Removing a Macro</A></LI>

<LI>

<A HREF="#I12">Renaming a Macro</A></LI>

<LI>

<A HREF="#I13">Adding to a Macro</A></LI></UL>

<LI>

<A HREF="#I14">A Simple Example</A></LI>

<LI>

<A HREF="#I15">Arguments</A></LI>

<LI>

<A HREF="#I16">Conditional Statements</A></LI>

<LI>

<A HREF="#I17">Arithmetic and Logical Expressions</A></LI>

<LI>

<A HREF="#I18">Diversions</A></LI>

<LI>

<A HREF="#I19">Traps</A></LI>

<LI>

<A HREF="#I20">Environments</A></LI>

<LI>

<A HREF="#I21">Debugging</A></LI>

<UL>

<LI>

<A HREF="#I22">troff Output</A></LI>

<LI>

<A HREF="#I23">PostScript Output</A></LI></UL>

<LI>

<A HREF="#I24">Hints for Creating a Macro Package</A></LI>

<LI>

<A HREF="#I25">Beyond Macro Packages</A></LI>

<LI>

<A HREF="#I26">Predefined Number Registers (nroff/troff)</A></LI>

<LI>

<A HREF="#I27">Predefined Read-Only Number Registers (nroff/troff)</A></LI>

<LI>

<A HREF="#I28">Summary</A></LI></UL></UL></UL>



<H1 ALIGN="CENTER">

<CENTER><A ID="I1" NAME="I1">

<BR>

<FONT SIZE=5><A ID="I2" NAME="I2"></A><B>27 &#151; Writing Your Own Macros</B>

<BR></FONT></A></CENTER></H1>

<H5 ALIGN="CENTER">

<CENTER><A ID="I3" NAME="I3">

<FONT SIZE=3><B>By Susan Peppard</B>

<BR></FONT></A></CENTER></H5>

<H3 ALIGN="CENTER">

<CENTER><A ID="I4" NAME="I4">

<FONT SIZE=4><B>Why Would Anyone Write a Macro?</B>

<BR></FONT></A></CENTER></H3>

<P>If you work with macros every day, sooner or later you'll get the urge to write one. Sometimes it's a mild case of the disease: You're creating a document with mm, but you want paragraphs with a first-line indent and no extra space between paragraphs. 
Occasionally you want to do something more elaborate&#151;like create a macro package for formatting that screen play.

<BR></P>

<P>Before you start, make sure you're familiar with the building blocks. troff provides you with the following: troff primitives (discussed in detail in Chapter 21, &quot;Basic Formatting with troff/nroff&quot;); escape sequences, such as \e and \^ (also 
discussed in detail in Chapter 21); other macros, either from a standard macro package, or ones you've written; number registers; and defined strings.

<BR></P>

<P>The next section reviews just what a macro is made of and introduces you to concepts that will be explained in detail later in the chapter.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I5" NAME="I5">

<FONT SIZE=3><B>Macro Review and Overview</B>

<BR></FONT></A></CENTER></H4>

<P>With embedded troff primitives, you can format a page just about any way you want. The trouble is you have to reinvent the wheel every time you write a new document. And every time you format a first-level heading, you have to remember just what 
sequence of primitives you used to produce that centered 14-point Helvetica Bold heading. Then you have to type three or four troff requests, the heading itself, and another three or four requests to return to your normal body style. (This is practical 
only if you're being paid by the line.) It's a laborious process and one that makes it difficult&#151;perhaps impossible&#151;to maintain consistency over a set of files.

<BR></P>

<P>Good news: You can use macros to simplify formatting and ensure consistency.

<BR></P>

<P>Macros take advantage of one of the UNIX system's distinguishing characteristics&#151;the ability to build complex processes from basic (primitive) units. A macro is nothing more than a series of troff requests, specified and named, that perform a 
special formatting task.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> The expression &quot;troff request&quot; is often used as a synonym for troff primitive. When I say &quot;troff request,&quot; I mean any or all of the facilities provided by troff: primitives, escape 

sequences, strings, registers, and special characters.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Macros can be simple or complex, short or long, straightforward or cryptic. For example, a new paragraph macro might entail

<BR></P>

<PRE>.sp .5

.ti .5i</PRE>

<P>This produces spacing of half a line space (nroff) or half an em (troff) between paragraphs, and indents the first line of each paragraph half an inch.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> You can use just about any unit of measurement you want&#151;inches, centimeters, points, picas&#151;as long as you specify the units.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Macro names consist of a period followed by one or two characters. Traditionally, these characters are uppercase, to distinguish them from primitives. (The me package is the exception to this rule.) The paragraph macro above could be called .P or .PP or 

.XX.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> In general, macro names, like primitive names, are mnemonic; there's some relationship, however farfetched, between the macro name and its function. Thus .P or .PP would be reasonable names for a 
paragraph macro, and .XX wouldn't.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Macros are invoked in a text file by typing their names. (The period must be in the first position on the line.) Macros can also be invoked with an apostrophe (single quote) instead of a period as the first character. This delays processing of the macro 

until the current line has been filled.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I6" NAME="I6">

<FONT SIZE=3><B>A Heading Macro, Dissected and Explained</B>

<BR></FONT></A></CENTER></H4>

<P>A fairly straightforward example of a macro is that centered heading I mentioned earlier. To create it, you need to provide spacing information before and after the heading, font information, size information, and position information (centering).

<BR></P>

<P>You could do this as follows:

<BR></P>

<PRE>.sp 2    \&quot;space before the heading

.ce 99   \&quot;turns on centering for the next 99 lines

.        \&quot;to accommodate headings that might start 

.        \&quot;out as 1 line and then get longer

.ft HB   \&quot;changes the font to Helvetica Bold

.ps 14   \changes the point size to 14 points

.vs 16   \&quot;changes vertical spacing to 16 points

first line of heading

second line of heading (optional)

third line of heading (optional)

.sp      \&quot;space after the heading

.ce 0    \turns off centering

.ft      \&quot;returns to default font

.ps      \returns to default point size

.vs      \&quot;returns to default vertical space</PRE>

<P>That simple series of troff primitives illustrates several important points.

<BR></P>

<P>Most important, it is full of comments. Comments are identified by the sequence.\&quot;

<BR></P>

<P>Note, however, that you can have as many spaces as you want between the initial period and the backslash. In this way, you can put a comment on a line by itself or you can add a comment at the end of a line of troff requests. You can use spaces to line 

up your comments so they're easy to read.

<BR></P>

<P>Another useful technique, illustrated in the sample above might be called generalization or thinking ahead. Instead of providing for a 1-line heading with a simple .ce, which centers the next line of text, the sample code turns centering on by 
requesting .ce 99 (which centers the next 99 lines of text). Most headings are not much longer than that. After the heading lines are specified, the code turns centering off with a .ce 0.

<BR></P>

<P>All of that code could be combined into a single pair of macros, called .H1 (for first-level heading) and .HE (for heading end), so that all you need type is

<BR></P>

<PRE>.H1

heading

.HE</PRE>

<P>A big improvement!

<BR></P>

<P>But wait. What if the heading came near the bottom of the page? There's nothing in the .H1 macro to prevent the heading from printing all by itself just before a page break. You need at least three lines of text after the heading. Fortunately there's a 

troff primitive trained for just this job&#151;.ne.

<BR></P>

<P>.ne (for need) says &quot;I need the number of lines specified right after me or else I'm going to start a new page.&quot; (This is similar to the &quot;Keep with Next&quot; feature of the better word processors and desktop publishing software.) 
Perfect. How many lines do you need? Three (so there will be at least three lines of text after the heading), plus one for the heading itself, two more for the spaces before the heading, and one last line for the space after the heading. So a real working 

version of the sample heading macro might have .ne 7 at the top.

<BR></P>

<P>This may seem like a lot of detail. (It gets worse.) If all you want to do is use a macro package to format documents, you may not want to learn how macros work. But, even if you have no intention of writing a macro yourself, it can be useful to 
understand how they work. (It can save you a lot of debugging time.) The more you know about the way macros are written, the easier it is to format a document.

<BR></P>

<P>What else might be done with a heading macro like .H1? Well, most often it would be combined with the .HE macro, so all you must type is

<BR></P>

<PRE>.H1 &quot;heading&quot;</PRE>

<P>Simple for the user, a bit harder for the macro writer.

<BR></P>

<P>To provide this kind of service, the .H1 macro would have to allow an argument. (An argument provides additional information for the macro or primitive&#151;like the 7 specified for the .ne primitive. The section &quot;Arguments,&quot; later in this 
chapter goes into more detail.)

<BR></P>

<P>An argument is coded in a special way. troff recognizes $1 as the first argument following the invocation of the macro. (This is a common UNIX convention.) There can be several (up to nine) arguments to a single macro (again, a common UNIX convention).

<BR></P>

<P>The code would look something like this:

<BR></P>

<PRE>.ne 7     \&quot;need 7 spaces on page or start new page

.sp 2     \&quot;space down 2 spaces before the heading

.ce 99    \&quot;turn centering on

\f(HB\s+2\\$1\fP\s0

.         \&quot;the font is Helvetica Bold, 2 points larger than 

.         \&quot;the body type, the heading itself - $1 -and then return to

.         \&quot;previous font and default point size

.ce 0     \&quot;turn centering off

.sp       \&quot;space down after the heading</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Don't worry about all the backslashes just yet. They are explained in the section &quot;Arguments,&quot; too. For now, just concentrate on the code and the comments.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>This macro is beginning to get complicated. (That means it's beginning to look like the kind of macro you'll see in a macro package.) But all it really says is the same old thing in a different way. UNIX is famous for providing 75 ways to do everything. 

troff code is no exception to this rule.

<BR></P>

<P>In the example above, the font change is accomplished by an escape sequence (\f(HB), instead of the .ft primitive. The point size is accomplished the same way (\s+2 instead of .ps), but note that a relative point size&#151;the current point size plus 
2&#151;is specified. Next comes the heading itself, the first argument to .H1, specified as $1.

<BR></P>

<P>To return to the previous font, use the escape sequence \fP. In many cases, \f1 works just as well. \f1 returns you to the default body type. To return to your original point size, use \s0 (or \s-2). \s0 returns you to the default point size. Since you 

don't always know what this is, \s0 can be very useful.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP:</B> When you use a heading macro, make a habit of surrounding the heading with quotation marks, even if it's a one-word heading. If you forget the quotes, your heading will be exactly one word long. troff 
simply disregards the rest of the line.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>There's just one more concept you need: conditional execution (if statements). Details on conditional statements can be found later in this chapter in the section, &quot;Conditional Statements.&quot; How would that work with the heading macro?

<BR></P>

<P>For one thing, you could change the macro name to plain .H and then use an argument to specify the heading level.

<BR></P>

<PRE>.H 1 &quot;first-level heading&quot;

.H 2 &quot;second-level heading&quot;

.

.

.

.H 7 &quot;seventh-level heading&quot;</PRE>

<P>And this is just what most macro packages do. They provide a general heading macro, and you supply the level and the text for the heading.

<BR></P>

<P>What would the code look like?

<BR></P>

<PRE>if \\$1 1 {     \&quot;if the heading level is 1, do everything within the

.               \&quot;curly braces; otherwise skip everything within them

.

.

.

\f(HB\s+2\\$fP\s0

.

.

.

}</PRE>

<P>Similarly,

<BR></P>

<PRE>if \\$1 2 {</PRE>

<P>for a second-level heading, and so on.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I7" NAME="I7">

<FONT SIZE=3><B>Number Registers</B>

<BR></FONT></A></CENTER></H4>

<P>Number registers are locations that store values. They store only whole numbers which can, but need not, have units attached to them. There are three things you can do with a number register:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -