[vim/vim] setline() will clear text property (Issue #15030)

32 views
Skip to first unread message

Yggdroot

unread,
Jun 17, 2024, 1:01:58ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

Steps to reproduce

setline({lnum}, {text}) 
                Set line {lnum} of the current buffer to {text}.  To insert
                lines use append(). To set lines in another buffer use
                setbufline().  **Any text properties in {lnum} are cleared.**

As the help says: Any text properties in {lnum} are cleared.
If a plugin uses setline, the text properties defined by other plugins will be removed. This is unexpected.

I think it is reasonable that setline() does not clear text property.
The following operations have the same effect as call setline(line('.'), '')ļ¼š

  1. in normal mode
  2. type 0 to go to the first column
  3. type D

This won't remove the text property.

Another reason, setline() won't clear extmarks set by nvim_buf_set_extmark() in neovim.

Expected behaviour

setline() does not clear text property.

Version of Vim

all

Environment

all

Logs and stack traces

No response

ā€”
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030@github.com>

Christian Brabandt

unread,
Jun 17, 2024, 2:37:13ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

Well, I can only say this is done intentionally since v8.1.0690. The idea probably was that once you replace a line, all attached properties to it may no longer be valid, which I think makes sense.

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172411188@github.com>

Yggdroot

unread,
Jun 17, 2024, 3:25:53ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

Although it was intentional, I can not see the advantage of it, but lots of disadvantages. Because I can not prevent the text properties defined by my plugin from being removed by others which use setline().

I don't think it make sense that the text property is removed after call setline(line('.'), getline('.')), the text is not changed in the user's view.

What is more, as I said above, Another reason, setline() won't clear extmarks set by nvim_buf_set_extmark() in neovimļ¼Œ where extmarks in neovim is equivalent to text property in vim.

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172494956@github.com>

Shane-XB-Qian

unread,
Jun 17, 2024, 3:34:56ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

looks it make sense if the text was changed.
but I am curious you said the by D then it would be remained?

--
shane.xb.qian

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172514852@github.com>

Christian Brabandt

unread,
Jun 17, 2024, 3:39:32ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

What is more, as I said above, Another reason, setline() won't clear extmarks set by nvim_buf_set_extmark() in neovimļ¼Œ where extmarks in neovim is equivalent to text property in vim.

I don't see how this relates to Vim. We don't have this: nvim_buf_set_extmark().

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172522740@github.com>

Yggdroot

unread,
Jun 17, 2024, 3:48:04ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

looks it make sense if the text was changed. but I am curious you said the by D then it would be remained?
ā€¦
-- shane.xb.qian

call prop_type_add('aaa', {'highlight': 'Number'})
call prop_add(line('.'), 0, {'type':'aaa', 'text': 'test'})

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172538821@github.com>

Yggdroot

unread,
Jun 17, 2024, 4:10:37ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

What is more, as I said above, Another reason, setline() won't clear extmarks set by nvim_buf_set_extmark() in neovimļ¼Œ where extmarks in neovim is equivalent to text property in vim.

I don't see how this relates to Vim. We don't have this: nvim_buf_set_extmark().

I mean in neovim's point of view, setline should not remove text property just like setline does not remove extmarks.

Another reason current behavior is thought to be buggy is : setline also removes zero-width text property.

https://github.com/vim/vim/blob/c52a8560792b47a19fd0686377f588ef25e5a7d4/runtime/doc/textprop.txt#L509-L517

However, a text property that was defined as zero-width will remain, unless the whole line is deleted.

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172585236@github.com>

Christian Brabandt

unread,
Jun 17, 2024, 4:14:14ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

However, a text property that was defined as zero-width will remain, unless the whole line is deleted.

I think this is essentially what setline does. It deletes the line and replaces it with a new one, even if it is empty.

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172592194@github.com>

zeertzjq

unread,
Jun 17, 2024, 4:17:45ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

Maybe this can be made configurable as a flag of text properties?

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172599381@github.com>

Yggdroot

unread,
Jun 17, 2024, 4:29:44ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

However, a text property that was defined as zero-width will remain, unless the whole line is deleted.

I think this is essentially what setline does. It deletes the line and replaces it with a new one, even if it is empty.

Is it not the same with :
norm! 0D
then append some text ?

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172636833@github.com>

Christian Brabandt

unread,
Jun 17, 2024, 4:37:51ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

I rahter thought of making the behaviour of setline() configurable.

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172694717@github.com>

Maxim Kim

unread,
Jun 17, 2024, 4:58:37ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

I rather thought of making the behaviour of setline() configurable.

How it would solve the issue then? There still will be setline() calls that would remove text properties:

Because I can not prevent the text properties defined by my plugin from being removed by others which use setline().

I think removal of text properties as of current implementation is expected.

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172735582@github.com>

Christian Brabandt

unread,
Jun 17, 2024, 5:35:35ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

I think removal of text properties as of current implementation is expected.

Yes, that is also my expectation, see also: https://github.com/vim/vim/blob/c52a8560792b47a19fd0686377f588ef25e5a7d4/runtime/doc/textprop.txt#L534-L538

it looks like even :move drops the text properties.

There still will be setline() calls that would remove text properties

Well, would you expect the text-property to be still there after doing

:5d
:4put ='foobar'

? Once you remove the line and add a new line, how would do you know the attached text-property attached to that line is still valid?

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172872267@github.com>

Shane-XB-Qian

unread,
Jun 17, 2024, 5:39:24ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

> > looks it make sense if the text was changed. but I am curious you said the by D then it would be remained?
> > [ā€¦](#)
> > -- shane.xb.qian
>
> ```vim

> call prop_type_add('aaa', {'highlight': 'Number'})
> call prop_add(line('.'), 0, {'type':'aaa', 'text': 'test'})
> ```

not sure why `D` was special..
// if delete by :delete, then it was removed too.

--
shane.xb.qian

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172879716@github.com>

Yggdroot

unread,
Jun 17, 2024, 5:43:38ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

looks it make sense if the text was changed. but I am curious you said the by D then it would be remained? > ā€¦ > -- shane.xb.qian vim call prop_type_add('aaa', {'highlight': 'Number'}) call prop_add(line('.'), 0, {'type':'aaa', 'text': 'test'})

not sure why D was special.. // if delete by :delete, then it was removed too.
ā€¦
-- shane.xb.qian

It is different.
:delete is the same as dd, not D

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172901087@github.com>

Shane-XB-Qian

unread,
Jun 17, 2024, 5:50:51ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

yes, it is different, but the answer maybe as @chrisbra commented above, to D maybe it was intended to make it happy with instant edit. šŸ¤·

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172919733@github.com>

Yggdroot

unread,
Jun 17, 2024, 5:56:59ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

I think removal of text properties as of current implementation is expected.

Yes, that is also my expectation, see also:

https://github.com/vim/vim/blob/c52a8560792b47a19fd0686377f588ef25e5a7d4/runtime/doc/textprop.txt#L534-L538

it looks like even :move drops the text properties.

There still will be setline() calls that would remove text properties

Well, would you expect the text-property to be still there after doing

:5d
:4put ='foobar'

? Once you remove the line and add a new line, how would do you know the attached text-property attached to that line is still valid?

Actually, my expectation is :
for example,

call prop_type_add('aaa', {'highlight': 'Number'})
call prop_add(line('.'), 0, {'type':'aaa', 'text': 'test'})

call setline(line('.'), '') has the same effect as norm! 0D.

call prop_type_add('aaa', {'highlight': 'Number'})
call prop_add(line('.'), 1, {'type':'aaa', 'length': 0})

call setline(line('.'), '') has the same effect as norm! 0D.

Because However, a text property that was defined as zero-width will remain, unless the whole line is deleted.
I think setline is not delete line 3 and insert a new line after line 2, it is replacing line 3 with new text. Am I right?

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172931661@github.com>

Shane-XB-Qian

unread,
Jun 17, 2024, 6:04:22ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

unless the whole line is deleted it is D

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172947883@github.com>

Shane-XB-Qian

unread,
Jun 17, 2024, 6:06:05ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

unless the whole line is deleted it was said for D šŸ˜…

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172951486@github.com>

Yggdroot

unread,
Jun 17, 2024, 6:07:31ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

unless the whole line is deleted it was said for D šŸ˜…

I think it's dd

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172954883@github.com>

Shane-XB-Qian

unread,
Jun 17, 2024, 6:08:40ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

i meant that statement was expressed why D was special.

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2172957237@github.com>

Christian Brabandt

unread,
Jun 17, 2024, 6:48:46ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

I posted the link to the documentation already. It's simply not possible to retain the text property, because how should Vim know where you added some text?

call setline(1, 'You are a hero!')
cal prop_type_add('Highlight', #{highlight: 'Search'})
call prop_add(1, 11, {'end_col': 15, 'type': 'Highlight'})
" doesn't work, but would add an off by one error
call getline(1)->substitute('\<a\>', 'my', 'g')->setline(1) 

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2173062152@github.com>

Yggdroot

unread,
Jun 17, 2024, 8:01:05ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

Notice that my example is special, one is virtual text, the column is 0, another is 0-width.

call prop_type_add('aaa', {'highlight': 'Number'})
call prop_add(line('.'), 0, {'type':'aaa', 'text': 'test'})
call prop_type_add('aaa', {'highlight': 'Number'})
call prop_add(line('.'), 1, {'type':'aaa', 'length': 0})

:call setline(line('.'), '') will remove the virtual text, :norm! 0D won't.
Can we make their behavior the same?

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2173197589@github.com>

Maxim Kim

unread,
Jun 17, 2024, 8:08:12ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

:call setline(line('.'), '') will remove the virtual text, :norm! 0D won't.
Can we make their behavior the same?

If properties wouldn't be removed, then a lot of things would need to be changed internally...
You have a text property, then you changed a line and now it is invalid.

image.png (view on web)

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2173225532@github.com>

Maxim Kim

unread,
Jun 17, 2024, 8:11:41ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

Now if the case is virtual text at the edges of the line -- it could be safe to preserve it, although I am not sure it worth the complexity it brings.

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2173233130@github.com>

Shane-XB-Qian

unread,
Jun 17, 2024, 8:46:07ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

> Can we make their behavior the same?

the 'question' had been answered above.

--
shane.xb.qian

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2173306000@github.com>

Yggdroot

unread,
Jun 17, 2024, 8:48:29ā€ÆAMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

Can we make their behavior the same?
the 'question' had been answered above.
ā€¦
-- shane.xb.qian

I don't think so.

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2173310394@github.com>

bfrg

unread,
Jun 17, 2024, 5:11:14ā€ÆPMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

I would also expect setline() and setbufline() to remove any text-properties.

I think what you want is something like subbufline() that substitutes text in a line while preserving text-properties in that line, similar to the :substitute command. There's already an open feature request for #5632. Bram wasn't opposed to this idea, it just needs to be implemented. šŸ˜„

ā€”
Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2174430078@github.com>

Yggdroot

unread,
Jun 17, 2024, 9:52:28ā€ÆPMĀ (9 days ago)Ā Jun 17
to vim/vim, Subscribed

In my view, :call setline(line('.'), '') and :norm! 0D do the same thing, why :norm! 0D don't remove the virtual-text column starts from 0 and the 0-width text property?

ā€”
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2174760568@github.com>

Christian Brabandt

unread,
Jun 18, 2024, 8:03:27ā€ÆAMĀ (8 days ago)Ā Jun 18
to vim/vim, Subscribed

The effect might be the same, but it is not the same, it's essentially a :d followed by a :put.
There is also listener_add() not sure, if one can use it to save and restore text-properties.

The current behaviour is well documented, so let's close this as a duplicate of #5632 then.

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15030/2175934090@github.com>

Christian Brabandt

unread,
Jun 18, 2024, 8:03:27ā€ÆAMĀ (8 days ago)Ā Jun 18
to vim/vim, Subscribed

Closed #15030 as not planned.

ā€”
Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/15030/issue_event/13200071849@github.com>

Reply all
Reply to author
Forward
0 new messages