Syntax: allow newline but not blank lines in a region

39 views
Skip to first unread message

BPJ

unread,
Nov 22, 2024, 12:28:16 PMNov 22
to vim_use
I have a syntax for a markup language where certain delimited constructs allow single newlines but not blank lines, i.e. this is OK

``` markup
{_foo
bar_}
```

but this is not OK

``` markup
{_foo

bar_}
```

is there any way to set up a region so that it behaves like this? I tried `oneline` plus a contained `syn match` which matches a newline not preceded or followed by a blank line but no luck!

I get the behavior I want with a `syn match` with this pattern

``` pattern
{_\%([^\n]\|\%(\_^\s*\)\@<!\n\%(\s*\_$\)\@!\)\{-}_}
```

but then highlighting obviosly doesn't kick in until I type the closing delimiter so I would prefer a region.

Also is there a better way to match the start or end of the file than `\_.\@<!` and `\_.\@!`?

TIA,

/bpj

Doug Kearns

unread,
Nov 22, 2024, 10:58:53 PMNov 22
to vim...@googlegroups.com
On Sat, 23 Nov 2024 at 04:28, BPJ <b...@melroch.se> wrote:
I have a syntax for a markup language where certain delimited constructs allow single newlines but not blank lines, i.e. this is OK

``` markup
{_foo
bar_}
```

but this is not OK

``` markup
{_foo

bar_}
```

is there any way to set up a region so that it behaves like this? I tried `oneline` plus a contained `syn match` which matches a newline not preceded or followed by a blank line but no luck!

I get the behavior I want with a `syn match` with this pattern

``` pattern
{_\%([^\n]\|\%(\_^\s*\)\@<!\n\%(\s*\_$\)\@!\)\{-}_}
```

Does something like this work?

:syn region foobar start="{_" end="_}" end="$" skip="\n\s*\S"
 
but then highlighting obviosly doesn't kick in until I type the closing delimiter so I would prefer a region.

Also is there a better way to match the start or end of the file than `\_.\@<!` and `\_.\@!`?

:help \%^

Regards,
Doug

BPJ

unread,
Nov 23, 2024, 8:04:46 AMNov 23
to vim_use


Den lör 23 nov. 2024 04:59Doug Kearns <dougk...@gmail.com> skrev:
On Sat, 23 Nov 2024 at 04:28, BPJ <b...@melroch.se> wrote:
I have a syntax for a markup language where certain delimited constructs allow single newlines but not blank lines, i.e. this is OK

``` markup
{_foo
bar_}
```

but this is not OK

``` markup
{_foo

bar_}
```

is there any way to set up a region so that it behaves like this? I tried `oneline` plus a contained `syn match` which matches a newline not preceded or followed by a blank line but no luck!

I get the behavior I want with a `syn match` with this pattern

``` pattern
{_\%([^\n]\|\%(\_^\s*\)\@<!\n\%(\s*\_$\)\@!\)\{-}_}
```

Does something like this work?

:syn region foobar start="{_" end="_}" end="$" skip="\n\s*\S"


I already tried that. No lunch unfortunately. I'm going with the `syn match` version which incorporates the single newline pattern. It is probably less efficient but is also probably my only choice now as I found that I also need to exclude backslash from the "normal" subpattern and skip past backslash + char lest the closing delimiter is directly preceded by a backslash.

BTW for reference the correct pattern, maybe inefficient but accurate, for a newline not preceded or followed by any blank line is

``````pattern
\%(\_^\s*\)\@<!\n\%(\s*\_$\)@!
``````

i.e. with negative lookbehind for optional horizontal whitespace and a start-of-line and the opposite negative lookahead for optional horizontal whitespace and an end-of-line.

FWIW the backslash escape pattern I used is `\\\_.`, with later Special highlighting of backslashes followed by ASCII punctuation or space/newline/return. Who said it has to be easy! :-)

 
but then highlighting obviosly doesn't kick in until I type the closing delimiter so I would prefer a region.

Also is there a better way to match the start or end of the file than `\_.\@<!` and `\_.\@!`?

:help \%^

Thanks. I still learn new things about Vim after having used it for ~20 years, which is both gratifying and frustrating!

/bpj



Regards,
Doug

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/CAJ1uvoB5GPjE1RKY4wQkRW_zawUvFxnYWDHgOdOQ8tgkMdU%2Bpw%40mail.gmail.com.

BPJ

unread,
Nov 23, 2024, 8:25:40 AMNov 23
to bpj, vim_use
I have noticed that patterns in published syntax files never use the `\v` (very magic) modifier. Is there any deeper reason than people's preferences for this? Being used to Perl patterns I practically always use it and find it hard to keep track of when to use backslashes when not using it. The result is that more or less subtle errors creep in when I'm writing a syntax file, so I'd prefer to use it unless it's a Really Bad Idea.

c.willis111

unread,
Nov 23, 2024, 8:34:23 AMNov 23
to vim...@googlegroups.com, bpj, vim_use

Hi

I deeply sympathise with you. It seems complete madness that the slashing is different for various quantifiers. (* recognised as a meta character, but not +  or ?). My reading of the help suggests that very magic doesn't cure this.

 

regards - Chris

-- 
-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups "vim_use" group.

To unsubscribe from this group and stop receiving emails from it, send an email to unsub...@googlegroups.com">vim_use+unsub...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/CADAJKhB8xGaYuvoY9-ZOu2%2BNifq4gbmP_x6TY%3Ddh-T2JkQN8%3Dw%40mail.gmail.com.
 

BPJ

unread,
Nov 23, 2024, 10:34:27 AMNov 23
to vim_use, bpj


Den lör 23 nov. 2024 14:35'c.willis111 ' via vim_use <vim...@googlegroups.com> skrev:



 

------ Original Message ------
From: b...@melroch.se
To: b...@melroch.se Cc: vim...@googlegroups.com
Sent: Saturday, November 23rd 2024, 13:25
Subject: Syntax: Using \v (very magic) in patterns in syntax files
 

I have noticed that patterns in published syntax files never use the `\v` (very magic) modifier. Is there any deeper reason than people's preferences for this? Being used to Perl patterns I practically always use it and find it hard to keep track of when to use backslashes when not using it. The result is that more or less subtle errors creep in when I'm writing a syntax file, so I'd prefer to use it unless it's a Really Bad Idea.

 

Hi

I deeply sympathise with you. It seems complete madness that the slashing is different for various quantifiers. (* recognised as a meta character, but not +  or ?). My reading of the help suggests that very magic doesn't cure this.

 


It does in that all ASCII punctuation characters can be escaped to be "normal" and those that are metacharacters are meta when not escaped. Word characters, quite sensibly do still need a backslash to be meta. That's consistent enough for me.


regards - Chris

-- 
-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unsub...@googlegroups.com">vim_use+unsub...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/CADAJKhB8xGaYuvoY9-ZOu2%2BNifq4gbmP_x6TY%3Ddh-T2JkQN8%3Dw%40mail.gmail.com.
 

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/304a56a7.2408d.193593adb00.Webtop.251%40btinternet.com.

c.willis111

unread,
Nov 23, 2024, 10:55:40 AMNov 23
to vim...@googlegroups.com, bpj

------ Original Message ------
From: b...@melroch.se

To: vim...@googlegroups.com Cc: b...@melroch.se
Sent: Saturday, November 23rd 2024, 15:34
Subject: Re: Syntax: Using \v (very magic) in patterns in syntax files
 

Den lör 23 nov. 2024 14:35'c.willis111 ' via vim_use <vim...@googlegroups.com> skrev:
 



 

------ Original Message ------
From: b...@melroch.se
To: b...@melroch.se Cc: vim...@googlegroups.com
Sent: Saturday, November 23rd 2024, 13:25
Subject: Syntax: Using \v (very magic) in patterns in syntax files
 

I have noticed that patterns in published syntax files never use the `\v` (very magic) modifier. Is there any deeper reason than people's preferences for this? Being used to Perl patterns I practically always use it and find it hard to keep track of when to use backslashes when not using it. The result is that more or less subtle errors creep in when I'm writing a syntax file, so I'd prefer to use it unless it's a Really Bad Idea.

 

Hi

I deeply sympathise with you. It seems complete madness that the slashing is different for various quantifiers. (* recognised as a meta character, but not +  or ?). My reading of the help suggests that very magic doesn't cure this.

 

It does in that all ASCII punctuation characters can be escaped to be "normal" and those that are metacharacters are meta when not escaped. Word characters, quite sensibly do still need a backslash to be meta. That's consistent enough for me.
 
You seem to be talking about the perl behaviour, which seems consistent to me. My whinge was about the vim behaviour. Heree's a help snippet:
 

4. Overview of pattern items                            pattern-overview
                                               E865 E866 E867 E869

Overview of multi items.                                /multi E61 E62
More explanation and examples below, follow the links.          E64 E871

         multi
    'magic' 'nomagic'  matches of the preceding atom
/star   *       \*      0 or more       as many as possible
/\+     \+      \+      1 or more       as many as possible
/\=     \=      \=      0 or 1          as many as possible
/\?     \?      \?      0 or 1          as many as possible

 

regards - Chris

-- 
-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unsub...@googlegroups.com">vim_use+unsub...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/CADAJKhB8xGaYuvoY9-ZOu2%2BNifq4gbmP_x6TY%3Ddh-T2JkQN8%3Dw%40mail.gmail.com.
 

 

-- 
-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups "vim_use" group.

To unsubscribe from this group and stop receiving emails from it, send an email to unsub...@googlegroups.com" target="_blank">vim_use+unsub...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/304a56a7.2408d.193593adb00.Webtop.251%40btinternet.com.
 

 

-- 
-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unsub...@googlegroups.com">vim_use+unsub...@googlegroups.com.

To view this discussion visit https://groups.google.com/d/msgid/vim_use/CADAJKhA4ztK4E-8fcDX40xn0s9hBg5v4VQ6URJmUm9E3967Jfw%40mail.gmail.com.
 

BPJ

unread,
Nov 23, 2024, 3:34:29 PMNov 23
to vim_use, bpj


Den lör 23 nov. 2024 16:56'c.willis111 ' via vim_use <vim...@googlegroups.com> skrev:



 

------ Original Message ------
From: b...@melroch.se
To: vim...@googlegroups.com Cc: b...@melroch.se
Sent: Saturday, November 23rd 2024, 15:34
Subject: Re: Syntax: Using \v (very magic) in patterns in syntax files
 



 

Den lör 23 nov. 2024 14:35'c.willis111 ' via vim_use <vim...@googlegroups.com> skrev:
 



 

------ Original Message ------
From: b...@melroch.se
To: b...@melroch.se Cc: vim...@googlegroups.com
Sent: Saturday, November 23rd 2024, 13:25
Subject: Syntax: Using \v (very magic) in patterns in syntax files
 

I have noticed that patterns in published syntax files never use the `\v` (very magic) modifier. Is there any deeper reason than people's preferences for this? Being used to Perl patterns I practically always use it and find it hard to keep track of when to use backslashes when not using it. The result is that more or less subtle errors creep in when I'm writing a syntax file, so I'd prefer to use it unless it's a Really Bad Idea.

 

Hi

I deeply sympathise with you. It seems complete madness that the slashing is different for various quantifiers. (* recognised as a meta character, but not +  or ?). My reading of the help suggests that very magic doesn't cure this.

 

 
It does in that all ASCII punctuation characters can be escaped to be "normal" and those that are metacharacters are meta when not escaped. Word characters, quite sensibly do still need a backslash to be meta. That's consistent enough for me.
 
You seem to be talking about the perl behaviour, which seems consistent to me. My whinge was about the vim behaviour. Heree's a help snippet:
That's magic. Very magic and very nomagic are distinct from magic and nomagic. They are each other's opposite and both are consistent but there are currently no options for them: you have to use the `\v` and `\V` modifiers in the pattern (or `\m` and `\M` but why? :-) You can even use them to change behavior in the middle of a pattern!

From the help:

*/\v* */\V*
Use of "\v" means that after it, all ASCII characters except '0'-'9', 'a'-'z',
'A'-'Z' and '_' have special meaning: "very magic"

Use of "\V" means that after it, only a backslash and the terminating
character (usually / or ?) have special meaning: "very nomagic"

Examples:
after:   \v    \m     \M      \V matches ~
'magic' 'nomagic'
  a    a     a      a literal 'a'
  \a    \a     \a      \a any alphabetic character
  .    .     \.      \. any character
  \.    \.     .      . literal dot
  $    $     $      \$ end-of-line
  *    *     \*      \* any number of the previous atom
  ~    ~     \~      \~ latest substitute string
  ()    \(\)     \(\)     \(\) group as an atom
  |    \|     \|      \| nothing: separates alternatives
  \\    \\     \\      \\ literal backslash
  \{    {     {      { literal curly brace


To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/3216be5b.24523.19359bc2e94.Webtop.251%40btinternet.com.

Doug Kearns

unread,
Nov 24, 2024, 1:22:44 AMNov 24
to vim...@googlegroups.com
On Sun, 24 Nov 2024 at 00:25, BPJ <b...@melroch.se> wrote:
I have noticed that patterns in published syntax files never use the `\v` (very magic) modifier. Is there any deeper reason than people's preferences for this? Being used to Perl patterns I practically always use it and find it hard to keep track of when to use backslashes when not using it. The result is that more or less subtle errors creep in when I'm writing a syntax file, so I'd prefer to use it unless it's a Really Bad Idea.


There's no reason not to use it and it appears to be employed in some of the distributed syntax files.

I generally find it harder to read, but you obviously don't, and there's never been any sort of restriction on its use.  These things are generally left up to the maintainers.

Regards,
Doug
 
Reply all
Reply to author
Forward
0 new messages