Thursday, September 17, 2015

Re: Why does synID() always return zero when inserting at the end of a line?

On Thu, Sep 17, 2015 at 11:58 PM,
Ben Fritz <fritzophrenic@gmail.com> wrote:
> On Thursday, September 17, 2015 at 2:43:46 PM UTC-5, ZyX wrote:
>> > Sure, but the region extends past this line on the next line. It's inside a /* ... */ syntax region. So Vim should be able to know what the syntax region is.
>>
>> No. There are ways to highlight past the end of line (though not with
>> :syn). Returning something other then zero means that text past the
>> end of line is highlighted according to the given syntax group which
>> was defined using :syn. This is not true.
>>
>
> OK I think I understand now. synID() doesn't mean "what syntax region is this position inside" it means "what highlighting is actively being drawn at this position". Since nothing at all is being drawn at a position past the end of the line, synID() correctly returns zero. Sound right?
>
> I'll just stick with my workaround I guess :-)
>

If you want to make it a little more precise, you might want to use
(with "trans" boolean variable as defined under :help synID() )

synID(line('.'), col('.'), trans) ? synID(line('.'), col('.'),
trans) : synID(line('.'), col('.')-1, trans)

or maybe (to avoid calling twice when in the middle of a line)

let id = synID(line('.'), col('.'), trans);
if !id | let id = synID(line('.'), col('.') - 1, trans) | endif

This might be necessary in order to get a meaningful result when in
the _first_ column.

Or on second thought, maybe use

min(col('.'), col('$') - 1))

as the "column" argument when in Insert mode (where, AFAICT, col('$')
is one past the end of the line) and not bother testing synID (What
happens if no highlight group is defined at the cursor position? Does
it return the ID of the Normal group?).


Hmm, ":help synstack()" says that

The position just after the last
character in a line and the first column in an empty line are
valid positions.

Maybe you should try and see if
synstack(line('.'), col('.'))[-1]
is or isn't better than synID() in borderline cases.


Best regards,
Tony.

--
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: