{"id":130,"date":"2012-06-13T13:33:59","date_gmt":"2012-06-13T11:33:59","guid":{"rendered":"https:\/\/blogs.gentoo.org\/mgorny\/?p=130"},"modified":"2013-07-23T11:05:47","modified_gmt":"2013-07-23T09:05:47","slug":"vim-smart-cc-boilerplate-templates","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/mgorny\/2012\/06\/13\/vim-smart-cc-boilerplate-templates\/","title":{"rendered":"vim: smart C\/C++ boilerplate templates"},"content":{"rendered":"<p>A\u00a0simple vim scriptie for\u00a0those who are interested. It is triggered when new C\/C++ files are created (e.g.\u00a0via <code>vim new-file.cxx<\/code>), and\u00a0fills it in with boilerplate unit code. What&#8217;s special about it is that it tries to\u00a0find tips about that code in\u00a0other files in\u00a0that or\u00a0parent directory.<\/p>\n<p><!--more--><\/p>\n<h2>The\u00a0templates<\/h2>\n<p>The\u00a0template used for\u00a0.c\/.cxx files is:<\/p>\n<pre><code>\/* header-comment\r\n *\/\r\n\r\n#ifdef HAVE_CONFIG_H\r\n#\tinclude \"config.h\"\r\n#endif<\/code><\/pre>\n<p>While for\u00a0.h\/.hxx files the\u00a0following is used:<\/p>\n<pre><code style='overflow:auto'>\/* header-comment\r\n *\/\r\n\r\n#pragma once\r\n\r\n#ifndef PREFIX_FN_H\r\n#define PREFIX_FN_H 1\r\n\r\n#endif \/*PREFIX_FN_H*\/<\/code><\/pre>\n<p>In\u00a0order to\u00a0simplify the\u00a0work flow, the\u00a0following magic is\u00a0done:<\/p>\n<ol>\n<li><em>header-comment<\/em> is actually copied from any\u00a0other file with the\u00a0same suffix,<\/li>\n<li><em>FN_H<\/em> is substituted for\u00a0actual filename,<\/li>\n<li><em>PREFIX<\/em> is copied from any\u00a0other file with the\u00a0same suffix,<\/li>\n<li>if\u00a0no file is\u00a0found in\u00a0the\u00a0same directory as\u00a0the\u00a0new file, the\u00a0scripts falls back to parent directory, and\u00a0appends the\u00a0subdirectory name to\u00a0<em>PREFIX<\/em>.<\/li>\n<\/ol>\n<h2>The\u00a0script<\/h2>\n<p>Just a\u00a0dirty, quick vimrc. Feel free to\u00a0improve:<\/p>\n<pre><code>function CTemplate()\r\n  let fext = expand('%:e')\r\n  let ucext = toupper(fext)\r\n  let myprefix = substitute(toupper(expand('%')), '[^A-Z0-9]', '_', 'g')\r\n\r\n  let head_done = 0\r\n  let head_line = 0\r\n  let prefix_done = 0\r\n\r\n  if fext =~ '^h'\r\n    call append(1, '#pragma once')\r\n    call append(2, '')\r\n    call append(3, '#ifndef @fn@')\r\n    call append(4, '#define @fn@ 1')\r\n    call append(5, '')\r\n    call append(6, '#endif \/*@fn@*\/')\r\n  else\r\n    call append(1, '#ifdef HAVE_CONFIG_H')\r\n    call append(2, '#  include \"config.h\"')\r\n    call append(3, '#endif')\r\n    let prefix_done = 1\r\n  endif\r\n\r\n  let mywd = expand(\"%:p:h\")\r\n  echo mywd\r\n  let myfiles = glob(mywd . '\/*.' . fext, 0, 1)\r\n  let myfiles += glob(mywd . '\/..\/*.' . fext, 0, 1)\r\n\r\n  for myfile in myfiles\r\n    if filereadable(myfile)\r\n      for myline in readfile(myfile)\r\n        if !head_done\r\n          if head_line != 0\r\n            call append(head_line, myline)\r\n            if myline =~ '[*][\/]'\r\n              let head_done = 1\r\n            else\r\n              let head_line += 1\r\n            endif\r\n          endif\r\n\r\n          if head_line == 0 &amp;&amp; myline =~ '^[\/][*]'\r\n            call append(0, myline)\r\n            let head_line += 1\r\n\r\n            if myline =~ '[*][\/]'\r\n              let head_done = 1\r\n            endif\r\n          endif\r\n        endif\r\n\r\n        if !prefix_done &amp;&amp; myline =~ '^#ifndef .*_'.ucext\r\n          if myfile =~ '^..\/'\r\n            let mydir = toupper(expand('%:p:h:t'))\r\n\r\n            let myprefix = mydir . '_' . myprefix\r\n          endif\r\n\r\n          let myprefix = substitute(myline[8:],\r\n            \\ '[^_]*_[^_]*$', '', '') . myprefix\r\n          let prefix_done = 1\r\n        endif\r\n      endfor\r\n    endif\r\n\r\n    if head_done &amp;&amp; prefix_done\r\n      break\r\n    endif\r\n  endfor\r\n\r\n  if fext =~ '^h'\r\n    exec '%s\/@fn@\/'.myprefix.'\/'\r\n  endif\r\nendfunction<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A\u00a0simple vim scriptie for\u00a0those who are interested. It is triggered when new C\/C++ files are created (e.g.\u00a0via vim new-file.cxx), and\u00a0fills it in with boilerplate unit code. What&#8217;s special about it is that it tries to\u00a0find tips about that code in\u00a0other files in\u00a0that or\u00a0parent directory.<\/p>\n","protected":false},"author":137,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[8],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts\/130"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/users\/137"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":11,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":208,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts\/130\/revisions\/208"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}