Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 236977 - app-editors/vim indents a piece of C code improperly
Summary: app-editors/vim indents a piece of C code improperly
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Development (show other bugs)
Hardware: All All
: High normal (vote)
Assignee: Vim Maintainers
Depends on:
Reported: 2008-09-07 14:53 UTC by Luis Fernando Schultz Xavier da Silveira
Modified: 2010-02-26 21:34 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Note You need to log in before you can comment on or make changes to this bug.
Description Luis Fernando Schultz Xavier da Silveira 2008-09-07 14:53:30 UTC
I don't know if this is a bug or is intended by VIM, but the statement below is legal C:

typedef struct {
    int x, y;
} vector;

vector v;

v = (vector) {
    .x = 2,
    .y = 4

Still, VIM indents this like:

typedef struct {
    int x, y;
} vector;

vector v;

v = (vector) {
    .x = 2,
        .y = 4

This is getting to annoy me.

(The spaces represents tabs.)

Reproducible: Always

Steps to Reproduce:
0. Write that code in VIM.
1. Do ggVG to select all and then = to indent.


0. Type that and it will happen automatically.

Actual Results:  

Expected Results:  

Portage (default-linux/x86/2007.0, gcc-4.1.2, glibc-2.6.1-r0, 2.6.25-gentoo-r7 i686)
System uname: 2.6.25-gentoo-r7 i686 Intel(R) Pentium(R) M processor 1.50GHz
Timestamp of tree: Sun, 07 Sep 2008 13:36:01 +0000
ccache version 2.4 [enabled]
app-shells/bash:     3.2_p33
dev-lang/python:     2.5.2-r6
dev-util/ccache:     2.4-r7
sys-devel/autoconf:  2.13, 2.61-r2
sys-devel/automake:  1.5, 1.7.9-r1, 1.9.6-r2, 1.10.1
sys-devel/binutils:  2.18-r3
sys-devel/gcc-config: 1.4.0-r4
sys-devel/libtool:   1.5.26
virtual/os-headers:  2.6.23-r3
CFLAGS="-march=pentium-m -O2 -pipe -fomit-frame-pointer"
CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/env.d /etc/fonts/fonts.conf /etc/gconf /etc/revdep-rebuild /etc/terminfo /etc/texmf/web2c /etc/udev/rules.d"
CXXFLAGS="-march=pentium-m -O2 -pipe -fomit-frame-pointer"
FEATURES="ccache distlocks metadata-transfer parallel-fetch sandbox sfperms strict unmerge-orphans userfetch"
PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --compress --force --whole-file --delete --stats --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages"
USE="X a52 aac acl acpi alsa amrnb amrwb bash-completion berkdb bidi binary bl branding bzip2 cairo cjk cli cracklib crypt cscope cups dga djvu dri dts dv dvd enca encode fftw flac fontconfig fortran gd gdbm gif gmp gnutls gpm graphviz gs gtk hddtemp hdri hpn iconv idea idn ieee1394 imlib ipv6 isdnlog javascript joystick jpeg jpeg2k lame lapack latex lcms ldap libedit libsamplerate loop-aes lzo mad md5sum midi mikmod mmap mmx mng motif mp2 mp3 mpeg mudflap musepack ncurses nemesi network nls nptl nptlonly objc objc++ objc-gc offensive ogg openal openexr opengl openmp pam pcmcia pcre pdf perl plotutils png pop portaudio pppd python qdbm quicktime readline reflection rtc sasl sdl session sndfile speex spell spl sqlite sse sse2 ssl svg svga tcl tcpd tetex tga theora threads tiff tk truetype unicode utils v4l v4l2 vidix vim-pager vim-syntax vim-with-x vorbis win32codecs x86 xanim xft xml xorg xpm xscreensaver xv xvid xvmc zlib" ALSA_CARDS="ali5451 als4000 atiixp atiixp-modem bt87x ca0106 cmipci emu10k1 emu10k1x ens1370 ens1371 es1938 es1968 fm801 hda-intel intel8x0 intel8x0m maestro3 trident usb-audio via82xx via82xx-modem ymfpci" ALSA_PCM_PLUGINS="adpcm alaw asym copy dmix dshare dsnoop empty extplug file hooks iec958 ioplug ladspa lfloat linear meter mmap_emul mulaw multi null plug rate route share shm softvol" APACHE2_MODULES="actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" ELIBC="glibc" INPUT_DEVICES="keyboard mouse synaptics evdev joystick" KERNEL="linux" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" USERLAND="GNU" VIDEO_CARDS="i810"
Comment 1 Jim Ramsay (lack) (RETIRED) gentoo-dev 2010-02-25 20:27:26 UTC
Well, to begin with, the ':help C-indenting' vim help page does contain this

  Note that 'cindent' indenting does not work for every code scenario.  Vim
  is not a C compiler: it does not recognize all syntax.  One requirement is
  that toplevel functions have a '{' in the first column.  Otherwise they are
  easily confused with declarations.

Based on your example (and some other quick testing I did here), it seems that another (unstated) limitation of the cindent feature is that it doesn't recognize a structure initialization if you put the '=' and a cast on the same line.

For example, the following blocks do format properly:

v = {
    .x = 2,
    .y = 4

v =
(vector) {
    .x = 2,
    .y = 4

Whereas these do not:

v = (vector) {
    .x = 2,
        .y = 4

v = (vector)
    .x = 2,
        .y = 4

v = (struct
        vector) {
    .x = 2,
        .y = 4

Now all 3 of these 'failing' cases are indeed valid C code, but after a quick glance through the code, it would be non-trivial to hack the code to recognize this kind of syntax (especially that last case!).

So, it's not really a *bug* per se, as Vim freely acknowledges that they can't parse every possible C syntax variation... I'm  closing this as UPSTREAM and encouraging you to reporting it upstream, or try fixing it yourself if you're really interested.

Please feel free to re-open this bug if you or upstream or someone else ends up adding support for this syntax, and I'll ensure I get the patch in to our ebuild.
Comment 2 Luis Fernando Schultz Xavier da Silveira 2010-02-26 00:51:56 UTC
Thank you for the information. I am not able to make that change myself, but I may talk to some friends who are.
Comment 3 Jim Ramsay (lack) (RETIRED) gentoo-dev 2010-02-26 18:52:08 UTC
Another possibility you *may* be able to do is to use the vim "indent-expression" mechanism to override the existing cindent and fix this case?  It's not a facility I'm very familiar with, and doing this may be harder than fixing the code itself, but I thought I'd suggest it just in case it helps somehow.
Comment 4 Luis Fernando Schultz Xavier da Silveira 2010-02-26 21:34:01 UTC
The problem is that I don't understand vim scripting, so I cannot write the indentation code. I can happily live with indenting that case myself as long as I have a tool to fix a whole file. I was thinking of using GNU indent to do that.

Thanks for the hint anyway.