包含文件并缩进每一行

include file and indent every line

我想包含一个文件并缩进所有行。我希望它成为降价文档中的代码块。

基本上我想要这样的东西:

text
include(somefile)
text

得到这个输出:

text
    content
    from
    somefile
text

我浏览了manual and found patsubst. I also found this question and answer:

这适用于不包含逗号的文件:

$ cat textnocomma 
some text.
with sentences.
and no commas.

$ cat patsubstincludenocomma
text
patsubst(include(textnocomma), `^', `    ')
text

$ m4 patsubstincludenocomma
text
    some text.
    with sentences.
    and no commas.

text

但是当我 include 一个包含逗号的文件时:

$ cat textcomma 
some text.
with sentences.
and, commas.

$ cat patsubstincludecomma
text
patsubst(include(textcomma), `^', `    ')
text

$ m4 patsubstincludecomma
text
m4:patsubstincludecommanoquote:3: Warning: excess arguments to builtin `patsubst' ignored
some text.
with sentences.
and
text

问题似乎是 m4 进行宏扩展的幼稚方式。包含文本中的逗号被解释为 patsubst 宏的语法。解决方案(应该)很简单:引用包含的文本。

但是如果我引用 include 那么只有第一行缩进:

$ cat patsubstincludecommaquote 
text
patsubst(`include(textcomma)', `^', `    ')
text

$ m4 patsubstincludecommaquote
text
    some text.
with sentences.
and, commas.

text

我尝试了不同的引号和文字换行符组合,而不是正则表达式换行符。但到目前为止,我得到的只是 excess arguments 错误消息,或者只是缩进的第一行。

如何使用逗号和其他 m4 语法包含文本,并在 m4 中缩进?

为什么不用外部命令?

esyscmd(`sed "s,^,    ," textcomma')

我做了一些研究,可以得出结论,引用 include 会使 patsubst 除了第一行之外不再识别文本中的 ^(行首)。至少在我的系统上。

$ m4 --version
m4 (GNU M4) 1.4.18
...

观察:

$ cat textnocomma 
some text.
with sentences.
and no commas.

$ cat includenocomma 
foo
patsubst(include(textnocomma), `^', `    ')
bar
patsubst(`include(textnocomma)', `^', `    ')
baz

$ m4 includenocomma 
foo
    some text.
    with sentences.
    and no commas.

bar
    some text.
with sentences.
and no commas.

baz

将文本定义为 "string literals" 而不是 include 时也会发生这种情况:

$ cat definestringliterals 
define(`sometext', `first line
second line
third line')dnl
foo
patsubst(sometext, `^', `    ')
bar
patsubst(`sometext', `^', `    ')
baz

$ m4 definestringliterals 
foo
    first line
    second line
    third line
bar
    first line
second line
third line
baz

这里有一个问题和答案支持这个观察:How to match newlines in GNU M4 _properly_

奇怪的是,如果将字符串文字直接放在 patsubst:

中,则不会发生这种情况
$ cat patsubststringliterals 
foo
patsubst(first line
second line
third line, `^', `    ')
bar
patsubst(`first line
second line
third line', `^', `    ')
baz

$ m4 patsubststringliterals 
foo
    first line
    second line
    third line
bar
    first line
    second line
    third line
baz

使用parens to "quote"文本没有这个问题。但现在我的文字周围有括号:

$ cat textcomma 
some text.
with sentences.
and, commas.

$ cat includecomma 
foo
patsubst(include(textcomma), `^', `    ')
bar
patsubst(`include(textcomma)', `^', `    ')
baz
patsubst((include(textcomma)), `^', `    ')
qux

$ m4 includecomma 
foo
m4:includecomma:2: Warning: excess arguments to builtin `patsubst' ignored
some text.
with sentences.
and
bar
    some text.
with sentences.
and, commas.

baz
    (some text.
    with sentences.
    and, commas.
    )
qux

所以我猜这是一个错误。如果引用 include 那么 patsubst 将不再识别除第一行之外的 ^ 。但是引用 include 是必要的,以防止文本中的逗号被解释为语法。