Atom のC言語用 Snippets(個人用)
コア・パッケージ「language-c」の Snippets は、次のサイトから取得している。
github.com自分用に細かい修正を加えたかったので、「File」→「Snippets」からファイル「snippets.cson」を開いて、そこに下記の内容を追加。
(下記をコピーする場合、カーソルを置くと現れる「select」ボタンを押すと楽)
################################## # My snippets for C language # # Last modified time: 2017/09/30 # ################################## '.source.c, .source.cpp, .source.objc, .source.objcpp': # header 'Header Include-Guard': 'prefix': 'IncludeGuard' 'body': '#ifndef ${1:SYMBOL} /* beginning of include guard ${1:SYMBOL} */\n#define ${1:SYMBOL}\n\n${2}\n\n#endif /* end of include guard: $1 */\n' '#include <>': 'prefix': 'Inc' 'body': '#include <${1:stdio.h}>' '#include ""': 'prefix': 'inc' 'body': '#include "${1:current.h}"' 'Function-like Macro': 'prefix': 'MacroFunc' 'body': '#define ${1:f}(${2:x}) do { ${3:macro} } while (0)' 'typedef enum': 'prefix': 'enum' 'body': 'typedef enum { $2 } ${1:Name};' 'typedef struct': 'prefix': 'struct' 'body': 'typedef struct {\n\t${2:int };\n} ${1:Name};' 'linear list': 'prefix': 'llist' 'body': 'typedef struct ${1:__node} {\n\t${3:int val};\n\tstruct ${1:__node} *next;\n} ${2:Node};' 'function': 'prefix': 'func' 'body': '${1:void} ${2:functionName}(${3:int parameter}) {\n\t${4:/* function body */}\n}' 'main()': 'prefix': 'main' 'body': 'int main(int argc, char const *argv[]) {\n\t${1:/* code */}\n\n\treturn 0;\n}' # stdio.h 'printf()': 'prefix': 'printf' 'body': 'printf("$1", ${2:var});' 'scanf()': 'prefix': 'scanf' 'body': 'scanf("%${1:s}", ${2:ptr});' 'putchar()': 'prefix': 'putchar' 'body': '''putchar('$1');''' 'puts()': 'prefix': 'puts' 'body': 'puts("$1");' 'Switch Statement': 'prefix': 'switch' 'body': 'switch (${1:/* expression */}) {\n\tcase ${2:/* value */} :\n\t\t${3:/* code */}\n}' 'case :': 'prefix': 'case' 'body': 'case ${1:value} :\n\t${2:/* code */}' 'Do Loop': 'prefix': 'do' 'body': 'do {\n\t${2:/* code */}\n} while (${1:/* code */});' 'While Loop': 'prefix': 'while' 'body': 'while (${1:/* code */}) {\n\t${2:/* code */}\n}' 'For Loop - customize': 'prefix': 'For' 'body': 'for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:count}; ${2:i}${5:++}) {\n\t${6:/* code */}\n}' 'For Loop': 'prefix': 'for' 'body': 'for (size_t ${1:i} = 0; ${1:i} < ${2:count}; ${1:i}++) {\n\t${3:/* code */}\n}' 'sizeof()': 'prefix': 'sizeof' 'body': 'sizeof(${1:int})' 'open file': 'prefix': 'fopen' 'body': '''if ((${1:fp} = fopen(${2:filename}, "${3:w}")) == NULL) {\n\tfprintf(stderr, "We have failed to open file \\\\"%s\\\\".\\\\n", ${2:filename});\n} else {\n\t${4:/* code */}\n\t\n\tfclose(${1:fp});\n}''' # stdlib.h 'rand()': 'prefix': 'rand' 'body': 'rand()' 'srand()': 'prefix': 'srand' 'body': 'srand(${1:(unsigned)time(NULL)});' 'calloc() - initialize': 'prefix': 'Calloc' 'body': '${1:int} *${2:ptr} = (${1:int} *)calloc(${3:num_arry}, sizeof(${1:int}))\n\nif (() == NULL) {\n\t${4:fprintf(stderr, "Failed to allocate memory.\\\\n")};\n} else {\n\t${5:/* code */}\n\t\n\tfree(${2:ptr});\n}' 'calloc()': 'prefix': 'calloc' 'body': '''/* allocate memory '${1:ptr}' */\nif ((${1:ptr} = (${2:int} *)calloc(${3:num_arry}, sizeof(${2:int}))) == NULL) {\n\tfprintf(stderr, "Failed to allocate memory '${1:ptr}'.\\\\n");\n} else {\n\t${5:/* code */}\n\t\n\tfree(${1:ptr});\n}''' 'bsearch()': 'prefix': 'bsearch' 'body': '''/* binary search '${1:sorted_array}' for '${2:key}' */\nif ((${3:res} = bsearch(&${2:key}, ${1:sorted_array}, ${4:nmemb}, sizeof(${5:int}), (int (*)(const void *, const void *))${6:compar})) == NULL) {\n\t${7:puts("We have failed to binary search.");}\n} else {\t/* Success: ${1:sorted_array}[(int)(${3:res} - ${1:sorted_array})] = ${2:key} */\n\t${8:/* code */}\n}''' # string.h 'strcmp()': 'prefix': 'strcmp' 'body': 'strcmp(${1:s1}, ${2:s2})'