[vim/vim] Add 'diffbuf' option to only compare with a single buffer for highlighting (#3527)

96 views
Skip to first unread message

flarn2006

unread,
Oct 9, 2018, 10:25:58 PM10/9/18
to vim/vim, Subscribed

I needed this feature at work today, and it seemed like it would be simple enough to add, so after I got home I decided to give it a shot.

Basically, what this does is add a new option called diffbuf (short name dbuf) which defaults to 0 (meaning standard behavior) but can also be set to a buffer number. If it's set to a valid buffer number, then instead of highlighting all the lines that any buffer has different, it will only highlight lines that that specific buffer has different. That is, for the purpose of highlighting lines, it will be as if the buffer whose lines are to be highlighted and the buffer specified by diffbuf are the only buffers open; others won't affect it, beyond adding filler lines if that option is turned on.

Lines inside the buffer specified by diffbuf will be highlighted as normal.

This is my first contribution to this project—or any project as popular as Vim, for that matter—so please forgive me if I did anything incorrectly.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/3527

Commit Summary

  • feat: add 'diffbuf' option (does nothing yet)
  • feat: implement diffbuf option

File Changes

Patch Links:


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub

flarn2006

unread,
Oct 10, 2018, 12:56:22 AM10/10/18
to vim/vim, Push

@flarn2006 pushed 1 commit.

  • 6b0e40a test: add 'diffbuf' to expected options output


You are receiving this because you are subscribed to this thread.

View it on GitHub

h_east

unread,
Oct 10, 2018, 3:23:16 AM10/10/18
to vim_dev
Hi,

I think Vim already has what you want to do.
See the following document.
:h diffthis

Example:
- Start Vim without specifying a file name.
$ vim
- Edit the buffer appropriately. Here we set numbers on each line.
:call setline(1, range(0,9))
- Open new buffer in split vertically
:vnew
- Edit the buffer appropriately.
:call setline(1, range(3,6))
- Highlight difference between two buffers.
:windo diffthis


--
Best regards,
Hirohito Higashi (h_east)

K.Takata

unread,
Oct 10, 2018, 6:06:06 AM10/10/18
to vim/vim, Subscribed

flarn2006

unread,
Oct 10, 2018, 9:58:20 AM10/10/18
to vim/vim, Push

@flarn2006 pushed 2 commits.

  • 1ed2849 style: make indentation consistent
  • 99d1576 fix: move var declarations to beginning


You are receiving this because you are subscribed to this thread.

View it on GitHub

flarn2006

unread,
Oct 10, 2018, 11:54:17 AM10/10/18
to vim/vim, Subscribed

@k-takata, I replied to you on Google Groups; just making sure you saw.

Also, I noticed a case just now where it isn't working properly. But I'm at work so I'll look into fixing that when I get home.

Michael Norton

unread,
Oct 10, 2018, 2:36:05 PM10/10/18
to vim_dev
I don't think that really does what this does. To clarify, my option is only significant when more than two diff buffers are open. Are you sure you're understanding what it does?

Download https://flarn2006.dyndns.org/files/difftest.zip and extract it somewhere, then run "vim -O difftest?.txt". Do ":windo diffthis", and you'll see that the same two lines are highlighted in each buffer, because each of those lines differs in at least one of them. Now let's say you only want to see what lines differ in the first one. Type ":set diffbuf=1" and you'll see that now, in each buffer (except the first), only the lines that are different in the first buffer are highlighted. If a line only differs in some other buffer, it won't care. It's like only having diff turned on for two of the buffers, except you can see the comparison with the master buffer on all of them at once.

Bram Moolenaar

unread,
Oct 10, 2018, 4:27:37 PM10/10/18
to vim/vim, Subscribed

Please add a screenshot or two, so that we can see how this works. Also, please add the help for this option, which a good explanation (can't add a screenshot in the help).

flarn2006

unread,
Oct 10, 2018, 7:49:07 PM10/10/18
to vim/vim, Subscribed

@brammool Will do. I'll also take a look at the part where it wasn't working and try and fix that.

Gary Johnson

unread,
Oct 10, 2018, 8:24:10 PM10/10/18
to vim...@googlegroups.com
On 2018-10-10, flarn2006 wrote:
> @brammool Will do. I'll also take a look at the part where it wasn't working
> and try and fix that.

If this turns out to be something useful and worth including in Vim,
I think we really need to come up with some better method of
invoking it than setting the value of an option to a buffer number.

I don't understand what this does well enough to suggest a name, but
I think some command like :diffref that sets the current buffer as
the reference buffer, or whatever 'diffbuf' means, would be much
better than requiring the user to figure out the number of the
buffer of interest, then setting some option to it. Further, an
option value will hang around and confuse the user the next time
they execute :diffthis.

Regards,
Gary

flarn2006

unread,
Oct 10, 2018, 10:05:47 PM10/10/18
to vim/vim, Subscribed

Screenshots added. Will add help next.

Also, Gary Johnson posted in the Google Group:

If this turns out to be something useful and worth including in Vim,
I think we really need to come up with some better method of
invoking it than setting the value of an option to a buffer number.

I don't understand what this does well enough to suggest a name, but
I think some command like :diffref that sets the current buffer as
the reference buffer, or whatever 'diffbuf' means, would be much
better than requiring the user to figure out the number of the
buffer of interest, then setting some option to it. Further, an
option value will hang around and confuse the user the next time
they execute :diffthis.

That's a good point. Maybe keep the option though, and just add a command to set it to the current buffer, because potentially it can also be extended in the future to support a list of multiple buffers to compare against.

Actually, I think I'm going to change it to be a per-buffer option, diffignore/nodiffignore or something, defaulting to nodiffignore. Or alternatively diffref/nodiffref, defaulting to diffref.

Gary Johnson

unread,
Oct 11, 2018, 12:07:15 AM10/11/18
to vim...@googlegroups.com
On 2018-10-10, flarn2006 wrote:
> Screenshots added. Will add help next.

Screenshots are added where? Many people, including me, are
following this discussion on the vim_dev email list, where it
belongs. You don't need to send the screenshots to the list, but
URLs would be nice.

Regards,
Gary

Michael Norton

unread,
Oct 11, 2018, 2:23:24 AM10/11/18
to vim...@googlegroups.com

--
--
You received this message from the "vim_dev" 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 a topic in the Google Groups "vim_dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_dev/9CwgjallJDg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Brabandt

unread,
Oct 11, 2018, 7:06:19 AM10/11/18
to vim...@googlegroups.com
I think I understand now. Maybe a ignore suboption for the diffoption setting that takes a buffer name as pattern?
You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+u...@googlegroups.com.

Bram Moolenaar

unread,
Oct 11, 2018, 10:42:47 AM10/11/18
to vim/vim, Subscribed

> Screenshots added. Will add help next.

Thanks, that makes it much clearer.


> Also, Gary Johnson posted in the Google Group:
>
> > If this turns out to be something useful and worth including in Vim,
> > I think we really need to come up with some better method of
> > invoking it than setting the value of an option to a buffer number.
> >
> > I don't understand what this does well enough to suggest a name, but
> > I think some command like :diffref that sets the current buffer as
> > the reference buffer, or whatever 'diffbuf' means, would be much
> > better than requiring the user to figure out the number of the
> > buffer of interest, then setting some option to it. Further, an
> > option value will hang around and confuse the user the next time
> > they execute :diffthis.
>
> That's a good point. Maybe keep the option though, and just add a command to set it to the current buffer, because potentially it can also be extended in the future to support a list of multiple buffers to compare against.
>
> Actually, I think I'm going to change it to be a per-buffer option,
> `diffignore`/`nodiffignore` or something, defaulting to
> `nodiffignore`. Or alternatively `diffref`/`nodiffref`, defaulting to
> `diffref`.

Not sure if this is useful. I think what most users will want is
setting one buffer as the one to diff all other buffers against. For
example, you have the original file, your version and the version you
need to merge in.

How about adding a buffer-local option "diffmaster" that can be set.
You set it on the original file, so you get your changes highlighted and
you get the merge changes highlighted, but not the difference between
your changes and the merge changes.

When you switch it on, it will be switched off in all other buffers in
the current tab. There can only be one master!

--
A bad peace is better than a good war. - Yiddish Proverb

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

flarn2006

unread,
Oct 11, 2018, 11:12:40 AM10/11/18
to vim/vim, Subscribed

Sure, so basically diffref from my suggestion above only it automatically turns off on all other other buffers when you set it on one?

I'll create a separate pull request when I'm ready; let me know if that sounds good.

Gary Johnson

unread,
Oct 11, 2018, 12:41:38 PM10/11/18
to reply+00b1d198b3bf7eea41731e3f0ccc816d9d8877d...@reply.github.com, vim...@googlegroups.com
On 2018-10-11, flarn2006 wrote:
> Sure, so basically diffref from my suggestion above only it automatically turns
> off on all other other buffers when you set it on one?
>
> I'll create a separate pull request when I'm ready; let me know if that sounds
> good.

So the value in all buffers is initially one, but [re]setting it to
one in one buffer seta all the others to zero? Can we think about
this a little more? Maybe a command to clear (i.e., set to zero)
the variable/option in all buffers but the current one?

Also, shouldn't this be per window rather than per buffer?

Regards,
Gary

vim-dev ML

unread,
Oct 11, 2018, 12:42:07 PM10/11/18
to vim/vim, vim-dev ML, Your activity

Houl

unread,
Oct 11, 2018, 1:55:25 PM10/11/18
to vim/vim, vim-dev ML, Comment

Reminds me of an old feature request: Humble feature request: 3way-merge diff mode .


You are receiving this because you commented.

Bram Moolenaar

unread,
Oct 11, 2018, 4:02:38 PM10/11/18
to vim/vim, vim-dev ML, Comment

> On 2018-10-11, flarn2006 wrote:
> > Sure, so basically diffref from my suggestion above only it automatically turns
> > off on all other other buffers when you set it on one?
> >
> > I'll create a separate pull request when I'm ready; let me know if that sounds
> > good.
>
> So the value in all buffers is initially one, but [re]setting it to
> one in one buffer seta all the others to zero? Can we think about
> this a little more? Maybe a command to clear (i.e., set to zero)
> the variable/option in all buffers but the current one?
>
> Also, shouldn't this be per window rather than per buffer?

Since 'diff' is per window, yes, this option would also be per window.

'diffmaster' would be default off, and it can be on for one window only.

--
A computer program does what you tell it to do, not what you want it to do.


/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///


You are receiving this because you commented.

flarn2006

unread,
Oct 11, 2018, 4:33:02 PM10/11/18
to vim/vim, vim-dev ML, Comment

How about I do it like I said before, calling it diffref instead of diffmaster and having it be on by default and settable on multiple buffers at once, but then I also add diffmaster as a command that turns it on for the current buffer and off for all others? That way it's just as easy as before to use it for that most common use case of only comparing with a single buffer, but at the same time if someone does ever get the need to turn it on for multiple (but not all) buffers, they can do that too? From my understanding of the way it's coded, it would be no more difficult than only allowing a single master (actually less difficult because I won't need to have it check other buffers when you set the variable) so it would be a shame to explicitly remove that functionality if there's another way to get the ease of use it would bring.

Also you know the code a lot more than I do, but I'm pretty sure it actually would be a buffer option, not a window option. It's the buffer that it compares against. If two windows have the same buffer, what difference would it make which of the two is set as the master? The diff option is window-specific because it's specific to the way that window is treated, but the diffref or diffmaster option, whatever it's called, doesn't really involve the window; it just tells other windows whether or not to reference the buffer inside.


You are receiving this because you commented.

Gary Johnson

unread,
Oct 11, 2018, 5:53:59 PM10/11/18
to reply+00b1d1981ca2ecb24b2f519100327ed64df6be0...@reply.github.com, vim...@googlegroups.com
On 2018-10-11, flarn2006 wrote:

> Also you know the code a lot more than I do, but I'm pretty sure it actually
> would be a buffer option, not a window option. It's the buffer that it compares
> against. If two windows have the same buffer, what difference would it make
> which of the two is set as the master? The diff option is window-specific
> because it's specific to the way that window is treated, but the diffref or
> diffmaster option, whatever it's called, doesn't really involve the window; it
> just tells other windows whether or not to reference the buffer inside.

Consider, for example, the case of one buffer being opened in two
windows, each window in a different tab. Each of those windows can
be diffed against any other window(s) in the same tab. A buffer
could be displayed in the master window in one tab and in
a non-master window in another tab.

Regards,
Gary

vim-dev ML

unread,
Oct 11, 2018, 5:54:28 PM10/11/18
to vim/vim, vim-dev ML, Your activity

flarn2006

unread,
Oct 11, 2018, 6:55:02 PM10/11/18
to vim/vim, vim-dev ML, Comment

Ah, good point. Window option it is. I'm going to close this pull request and open a new one when it's ready.


You are receiving this because you commented.

flarn2006

unread,
Oct 11, 2018, 6:55:04 PM10/11/18
to vim/vim, vim-dev ML, Comment

Closed #3527.


You are receiving this because you commented.

flarn2006

unread,
Oct 11, 2018, 7:04:11 PM10/11/18
to vim/vim, vim-dev ML, Comment

Since I'm not going to use this code (and shouldn't have used the master branch for it) I'm going to change history in my fork and put the master branch back to the official one. I've created a separate branch in my fork, pullreq3527, to hold these changes that aren't going to be use, since I'm not sure how this will affect the pull request.


You are receiving this because you commented.

Bram Moolenaar

unread,
Oct 12, 2018, 3:48:55 PM10/12/18
to vim/vim, vim-dev ML, Comment

> How about I do it like I said before, calling it `diffref` instead of
> `diffmaster` and having it be on by default and settable on multiple
> buffers at once, but then I also add `diffmaster` as a command that
> turns it on for the current buffer and off for all others? That way it's
> just as easy as before to use it for that most common use case of only
> comparing with a single buffer, but at the same time if someone does
> ever get the need to turn it on for multiple (but not all) buffers, they
> can do that too? From my understanding of the way it's coded, it would
> be no more difficult than only allowing a single master (actually less
> difficult because I won't need to have it check other buffers when you
> set the variable) so it would be a shame to explicitly remove that
> functionality if there's another way to get the ease of use it would
> bring.

I have a hard time imagining how it would be used, have 'diffref' in
one window/buffer to be different from another one. What are you
looking at then?


> Also you know the code a lot more than I do, but I'm pretty sure it
> actually would be a buffer option, not a window option. It's the
> buffer that it compares against. If two windows have the same buffer,
> what difference would it make which of the two is set as the master?
> The `diff` option is window-specific because it's specific to the way
> that window is treated, but the `diffref` or `diffmaster` option,
> whatever it's called, doesn't really involve the window; it just tells
> other windows whether or not to reference the buffer inside.

One of the most common things to do is the disable 'diff' and switch it
on in another window. What would then happen with the buffer-local
option? Having 'diffref' set in a buffer that doesn't appear in any
window with 'diff' set is like a misconfiguration. We should try to
avoid that from happening. Also, setting it for a hidden buffer doesn't
make sense.

It's true that setting the option really means to use the buffer in that
window as the master. I don't see a problem with that.

Gary's remark also mentions a situation where a window-local option is
needed.

--
hundred-and-one symptoms of being an internet addict:
190. You quickly hand over your wallet, leather jacket, and car keys
during a mugging, then proceed to beat the crap out of your
assailant when he asks for your laptop.


/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Michael Norton

unread,
Oct 12, 2018, 5:35:45 PM10/12/18
to vim...@googlegroups.com, reply+00b1d1985d55b629bc0e08301c42246e5585ace...@reply.github.com
I actually did implement it as a window-local option; see #3535. Let me know if that looks good. :)

--

vim-dev ML

unread,
Oct 12, 2018, 5:36:16 PM10/12/18
to vim/vim, vim-dev ML, Your activity
> <https://github.com/vim/vim/pull/3527#issuecomment-429442676>

>
> --
> --
> You received this message from the "vim_dev" 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 a topic in the
> Google Groups "vim_dev" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/vim_dev/9CwgjallJDg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> vim_dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>


You are receiving this because you are subscribed to this thread.

Reply all
Reply to author
Forward
0 new messages