Next: , Previous: , Up: Input Control   [Contents][Index]


8.3 Changing the comment delimiters

The default comment delimiters can be changed with the builtin macro changecom:

Builtin: changecom ([start], [end = ‘NL])

This sets start as the new begin-comment delimiter and end as the new end-comment delimiter. If both arguments are missing, or start is void, then comments are disabled. Otherwise, if end is missing or void, the default end-comment delimiter of newline is used. The comment delimiters can be of any length.

The expansion of changecom is void.

define(`comment', `COMMENT')
⇒
# A normal comment
⇒# A normal comment
changecom(`/*', `*/')
⇒
# Not a comment anymore
⇒# Not a COMMENT anymore
But: /* this is a comment now */ while this is not a comment
⇒But: /* this is a comment now */ while this is not a COMMENT

Note how comments are copied to the output, much as if they were quoted strings. If you want the text inside a comment expanded, quote the begin-comment delimiter.

Calling changecom without any arguments, or with start as the empty string, will effectively disable the commenting mechanism. To restore the original comment start of ‘#’, you must explicitly ask for it. If start is not empty, then an empty end will use the default end-comment delimiter of newline, as otherwise, it would be impossible to end a comment. However, this is not portable, as some other m4 implementations preserve the previous non-empty delimiters instead.

define(`comment', `COMMENT')
⇒
changecom
⇒
# Not a comment anymore
⇒# Not a COMMENT anymore
changecom(`#', `')
⇒
# comment again
⇒# comment again

The comment strings can safely contain non-ASCII characters.

define(`a', `b')
⇒
«a»
⇒«b»
changecom(`«', `»')
⇒
«a»
⇒«a»

If no single character is appropriate, start and end can be of any length. Other implementations cap the delimiter length to five characters, but GNU has no inherent limit.

Comments are recognized in preference to macros. However, this is not compatible with other implementations, where macros and even quoting takes precedence over comments, so it may change in a future release. For portability, this means that start should not begin with a letter, digit, or ‘_’ (underscore), and that neither the start-quote nor the start-comment string should be a prefix of the other.

define(`hi', `HI')
⇒
define(`hi1hi2', `hello')
⇒
changecom(`q', `Q')
⇒
q hi Q hi
⇒q hi Q HI
changecom(`1', `2')
⇒
hi1hi2
⇒hello
hi 1hi2
⇒HI 1hi2

Comments are recognized in preference to argument collection. In particular, if start is a single ‘(’, then argument collection is effectively disabled. For portability with other implementations, it is a good idea to avoid ‘(’, ‘,’, and ‘)’ as the first character in start.

define(`echo', `$#:$*:$@:')
⇒
define(`hi', `HI')
⇒
changecom(`(',`)')
⇒
echo(hi)
⇒0:::(hi)
changecom
⇒
changecom(`((', `))')
⇒
echo(hi)
⇒1:HI:HI:
echo((hi))
⇒0:::((hi))
changecom(`,', `)')
⇒
echo(hi,hi)bye)
⇒1:HI,hi)bye:HI,hi)bye:
changecom
⇒
echo(hi,`,`'hi',hi)
⇒3:HI,,HI,HI:HI,,`'hi,HI:
echo(hi,`,`'hi',hi`'changecom(`,,', `hi'))
⇒3:HI,,`'hi,HI:HI,,`'hi,HI:

It is an error if the end of file occurs within a comment.

changecom(`/*', `*/')
⇒
/*dangling comment
^D
error→m4:stdin:2: ERROR: end of file in comment

Next: , Previous: , Up: Input Control   [Contents][Index]