假设我定义了一个 m4 宏 FOOBAR,其中包含一段任意文本,其中可能包含逗号字符。请创建一个 m4 宏以将 5 个空格的左边距添加到 FOOBAR 文本的扩展中。
如果文本不包含任何逗号,问题很简单(只需使用带有“^”正则表达式的 patsubst)。但我需要处理逗号。
谢谢!
假设我定义了一个 m4 宏 FOOBAR,其中包含一段任意文本,其中可能包含逗号字符。请创建一个 m4 宏以将 5 个空格的左边距添加到 FOOBAR 文本的扩展中。
如果文本不包含任何逗号,问题很简单(只需使用带有“^”正则表达式的 patsubst)。但我需要处理逗号。
谢谢!
需要打磨,但这是一个开始:
m4_divert(-1)
I changed the m4 quoting to use [<{}>]
m4_define(_spaces,[<{m4_format([<{%}>]$1[<{s}>],)}>])
m4_define(_indent,[<{m4_patsubst($1,[<{^}>],_spaces($2))}>])
m4_define(_Comma_and_Paren_Safe,[<{m4_patsubst([<{[<{Some. arbitrary text,with unbalanced parentheses like:
((()((((
and several commas ,,,, and a few, more , of those
commas,,.
}>]}>],[<{,}>],[<{[<{,}>]}>])}>])
m4_divert(0)m4_dnl
_indent([<{_Comma_and_Paren_Safe}>],7)
示例运行(_m42 是简单的 m4 包装脚本):
$ _m42 < ~/tmp/2v.STDIN.m4-margin.SatMay16.2052.190
Some. arbitrary text,with unbalanced parentheses like:
((()((((
and several commas ,,,, and a few, more , of those
commas,,.
比以前的尝试更简单:保存到宏 FOOBAR - 嵌套在四引号内的任意文本段落以保护逗号和不平衡的括号:
m4_divert(-1)
m4_define(_spaces,[<{m4_format([<{%}>]$1[<{s}>],)}>])
m4_define(_indent,[<{m4_patsubst($1,[<{^}>],_spaces($2))}>])
--
Simpler than prior attempt: save arbitrary text paragraph nested inside
quadruple quote to protect commas, and unbalanced parentheses:
m4_define(FOOBAR,[<{[<{[<{[<{Some. arbitrary text,with unbalanced parentheses like:
((()((((
and several commas ,,,, and a few, more , of those
commas,,.
}>]}>] }>]}>])
m4_divert(0)m4_dnl
_indent(FOOBAR,15)
示例运行:
$ _m42 < ~/tmp/2v.STDIN.m4-margin.SatMay16.2052.190
Some. arbitrary text,with unbalanced parentheses like:
((()((((
and several commas ,,,, and a few, more , of those
commas,,.