c.snippets 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. ## Main
  2. # main
  3. snippet maina
  4. #include <stdio.h>
  5. int
  6. main(int argc, char *argv[])
  7. {
  8. for (int i = 0; i < argc; i++) {
  9. printf("argv[%d] = '%s'\n", i, argv[i]);
  10. }
  11. ${0}
  12. return 0;
  13. }
  14. # main(void)
  15. snippet main
  16. #include <stdio.h>
  17. int
  18. main(void)
  19. {
  20. printf("hi\n");
  21. ${0}
  22. return 0;
  23. }
  24. ##
  25. ## Preprocessor
  26. # #include <...>
  27. snippet inc
  28. #include <${1:stdio}.h>
  29. # #include "..."
  30. snippet Inc
  31. #include "${1:`vim_snippets#Filename("$1.h")`}"
  32. # ifndef...define...endif
  33. snippet ndef
  34. #ifndef $1
  35. #define ${1:SYMBOL} ${2:value}
  36. #endif /* ifndef $1 */
  37. # define
  38. snippet def
  39. #define
  40. # ifdef...endif
  41. snippet ifdef
  42. #ifdef ${1:FOO}
  43. ${2:#define }
  44. #endif
  45. # if
  46. snippet #if
  47. #if ${1:FOO}
  48. ${0:${VISUAL}}
  49. #endif
  50. # header include guard
  51. snippet once
  52. #ifndef ${1:`toupper(vim_snippets#Filename('$1_H', 'UNTITLED_H'))`}
  53. #define $1
  54. ${0}
  55. #endif /* end of include guard: $1 */
  56. # Disable C++ name mangling in C headers
  57. snippet nocxx
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. ${0}
  62. #ifdef __cplusplus
  63. } /* extern "C" */
  64. #endif
  65. ##
  66. ## Control Statements
  67. # if
  68. snippet if
  69. if (${1:true}) {
  70. ${0:${VISUAL}}
  71. }
  72. snippet ife
  73. if (${1:true}) {
  74. ${2:${VISUAL}}
  75. } else {
  76. ${0}
  77. }
  78. # else
  79. snippet el
  80. else {
  81. ${0:${VISUAL}}
  82. }
  83. # else if
  84. snippet elif
  85. else if (${1:true}) {
  86. ${0:${VISUAL}}
  87. }
  88. # ifi
  89. snippet ifi
  90. if (${1:true}) ${0};
  91. # ternary
  92. snippet t Ternary: `condition ? true : false`
  93. $1 ? $2 : $0
  94. # switch
  95. snippet switch
  96. switch (${1:/* variable */}) {
  97. case ${2:/* variable case */}:
  98. ${3}
  99. ${4:break;}${5}
  100. default:
  101. ${6}
  102. }
  103. # switch without default
  104. snippet switchndef
  105. switch (${1:/* variable */}) {
  106. case ${2:/* variable case */}:
  107. ${3}
  108. ${4:break;}${5}
  109. }
  110. # case
  111. snippet case
  112. case ${1:/* variable case */}:
  113. ${2}
  114. ${3:break;}
  115. snippet ret
  116. return ${0};
  117. snippet ex
  118. exit($0);
  119. ##
  120. ## Loops
  121. # for
  122. snippet for
  123. for (int ${2:i} = 0; $2 < ${1:count}; $2${3:++}) {
  124. ${4}
  125. }
  126. # for (custom)
  127. snippet forr
  128. for (int ${1:i} = ${2:0}; ${3:$1 < 10}; $1${4:++}) {
  129. ${5}
  130. }
  131. # while
  132. snippet wh
  133. while (${1:1}) {
  134. ${0:${VISUAL}}
  135. }
  136. snippet wht
  137. while (true) {
  138. ${0:${VISUAL}}
  139. }
  140. # do... while
  141. snippet do
  142. do {
  143. ${0:${VISUAL}}
  144. } while ($1);
  145. ##
  146. ## Functions
  147. # function definition
  148. snippet fun
  149. ${1:void} ${2:function_name}(${3})
  150. {
  151. ${4}
  152. }
  153. # function definition with zero parameters
  154. snippet fun0
  155. ${1:void} ${2:function_name}()
  156. {
  157. ${3}
  158. }
  159. # function definition with Doxygen documentation
  160. snippet dfun0
  161. /*! \brief ${1:Brief function description here}
  162. *
  163. * ${2:Detailed description of the function}
  164. *
  165. * \return ${3:Return parameter description}
  166. */
  167. ${4:void} ${5:function_name}()
  168. {
  169. ${6}
  170. }
  171. # function definition with one parameter
  172. snippet fun1
  173. ${1:void} ${2:function_name}(${3:Type} ${4:Parameter})
  174. {
  175. ${5}
  176. }
  177. # function definition with one parameter with Doxygen documentation
  178. snippet dfun1
  179. /*! \brief ${1:Brief function description here}
  180. *
  181. * ${2:Detailed description of the function}
  182. *
  183. * \param $3 ${4:Parameter description}
  184. * \return ${5:Return parameter description}
  185. */
  186. ${6:void} ${7:function_name}(${8:Type} ${3:Parameter})
  187. {
  188. ${9}
  189. }
  190. # function definition with two parameters
  191. snippet fun2
  192. ${1:void} ${2:function_name}(${3:Type} ${4:Parameter}, ${5:Type} ${6:Parameter})
  193. {
  194. ${7}
  195. }
  196. # function definition with two parameters with Doxygen documentation
  197. snippet dfun2
  198. /*! \brief ${1:Brief function description here}
  199. *
  200. * ${2:Detailed description of the function}
  201. *
  202. * \param $3 ${4:Parameter description}
  203. * \param $5 ${6:Parameter description}
  204. * \return ${7:Return parameter description}
  205. */
  206. ${8:void} ${9:function_name}(${10:Type} ${3:Parameter}, ${11:Type} ${5:Parameter})
  207. {
  208. ${12}
  209. }
  210. # function definition with three parameters
  211. snippet fun3
  212. ${1:void} ${2:function_name}(${3:Type} ${4:Parameter}, ${5:Type} ${6:Parameter}, ${7:Type} ${8:Parameter})
  213. {
  214. ${9}
  215. }
  216. # function definition with three parameters with Doxygen documentation
  217. snippet dfun3
  218. /*! \brief ${1:Brief function description here}
  219. *
  220. * ${2:Detailed description of the function}
  221. *
  222. * \param $3 ${4:Parameter description}
  223. * \param $5 ${6:Parameter description}
  224. * \param $7 ${8:Parameter description}
  225. * \return ${9:Return parameter description}
  226. */
  227. ${10:void} ${11:function_name}(${12:Type} ${3:Parameter}, ${13:Type} ${5:Parameter}, ${14:Type} ${7:Parameter})
  228. {
  229. ${15}
  230. }
  231. # function declaration
  232. snippet fund
  233. ${1:void} ${2:function_name}(${3});
  234. ##
  235. ## Types
  236. # typedef
  237. snippet td
  238. typedef ${1:int} ${2:MyCustomType};
  239. # struct
  240. snippet st
  241. /*! \struct $1
  242. * \brief ${3:Brief struct description}
  243. *
  244. * ${4:Detailed description}
  245. */
  246. struct ${1:`vim_snippets#Filename('$1_t', 'name')`} {
  247. ${2:Data} /*!< ${4:Description} */
  248. }${5: /* optional variable list */};
  249. # typedef struct
  250. snippet tds
  251. /*! \struct $2
  252. * \brief ${5:Brief struct description}
  253. *
  254. * ${6:Detailed description}
  255. */
  256. typedef struct ${2:_$1 }{
  257. m_${3:Data} /*!< ${4:Description} */
  258. } ${1:`vim_snippets#Filename('$1_t', 'name')`};
  259. snippet enum
  260. /*! \enum $1
  261. *
  262. * ${2:Detailed description}
  263. */
  264. enum ${1:name} { ${0} };
  265. # typedef enum
  266. snippet tde
  267. /*! \enum $2
  268. *
  269. * ${4:Detailed description}
  270. */
  271. typedef enum {
  272. ${1:Data} /*!< ${3:Description} */
  273. } ${2:foo};
  274. ##
  275. ## Input/Output
  276. # printf
  277. snippet pr
  278. printf("${1:%s}\n"${2});
  279. # fprintf (again, this isn't as nice as TextMate's version, but it works)
  280. snippet fpr
  281. fprintf(${1:stderr}, "${2:%s}\n"${3});
  282. snippet prd
  283. printf("${1:} = %d\n", $1);
  284. snippet prf
  285. printf("${1:} = %f\n", $1);
  286. snippet prx
  287. printf("${1:} = %${2}\n", $1);
  288. snippet warn
  289. warn("${1:%s}"$0);
  290. snippet warnx
  291. warnx("${1:%s}"$0);
  292. snippet err
  293. err(${1:1}, "${2:%s}"$0);
  294. snippet errx
  295. errx(${1:1}, "${2:%s}"$0);
  296. # getopt
  297. snippet getopt
  298. int choice;
  299. while (1)
  300. {
  301. static struct option long_options[] =
  302. {
  303. /* Use flags like so:
  304. {"verbose", no_argument, &verbose_flag, 'V'}*/
  305. /* Argument styles: no_argument, required_argument, optional_argument */
  306. {"version", no_argument, 0, 'v'},
  307. {"help", no_argument, 0, 'h'},
  308. ${1}
  309. {0,0,0,0}
  310. };
  311. int option_index = 0;
  312. /* Argument parameters:
  313. no_argument: " "
  314. required_argument: ":"
  315. optional_argument: "::" */
  316. choice = getopt_long( argc, argv, "vh",
  317. long_options, &option_index);
  318. if (choice == -1)
  319. break;
  320. switch( choice )
  321. {
  322. case 'v':
  323. ${2}
  324. break;
  325. case 'h':
  326. ${3}
  327. break;
  328. case '?':
  329. /* getopt_long will have already printed an error */
  330. break;
  331. default:
  332. /* Not sure how to get here... */
  333. return EXIT_FAILURE;
  334. }
  335. }
  336. /* Deal with non-option arguments here */
  337. if ( optind < argc )
  338. {
  339. while ( optind < argc )
  340. {
  341. ${0}
  342. }
  343. }
  344. ## Assertions
  345. snippet asr
  346. assert($1);
  347. snippet anl
  348. assert(${1:ptr} != NULL);
  349. ## Dynamic Allocation
  350. snippet mlc
  351. ${1:ptr} = (${2:type}*) malloc(sizeof($2));
  352. snippet clc
  353. ${1:ptr} = (${2:type}*) calloc(${3:size}, sizeof($2));
  354. snippet rlc
  355. ${1:ptr} = realloc($1, ${2:size} * sizeof(${3:type}));
  356. snippet mlcd
  357. ${1:type} ${2:ptr} = ($1*) malloc(sizeof($1));
  358. snippet clcd
  359. ${1:type} ${2:ptr} = ($1*) calloc(${3:size}, sizeof($1));
  360. snippet fre
  361. free(${1:ptr});
  362. ##
  363. # TODO section
  364. snippet todo
  365. /*! TODO: ${1:Todo description here}
  366. * \todo $1
  367. */
  368. ## Miscellaneous
  369. # This is kind of convenient
  370. snippet .
  371. [${1}]
  372. snippet asm
  373. __asm__ __volatile__(
  374. "${0}\n\t"
  375. :
  376. :
  377. );