Improve c-family highlighting of preprocessor directives

The current handling of preprocessor directives in filetype/c-family.kak
leads to a wall of solid colour for more complicated #if or #define
directives, although #include is already nicely highlighted.

Instead of highlighting an entire directive with the meta face, highlight
just the #define, #if or #elif keyword as meta, treating the rest of the
directive as normal c-family expressions. This significantly improves
the readability of complex macro definitions.

For directives other than #define, #if and #elif, we treat the rest of
the directive as an opaque string in normal face rather than trying to
highlight it, covering cases like #error, #pragma, etc. where the rest
of the line is an error message string or other non-expression content.

This does the right thing for #ifdef and #ifndef too, as we don't highlight
identifiers in c-family text so their arguments should be normal face
anyway.
This commit is contained in:
Chris Webb 2023-12-10 13:50:29 +00:00
parent 7f49395cf9
commit 86979841b7

View File

@ -213,10 +213,10 @@ evaluate-commands %sh{
add-highlighter shared/$ft/comment region /\\* \\*/ fill comment add-highlighter shared/$ft/comment region /\\* \\*/ fill comment
add-highlighter shared/$ft/line_comment region // (?<!\\\\)(?=\\n) fill comment add-highlighter shared/$ft/line_comment region // (?<!\\\\)(?=\\n) fill comment
add-highlighter shared/$ft/disabled region -recurse "#\\h*if(?:def)?" ^\\h*?#\\h*if\\h+(?:0|FALSE)\\b "#\\h*(?:else|elif|endif)" fill comment add-highlighter shared/$ft/disabled region -recurse "#\\h*if(?:def)?" ^\\h*?#\\h*if\\h+(?:0|FALSE)\\b "#\\h*(?:else|elif|endif)" fill comment
add-highlighter shared/$ft/macro region %{^\\h*?\\K#} %{(?<!\\\\)(?=\\n)|(?=//)} group add-highlighter shared/$ft/ifdef region %{^\\h*\\K#\\h*(?:define|elif|if)(?=\\h)} %{(?<!\\\\)(?=\\s)|(?=//)} fill meta
add-highlighter shared/$ft/macro region %{^\\h*#} %{(?<!\\\\)(?=\\n)|(?=//)} group
add-highlighter shared/$ft/macro/ fill meta add-highlighter shared/$ft/macro/ regex ^\\h*(#\\h*\\S*) 1:meta
add-highlighter shared/$ft/macro/ regex ^\\h*#\\h*include\\h+(\\S*) 1:module add-highlighter shared/$ft/macro/ regex ^\\h*#\\h*include\\h+(<[^>]*>|"(?:[^"\\\\]|\\\\.)*") 1:module
add-highlighter shared/$ft/macro/ regex /\\*.*?\\*/ 0:comment add-highlighter shared/$ft/macro/ regex /\\*.*?\\*/ 0:comment
EOF EOF
done done