Tuesday, April 27, 2010

Re: How to fix broken automatic indentation in vim

On 2010-04-27, Didlybom wrote:
> Hi,
>
> I am trying to use vim 7.2 (on Windows XP) to automatically indent and
> format some VHDL and Matlab code. To do so I am trying to use the "gg=G"
> command. However this does not work properly. The code is not properly
> indented at all.
>
> To give you an example, I had the following source code, which was already
> properly indented:
>
> %%%%%%%%%%% BEGIN_OF_CODE %%%%%%%%%%%%
> % This function is based on the code_g_generator() function
> function [v_code] = get_code(n_code_number)
> % There is no need to clear the persistent variables in this function
> mlock
> %% Initialize the internal variables
> persistent n_fifo_top;
> if isempty(n_fifo_top)
> n_fifo_top = 1;
> end
>
> N_MEMORY_SIZE = 4;
> if n_code_number > 4
> c_saved_code_fifo = {-1*ones(1, N_MEMORY_SIZE)};
> end
> %%%%%%%%%%% END_OF_CODE %%%%%%%%%%%%
>
> If I use the "gg=G" command I get:
>
> %%%%%%%%%%% BEGIN_OF_CODE %%%%%%%%%%%%
> % This function is based on the code_g_generator() function
> function [v_code] = get_code(n_code_number)
> % There is no need to clear the persistent variables in this function
> mlock
> %% Initialize the internal variables
> persistent n_fifo_top;
> if isempty(n_fifo_top)
> n_fifo_top = 1;
> end
>
> N_MEMORY_SIZE = 4;
> if n_code_number > 4
> c_saved_code_fifo = {-1*ones(1, N_MEMORY_SIZE)};
> end
> %%%%%%%%%%% END_OF_CODE %%%%%%%%%%%%
>
> As you can see, in this example Vim incorrectly indents the code after the
> first "if" block. For other files I get similar problems (although not
> necessarily on the first if block).
>
> For VHDL files I get similar problems.
>
> I have tried using different combinations of the autoindent, smartindent and
> cindent settings. After going through these forums I have also made sure
> that the "syntax", "filetype", "filetype indent" and "filetype plugin
> indent" are set to on. Still, it does not work. Also, if I do "set syntax?"
> I get "matlab" for matlab files and "vhdl" for vhdl files which is correct.
> And if I do "set indentexpr?" I get "GetMatlabIndent(v:lnum)" for matlab
> files and "GetVHDLindent()" for vhdl files.
>
> To try to isolate the problem (and ensure that it is not due to one of the
> vim plugins that I have installed) by doing a fresh install of VIM on a
> different computer (in which VIM had never been installed before). On that
> computer I get the same sort of problems (that is why I do not think that I
> need to give you the .vimrc, but if you need it I can upload it here too).

You have the same problem on both computers and you're using the
same ~/.vimrc on both? Hmm.

It works fine for me. I copied your example code into a file named
foo.m. Then I started vim as

vim -N -u NONE

to avoid loading any configuration files at all. Within vim I
executed

:filetype plugin indent on
:e foo.m
gg=G

The result was that both indented lines were re-indented by 8
spaces, which makes sense since 'shiftwidth' defaults to 8.

I don't know what's specifically wrong with your ~/.vimrc since I
don't know what's in it. The indent plugin should override your
'cindent' and 'smartindent' settings, so those shouldn't be a
problem. As I demonstrated above, you don't need anything in your
~/.vimrc other than

filetype plugin indent on

and

set shiftwidth=4

to get the indentation you want.

HTH,
Gary

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

Subscription settings: http://groups.google.com/group/vim_use/subscribe?hl=en

No comments:

Post a Comment