1
votes

When I reply to a mail which contains a patch I would like to apply syntax highlighting after skipping the "> " quote. Specifically, given a quoted patch like this:

> ---
>  fs/kernfs/dir.c        |  195 ++++++++++++++++++++++++++++++++++++++++++------
>  include/linux/kernfs.h |    3 +
>  2 files changed, 177 insertions(+), 21 deletions(-)
> 
> diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
> index 91e0045..dba0d42 100644
> --- a/fs/kernfs/dir.c
> +++ b/fs/kernfs/dir.c
> @@ -44,28 +44,159 @@ static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
>   return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
>  }
>  
> -static char * __must_check kernfs_path_locked(struct kernfs_node *kn, char *buf,
> -                       size_t buflen)
> +/**
> + * kernfs_node_depth - compute depth of the kernfs node from root.
> + * The root node itself is considered to be at depth 0.
> + */
> +static size_t kernfs_node_depth(struct kernfs_node *kn)
>  {
> - char *p = buf + buflen;
> + size_t depth = 0;
> +
> + BUG_ON(!kn);
> + while (kn->parent) {
> +     depth++;
> +     kn = kn->parent;
> + }
> + return depth;
> +}
> +

I want a way to tell neovim to ignore "> " at the beginning of the lines and treat the rest as a patch.

1

1 Answers

0
votes

Vim's current syntax highlighting design cannot easily handle this; matches and regions often are anchored to the start of line (^). With the email quoting prefixes, those characters would have to be ignored; as far as the (e.g. diff) syntax is concerned, a ^ match should start at column 3, i.e. after the > prefix that has nothing to do with diffs.

If each syntax plugin would support an optional b:syntax_prefix instead of hard-coding ^, the email syntax could set that variable to ^\%(>\s*\)\+ and then :syntax include the (diff) syntax. As there are hundreds of supported syntaxes that people may want to prefix and include, this is unlikely to happen. Also, it would make the syntax plugins more complex (as :execute would have to be used to interpolate that variable into otherwise static patterns).

Alternatives

If you change the reply style to drop the > prefixes (or just write mappings that let you drop / restore those), embedded fragments (especially those like diffs that can be detected easily through its characteristic header) can be automatically highlighting in their syntax, through my SyntaxRange plugin. The following configuration (put into ~/.vim/after/syntax/mail/SyntaxInclude.vim) does this for me:

" mail/SyntaxInclude.vim: Include other filetype syntaxes in detected regions.
"
" INSTALL:
"   Put this into .vim/after/syntax/mail/; this way, this is sourced after the
"   core mail syntax.
"   It cannot be in .vim/ftplugin/, because these are sourced _before_ the
"   syntax files, and setting 'syntax' will clear all previous syntax
"   definitions through the functionality in $VIMRUNTIME/syntax/synload.vim,
"   function s:SynSet().
"
" DEPENDENCIES:
"   - SyntaxRange.vim autoload script
"
" Copyright: (C) 2012 Ingo Karkat
"   The VIM LICENSE applies to this script; see ':help copyright'.
"
" Maintainer:   Ingo Karkat <[email protected]>
"
" REVISION  DATE        REMARKS
"   001 05-Jul-2012 file creation

" Include diff syntax for inline diffs.
call SyntaxRange#IncludeEx('start="^changeset\|^Index: \|^diff \|^--- .*\%( ----\)\@<!$" skip="^[-+@    ]" end="^$"', 'diff')

" vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax :