summaryrefslogtreecommitdiff
path: root/plugin/gobgen.vim
blob: 65d7d132c4018c7b50fcfc143496fea3cee8e793 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
" Vim GObject generator plugin
" Last Change: 2007 Nov 6
" Maintainer: Andrey Dubravin <daa84@inbox.ru>
" License: This file is placed in the public domain.

if exists("loaded_gobgen")
    finish
endif
let loaded_gobgen = 1

function! GOBGetPrefix()
    let prefix = expand("%:t:r")
    return input("Enter object prefix name (e.g. flybird-directory):", prefix)
endfunction

function! s:GOBGenerateCConstAndDest(prefix, typeName, typeNamePrivate, defineName)
    normal o
    exec "normal ostruct _" . a:typeNamePrivate "{"
    normal o};

    normal 2o

    " dispose
    normal ostatic void
    exec "normal o" . a:prefix . "_dispose (" . a:typeName "*self)"
    normal o{
    normal o}
    normal o

    " finalize
    normal ostatic void
    exec "normal o" . a:prefix . "_finalize (" . a:typeName "*self)"
    normal o{
    normal o}
    normal o

    " init
    normal ostatic void
    exec "normal o" . a:prefix . "_init (" . a:typeName "*self)"
    normal o{
    exec "normal o" . a:typeNamePrivate "*priv;"
    normal o
    exec "normal opriv =" a:defineName . "_GET_PRIVATE (self);"
    normal o}
    normal o

    " class init
    normal ostatic void
    exec "normal o" . a:prefix . "_class_init (" . a:typeName . "Class *self_class)"
    normal o{

    normal oGObjectClass *object_class = G_OBJECT_CLASS (self_class);
    normal o
    exec "normal og_type_class_add_private (self_class, sizeof (" . a:typeNamePrivate . "));"

    exec "normal oobject_class->dispose = (void (*) (GObject *object))" a:prefix . "_dispose;"
    exec "normal oobject_class->finalize = (void (*) (GObject *object))" a:prefix . "_finalize;"

    normal o}
    normal o
endfunction

function! GOBGenerateCModule()
    let prefix = GOBGetPrefix()

    if prefix == ""
	echohl ErrorMsg
	echo "Can't create class without prefix"
	echohl None
	return
    endif

    let prefix = substitute(prefix, "-", "_", "g")

    let typeName = substitute(prefix, "_\\(.\\)\\|^\\(.\\)", "\\U\\1\\U\\2", "g")
    let typeNamePrivate = typeName . "Private"

    let defineName = toupper(prefix)

    exec "normal ostatic GType" prefix . "_type = 0;"
    normal o

    call s:GOBGenerateCConstAndDest (prefix, typeName, typeNamePrivate, defineName)

    normal ovoid
    exec "normal o" . prefix . "_register_type (GTypeModule *module)"
    normal o{
    normal ostatic const GTypeInfo info = {
    exec "normal osizeof (" . typeName . "Class),"
    normal oNULL,
    normal oNULL,
    exec "normal o(GClassInitFunc)" prefix . "_class_init,"
    normal oNULL,
    normal oNULL,
    exec "normal osizeof (" . typeName . "),"
    normal o0,
    exec "normal o(GInstanceInitFunc)" prefix . "_init,"
    normal o};
    normal o

    exec "normal o" . prefix . "_type = g_type_module_register_type (module,"
    normal oG_TYPE_OBJECT,
    exec "normal o\"" . typeName . "\","
    normal o&info, 0);

    normal o}
    normal o

    normal oGType
    exec "normal o" . prefix . "_get_type ()"
    normal o{
    exec "normal oreturn" prefix . "_type;"
    normal o}
    normal o
endfunction


function! GOBGenerateC()
    let prefix = GOBGetPrefix()

    if prefix == ""
	echohl ErrorMsg
	echo "Can't create class without prefix"
	echohl None
	return
    endif

    let prefix = substitute(prefix, "-", "_", "g")

    let typeName = substitute(prefix, "_\\(.\\)\\|^\\(.\\)", "\\U\\1\\U\\2", "g")
    let typeNamePrivate = typeName . "Private"

    let defineName = toupper(prefix)

    exec "normal oG_DEFINE_TYPE (" . typeName . "," prefix . ", <ENTER_TYPE_HERE>);"
    normal o
    exec "normal o#define" defineName . "_GET_PRIVATE(o)\\"
    exec "normal o(G_TYPE_INSTANCE_GET_PRIVATE ((o)," defineName . "_TYPE," typeNamePrivate . "))"

    call s:GOBGenerateCConstAndDest (prefix, typeName, typeNamePrivate, defineName)
endfunction

function! GOBGenerateIC()
    let prefix = GOBGetPrefix()

    if prefix == ""
	echohl ErrorMsg
	echo "Can't create class without prefix"
	echohl None
	return
    endif

    let prefix = substitute(prefix, "-", "_", "g")
    let typeName = substitute(prefix, "_\\(i.\\)\\|^\\(.\\)\\|_\\(.\\)\\", "\\U\\1\\U\\2", "g")

    normal oGType
    exec "normal o" . prefix . "_get_type ()"
    normal o{
    normal ostatic GType type = 0;
    normal o
    normal oif (!type)
    normal o{
    normal ostatic const GTypeInfo info =
    normal o{
    exec "normal osizeof (" . typeName . "Interface), /*class_size*/"
    normal oNULL,		/* base_init */
    normal oNULL,		/* base_finalize */
    normal oNULL,
    normal oNULL,		/* class_finalize */
    normal oNULL,		/* class_data */
    normal o0,
    normal o0,              /* n_preallocs */
    normal oNULL
    normal o};
    normal o

    exec "normal otype = g_type_register_static (G_TYPE_INTERFACE, \"" . typeName . "\", &info, 0);"
    normal o}
    normal o
    normal oreturn type;
    normal o}

endfunction

function! GOBGenerateH()
    let prefix = GOBGetPrefix()

    if prefix == ""
	echohl ErrorMsg
	echo "Can't create class without prefix"
	echohl None
	return
    endif

    let parentName = input("Enter parent class name (Default GObject):")
    if parentName == ""
	let parentName = "GObject"
    endif

    let prefix = substitute(prefix, "-", "_", "g")

    let typeName = substitute(prefix, "_\\(.\\)\\|^\\(.\\)", "\\U\\1\\U\\2", "g")
    let typeNamePrivate = typeName . "Private"

    let defineName = toupper(prefix)

    normal IG_BEGIN_DECLS
    normal o
    exec "normal otypedef struct _" . typeNamePrivate typeNamePrivate . ";"
    normal o

    " Variable memeber structure
    normal otypedef struct {
    exec "normal o" . parentName "parent;"
    normal o
    exec "normal o" . typeNamePrivate "*priv;"
    exec "normal o}" typeName . ";"
    normal o

    normal otypedef struct {
    exec "normal o" . parentName . "Class parent;"
    exec "normal o}" typeName . "Class;"
    normal o

    " setup needed defines
    exec "normal o#define" defineName . "_TYPE (" . prefix . "_get_type ())"
    exec "normal o#define" defineName . "(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), " . defineName . "_TYPE," typeName . "))"
    exec "normal o#define" defineName . "_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), " . defineName . "_TYPE," typeName . "Class))"
    exec "normal o#define IS_" . defineName . "(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), " . defineName . "_TYPE))"
    exec "normal o#define IS_" . defineName . "_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), " . defineName . "_TYPE))"
    exec "normal o#define" defineName . "_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), " . defineName . "_TYPE," typeName . "Class))"

    normal 3o

    normal oG_END_DECLS
endfunction

function! GOBGenerateIH()
    let prefix = GOBGetPrefix()

    if prefix == ""
	echohl ErrorMsg
	echo "Can't create class without prefix"
	echohl None
	return
    endif


    let prefix = substitute(prefix, "-", "_", "g")
    let typeName = substitute(prefix, "_\\(i.\\)\\|^\\(.\\)\\|_\\(.\\)\\", "\\U\\1\\U\\2", "g")
    let defineName = toupper(prefix)

    normal IG_BEGIN_DECLS
    normal o
    exec "normal otypedef struct _" . typeName typeName . ";"
    normal o

    normal otypedef struct {
    normal oGTypeInterface parent;
    exec "normal o}" typeName . "Interface;"
    normal o

    " setup needed defines
    exec "normal o#define" defineName . "_TYPE (" . prefix . "_get_type ())"
    exec "normal o#define" defineName . "(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), " . defineName . "_TYPE," typeName . "))"
    exec "normal o#define IS_" . defineName . "(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), " . defineName . "_TYPE))"
    exec "normal o#define" defineName . "_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), " . defineName . "_TYPE," typeName . "Class))"

    normal 3o

    normal oG_END_DECLS
endfunction

command! GOBGenerateC call GOBGenerateC()
command! GOBGenerateH call GOBGenerateH()
command! GOBGenerateCModule call GOBGenerateCModule()
command! GOBGenerateIH call GOBGenerateIH()
command! GOBGenerateIC call GOBGenerateIC()