Max
Max

Reputation: 1

vim syntax checking whitespace

I want configuring my .vimrc for do somes auto syntax checking. That is my problem, i want auto change somes syntax by another. I deal with the specific caracter in computer programation like = ; , . ( { [ <.

An exemple it's better than words :

void bibi(int param1,char *words)
{
 unsigned int locale=param;
 cout<<words<<endl;
}

became :

void bibi( int param1,char* words)
{
 unsigned int locale = param;
 cout << words << endl;
}

Just formating with add or remove some whitespaces.

I write this :

    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    " Formating of text in code
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

    function! ChangeSpaces()
    "" search and replace "= " or " =" or "= " to " = " 
    silent! %s/\s*[=]\s*/ = /g
    endfunction

    ""autocmd CursorMovedI * call ChangeSpaces()
    ""autocmd BufWrite * call ChangeSpaces()
    autocmd FileAppendPre * call ChangeSpaces()

But i have not the result, in this case, if i write " i=e" , they do nothing but if i write 'i= ', it's work, the regex doesn't run, they replace after the end of the "pattern".

By the way if you have a more "sexy way" to do what i want, let me know. In fact, when i want add some other specific caracter the code became :

    "function! ChangeSpaces()
     "" search and replace "= " or " =" or "= " to " = " 
     "silent! %s/\s*[=]\s*/ = /g

     """ search and replace "( " or " (" or "(" to " ( "
     ""      silent! %s/\s*[(]\s*/ ( /g
     """ search and replace "[ " or " [" or "[" to " [ "
     ""      silent! %s/\s*[[]\s*/ [ /g

     """ search and replace ", " or " ," or "," to " , "
     ""      silent! %s/\s*[,]\s*/ , /g

     """ search and replace "== " or " ==" or "==" to " == "
     ""      silent! %s/\s*[==]\s*/ = /g

     """ search and replace "> " or " >" or ">" to " > "
     ""      silent! %s/\s*[>]\s*/ > /g
     """ search and replace ">= " or " >=" or ">=" to " >= "
     "      silent! %s/\s*[>=]\s*/ >= /g

     """ search and replace "< " or " <" or "<" to " < "
     ""      silent! %s/\s*[<]\s*/ < /g
     """ search and replace "<= " or " <=" or "<=" to " <= "
     ""      silent! %s/\s*[=]\s*/ <= /g


     ""       let repl=substitute(cline,\s*[= ]\s*," = ", "g")
     ""       call setline(".",repl)
     ""       let cline=line(".")
     ""       let ccol=col(".")
     ""       call cursor(cline, ccol)
   "endfunction

   ""autocmd CursorMovedI * call ChangeSpaces()
   ""autocmd BufWrite * call ChangeSpaces()
   "autocmd FileAppendPre * call ChangeSpaces()

Best regards.

PS: my bad, i want this kind of formating, for every language i use, not just C++.

Upvotes: 0

Views: 111

Answers (1)

Jens
Jens

Reputation: 72629

What about filtering your file through an external C++ indenter? While GNU indent says it was not designed for C++ it works reasonably well. If it doesn't, you might try astyle. Then all you have to do is

map <F8> :w<CR>m':%!astyle<CR>`'

That way even folks using other editors can use the same indent style.

Upvotes: 1

Related Questions