Vim Tips Wiki
(Created page with "video|right|300px right|300px This tip shows function to expand macro in C/C++ using gcc compiler. The function expands a specifi...")
 
(Format.)
 
(6 intermediate revisions by one other user not shown)
Line 1: Line 1:
  +
{{TipProposed
[[File:Placeholder|video|right|300px]] [[File:Placeholder|right|300px]]
 
  +
|id=0
  +
|previous=0
  +
|next=0
  +
|created=April 10, 2014
  +
|complexity=basic
  +
|author=[[User:TTRh|TTRh]]
  +
|version=7.0
  +
|subpage=/2014
  +
|category1=
  +
|category2=
  +
}}
 
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.
This tip shows function to expand macro in C/C++ using gcc compiler.
 
 
The function expands a specific ''line'' in a preview window as the gcc compiler see it.
 
   
 
==Macro expansion with gcc==
 
==Macro expansion with gcc==
 
 
Here is the function :
 
Here is the function :
 
 
<pre>
 
<pre>
 
function! ExpandCMacro()
 
function! ExpandCMacro()
"get current info
+
"get current info
let l:macro_file_name = "__macroexpand__" . tabpagenr()
+
let l:macro_file_name = "__macroexpand__" . tabpagenr()
let l:file_row = line(".")
+
let l:file_row = line(".")
let l:file_name = expand("%")
+
let l:file_name = expand("%")
let l:file_window = winnr()
+
let l:file_window = winnr()
"create mark
+
"create mark
execute "normal! Oint " . l:macro_file_name . ";"
+
execute "normal! Oint " . l:macro_file_name . ";"
execute "w"
+
execute "w"
"open tiny window ... check if we have already an open buffer for macro
+
"open tiny window ... check if we have already an open buffer for macro
if bufwinnr( l:macro_file_name ) != -1
+
if bufwinnr( l:macro_file_name ) != -1
execute bufwinnr( l:macro_file_name) . "wincmd w"
+
execute bufwinnr( l:macro_file_name) . "wincmd w"
setlocal modifiable
+
setlocal modifiable
execute "normal! ggdG"
+
execute "normal! ggdG"
else
+
else
execute "bot 10split " . l:macro_file_name
+
execute "bot 10split " . l:macro_file_name
execute "setlocal filetype=cpp"
+
execute "setlocal filetype=cpp"
execute "setlocal buftype=nofile"
+
execute "setlocal buftype=nofile"
nnoremap <buffer> q :q!<CR>
+
nnoremap <buffer> q :q!<CR>
endif
+
endif
"read file with gcc
+
"read file with gcc
silent! execute "r!gcc -E " . l:file_name
+
silent! execute "r!gcc -E " . l:file_name
"keep specific macro line
+
"keep specific macro line
execute "normal! ggV/int " . l:macro_file_name . ";$\<CR>d"
+
execute "normal! ggV/int " . l:macro_file_name . ";$\<CR>d"
execute "normal! jdG"
+
execute "normal! jdG"
"indent
+
"indent
silent! execute '%s/\( { \| } \| };\)/\r\1\r/g'
+
execute "%!indent -st -kr"
execute "normal! gg=G"
+
execute "normal! gg=G"
"resize window<gallery type="slideshow" widths="495" position="left">
+
"resize window
 
execute "normal! G"
macro_expansion.png|macro_expansion_example_1
 
 
let l:macro_end_row = line(".")
macro_expansion_2.png|macro_expansion_example_2
 
 
execute "resize " . l:macro_end_row
</gallery>
 
execute "normal! G"
+
execute "normal! gg"
 
"no modifiable
let l:macro_end_row = line(".")
 
 
setlocal nomodifiable
execute "resize " . l:macro_end_row
 
 
"return to origin place
execute "normal! gg"
 
  +
execute l:file_window . "wincmd w"
"no modifiable
 
 
execute l:file_row
setlocal nomodifiable
 
 
execute "normal!u"
"return to origin place
 
execute l:file_window . "wincmd w"
+
execute "w"
 
"highlight origin line
execute l:file_row
 
 
let @/ = getline('.')
execute "normal!u"
 
execute "w"
 
"highlight origin line
 
let @/ = getline('.')
 
 
endfunction
 
endfunction
 
</pre>
 
</pre>
   
And a usefull mapping to use it :
+
A useful mapping to call the function is:
 
 
<pre>
 
<pre>
 
autocmd FileType cpp nnoremap <leader>m :call ExpandCMacro()<CR>
 
autocmd FileType cpp nnoremap <leader>m :call ExpandCMacro()<CR>
 
</pre>
 
</pre>
   
  +
The function depends on the gnu program ''indent'' which could be replaced by some tricky search/replace operations.
Normally this function deals with severals tab/buffer/windows
 
  +
  +
==Preview==
  +
<gallery type="slideshow" widths="495" position="left">
 
macro_expansion.png
 
macro_expansion_2.png
 
</gallery>
  +
{{clear}}
  +
==Comments==
  +
Any ideas about the preview? It shows nothing for me, presumably because I don't have all scripts enabled. [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 06:17, April 11, 2014 (UTC)

Latest revision as of 06:17, 11 April 2014

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created April 10, 2014 · complexity basic · author TTRh · version 7.0

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

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.

Preview[]

Comments[]

Any ideas about the preview? It shows nothing for me, presumably because I don't have all scripts enabled. JohnBeckett (talk) 06:17, April 11, 2014 (UTC)