Ad blocker interference detected!
Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers
Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.
This tip shows a function to expand a C/C++ macro using the gcc compiler.
The function expands a specific line in a preview window as the gcc compiler sees it. Normally this function deals with severals tab/buffer/windows.
Macro expansion with gccEdit
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
A useful mapping to call the function is:
autocmd FileType cpp nnoremap <leader>m :call ExpandCMacro()<CR>
The function depends on the gnu program indent which could be replaced by some tricky search/replace operations.