-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathc.snippets
186 lines (156 loc) · 4.02 KB
/
c.snippets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
###########################################################################
# TextMate Snippets #
###########################################################################
# --------------
# Functions
# --------------
global !p
def printf_expand_args(snip):
"""
This will look how many placeholders printf has and adds the separated commas
at the end.
"""
# now add so many "," as much as the amount of placeholders
amount_placeholders = snip.tabstops[1].current_text.count("%")
output = ""
# Add the amount of tabstops
for placeholder_index in range(3, amount_placeholders + 3):
output += f", ${placeholder_index}"
# convert them into tabstops
snip.expand_anon(output)
endglobal
# ==============
# Snippets
# ==============
priority -50
snippet def "#define ..."
#define $1
endsnippet
snippet #ifndef "#ifndef ... #define ... #endif"
#ifndef ${1/([A-Za-z0-9_]+).*/$1/}
#define ${1:SYMBOL} ${2:value}
#endif /* ifndef $1 */
endsnippet
snippet #if "#if #endif" b
#if ${1:0}
${VISUAL}$0
#endif
endsnippet
snippet mark "#pragma mark (mark)"
#if 0
${1:#pragma mark -
}#pragma mark $2
#endif
$0
endsnippet
snippet main "main() (main)"
int main(int argc, char *argv[])
{
${VISUAL}$0
return 0;
}
endsnippet
snippet for "for loop (for)"
for (${2:i} = 0; $2 < ${1:count}; ${3:++$2}) {
${VISUAL}$0
}
endsnippet
snippet fori "for int loop (fori)"
for (${4:int} ${2:i} = 0; $2 < ${1:count}; ${3:++$2}) {
${VISUAL}$0
}
endsnippet
snippet fora "for-loop" b
for (${1:var}; ${2:condition}; `!p
if len(t[1]) > 0:
snip.rv = t[1].split('=')[0].split()[-1]
`++) {
$0
} /* for ($1; $2; `!p if len(t[1]) > 0: snip.rv = t[1].split('=')[0].split()[-1]`++) */
endsnippet
snippet once "Include header once only guard"
#ifndef ${1:`!p
if not snip.c:
import random, string
name = re.sub(r'[^A-Za-z0-9]+','_', snip.fn).upper()
rand = ''.join(random.sample(string.ascii_letters+string.digits, 8))
snip.rv = ('%s_%s' % (name,rand)).upper()
else:
snip.rv = snip.c`}
#define $1
${VISUAL}$0
#endif /* end of include guard: $1 */
endsnippet
snippet fprintf "fprintf ..."
fprintf(${1:stderr}, "${2:%s}\n"${2/([^%]|%%)*(%.)?.*/(?2:, :\);)/}$3${2/([^%]|%%)*(%.)?.*/(?2:\);)/}
endsnippet
snippet eli "else if .. (eli)"
else if (${1:/* condition */}) {
${VISUAL}$0
}
endsnippet
post_jump "printf_expand_args(snip)"
snippet "printf" "printf with auto-expand args" wr
printf("$1\n"$2);
endsnippet
snippet st "struct"
struct ${1:`!p snip.rv = (snip.basename or "name") + "_t"`} {
${0:/* data */}
};
endsnippet
snippet fun "function" b
${1:void} ${2:function_name}($3)
{
${VISUAL}$0
}
endsnippet
snippet fund "function declaration" b
${1:void} ${2:function_name}($3);
endsnippet
global !p
def split_line(text):
import textwrap
lines = textwrap.wrap(text, 78 - 19)
output = list()
for line in lines:
output.append('*' + ' '*19 + line)
snip_line = snip.tabstops[4].end[0]
snip.buffer.append(output, snip_line + 1)
del snip.buffer[snip_line]
def get_args(arglist):
args = [arg.strip() for arg in arglist.split(',') if arg]
return args
endglobal
post_jump "if snip.tabstop == 0 : split_line(snip.tabstops[4].current_text)"
snippet head "File Header" b
/******************************************************************************
* File: `!p snip.rv = fn`
*
* Author: ${2}
* Created: `date +%m/%d/%y`
* Description: ${4:${VISUAL}}
*****************************************************************************/
${0}
endsnippet
post_jump "if snip.tabstop == 0 : split_line(snip.tabstops[4].current_text)"
snippet func "Function Header"
/******************************************************************************
* Function: $1
* Description: ${4:${VISUAL}}
* Where:`!p
snip.rv = ""
snip >> 2
args = get_args(t[2])
if args:
for arg in args:
snip.rv += '\n' + '*' + ' '*19 + arg + ' - TODO'
snip << 2
`
* Return: $5
* Error: $6
*****************************************************************************/
${1}($2){
${0}
}
endsnippet
# vim:ft=snippets: