colorize: make it compatible with BSD awk

Fix the following error seen on MacOS:

/usr/bin/awk: syntax error at source line 22 source file
                header_pattern = >>>  @ <<< /^[A-Z][[:alnum:]-]+:/

The @ character in front of regular expressions to pre-compile them
seems not in the POSIX specification. Replace them with regular strings
and call match() instead of the ~ operator.

Also, adjust the url_pattern expression for BSD awk which explicitly
states:

 The awk utility is compliant with the IEEE Std 1003.1-2008 (“POSIX.1”)
 specification, except awk does not support {n,m} pattern matching.

Use [[:lower:]]+ instead of [a-z]{2,6}.

Tested with:

  GNU Awk 5.1.1
  awk version 20121220 (FreeBSD)

Link: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html
Fixes: https://todo.sr.ht/~rjarry/aerc/96
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Koni Marti <koni.marti@gmail.com>
This commit is contained in:
Robin Jarry 2022-10-21 13:58:15 +02:00
parent 8e37d16a32
commit 9d71da175a

View file

@ -23,9 +23,9 @@ BEGIN {
in_headers = 0
in_body = 0
# patterns
header_pattern = @/^[A-Z][[:alnum:]-]+:/
url_pattern = @/[a-z]{2,6}:\/\/[[:graph:]]+|(mailto:)?[[:alnum:]_\+\.~\/-]*[[:alnum:]_]@[[:lower:]][[:alnum:]\.-]*[[:lower:]]/
meta_pattern = @/^(diff --git|(new|deleted) file|similarity index|(rename|copy) (to|from)|index|---|\+\+\+) /
header_pattern = "^[A-Z][[:alnum:]-]+:"
url_pattern = "[[:lower:]]+://[[:graph:]]+|(mailto:)?[[:alnum:]_\\+\\.~/-]*[[:alnum:]_]@[[:lower:]][[:alnum:]\\.-]*[[:lower:]]"
meta_pattern = "^(diff --git|(new|deleted) file|similarity index|(rename|copy) (to|from)|index|---|\\+\\+\\+) "
}
function color_quote(line) {
level = 0
@ -50,7 +50,7 @@ function color_quote(line) {
} else {
color = quote_x
}
if (line ~ meta_pattern) {
if (match(line, meta_pattern)) {
return color quotes bold line reset
} else if (line ~ /^\+/) {
return color quotes diff_add line reset
@ -71,7 +71,7 @@ function color_quote(line) {
$0 = signature $0 reset
} else if ($0 ~ /^@@ /) {
gsub(/^@@[^@]+@@/, diff_chunk "&" reset)
} else if ($0 ~ meta_pattern) {
} else if (match($0, meta_pattern)) {
$0 = diff_meta $0 reset
} else if ($0 ~ /^\+/) {
$0 = diff_add $0 reset
@ -117,7 +117,7 @@ function color_quote(line) {
} else if ($0 ~ /^-- ?$/) {
in_signature = 1
$0 = signature $0 reset
} else if ($0 ~ header_pattern) {
} else if (match($0, header_pattern)) {
in_headers = 1
sub(header_pattern, header "&" reset)
gsub(url_pattern, url "&" reset)