Bug in the behavior of &diff when executing vimdiff?

45 views
Skip to first unread message

Gary Johnson

unread,
Mar 25, 2011, 2:21:47 PM3/25/11
to vim...@googlegroups.com
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.

Regards,
Gary

Ben Fritz

unread,
Mar 25, 2011, 2:52:42 PM3/25/11
to vim_use


On Mar 25, 1:21 pm, Gary Johnson <garyj...@spocom.com> 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'm curious why you don't just go with the (to me) obvious solution of
tweaking your color scheme to make diff view readable.

Gary Johnson

unread,
Mar 25, 2011, 4:10:21 PM3/25/11
to vim_use

Because syntax highlighting conveys information about the elements
and structure of a file. The way I read a file changes depending on
whether syntax is on or off. When it is off, I _read_ the file.
When it is on, I use the colors and the patterns of colors to
infer meaning.

For example, when programming C I prefer to have syntax off because
I have always _read_ C and looked at indentation patterns and I find
the colors distracting. I am less familiar with XML, so when I am
editing an XML file, I prefer to have syntax on because it makes the
structure of the file easier for me to see.

I can switch back and forth between reading modes well enough from
file to file, or between diff and non-diff modes, but I think using
both modes side by side would be annoying. That is, if I can't use
syntax highlighting within a Diff* region, I would rather not use it
at all within that file.

I've also gotten used to most of Vim's default foreground colors and
its garish Diff* background colors. I think it would be difficult
to choose a set of foreground colors and Diff* background colors
that are distinct from one another and have good contrast between
foreground, Diff* background and normal background.

Regards,
Gary

Bram Moolenaar

unread,
Mar 25, 2011, 4:47:40 PM3/25/11
to Gary Johnson, vim...@googlegroups.com

Gary Johnson wrote:

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 -- 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 ///

Gary Johnson

unread,
Mar 25, 2011, 5:43:20 PM3/25/11
to vim...@googlegroups.com, Bram Moolenaar
On 2011-03-25, Bram Moolenaar wrote:
> 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 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:

Thanks very much, Bram! When I try that, though, I get these
errors from 'make':

...
gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -g -O2 -D_FORTIFY_SOURCE=1 -o objects/main.o main.c
main.c: In function 'edit_buffers':
main.c:2710: error: 'params' undeclared (first use in this function)
main.c:2710: error: (Each undeclared identifier is reported only once
main.c:2710: error: for each function it appears in.)
make[1]: *** [objects/main.o] Error 1
make[1]: Leaving directory `/home/gajohnso/src/vim-hg/vim/src'
make: *** [first] Error 2

I thought maybe there was a conditional-compile problem, but there's
no sign of a declaration of params within main(). I didn't
investigate further.

Regards,
Gary

Bram Moolenaar

unread,
Mar 26, 2011, 10:16:20 AM3/26/11
to Gary Johnson, vim...@googlegroups.com

Gary Johnson wrote:

Small mistake in the patch:

+ if (params.diff_mode)

should be:

+ if (parmp->diff_mode)

--
God made machine language; all the rest is the work of man.

Gary Johnson

unread,
Mar 28, 2011, 12:24:17 PM3/28/11
to vim...@googlegroups.com, Bram Moolenaar

That worked to fix the build error but didn't work to fix the &diff
problem. When I run the same example as before but with this
patched vim I get the same results:

:echo diffdict
{'bar.xml': 0, 'foo.xml': 1, 'vimrc': 1}

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages