Friday, March 25, 2011

Re: Bug in the behavior of &diff when executing vimdiff?

Gary Johnson wrote:

> I'm trying to configure Vim so that I can
>
> 1. choose which filetypes have syntax coloring enabled and
>
> 2. set syntax off when diffing files.
>
> Without the latter, some syntax foreground colors and diff
> background colors are such that the text is impossible to read.
>
> I've discovered when diffing two files like this,
>
> $ vimdiff foo.xml bar.xml
>
> that the value of the 'diff' option, &diff, is true when the
> ~/.vimrc is read, false when filetype pluging for the first file,
> foo.xml, is read, and true again when the filetype plugin for the
> second file, bar.xml, is read. That makes it difficult to determine
> in the filetype plugin whether syntax coloring should be on or off.
>
> I think this is a bug.
>
> The filetype doesn't matter. XML was a convenient example.
>
>
> Here is a demonstration of this behavior. I created two special
> files, ~/.vim/after/ftplugin/xml.vim:
>
> let diffdict[expand("%s")] = &diff
> if !&diff
> setlocal syntax=ON
> endif
>
> and ~/testvimrc:
>
> set nocompatible
> set noloadplugins
> let diffdict = {}
> let diffdict["vimrc"] = &diff
> filetype plugin on
> syntax manual
>
> and I copied two arbitrary XML files to my home directory and named
> them foo.xml and bar.xml. Then I diffed the two files and examined
> the diffdict dictionary.
>
> $ vimdiff -u testvimrc foo.xml bar.xml
> :echo diffdict
> {'bar.xml': 0, 'foo.xml': 1, 'vimrc': 1}
>
> Note that &diff was 0 when bar.xml was being read. Consistent with
> those results, the buffer on the left, containing foo.xml, has
> syntax coloring off while the buffer on the right, containing
> bar.xml, has syntax coloring on.
>
>
> I've worked around the problem so far by using the following test in
> my filetype plugins,
>
> if !&diff && (v:progname !~ "diff")
> setlocal syntax=ON
> endif
>
> and I may do something like this in my ~/.vimrs,
>
> let g:diff = &diff
>
> with a test of g:diff in my filetype plugins so that "vim -d" works
> as well, but it would be nice if &diff worked as it seems it should.
>
> The behavior of &diff has like this for as long as I've been trying
> to use it this way, early 7.2 if not before. The version I used for
> this test was 7.3.138, a normal version built on a Fedora 11 system.

How about this patch:

*** ../vim-7.3.143/src/diff.c 2010-09-21 16:56:29.000000000 +0200
--- src/diff.c 2011-03-25 21:30:05.000000000 +0100
***************
*** 1084,1099 ****

if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
{
- /* Pretend it was a ":split fname" command */
- eap->cmdidx = CMD_split;
- curwin->w_p_diff = TRUE;
- do_exedit(eap, old_curwin);
-
if (curwin != old_curwin) /* split must have worked */
{
/* Set 'diff', 'scrollbind' on and 'wrap' off. */
diff_win_options(curwin, TRUE);
diff_win_options(old_curwin, TRUE);
}
}
}
--- 1084,1099 ----

if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
{
if (curwin != old_curwin) /* split must have worked */
{
/* Set 'diff', 'scrollbind' on and 'wrap' off. */
diff_win_options(curwin, TRUE);
diff_win_options(old_curwin, TRUE);
+
+ /* Pretend it was a ":split fname" command */
+ eap->cmdidx = CMD_split;
+ curwin->w_p_diff = TRUE;
+ do_exedit(eap, old_curwin);
}
}
}
*** ../vim-7.3.143/src/main.c 2011-03-22 18:10:34.000000000 +0100
--- src/main.c 2011-03-25 21:32:56.000000000 +0100
***************
*** 2706,2711 ****
--- 2706,2716 ----
* happen when .vimrc contains ":sall"). */
if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
{
+ # ifdef FEAT_DIFF
+ if (params.diff_mode)
+ /* set options for "vimdiff" */
+ diff_win_options(curwin, TRUE);
+ # endif
curwin->w_arg_idx = arg_idx;
/* Edit file from arg list, if there is one. When "Quit" selected
* at the ATTENTION prompt close the window. */


--
Did Adam and Eve have navels?

/// Bram Moolenaar -- Bram@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 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

No comments:

Post a Comment