• Tidak ada hasil yang ditemukan

Getting the kernel run

Dalam dokumen The Listings Package (Halaman 79-82)

changed \lst@Delim

hdefault style macroi \lst@commentstyle

[*[*]][htypei][[hstylei][[htype optioni]]]

hdelimiter(s)i\relax #2\relax

{hdelimiter namei} {Comment}

hdelimiter types macroi \lst@commenttypes

\@empty|\@nil|\relax #1

{hbegin- and end-delim macroi} {\lst@BeginComment\lst@EndComment}

hextra prefixi i

hextra conversioni \@empty

{hbegin- and end-delim macroi} {\lst@BeginIComment\lst@EndIComment}

Most arguments should be clear. We’ll discuss the last four. Both {hbegin- and end-delim macroi} must contain exactly two control sequences, which are given to \lst@hnamei[DM]@htypei to begin and end a delimiter. These are the arguments #3 and #4 in our first example of \lst@StringDM@l.

Depending on whether the user chosen type starts with hextra prefixi, the first two or the last control sequences are used.

By default the package takes the delimiter(s), makes the characters active, and places them after \lst@hnamei[DM]@htypei. If the user type starts with hextra prefixi,hextra conversionimight change the definition of\lst@next to choose a different conversion. The default is equivalent to\lst@XConvert with\lst@false.

Note that htypei never starts withhextra prefixi since it is discarded. The functionality must be fully implemented by choosing a different{hbegin- and end-delim macroi}pair.

You might need to know the syntaxes of thehbegin- and end-delim macrois. They are called as follows.

\lst@Beginhwhateveri

{hmodei} {{hstylei}hstart tokensi}hdelimiteri\@empty

\lst@Endhwhateveri

{hmodei}hdelimiteri\@empty

The existing macros are internally defined in terms of \lst@DelimOpen and

\lst@DelimClose, see the implementation.

5. hDo whatever you want.i

6. Execute \lst@Init\relaxto finish initialization.

7. hDo whatever you want.i

8. Eventually comes the source code, which is processed by the kernel. You must ensure that the characters are either not already read or all active.

Moreover you must install a way to detect the end of the source code. If you’ve reached the end, you must . . .

9. . . . call \lst@DeInitto shutdown the kernel safely.

10. hDo whatever you want.i

11. Close the group from the beginning.

For example, consider the \lstinline command in case of being not inside an argument. Then the steps are as follows.

1. \leavevmode\bgroup opens a group.

2. \def\lst@boxpos{b} ‘baseline’ aligns the listing.

3. \lsthk@PreSet

4. \lstset{flexiblecolumns,#1}(#1is the user provided key=value list) 5. \lsthk@TextStyledeactivates all features not safe here.

6. \lst@Init\relax

7. \lst@Def{‘#1}{\lst@DeInit\egroup} installs the ‘end inline’ detection, where #1is the next character after\lstinline. Moreover chr(13) is rede- fined to end the fragment in the same way but also issues an error message.

8. Now comes the source code and . . .

9. . . . \lst@DeInit (from\lst@Defabove) ends the code snippet correctly.

10. Nothing.

11. \egroup(also from\lst@Def) closes the group.

The real definition is different since we allow source code inside arguments. Read also section18.5if you really want to write pretty-printing commands.

11 Useful internal definitions

This section requires an update.

11.1 General purpose macros

\lst@AddTohmacroi{hTEX materiali}

addshTEX materialiglobally to the contents of hmacroi.

\lst@Extendhmacroi{hTEX materiali}

calls\lst@AddToafter the first token ofhTEX materialiis\expandedafter.

For example, \lst@Extend \a \b merges the contents of the two macros and stores it globally in\a.

\lst@lAddTohmacroi{hTEX materiali}

\lst@lExtendhmacroi{hTEX materiali}

are local versions of\lst@AddTo and\lst@Extend.

\lst@DeleteKeysInhmacroihmacro (keys to remove)i

Both macros contain a comma separated list of keys (or keywords). All keys appearing in the second macro are removed (locally) from the first.

\lst@ReplaceInhmacroihmacro (containing replacement list)i

\lst@ReplaceInArghmacroi{hreplacement listi}

The replacement list has the form a1b1. . .anbn, where each ai and bi is a character sequence (enclosed in braces if necessary) and may contain macros, but the first token of bi must not be equivalent to\@empty. Each sequence ai inside the first macro is (locally) replaced by bi. The suffixArgrefers to thebracedsecond argument instead of a (nonbraced) macro. It’s a hint that we get the ‘real’ argument and not a ‘pointer’ to the argument.

\lst@IfSubstring{hcharacter sequencei}hmacroi{htheni}{helsei}

htheni is executed if hcharacter sequenceiis a substring of the contents of hmacroi. Otherwisehelseiis called.

\lst@IfOneOfhcharacter sequencei\relaxhmacroi{htheni}{helsei}

\relax terminates the first parameter here since it is faster than enclosing it in braces. hmacroi contains a comma separated list of identifiers. If the character sequence is one of these indentifiers, htheniis executed, and otherwisehelsei.

\lst@Swap{htok1i}{htok2i}

changes places of the following two tokens or arguments without inserting braces. For example, \lst@Swap{abc}{def}expands todefabc.

\lst@IfNextCharshmacroi{htheni}{helsei}

\lst@IfNextCharsArg{hcharacter sequencei}{htheni}{helsei}

Both macros execute either htheniorhelseiaccording to whether the given character sequence respectively the contents of the given macro is found (after the three arguments). Note an important difference between these macros and LATEX’s \@ifnextchar: We remove the characters behind the arguments until it is possible to decide which part must be executed. How- ever, we save these characters in the macro \lst@eaten, so they can be inserted usinghtheniorhelsei.

\lst@IfNextCharActive{htheni}{helsei}

executes htheniif next character is active, andhelseiotherwise.

\lst@DefActivehmacroi{hcharacter sequencei}

stores the character sequence in hmacroi, but all characters become active.

The string must not contain a begin group, end group or escape charac- ter ({}\); it may contain a left brace, right brace or backslash with other meaning (= catcode). This command would be quite surplus if hcharacter sequencei is not already read by TEX since such catcodes can be changed easily. It is explicitly allowed that the charcaters have been read, e.g. in

\def\test{\lst@DefActive\temp{ABC}}!

Note that this macro changes\lccodes 0–9 without restoring them.

\lst@DefOtherhmacroi{hcharacter sequencei}

stores hcharacter sequenceiin hmacroi, but all characters have catcode 12.

Moreover all spaces are removed and control sequences are converted to their name without preceding backslash. For example, \{ Chip \}leads to {Chip}where all catcodes are 12—internally the primitive\meaningis used.

Dalam dokumen The Listings Package (Halaman 79-82)