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

📄 unx27.htm

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

<FONT SIZE=3><B>Defining a Macro</B>

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

<P>To define a macro, use .de. You can end the definition, as shown above, with .., or you can use the delimiters of your choice, like this:

<BR></P>

<PRE>,deP!!

.ti 3P

!!</PRE>

<P>(I've never actually known anyone who used this form of the definition primitive, but it's there, if you want it. You know how UNIX always provides you with many roads to the same end.)

<BR></P>

<P>Once you have written your macro definition, you can add it to an existing macro package, add it to your own file of new macros, and source the file into your text files with .so, or just put the macro definition in your text file.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP:</B> Creating an add-on file of macros is the least desirable way of incorporating your macros. If your current macro package needs that much help, someone should rewrite the package. The purpose of a macro 

package is to ensure a consistent look to documents prepared with the package. If everyone defines her or his own set of paragraph macros, this purpose is defeated.

<BR></NOTE>

<HR ALIGN=CENTER>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Removing a Macro</B>

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

<P>To remove a macro, use the .rm primitive:

<BR></P>

<PRE>.rmP</PRE>

<P>Again, the space between the .rm and the macro name is optional.

<BR></P>

<P>This is not something that you do on a whim. Removing a macro requires serious, mature consideration. You might do it if you were experimenting with a better version of an existing macro&#151;a list end macro (.LE) that left the right amount of space 
after it, for example. Your new, improved macro might be called .lE, or .Le. You could encourage people to use your new macro by removing .LE. (This is unlikely to be wise; you always forget to tell your department head who is working on a weekend on a 
crucial document, and&#151;well, you can imagine the rest.) A safer way to use your new .Le might be to substitute the definition of .Le for .LE (after it's been tested and found to be truly superior), but to leave the .LE macro definition in the package 
and remove it at the end of the macro package file. (Even better, you could comment it out.)

<BR></P>

<P>Unless you are very knowledgeable about macros and are in charge of maintaining one or more macro packages, you will never remove a macro.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Renaming a Macro</B>

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

<P>To rename a macro, use the .rn primitive:

<BR></P>

<PRE>.rnP Pp</PRE>

<P>As usual, the space between the .rn and the macro name is optional. The space between the old name and the new name is not optional.

<BR></P>

<P>Renaming a macro is almost as serious as removing it. And it can be a great deal more complicated. For example, you might want to fix mm's list macro by adding some space after the .LE. You can do this by renaming. Here's what you do:

<BR></P>

<OL>

<LI>Rename the .LE macro. (.rn LE Le)

<BR>

<BR></LI>

<LI>Define a new .LE.

<BR>

<BR>.deLE     \&quot;This is a new improved version of LE - adds space

<BR>.Le

<BR>.sp .5

<BR>..

<BR>

<BR></LI>

<LI>Invoke .LE as usual.

<BR>

<BR></LI></OL>

<P>The new .LE (which is the old .LE plus a half-line space) takes the place of the old .LE.

<BR></P>

<P>You might think of using .rn so that you could include the .TH (table column heading) macro in a man file. (.TH is the basic title heading macro used in all man files.)

<BR></P>

<P>This seems to be a reasonable idea. If this sort of thing interests you, you can think through the process with me. (Otherwise, skip to &quot;Adding to a Macro.&quot;)

<BR></P>

<P>The first thing to establish is the conditions for each of the .TH macros: When should .TH mean table heading, and when should it mean title?

<BR></P>

<P>That's easy to answer. You want the normal .TH to mean title all the time except following a .TS H. So when do you rename .TH&#151;and which .TH do you rename? And, if you boldly put .rnTH Th in your file, to which .TH does it refer?

<BR></P>

<P>Think about that, and you'll begin to see that maybe .TH is not the ideal candidate for renaming.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Adding to a Macro</B>

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

<P>To add to a macro definition, use .am:

<BR></P>

<PRE>.amP

.ne 2     \&quot;of course this isn't the right place for this request

..</PRE>

<P>(Yes, the space after the .am is optional.)

<BR></P>

<P>Adding to a macro, while not a task for beginners, is a lot more straightforward. .am is often used to collect information for a table of contents. Whenever the file has a .H (of any level, or of specified levels) you want to write that information into 

a data file which will be processed and turned into a TOC.

<BR></P>

<H3 ALIGN="CENTER">

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

<FONT SIZE=4><B>A Simple Example</B>

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

<P>Suppose you've found yourself in the unenviable position of typing a term paper for your child, spouse, or self. It's easy enough to double space the paper&#151;just use .ls 2. But, if you're not using ms, you don't have an easy way of handling long 
quotes (which are supposed to be single spaced and indented from the left and right). What do you have to do every time you type a long quotation?

<BR></P>

<PRE>.in +1i

.ll -2i

.ls 1</PRE>

<P>And at the end of the quotation, you have to reverse that coding:

<BR></P>

<PRE>.in -1i

.ll +2i

.ls 2</PRE>

<P>Instead of typing those three lines, you could define a .Qb and a .Qe macro. Those two sets of three lines are the definitions. All you need to add is a .deQb (or .deQe) to start the macro definition and two dots to end it. If you want to refine the 
definition, you can add some space before and after the quotation and a .ne 2 so you don't get one line of the quotation at the bottom of page 5 and the other six lines on page 6:

<BR></P>

<PRE>.deQb

.sp

.ls 1

.ne 2

.in +1i

.ll -2i

..

.deOe

.br

.ls 2

.sp

.ne 2

.in -1i

.ll +2i

..</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> There's no rule that says user-defined macros have to consist of an uppercase character followed by a lowercase character. It just makes things easier when you have guidelines.

<BR></NOTE>

<HR ALIGN=CENTER>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="more.gif" WIDTH = 35 HEIGHT = 35><B>troff</B><B> Copy Mode</B>

<BR>

<BR>troff processes each file twice. The first time, called &quot;copy mode,&quot; consists of copying without much interpretation. There is some interpretation, however. In copy mode, troff interprets the following immediately: the contents of number 
registers (\n); strings (\*); and arguments (\$1).

<BR>

<BR>You do not want this to happen. troff will find \ns and \*s and \$1s in your macro package file&#151;before the number register or string or argument has any meaningful contents. Fortunately, troff also interprets \\ as \, so you can &quot;hide&quot; 
these constructs by preceding them with an extra backslash. \\n copies as \n&#151;which is what you want when the macro using that number register is invoked.

<BR>

<BR>Note, however, that this rule does not apply to number registers invoked in your text file. When you invoke a number register in your text file, you want it interpreted then and there. So you don't use the extra backslash.

<BR>

<BR>This seems simple. In fact, it is simple in theory. In practice, it's a horrible nuisance. A glance at a macro package like ms or mm will show you triple, even quad-ruple, backslashes. If you don't enjoy thinking through processes step by painful step, 

you will not enjoy this aspect of macro writing.

<BR>

<BR>troff does not interpret ordinary escape sequences in copy mode. \h, \&amp;, \d are all safe and do not have to be hidden.

<BR>

<BR>During copy mode, troff eliminates comments following \&quot;.

<BR></NOTE>

<HR ALIGN=CENTER>

<H3 ALIGN="CENTER">

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

<FONT SIZE=4><B>Arguments</B>

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

<P>Macros, like other UNIX constructs, can take arguments. You specify an argument every time you type a heading after a .H 1 or a .NH. You specify arguments to primitives, too, like .sp .5 or .in +3P. In a macro definition, arguments are represented by 
\$1 through \$9. (Yes, you are limited to nine arguments.)

<BR></P>

<P>A couple of examples of arguments are:

<BR></P>

<PRE>.deCo     \&quot;computer output (CW) font

\f(CW\\$1\fP

..

.dePi     \&quot;paragraph indented amount specified by $1

.br

.ne 2

.ti \\$1

..</PRE>

<P>(Note that you must hide the argument (with the extra backslash) in order to survive copy mode.)

<BR></P>

<P>If you omit an argument, troff treats it as a null argument. In the case of the .Co macro, nothing at all would happen. In the case of the .Pi macro, the paragraph would not be indented. If you specify too many arguments (which would happen if you had 
.Co Press Enter in your file), troff merrily throws away the extras. You'd get &quot;Press&quot; in CW font; &quot;Enter&quot; would disappear. Use double quotation marks (.Co &quot;Press Enter&quot;) to hide spaces from troff.

<BR></P>

<H3 ALIGN="CENTER">

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

<FONT SIZE=4><B>Conditional Statements</B>

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

<P>A conditional statement says, &quot;Do this under certain (specified) conditions.&quot; It may add, &quot;and under any other conditions, do that.&quot; You know the conditional statement as an &quot;if&quot; or an &quot;if-else.&quot; The troff 
versions are .if (if) and .ie (if-else). The troff if has a different syntax from the shell if, but the principle is the same.

<BR></P>

<P>A simple if is coded like this:

<BR></P>

<PRE>.if condition simple-action

.if condition \{

complex-action

\}</PRE>

<P>The backslash-brace combinations delimit the actions to be taken when the condition is true.

<BR></P>

<P>The if-else works like this:

<BR></P>

<PRE>.ie condition simple-action

.el simple-action

.ie condition \{

complex-action

\}

.el \{

complex-action

\}</PRE>

<P>You use the conditional statement whenever you want to test for a condition. Is this an even page? Okay, then use the even-page footer. Are these files being nroffed (as opposed to troffed)? Okay, then make the next few lines bold instead of increasing 

the point size.

<BR></P>

<P>Believe it or not, troff has four built-in conditions to test for just those conditions:

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>o</P>

<TD>

<P>current page is odd</P>

<TR>

<TD>

<P>e</P>

<TD>

<P>current page is even</P>

<TR>

<TD>

<P>t</P>

<TD>

<P>file is being formatted by troff</P>

<TR>

<TD>

<P>n</P>

<TD>

<P>file is being formatted by nroff</P></TABLE>

<P>The odd-even conditions simplify writing page header and footer macros. You can simply say:

<BR></P>

<PRE>.if o .tl '''%'     \&quot;if odd - page no. on right

.if e .tl '%'''     \&quot;if even - page no. on left</PRE>

<P>The single quotation marks delimit fields (left, center, and right). Thus, '''%' places the page number on the right side of the page and '%''' places it on the left side.

<BR></P>

<P>You could do the same thing with .ie:

<BR></P>

<PRE>.ie o .tl '''%'     \&quot;if odd - page no. on right

.el .tl '%'''       \&quot;else if even - page no. on left</PRE>

<P>The .if, even when it requires a seemingly endless list of conditions, is easier to use.

<BR></P>

<P>Suppose you are writing that heading macro discussed earlier in this chapter. You want to specify different spacing and different point sizes, depending on the level of the heading. You might start like this:

<BR></P>

<PRE>.deH

.if \\$1=1 \{

.bp

\s14\f(HB\\$1\fP\s0

.sp

\}

.if \\$1=2 \{

.sp 2

\s12\f(HB\\$1\fP\s0

.sp

|}

.

.

.</PRE>

<P>You can compare strings, but you use delimiters instead of an equal sign:

<BR></P>

<PRE>.if &quot;\\$1&quot;A&quot;

.if '\\$2'Index'</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP:</B> The bell character, made by pressing Ctrl+G, is often used as a delimiter because it's not much use in a text file. It looks like ^G in a file, but don't be fooled. This is a non-printing character. 
Before you print out every macro file on your system, check them for ^Gs. Unless you want to spend a lot of time drawing little bells or printing ^G, try substituting another character for the bell before you print. (Try this on small portions of the file 

at a time.)

<BR></NOTE>

<HR ALIGN=CENTER>

<P>In addition to comparing numbers and strings, you can also test for inverse conditions. troff recognizes the exclamation mark (!) as the reverse of an expression, for example:

<BR></P>

<PRE>.if !o        \&quot;same as .if e

.if !\\$1=0   \&quot;if $1 is not equal to 0

.if !&quot;\\$1&quot;&quot;</PRE>

<P>(The last example above tests for a null argument.)

⌨️ 快捷键说明

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