Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix broken automatic indentation in vim

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:

% 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

If I use the "gg=G" command I get:

% 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

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

Any ideas?

like image 886
didlybom Avatar asked Nov 15 '22 11:11

didlybom


1 Answers

See this wiki page for explanations of the different methods for automatic indentation in vim.

On this page you can find an indent file for matlab which you can use with filetype based indenting. Here is a similar one for VHDL.

like image 75
akosch Avatar answered Dec 11 '22 01:12

akosch