Vim Tips Wiki
No edit summary
Line 65: Line 65:
 
==Note==
 
==Note==
   
This function depends of gnu programm ''indent'' but it could be replaced by some tricky search/replace operation(s)
+
This function depends of gnu program ''indent'' but it could be replaced by some tricky search/replace operation(s)
   
 
==Preview==
 
==Preview==

Revision as of 22:04, 10 April 2014

This tip shows function to expand macro in C/C++ using gcc compiler.

This function expands a specific line in a preview window as the gcc compiler see it.

Normally this function deals with severals tab/buffer/windows.

Macro expansion with gcc

Here is the function :

function! ExpandCMacro()
   "get current info
   let l:macro_file_name = "__macroexpand__" . tabpagenr()
   let l:file_row = line(".")
   let l:file_name = expand("%")
   let l:file_window = winnr()
   "create mark
   execute "normal! Oint " . l:macro_file_name . ";" 
   execute "w" 
   "open tiny window ... check if we have already an open buffer for macro
   if bufwinnr( l:macro_file_name ) != -1
       execute bufwinnr( l:macro_file_name) . "wincmd w"
       setlocal modifiable
       execute "normal! ggdG"
   else
       execute "bot 10split " . l:macro_file_name
       execute "setlocal filetype=cpp"
       execute "setlocal buftype=nofile"
       nnoremap <buffer> q :q!<CR>
   endif
   "read file with gcc
   silent! execute "r!gcc -E " . l:file_name
   "keep specific macro line
   execute "normal! ggV/int " . l:macro_file_name . ";$\<CR>d"
   execute "normal! jdG"
   "indent
   execute "%!indent -st -kr"
   execute "normal! gg=G"
   "resize window
   execute "normal! G"
   let l:macro_end_row = line(".")
   execute "resize " . l:macro_end_row
   execute "normal! gg"
   "no modifiable
   setlocal nomodifiable
   "return to origin place
   execute l:file_window . "wincmd w"
   execute l:file_row
   execute "normal!u"
   execute "w" 
   "highlight origin line
   let @/ = getline('.')
endfunction

And a usefull mapping to use it :

autocmd FileType cpp nnoremap <leader>m :call ExpandCMacro()<CR>

Note

This function depends of gnu program indent but it could be replaced by some tricky search/replace operation(s)

Preview