blob: f78391c9f3789f55fc16242f30fe8d642c835ec4 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020016#if PY_VERSION_HEX < 0x02050000
17typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
18#endif
19
Bram Moolenaar91805fc2011-06-26 04:01:44 +020020#ifdef FEAT_MBYTE
21# define ENC_OPT p_enc
22#else
23# define ENC_OPT "latin1"
24#endif
25
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020026#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
27
28#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
29#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020030#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020031
32static int ConvertFromPyObject(PyObject *, typval_T *);
33static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020034static PyObject *WindowNew(win_T *, tabpage_T *);
35static PyObject *BufferNew (buf_T *);
36static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037
38static PyInt RangeStart;
39static PyInt RangeEnd;
40
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020041/*
42 * obtain a lock on the Vim data structures
43 */
44 static void
45Python_Lock_Vim(void)
46{
47}
48
49/*
50 * release a lock on the Vim data structures
51 */
52 static void
53Python_Release_Vim(void)
54{
55}
56
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020057/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020058 */
59
Bram Moolenaar2eea1982010-09-21 16:49:37 +020060/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020061typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020062
63static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020064
65typedef struct
66{
67 PyObject_HEAD
68 long softspace;
69 long error;
70} OutputObject;
71
Bram Moolenaar77045652012-09-21 13:46:06 +020072 static int
73OutputSetattr(PyObject *self, char *name, PyObject *val)
74{
75 if (val == NULL)
76 {
Bram Moolenaar8661b172013-05-15 15:44:28 +020077 PyErr_SetString(PyExc_AttributeError,
78 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +020079 return -1;
80 }
81
82 if (strcmp(name, "softspace") == 0)
83 {
84 if (!PyInt_Check(val))
85 {
86 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
87 return -1;
88 }
89
90 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
91 return 0;
92 }
93
94 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
95 return -1;
96}
97
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020098/* Buffer IO, we write one whole line at a time. */
99static garray_T io_ga = {0, 0, 1, 80, NULL};
100static writefn old_fn = NULL;
101
102 static void
103PythonIO_Flush(void)
104{
105 if (old_fn != NULL && io_ga.ga_len > 0)
106 {
107 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
108 old_fn((char_u *)io_ga.ga_data);
109 }
110 io_ga.ga_len = 0;
111}
112
113 static void
114writer(writefn fn, char_u *str, PyInt n)
115{
116 char_u *ptr;
117
118 /* Flush when switching output function. */
119 if (fn != old_fn)
120 PythonIO_Flush();
121 old_fn = fn;
122
123 /* Write each NL separated line. Text after the last NL is kept for
124 * writing later. */
125 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
126 {
127 PyInt len = ptr - str;
128
129 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
130 break;
131
132 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
133 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
134 fn((char_u *)io_ga.ga_data);
135 str = ptr + 1;
136 n -= len + 1;
137 io_ga.ga_len = 0;
138 }
139
140 /* Put the remaining text into io_ga for later printing. */
141 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
142 {
143 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
144 io_ga.ga_len += (int)n;
145 }
146}
147
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200148 static PyObject *
149OutputWrite(PyObject *self, PyObject *args)
150{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200151 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200152 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200153 int error = ((OutputObject *)(self))->error;
154
Bram Moolenaar27564802011-09-07 19:30:21 +0200155 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200156 return NULL;
157
158 Py_BEGIN_ALLOW_THREADS
159 Python_Lock_Vim();
160 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
161 Python_Release_Vim();
162 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200163 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200164
165 Py_INCREF(Py_None);
166 return Py_None;
167}
168
169 static PyObject *
170OutputWritelines(PyObject *self, PyObject *args)
171{
172 PyInt n;
173 PyInt i;
174 PyObject *list;
175 int error = ((OutputObject *)(self))->error;
176
177 if (!PyArg_ParseTuple(args, "O", &list))
178 return NULL;
179 Py_INCREF(list);
180
Bram Moolenaardb913952012-06-29 12:54:53 +0200181 if (!PyList_Check(list))
182 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200183 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
184 Py_DECREF(list);
185 return NULL;
186 }
187
188 n = PyList_Size(list);
189
190 for (i = 0; i < n; ++i)
191 {
192 PyObject *line = PyList_GetItem(list, i);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200193 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200194 PyInt len;
195
Bram Moolenaardb913952012-06-29 12:54:53 +0200196 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
197 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200198 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
199 Py_DECREF(list);
200 return NULL;
201 }
202
203 Py_BEGIN_ALLOW_THREADS
204 Python_Lock_Vim();
205 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
206 Python_Release_Vim();
207 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200208 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200209 }
210
211 Py_DECREF(list);
212 Py_INCREF(Py_None);
213 return Py_None;
214}
215
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100216 static PyObject *
217OutputFlush(PyObject *self UNUSED, PyObject *args UNUSED)
218{
219 /* do nothing */
220 Py_INCREF(Py_None);
221 return Py_None;
222}
223
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200224/***************/
225
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200226static struct PyMethodDef OutputMethods[] = {
227 /* name, function, calling, documentation */
228 {"write", OutputWrite, 1, ""},
229 {"writelines", OutputWritelines, 1, ""},
230 {"flush", OutputFlush, 1, ""},
231 { NULL, NULL, 0, NULL}
232};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200233
234static OutputObject Output =
235{
236 PyObject_HEAD_INIT(&OutputType)
237 0,
238 0
239};
240
241static OutputObject Error =
242{
243 PyObject_HEAD_INIT(&OutputType)
244 0,
245 1
246};
247
248 static int
249PythonIO_Init_io(void)
250{
251 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
252 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
253
254 if (PyErr_Occurred())
255 {
256 EMSG(_("E264: Python: Error initialising I/O objects"));
257 return -1;
258 }
259
260 return 0;
261}
262
263
264static PyObject *VimError;
265
266/* Check to see whether a Vim error has been reported, or a keyboard
267 * interrupt has been detected.
268 */
269 static int
270VimErrorCheck(void)
271{
272 if (got_int)
273 {
274 PyErr_SetNone(PyExc_KeyboardInterrupt);
275 return 1;
276 }
277 else if (did_emsg && !PyErr_Occurred())
278 {
279 PyErr_SetNone(VimError);
280 return 1;
281 }
282
283 return 0;
284}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200285
286/* Vim module - Implementation
287 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200288
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200289 static PyObject *
290VimCommand(PyObject *self UNUSED, PyObject *args)
291{
292 char *cmd;
293 PyObject *result;
294
295 if (!PyArg_ParseTuple(args, "s", &cmd))
296 return NULL;
297
298 PyErr_Clear();
299
300 Py_BEGIN_ALLOW_THREADS
301 Python_Lock_Vim();
302
303 do_cmdline_cmd((char_u *)cmd);
304 update_screen(VALID);
305
306 Python_Release_Vim();
307 Py_END_ALLOW_THREADS
308
309 if (VimErrorCheck())
310 result = NULL;
311 else
312 result = Py_None;
313
314 Py_XINCREF(result);
315 return result;
316}
317
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200318/*
319 * Function to translate a typval_T into a PyObject; this will recursively
320 * translate lists/dictionaries into their Python equivalents.
321 *
322 * The depth parameter is to avoid infinite recursion, set it to 1 when
323 * you call VimToPython.
324 */
325 static PyObject *
326VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
327{
328 PyObject *result;
329 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200330 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200331
332 /* Avoid infinite recursion */
333 if (depth > 100)
334 {
335 Py_INCREF(Py_None);
336 result = Py_None;
337 return result;
338 }
339
340 /* Check if we run into a recursive loop. The item must be in lookupDict
341 * then and we can use it again. */
342 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
343 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
344 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200345 sprintf(ptrBuf, "%p",
346 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
347 : (void *)our_tv->vval.v_dict);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200348 result = PyDict_GetItemString(lookupDict, ptrBuf);
349 if (result != NULL)
350 {
351 Py_INCREF(result);
352 return result;
353 }
354 }
355
356 if (our_tv->v_type == VAR_STRING)
357 {
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200358 result = Py_BuildValue("s", our_tv->vval.v_string == NULL
359 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200360 }
361 else if (our_tv->v_type == VAR_NUMBER)
362 {
363 char buf[NUMBUFLEN];
364
365 /* For backwards compatibility numbers are stored as strings. */
366 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
367 result = Py_BuildValue("s", buf);
368 }
369# ifdef FEAT_FLOAT
370 else if (our_tv->v_type == VAR_FLOAT)
371 {
372 char buf[NUMBUFLEN];
373
374 sprintf(buf, "%f", our_tv->vval.v_float);
375 result = Py_BuildValue("s", buf);
376 }
377# endif
378 else if (our_tv->v_type == VAR_LIST)
379 {
380 list_T *list = our_tv->vval.v_list;
381 listitem_T *curr;
382
383 result = PyList_New(0);
384
385 if (list != NULL)
386 {
387 PyDict_SetItemString(lookupDict, ptrBuf, result);
388
389 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
390 {
391 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
392 PyList_Append(result, newObj);
393 Py_DECREF(newObj);
394 }
395 }
396 }
397 else if (our_tv->v_type == VAR_DICT)
398 {
399 result = PyDict_New();
400
401 if (our_tv->vval.v_dict != NULL)
402 {
403 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
404 long_u todo = ht->ht_used;
405 hashitem_T *hi;
406 dictitem_T *di;
407
408 PyDict_SetItemString(lookupDict, ptrBuf, result);
409
410 for (hi = ht->ht_array; todo > 0; ++hi)
411 {
412 if (!HASHITEM_EMPTY(hi))
413 {
414 --todo;
415
416 di = dict_lookup(hi);
417 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
418 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
419 Py_DECREF(newObj);
420 }
421 }
422 }
423 }
424 else
425 {
426 Py_INCREF(Py_None);
427 result = Py_None;
428 }
429
430 return result;
431}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200432
433 static PyObject *
Bram Moolenaar09092152010-08-08 16:38:42 +0200434VimEval(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200435{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200436 char *expr;
437 typval_T *our_tv;
438 PyObject *result;
439 PyObject *lookup_dict;
440
441 if (!PyArg_ParseTuple(args, "s", &expr))
442 return NULL;
443
444 Py_BEGIN_ALLOW_THREADS
445 Python_Lock_Vim();
446 our_tv = eval_expr((char_u *)expr, NULL);
447
448 Python_Release_Vim();
449 Py_END_ALLOW_THREADS
450
451 if (our_tv == NULL)
452 {
453 PyErr_SetVim(_("invalid expression"));
454 return NULL;
455 }
456
457 /* Convert the Vim type into a Python type. Create a dictionary that's
458 * used to check for recursive loops. */
459 lookup_dict = PyDict_New();
460 result = VimToPython(our_tv, 1, lookup_dict);
461 Py_DECREF(lookup_dict);
462
463
464 Py_BEGIN_ALLOW_THREADS
465 Python_Lock_Vim();
466 free_tv(our_tv);
467 Python_Release_Vim();
468 Py_END_ALLOW_THREADS
469
470 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200471}
472
Bram Moolenaardb913952012-06-29 12:54:53 +0200473static PyObject *ConvertToPyObject(typval_T *);
474
475 static PyObject *
476VimEvalPy(PyObject *self UNUSED, PyObject *args UNUSED)
477{
Bram Moolenaardb913952012-06-29 12:54:53 +0200478 char *expr;
479 typval_T *our_tv;
480 PyObject *result;
481
482 if (!PyArg_ParseTuple(args, "s", &expr))
483 return NULL;
484
485 Py_BEGIN_ALLOW_THREADS
486 Python_Lock_Vim();
487 our_tv = eval_expr((char_u *)expr, NULL);
488
489 Python_Release_Vim();
490 Py_END_ALLOW_THREADS
491
492 if (our_tv == NULL)
493 {
494 PyErr_SetVim(_("invalid expression"));
495 return NULL;
496 }
497
498 result = ConvertToPyObject(our_tv);
499 Py_BEGIN_ALLOW_THREADS
500 Python_Lock_Vim();
501 free_tv(our_tv);
502 Python_Release_Vim();
503 Py_END_ALLOW_THREADS
504
505 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200506}
507
508 static PyObject *
509VimStrwidth(PyObject *self UNUSED, PyObject *args)
510{
511 char *expr;
512
513 if (!PyArg_ParseTuple(args, "s", &expr))
514 return NULL;
515
Bram Moolenaara54bf402012-12-05 16:30:07 +0100516 return PyLong_FromLong(
517#ifdef FEAT_MBYTE
518 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
519#else
520 STRLEN(expr)
521#endif
522 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200523}
524
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200525/*
526 * Vim module - Definitions
527 */
528
529static struct PyMethodDef VimMethods[] = {
530 /* name, function, calling, documentation */
531 {"command", VimCommand, 1, "Execute a Vim ex-mode command" },
532 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200533 {"bindeval", VimEvalPy, 1, "Like eval(), but returns objects attached to vim ones"},
534 {"strwidth", VimStrwidth, 1, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200535 { NULL, NULL, 0, NULL }
536};
537
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200538/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200539 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200540 */
541
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200542static PyTypeObject IterType;
543
544typedef PyObject *(*nextfun)(void **);
545typedef void (*destructorfun)(void *);
546
547/* Main purpose of this object is removing the need for do python initialization
548 * (i.e. PyType_Ready and setting type attributes) for a big bunch of objects.
549 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200550
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200551typedef struct
552{
553 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200554 void *cur;
555 nextfun next;
556 destructorfun destruct;
557} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200558
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200559 static PyObject *
560IterNew(void *start, destructorfun destruct, nextfun next)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200561{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200562 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200563
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200564 self = PyObject_NEW(IterObject, &IterType);
565 self->cur = start;
566 self->next = next;
567 self->destruct = destruct;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200568
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200569 return (PyObject *)(self);
570}
571
572 static void
573IterDestructor(PyObject *self)
574{
575 IterObject *this = (IterObject *)(self);
576
577 this->destruct(this->cur);
578
579 DESTRUCTOR_FINISH(self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200580}
581
582 static PyObject *
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200583IterNext(PyObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200584{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200585 IterObject *this = (IterObject *)(self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200586
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200587 return this->next(&this->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200588}
589
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200590 static PyObject *
591IterIter(PyObject *self)
592{
593 return self;
594}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200595
Bram Moolenaardb913952012-06-29 12:54:53 +0200596typedef struct pylinkedlist_S {
597 struct pylinkedlist_S *pll_next;
598 struct pylinkedlist_S *pll_prev;
599 PyObject *pll_obj;
600} pylinkedlist_T;
601
602static pylinkedlist_T *lastdict = NULL;
603static pylinkedlist_T *lastlist = NULL;
604
605 static void
606pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
607{
608 if (ref->pll_prev == NULL)
609 {
610 if (ref->pll_next == NULL)
611 {
612 *last = NULL;
613 return;
614 }
615 }
616 else
617 ref->pll_prev->pll_next = ref->pll_next;
618
619 if (ref->pll_next == NULL)
620 *last = ref->pll_prev;
621 else
622 ref->pll_next->pll_prev = ref->pll_prev;
623}
624
625 static void
626pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
627{
628 if (*last == NULL)
629 ref->pll_prev = NULL;
630 else
631 {
632 (*last)->pll_next = ref;
633 ref->pll_prev = *last;
634 }
635 ref->pll_next = NULL;
636 ref->pll_obj = self;
637 *last = ref;
638}
639
640static PyTypeObject DictionaryType;
641
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200642#define DICTKEY_GET_NOTEMPTY(err) \
643 DICTKEY_GET(err) \
644 if (*key == NUL) \
645 { \
646 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
647 return err; \
648 }
649
Bram Moolenaardb913952012-06-29 12:54:53 +0200650typedef struct
651{
652 PyObject_HEAD
653 dict_T *dict;
654 pylinkedlist_T ref;
655} DictionaryObject;
656
657 static PyObject *
658DictionaryNew(dict_T *dict)
659{
660 DictionaryObject *self;
661
662 self = PyObject_NEW(DictionaryObject, &DictionaryType);
663 if (self == NULL)
664 return NULL;
665 self->dict = dict;
666 ++dict->dv_refcount;
667
668 pyll_add((PyObject *)(self), &self->ref, &lastdict);
669
670 return (PyObject *)(self);
671}
672
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200673 static void
674DictionaryDestructor(PyObject *self)
675{
676 DictionaryObject *this = ((DictionaryObject *) (self));
677
678 pyll_remove(&this->ref, &lastdict);
679 dict_unref(this->dict);
680
681 DESTRUCTOR_FINISH(self);
682}
683
Bram Moolenaardb913952012-06-29 12:54:53 +0200684 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200685DictionarySetattr(PyObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200686{
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200687 DictionaryObject *this = (DictionaryObject *)(self);
688
Bram Moolenaar66b79852012-09-21 14:00:35 +0200689 if (val == NULL)
690 {
691 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
692 return -1;
693 }
694
695 if (strcmp(name, "locked") == 0)
696 {
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200697 if (this->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200698 {
699 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
700 return -1;
701 }
702 else
703 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200704 int istrue = PyObject_IsTrue(val);
705 if (istrue == -1)
706 return -1;
707 else if (istrue)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200708 this->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200709 else
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200710 this->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200711 }
712 return 0;
713 }
714 else
715 {
716 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
717 return -1;
718 }
719}
720
721 static PyInt
Bram Moolenaardb913952012-06-29 12:54:53 +0200722DictionaryLength(PyObject *self)
723{
724 return ((PyInt) ((((DictionaryObject *)(self))->dict->dv_hashtab.ht_used)));
725}
726
727 static PyObject *
728DictionaryItem(PyObject *self, PyObject *keyObject)
729{
730 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200731 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200732 DICTKEY_DECL
733
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200734 DICTKEY_GET_NOTEMPTY(NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200735
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200736 di = dict_find(((DictionaryObject *) (self))->dict, key, -1);
737
Bram Moolenaar696c2112012-09-21 13:43:14 +0200738 DICTKEY_UNREF
739
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200740 if (di == NULL)
741 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200742 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200743 return NULL;
744 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200745
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200746 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200747}
748
749 static PyInt
750DictionaryAssItem(PyObject *self, PyObject *keyObject, PyObject *valObject)
751{
752 char_u *key;
753 typval_T tv;
754 dict_T *d = ((DictionaryObject *)(self))->dict;
755 dictitem_T *di;
756 DICTKEY_DECL
757
758 if (d->dv_lock)
759 {
760 PyErr_SetVim(_("dict is locked"));
761 return -1;
762 }
763
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200764 DICTKEY_GET_NOTEMPTY(-1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200765
766 di = dict_find(d, key, -1);
767
768 if (valObject == NULL)
769 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200770 hashitem_T *hi;
771
Bram Moolenaardb913952012-06-29 12:54:53 +0200772 if (di == NULL)
773 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200774 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200775 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +0200776 return -1;
777 }
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200778 hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaardb913952012-06-29 12:54:53 +0200779 hash_remove(&d->dv_hashtab, hi);
780 dictitem_free(di);
781 return 0;
782 }
783
784 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200785 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +0200786
787 if (di == NULL)
788 {
789 di = dictitem_alloc(key);
790 if (di == NULL)
791 {
792 PyErr_NoMemory();
793 return -1;
794 }
795 di->di_tv.v_lock = 0;
796
797 if (dict_add(d, di) == FAIL)
798 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200799 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +0200800 vim_free(di);
801 PyErr_SetVim(_("failed to add key to dictionary"));
802 return -1;
803 }
804 }
805 else
806 clear_tv(&di->di_tv);
807
808 DICTKEY_UNREF
809
810 copy_tv(&tv, &di->di_tv);
811 return 0;
812}
813
814 static PyObject *
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100815DictionaryListKeys(PyObject *self UNUSED)
Bram Moolenaardb913952012-06-29 12:54:53 +0200816{
817 dict_T *dict = ((DictionaryObject *)(self))->dict;
818 long_u todo = dict->dv_hashtab.ht_used;
819 Py_ssize_t i = 0;
820 PyObject *r;
821 hashitem_T *hi;
822
823 r = PyList_New(todo);
824 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
825 {
826 if (!HASHITEM_EMPTY(hi))
827 {
828 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
829 --todo;
830 ++i;
831 }
832 }
833 return r;
834}
835
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200836static PyMappingMethods DictionaryAsMapping = {
837 (lenfunc) DictionaryLength,
838 (binaryfunc) DictionaryItem,
839 (objobjargproc) DictionaryAssItem,
840};
841
Bram Moolenaardb913952012-06-29 12:54:53 +0200842static struct PyMethodDef DictionaryMethods[] = {
843 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
844 { NULL, NULL, 0, NULL }
845};
846
847static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200848static PySequenceMethods ListAsSeq;
849static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +0200850
851typedef struct
852{
853 PyObject_HEAD
854 list_T *list;
855 pylinkedlist_T ref;
856} ListObject;
857
858 static PyObject *
859ListNew(list_T *list)
860{
861 ListObject *self;
862
863 self = PyObject_NEW(ListObject, &ListType);
864 if (self == NULL)
865 return NULL;
866 self->list = list;
867 ++list->lv_refcount;
868
869 pyll_add((PyObject *)(self), &self->ref, &lastlist);
870
871 return (PyObject *)(self);
872}
873
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200874 static void
875ListDestructor(PyObject *self)
876{
877 ListObject *this = (ListObject *)(self);
878
879 pyll_remove(&this->ref, &lastlist);
880 list_unref(this->list);
881
882 DESTRUCTOR_FINISH(self);
883}
884
Bram Moolenaardb913952012-06-29 12:54:53 +0200885 static int
886list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict)
887{
888 Py_ssize_t i;
889 Py_ssize_t lsize = PySequence_Size(obj);
890 PyObject *litem;
891 listitem_T *li;
892
893 for(i=0; i<lsize; i++)
894 {
895 li = listitem_alloc();
896 if (li == NULL)
897 {
898 PyErr_NoMemory();
899 return -1;
900 }
901 li->li_tv.v_lock = 0;
902
903 litem = PySequence_GetItem(obj, i);
904 if (litem == NULL)
905 return -1;
906 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1)
907 return -1;
908
909 list_append(l, li);
910 }
911 return 0;
912}
913
Bram Moolenaardb913952012-06-29 12:54:53 +0200914 static PyInt
915ListLength(PyObject *self)
916{
917 return ((PyInt) (((ListObject *) (self))->list->lv_len));
918}
919
920 static PyObject *
921ListItem(PyObject *self, Py_ssize_t index)
922{
923 listitem_T *li;
924
925 if (index>=ListLength(self))
926 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200927 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200928 return NULL;
929 }
930 li = list_find(((ListObject *) (self))->list, (long) index);
931 if (li == NULL)
932 {
933 PyErr_SetVim(_("internal error: failed to get vim list item"));
934 return NULL;
935 }
936 return ConvertToPyObject(&li->li_tv);
937}
938
939#define PROC_RANGE \
940 if (last < 0) {\
941 if (last < -size) \
942 last = 0; \
943 else \
944 last += size; \
945 } \
946 if (first < 0) \
947 first = 0; \
948 if (first > size) \
949 first = size; \
950 if (last > size) \
951 last = size;
952
953 static PyObject *
954ListSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last)
955{
956 PyInt i;
957 PyInt size = ListLength(self);
958 PyInt n;
959 PyObject *list;
960 int reversed = 0;
961
962 PROC_RANGE
963 if (first >= last)
964 first = last;
965
966 n = last-first;
967 list = PyList_New(n);
968 if (list == NULL)
969 return NULL;
970
971 for (i = 0; i < n; ++i)
972 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +0200973 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +0200974 if (item == NULL)
975 {
976 Py_DECREF(list);
977 return NULL;
978 }
979
980 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
981 {
982 Py_DECREF(item);
983 Py_DECREF(list);
984 return NULL;
985 }
986 }
987
988 return list;
989}
990
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200991typedef struct
992{
993 listwatch_T lw;
994 list_T *list;
995} listiterinfo_T;
996
997 static void
998ListIterDestruct(listiterinfo_T *lii)
999{
1000 list_rem_watch(lii->list, &lii->lw);
1001 PyMem_Free(lii);
1002}
1003
1004 static PyObject *
1005ListIterNext(listiterinfo_T **lii)
1006{
1007 PyObject *r;
1008
1009 if (!((*lii)->lw.lw_item))
1010 return NULL;
1011
1012 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1013 return NULL;
1014
1015 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1016
1017 return r;
1018}
1019
1020 static PyObject *
1021ListIter(PyObject *self)
1022{
1023 listiterinfo_T *lii;
1024 list_T *l = ((ListObject *) (self))->list;
1025
1026 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1027 {
1028 PyErr_NoMemory();
1029 return NULL;
1030 }
1031
1032 list_add_watch(l, &lii->lw);
1033 lii->lw.lw_item = l->lv_first;
1034 lii->list = l;
1035
1036 return IterNew(lii,
1037 (destructorfun) ListIterDestruct, (nextfun) ListIterNext);
1038}
1039
Bram Moolenaardb913952012-06-29 12:54:53 +02001040 static int
1041ListAssItem(PyObject *self, Py_ssize_t index, PyObject *obj)
1042{
1043 typval_T tv;
1044 list_T *l = ((ListObject *) (self))->list;
1045 listitem_T *li;
1046 Py_ssize_t length = ListLength(self);
1047
1048 if (l->lv_lock)
1049 {
1050 PyErr_SetVim(_("list is locked"));
1051 return -1;
1052 }
1053 if (index>length || (index==length && obj==NULL))
1054 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001055 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001056 return -1;
1057 }
1058
1059 if (obj == NULL)
1060 {
1061 li = list_find(l, (long) index);
1062 list_remove(l, li, li);
1063 clear_tv(&li->li_tv);
1064 vim_free(li);
1065 return 0;
1066 }
1067
1068 if (ConvertFromPyObject(obj, &tv) == -1)
1069 return -1;
1070
1071 if (index == length)
1072 {
1073 if (list_append_tv(l, &tv) == FAIL)
1074 {
1075 PyErr_SetVim(_("Failed to add item to list"));
1076 return -1;
1077 }
1078 }
1079 else
1080 {
1081 li = list_find(l, (long) index);
1082 clear_tv(&li->li_tv);
1083 copy_tv(&tv, &li->li_tv);
1084 }
1085 return 0;
1086}
1087
1088 static int
1089ListAssSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
1090{
1091 PyInt size = ListLength(self);
1092 Py_ssize_t i;
1093 Py_ssize_t lsize;
1094 PyObject *litem;
1095 listitem_T *li;
1096 listitem_T *next;
1097 typval_T v;
1098 list_T *l = ((ListObject *) (self))->list;
1099
1100 if (l->lv_lock)
1101 {
1102 PyErr_SetVim(_("list is locked"));
1103 return -1;
1104 }
1105
1106 PROC_RANGE
1107
1108 if (first == size)
1109 li = NULL;
1110 else
1111 {
1112 li = list_find(l, (long) first);
1113 if (li == NULL)
1114 {
1115 PyErr_SetVim(_("internal error: no vim list item"));
1116 return -1;
1117 }
1118 if (last > first)
1119 {
1120 i = last - first;
1121 while (i-- && li != NULL)
1122 {
1123 next = li->li_next;
1124 listitem_remove(l, li);
1125 li = next;
1126 }
1127 }
1128 }
1129
1130 if (obj == NULL)
1131 return 0;
1132
1133 if (!PyList_Check(obj))
1134 {
1135 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1136 return -1;
1137 }
1138
1139 lsize = PyList_Size(obj);
1140
1141 for(i=0; i<lsize; i++)
1142 {
1143 litem = PyList_GetItem(obj, i);
1144 if (litem == NULL)
1145 return -1;
1146 if (ConvertFromPyObject(litem, &v) == -1)
1147 return -1;
1148 if (list_insert_tv(l, &v, li) == FAIL)
1149 {
1150 PyErr_SetVim(_("internal error: failed to add item to list"));
1151 return -1;
1152 }
1153 }
1154 return 0;
1155}
1156
1157 static PyObject *
1158ListConcatInPlace(PyObject *self, PyObject *obj)
1159{
1160 list_T *l = ((ListObject *) (self))->list;
1161 PyObject *lookup_dict;
1162
1163 if (l->lv_lock)
1164 {
1165 PyErr_SetVim(_("list is locked"));
1166 return NULL;
1167 }
1168
1169 if (!PySequence_Check(obj))
1170 {
1171 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1172 return NULL;
1173 }
1174
1175 lookup_dict = PyDict_New();
1176 if (list_py_concat(l, obj, lookup_dict) == -1)
1177 {
1178 Py_DECREF(lookup_dict);
1179 return NULL;
1180 }
1181 Py_DECREF(lookup_dict);
1182
1183 Py_INCREF(self);
1184 return self;
1185}
1186
Bram Moolenaar66b79852012-09-21 14:00:35 +02001187 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001188ListSetattr(PyObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001189{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001190 ListObject *this = (ListObject *)(self);
1191
Bram Moolenaar66b79852012-09-21 14:00:35 +02001192 if (val == NULL)
1193 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001194 PyErr_SetString(PyExc_AttributeError,
1195 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001196 return -1;
1197 }
1198
1199 if (strcmp(name, "locked") == 0)
1200 {
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001201 if (this->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001202 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001203 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001204 return -1;
1205 }
1206 else
1207 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001208 int istrue = PyObject_IsTrue(val);
1209 if (istrue == -1)
1210 return -1;
1211 else if (istrue)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001212 this->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001213 else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001214 this->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001215 }
1216 return 0;
1217 }
1218 else
1219 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001220 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001221 return -1;
1222 }
1223}
1224
Bram Moolenaardb913952012-06-29 12:54:53 +02001225static struct PyMethodDef ListMethods[] = {
1226 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1227 { NULL, NULL, 0, NULL }
1228};
1229
1230typedef struct
1231{
1232 PyObject_HEAD
1233 char_u *name;
1234} FunctionObject;
1235
1236static PyTypeObject FunctionType;
1237
1238 static PyObject *
1239FunctionNew(char_u *name)
1240{
1241 FunctionObject *self;
1242
1243 self = PyObject_NEW(FunctionObject, &FunctionType);
1244 if (self == NULL)
1245 return NULL;
1246 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1247 if (self->name == NULL)
1248 {
1249 PyErr_NoMemory();
1250 return NULL;
1251 }
1252 STRCPY(self->name, name);
1253 func_ref(name);
1254 return (PyObject *)(self);
1255}
1256
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001257 static void
1258FunctionDestructor(PyObject *self)
1259{
1260 FunctionObject *this = (FunctionObject *) (self);
1261
1262 func_unref(this->name);
1263 PyMem_Del(this->name);
1264
1265 DESTRUCTOR_FINISH(self);
1266}
1267
Bram Moolenaardb913952012-06-29 12:54:53 +02001268 static PyObject *
1269FunctionCall(PyObject *self, PyObject *argsObject, PyObject *kwargs)
1270{
1271 FunctionObject *this = (FunctionObject *)(self);
1272 char_u *name = this->name;
1273 typval_T args;
1274 typval_T selfdicttv;
1275 typval_T rettv;
1276 dict_T *selfdict = NULL;
1277 PyObject *selfdictObject;
1278 PyObject *result;
1279 int error;
1280
1281 if (ConvertFromPyObject(argsObject, &args) == -1)
1282 return NULL;
1283
1284 if (kwargs != NULL)
1285 {
1286 selfdictObject = PyDict_GetItemString(kwargs, "self");
1287 if (selfdictObject != NULL)
1288 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001289 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001290 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001291 PyErr_SetString(PyExc_TypeError,
1292 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001293 clear_tv(&args);
1294 return NULL;
1295 }
1296 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
1297 return NULL;
1298 selfdict = selfdicttv.vval.v_dict;
1299 }
1300 }
1301
Bram Moolenaar71700b82013-05-15 17:49:05 +02001302 Py_BEGIN_ALLOW_THREADS
1303 Python_Lock_Vim();
1304
Bram Moolenaardb913952012-06-29 12:54:53 +02001305 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001306
1307 Python_Release_Vim();
1308 Py_END_ALLOW_THREADS
1309
Bram Moolenaardb913952012-06-29 12:54:53 +02001310 if (error != OK)
1311 {
1312 result = NULL;
1313 PyErr_SetVim(_("failed to run function"));
1314 }
1315 else
1316 result = ConvertToPyObject(&rettv);
1317
1318 /* FIXME Check what should really be cleared. */
1319 clear_tv(&args);
1320 clear_tv(&rettv);
1321 /*
1322 * if (selfdict!=NULL)
1323 * clear_tv(selfdicttv);
1324 */
1325
1326 return result;
1327}
1328
1329static struct PyMethodDef FunctionMethods[] = {
1330 {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1331 { NULL, NULL, 0, NULL }
1332};
1333
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001334/*
1335 * Options object
1336 */
1337
1338static PyTypeObject OptionsType;
1339
1340typedef int (*checkfun)(void *);
1341
1342typedef struct
1343{
1344 PyObject_HEAD
1345 int opt_type;
1346 void *from;
1347 checkfun Check;
1348 PyObject *fromObj;
1349} OptionsObject;
1350
1351 static PyObject *
1352OptionsItem(OptionsObject *this, PyObject *keyObject)
1353{
1354 char_u *key;
1355 int flags;
1356 long numval;
1357 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001358 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001359
1360 if (this->Check(this->from))
1361 return NULL;
1362
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001363 DICTKEY_GET_NOTEMPTY(NULL)
1364
1365 flags = get_option_value_strict(key, &numval, &stringval,
1366 this->opt_type, this->from);
1367
1368 DICTKEY_UNREF
1369
1370 if (flags == 0)
1371 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001372 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001373 return NULL;
1374 }
1375
1376 if (flags & SOPT_UNSET)
1377 {
1378 Py_INCREF(Py_None);
1379 return Py_None;
1380 }
1381 else if (flags & SOPT_BOOL)
1382 {
1383 PyObject *r;
1384 r = numval ? Py_True : Py_False;
1385 Py_INCREF(r);
1386 return r;
1387 }
1388 else if (flags & SOPT_NUM)
1389 return PyInt_FromLong(numval);
1390 else if (flags & SOPT_STRING)
1391 {
1392 if (stringval)
1393 return PyBytes_FromString((char *) stringval);
1394 else
1395 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001396 PyErr_SetString(PyExc_RuntimeError,
1397 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001398 return NULL;
1399 }
1400 }
1401 else
1402 {
1403 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1404 return NULL;
1405 }
1406}
1407
1408 static int
1409set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1410 char_u *key;
1411 int numval;
1412 char_u *stringval;
1413 int opt_flags;
1414 int opt_type;
1415 void *from;
1416{
1417 win_T *save_curwin;
1418 tabpage_T *save_curtab;
Bram Moolenaar105bc352013-05-17 16:03:57 +02001419 buf_T *save_curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001420 int r = 0;
1421
1422 switch (opt_type)
1423 {
1424 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001425 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1426 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001427 {
1428 PyErr_SetVim("Problem while switching windows.");
1429 return -1;
1430 }
1431 set_option_value(key, numval, stringval, opt_flags);
1432 restore_win(save_curwin, save_curtab);
1433 break;
1434 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001435 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001436 set_option_value(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001437 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001438 break;
1439 case SREQ_GLOBAL:
1440 set_option_value(key, numval, stringval, opt_flags);
1441 break;
1442 }
1443 return r;
1444}
1445
1446 static int
1447OptionsAssItem(OptionsObject *this, PyObject *keyObject, PyObject *valObject)
1448{
1449 char_u *key;
1450 int flags;
1451 int opt_flags;
1452 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001453 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001454
1455 if (this->Check(this->from))
1456 return -1;
1457
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001458 DICTKEY_GET_NOTEMPTY(-1)
1459
1460 flags = get_option_value_strict(key, NULL, NULL,
1461 this->opt_type, this->from);
1462
1463 DICTKEY_UNREF
1464
1465 if (flags == 0)
1466 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001467 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001468 return -1;
1469 }
1470
1471 if (valObject == NULL)
1472 {
1473 if (this->opt_type == SREQ_GLOBAL)
1474 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001475 PyErr_SetString(PyExc_ValueError,
1476 _("unable to unset global option"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001477 return -1;
1478 }
1479 else if (!(flags & SOPT_GLOBAL))
1480 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001481 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1482 "without global value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001483 return -1;
1484 }
1485 else
1486 {
1487 unset_global_local_option(key, this->from);
1488 return 0;
1489 }
1490 }
1491
1492 opt_flags = (this->opt_type ? OPT_LOCAL : OPT_GLOBAL);
1493
1494 if (flags & SOPT_BOOL)
1495 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001496 int istrue = PyObject_IsTrue(valObject);
1497 if (istrue == -1)
1498 return -1;
1499 r = set_option_value_for(key, istrue, NULL,
Bram Moolenaar03db85b2013-05-15 14:51:35 +02001500 opt_flags, this->opt_type, this->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001501 }
1502 else if (flags & SOPT_NUM)
1503 {
1504 int val;
1505
1506#if PY_MAJOR_VERSION < 3
1507 if (PyInt_Check(valObject))
1508 val = PyInt_AsLong(valObject);
1509 else
1510#endif
1511 if (PyLong_Check(valObject))
1512 val = PyLong_AsLong(valObject);
1513 else
1514 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001515 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001516 return -1;
1517 }
1518
1519 r = set_option_value_for(key, val, NULL, opt_flags,
1520 this->opt_type, this->from);
1521 }
1522 else
1523 {
1524 char_u *val;
1525 if (PyBytes_Check(valObject))
1526 {
1527
1528 if (PyString_AsStringAndSize(valObject, (char **) &val, NULL) == -1)
1529 return -1;
1530 if (val == NULL)
1531 return -1;
1532
1533 val = vim_strsave(val);
1534 }
1535 else if (PyUnicode_Check(valObject))
1536 {
1537 PyObject *bytes;
1538
1539 bytes = PyUnicode_AsEncodedString(valObject, (char *)ENC_OPT, NULL);
1540 if (bytes == NULL)
1541 return -1;
1542
1543 if(PyString_AsStringAndSize(bytes, (char **) &val, NULL) == -1)
1544 return -1;
1545 if (val == NULL)
1546 return -1;
1547
1548 val = vim_strsave(val);
1549 Py_XDECREF(bytes);
1550 }
1551 else
1552 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001553 PyErr_SetString(PyExc_TypeError, _("object must be string"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001554 return -1;
1555 }
1556
1557 r = set_option_value_for(key, 0, val, opt_flags,
1558 this->opt_type, this->from);
1559 vim_free(val);
1560 }
1561
1562 return r;
1563}
1564
1565 static int
1566dummy_check(void *arg UNUSED)
1567{
1568 return 0;
1569}
1570
1571 static PyObject *
1572OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1573{
1574 OptionsObject *self;
1575
1576 self = PyObject_NEW(OptionsObject, &OptionsType);
1577 if (self == NULL)
1578 return NULL;
1579
1580 self->opt_type = opt_type;
1581 self->from = from;
1582 self->Check = Check;
1583 self->fromObj = fromObj;
1584 if (fromObj)
1585 Py_INCREF(fromObj);
1586
1587 return (PyObject *)(self);
1588}
1589
1590 static void
1591OptionsDestructor(PyObject *self)
1592{
1593 if (((OptionsObject *)(self))->fromObj)
1594 Py_DECREF(((OptionsObject *)(self))->fromObj);
1595 DESTRUCTOR_FINISH(self);
1596}
1597
1598static PyMappingMethods OptionsAsMapping = {
1599 (lenfunc) NULL,
1600 (binaryfunc) OptionsItem,
1601 (objobjargproc) OptionsAssItem,
1602};
1603
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001604/* Tabpage object
1605 */
1606
1607typedef struct
1608{
1609 PyObject_HEAD
1610 tabpage_T *tab;
1611} TabPageObject;
1612
1613static PyObject *WinListNew(TabPageObject *tabObject);
1614
1615static PyTypeObject TabPageType;
1616
1617 static int
1618CheckTabPage(TabPageObject *this)
1619{
1620 if (this->tab == INVALID_TABPAGE_VALUE)
1621 {
1622 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1623 return -1;
1624 }
1625
1626 return 0;
1627}
1628
1629 static PyObject *
1630TabPageNew(tabpage_T *tab)
1631{
1632 TabPageObject *self;
1633
1634 if (TAB_PYTHON_REF(tab))
1635 {
1636 self = TAB_PYTHON_REF(tab);
1637 Py_INCREF(self);
1638 }
1639 else
1640 {
1641 self = PyObject_NEW(TabPageObject, &TabPageType);
1642 if (self == NULL)
1643 return NULL;
1644 self->tab = tab;
1645 TAB_PYTHON_REF(tab) = self;
1646 }
1647
1648 return (PyObject *)(self);
1649}
1650
1651 static void
1652TabPageDestructor(PyObject *self)
1653{
1654 TabPageObject *this = (TabPageObject *)(self);
1655
1656 if (this->tab && this->tab != INVALID_TABPAGE_VALUE)
1657 TAB_PYTHON_REF(this->tab) = NULL;
1658
1659 DESTRUCTOR_FINISH(self);
1660}
1661
1662 static PyObject *
1663TabPageAttr(TabPageObject *this, char *name)
1664{
1665 if (strcmp(name, "windows") == 0)
1666 return WinListNew(this);
1667 else if (strcmp(name, "number") == 0)
1668 return PyLong_FromLong((long) get_tab_number(this->tab));
1669 else if (strcmp(name, "vars") == 0)
1670 return DictionaryNew(this->tab->tp_vars);
1671 else if (strcmp(name, "window") == 0)
1672 {
1673 /* For current tab window.c does not bother to set or update tp_curwin
1674 */
1675 if (this->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001676 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001677 else
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001678 return WindowNew(this->tab->tp_curwin, this->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001679 }
1680 return NULL;
1681}
1682
1683 static PyObject *
1684TabPageRepr(PyObject *self)
1685{
1686 static char repr[100];
1687 TabPageObject *this = (TabPageObject *)(self);
1688
1689 if (this->tab == INVALID_TABPAGE_VALUE)
1690 {
1691 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
1692 return PyString_FromString(repr);
1693 }
1694 else
1695 {
1696 int t = get_tab_number(this->tab);
1697
1698 if (t == 0)
1699 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
1700 (self));
1701 else
1702 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
1703
1704 return PyString_FromString(repr);
1705 }
1706}
1707
1708static struct PyMethodDef TabPageMethods[] = {
1709 /* name, function, calling, documentation */
1710 { NULL, NULL, 0, NULL }
1711};
1712
1713/*
1714 * Window list object
1715 */
1716
1717static PyTypeObject TabListType;
1718static PySequenceMethods TabListAsSeq;
1719
1720typedef struct
1721{
1722 PyObject_HEAD
1723} TabListObject;
1724
1725 static PyInt
1726TabListLength(PyObject *self UNUSED)
1727{
1728 tabpage_T *tp = first_tabpage;
1729 PyInt n = 0;
1730
1731 while (tp != NULL)
1732 {
1733 ++n;
1734 tp = tp->tp_next;
1735 }
1736
1737 return n;
1738}
1739
1740 static PyObject *
1741TabListItem(PyObject *self UNUSED, PyInt n)
1742{
1743 tabpage_T *tp;
1744
1745 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
1746 if (n == 0)
1747 return TabPageNew(tp);
1748
1749 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
1750 return NULL;
1751}
1752
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001753/* Window object
1754 */
1755
1756typedef struct
1757{
1758 PyObject_HEAD
1759 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001760 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001761} WindowObject;
1762
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001763static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001764
1765 static int
1766CheckWindow(WindowObject *this)
1767{
1768 if (this->win == INVALID_WINDOW_VALUE)
1769 {
1770 PyErr_SetVim(_("attempt to refer to deleted window"));
1771 return -1;
1772 }
1773
1774 return 0;
1775}
1776
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001777 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001778WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02001779{
1780 /* We need to handle deletion of windows underneath us.
1781 * If we add a "w_python*_ref" field to the win_T structure,
1782 * then we can get at it in win_free() in vim. We then
1783 * need to create only ONE Python object per window - if
1784 * we try to create a second, just INCREF the existing one
1785 * and return it. The (single) Python object referring to
1786 * the window is stored in "w_python*_ref".
1787 * On a win_free() we set the Python object's win_T* field
1788 * to an invalid value. We trap all uses of a window
1789 * object, and reject them if the win_T* field is invalid.
1790 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001791 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02001792 * w_python_ref and w_python3_ref fields respectively.
1793 */
1794
1795 WindowObject *self;
1796
1797 if (WIN_PYTHON_REF(win))
1798 {
1799 self = WIN_PYTHON_REF(win);
1800 Py_INCREF(self);
1801 }
1802 else
1803 {
1804 self = PyObject_NEW(WindowObject, &WindowType);
1805 if (self == NULL)
1806 return NULL;
1807 self->win = win;
1808 WIN_PYTHON_REF(win) = self;
1809 }
1810
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001811 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
1812
Bram Moolenaar971db462013-05-12 18:44:48 +02001813 return (PyObject *)(self);
1814}
1815
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001816 static void
1817WindowDestructor(PyObject *self)
1818{
1819 WindowObject *this = (WindowObject *)(self);
1820
1821 if (this->win && this->win != INVALID_WINDOW_VALUE)
1822 WIN_PYTHON_REF(this->win) = NULL;
1823
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001824 Py_DECREF(((PyObject *)(this->tabObject)));
1825
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001826 DESTRUCTOR_FINISH(self);
1827}
1828
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001829 static win_T *
1830get_firstwin(TabPageObject *tabObject)
1831{
1832 if (tabObject)
1833 {
1834 if (CheckTabPage(tabObject))
1835 return NULL;
1836 /* For current tab window.c does not bother to set or update tp_firstwin
1837 */
1838 else if (tabObject->tab == curtab)
1839 return firstwin;
1840 else
1841 return tabObject->tab->tp_firstwin;
1842 }
1843 else
1844 return firstwin;
1845}
1846
Bram Moolenaar971db462013-05-12 18:44:48 +02001847 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001848WindowAttr(WindowObject *this, char *name)
1849{
1850 if (strcmp(name, "buffer") == 0)
1851 return (PyObject *)BufferNew(this->win->w_buffer);
1852 else if (strcmp(name, "cursor") == 0)
1853 {
1854 pos_T *pos = &this->win->w_cursor;
1855
1856 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1857 }
1858 else if (strcmp(name, "height") == 0)
Bram Moolenaar99add412013-05-12 19:09:51 +02001859 return PyLong_FromLong((long)(this->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001860#ifdef FEAT_WINDOWS
1861 else if (strcmp(name, "row") == 0)
1862 return PyLong_FromLong((long)(this->win->w_winrow));
1863#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001864#ifdef FEAT_VERTSPLIT
1865 else if (strcmp(name, "width") == 0)
Bram Moolenaar99add412013-05-12 19:09:51 +02001866 return PyLong_FromLong((long)(W_WIDTH(this->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001867 else if (strcmp(name, "col") == 0)
1868 return PyLong_FromLong((long)(W_WINCOL(this->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001869#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001870 else if (strcmp(name, "vars") == 0)
1871 return DictionaryNew(this->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001872 else if (strcmp(name, "options") == 0)
1873 return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow,
1874 (PyObject *) this);
Bram Moolenaar6d216452013-05-12 19:00:41 +02001875 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001876 {
1877 if (CheckTabPage(this->tabObject))
1878 return NULL;
1879 return PyLong_FromLong((long)
1880 get_win_number(this->win, get_firstwin(this->tabObject)));
1881 }
1882 else if (strcmp(name, "tabpage") == 0)
1883 {
1884 Py_INCREF(this->tabObject);
1885 return (PyObject *)(this->tabObject);
1886 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001887 else if (strcmp(name,"__members__") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001888 return Py_BuildValue("[sssssssss]", "buffer", "cursor", "height",
1889 "vars", "options", "number", "row", "col", "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001890 else
1891 return NULL;
1892}
1893
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001894 static int
1895WindowSetattr(PyObject *self, char *name, PyObject *val)
1896{
1897 WindowObject *this = (WindowObject *)(self);
1898
1899 if (CheckWindow(this))
1900 return -1;
1901
1902 if (strcmp(name, "buffer") == 0)
1903 {
1904 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
1905 return -1;
1906 }
1907 else if (strcmp(name, "cursor") == 0)
1908 {
1909 long lnum;
1910 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001911
1912 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
1913 return -1;
1914
1915 if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count)
1916 {
1917 PyErr_SetVim(_("cursor position outside buffer"));
1918 return -1;
1919 }
1920
1921 /* Check for keyboard interrupts */
1922 if (VimErrorCheck())
1923 return -1;
1924
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001925 this->win->w_cursor.lnum = lnum;
1926 this->win->w_cursor.col = col;
1927#ifdef FEAT_VIRTUALEDIT
1928 this->win->w_cursor.coladd = 0;
1929#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02001930 /* When column is out of range silently correct it. */
1931 check_cursor_col_win(this->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001932
Bram Moolenaar03a807a2011-07-07 15:08:58 +02001933 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001934 return 0;
1935 }
1936 else if (strcmp(name, "height") == 0)
1937 {
1938 int height;
1939 win_T *savewin;
1940
1941 if (!PyArg_Parse(val, "i", &height))
1942 return -1;
1943
1944#ifdef FEAT_GUI
1945 need_mouse_correct = TRUE;
1946#endif
1947 savewin = curwin;
1948 curwin = this->win;
1949 win_setheight(height);
1950 curwin = savewin;
1951
1952 /* Check for keyboard interrupts */
1953 if (VimErrorCheck())
1954 return -1;
1955
1956 return 0;
1957 }
1958#ifdef FEAT_VERTSPLIT
1959 else if (strcmp(name, "width") == 0)
1960 {
1961 int width;
1962 win_T *savewin;
1963
1964 if (!PyArg_Parse(val, "i", &width))
1965 return -1;
1966
1967#ifdef FEAT_GUI
1968 need_mouse_correct = TRUE;
1969#endif
1970 savewin = curwin;
1971 curwin = this->win;
1972 win_setwidth(width);
1973 curwin = savewin;
1974
1975 /* Check for keyboard interrupts */
1976 if (VimErrorCheck())
1977 return -1;
1978
1979 return 0;
1980 }
1981#endif
1982 else
1983 {
1984 PyErr_SetString(PyExc_AttributeError, name);
1985 return -1;
1986 }
1987}
1988
1989 static PyObject *
1990WindowRepr(PyObject *self)
1991{
1992 static char repr[100];
1993 WindowObject *this = (WindowObject *)(self);
1994
1995 if (this->win == INVALID_WINDOW_VALUE)
1996 {
1997 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
1998 return PyString_FromString(repr);
1999 }
2000 else
2001 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002002 int w = get_win_number(this->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002003
Bram Moolenaar6d216452013-05-12 19:00:41 +02002004 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002005 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2006 (self));
2007 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002008 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002009
2010 return PyString_FromString(repr);
2011 }
2012}
2013
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002014static struct PyMethodDef WindowMethods[] = {
2015 /* name, function, calling, documentation */
2016 { NULL, NULL, 0, NULL }
2017};
2018
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002019/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002020 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002021 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002022
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002023static PyTypeObject WinListType;
2024static PySequenceMethods WinListAsSeq;
2025
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002026typedef struct
2027{
2028 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002029 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002030} WinListObject;
2031
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002032 static PyObject *
2033WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002034{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002035 WinListObject *self;
2036
2037 self = PyObject_NEW(WinListObject, &WinListType);
2038 self->tabObject = tabObject;
2039 Py_INCREF(tabObject);
2040
2041 return (PyObject *)(self);
2042}
2043
2044 static void
2045WinListDestructor(PyObject *self)
2046{
2047 TabPageObject *tabObject = ((WinListObject *)(self))->tabObject;
2048
2049 if (tabObject)
2050 Py_DECREF((PyObject *)(tabObject));
2051
2052 DESTRUCTOR_FINISH(self);
2053}
2054
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002055 static PyInt
2056WinListLength(PyObject *self)
2057{
2058 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002059 PyInt n = 0;
2060
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002061 if (!(w = get_firstwin(((WinListObject *)(self))->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002062 return -1;
2063
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002064 while (w != NULL)
2065 {
2066 ++n;
2067 w = W_NEXT(w);
2068 }
2069
2070 return n;
2071}
2072
2073 static PyObject *
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002074WinListItem(PyObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002075{
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002076 WinListObject *this = ((WinListObject *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002077 win_T *w;
2078
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002079 if (!(w = get_firstwin(this->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002080 return NULL;
2081
2082 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002083 if (n == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002084 return WindowNew(w, this->tabObject? this->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002085
2086 PyErr_SetString(PyExc_IndexError, _("no such window"));
2087 return NULL;
2088}
2089
2090/* Convert a Python string into a Vim line.
2091 *
2092 * The result is in allocated memory. All internal nulls are replaced by
2093 * newline characters. It is an error for the string to contain newline
2094 * characters.
2095 *
2096 * On errors, the Python exception data is set, and NULL is returned.
2097 */
2098 static char *
2099StringToLine(PyObject *obj)
2100{
2101 const char *str;
2102 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002103 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002104 PyInt len;
2105 PyInt i;
2106 char *p;
2107
2108 if (obj == NULL || !PyString_Check(obj))
2109 {
2110 PyErr_BadArgument();
2111 return NULL;
2112 }
2113
Bram Moolenaar19e60942011-06-19 00:27:51 +02002114 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2115 str = PyString_AsString(bytes);
2116 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002117
2118 /*
2119 * Error checking: String must not contain newlines, as we
2120 * are replacing a single line, and we must replace it with
2121 * a single line.
2122 * A trailing newline is removed, so that append(f.readlines()) works.
2123 */
2124 p = memchr(str, '\n', len);
2125 if (p != NULL)
2126 {
2127 if (p == str + len - 1)
2128 --len;
2129 else
2130 {
2131 PyErr_SetVim(_("string cannot contain newlines"));
2132 return NULL;
2133 }
2134 }
2135
2136 /* Create a copy of the string, with internal nulls replaced by
2137 * newline characters, as is the vim convention.
2138 */
2139 save = (char *)alloc((unsigned)(len+1));
2140 if (save == NULL)
2141 {
2142 PyErr_NoMemory();
2143 return NULL;
2144 }
2145
2146 for (i = 0; i < len; ++i)
2147 {
2148 if (str[i] == '\0')
2149 save[i] = '\n';
2150 else
2151 save[i] = str[i];
2152 }
2153
2154 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002155 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002156
2157 return save;
2158}
2159
2160/* Get a line from the specified buffer. The line number is
2161 * in Vim format (1-based). The line is returned as a Python
2162 * string object.
2163 */
2164 static PyObject *
2165GetBufferLine(buf_T *buf, PyInt n)
2166{
2167 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2168}
2169
2170
2171/* Get a list of lines from the specified buffer. The line numbers
2172 * are in Vim format (1-based). The range is from lo up to, but not
2173 * including, hi. The list is returned as a Python list of string objects.
2174 */
2175 static PyObject *
2176GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2177{
2178 PyInt i;
2179 PyInt n = hi - lo;
2180 PyObject *list = PyList_New(n);
2181
2182 if (list == NULL)
2183 return NULL;
2184
2185 for (i = 0; i < n; ++i)
2186 {
2187 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2188
2189 /* Error check - was the Python string creation OK? */
2190 if (str == NULL)
2191 {
2192 Py_DECREF(list);
2193 return NULL;
2194 }
2195
2196 /* Set the list item */
2197 if (PyList_SetItem(list, i, str))
2198 {
2199 Py_DECREF(str);
2200 Py_DECREF(list);
2201 return NULL;
2202 }
2203 }
2204
2205 /* The ownership of the Python list is passed to the caller (ie,
2206 * the caller should Py_DECREF() the object when it is finished
2207 * with it).
2208 */
2209
2210 return list;
2211}
2212
2213/*
2214 * Check if deleting lines made the cursor position invalid.
2215 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2216 * deleted).
2217 */
2218 static void
2219py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2220{
2221 if (curwin->w_cursor.lnum >= lo)
2222 {
2223 /* Adjust the cursor position if it's in/after the changed
2224 * lines. */
2225 if (curwin->w_cursor.lnum >= hi)
2226 {
2227 curwin->w_cursor.lnum += extra;
2228 check_cursor_col();
2229 }
2230 else if (extra < 0)
2231 {
2232 curwin->w_cursor.lnum = lo;
2233 check_cursor();
2234 }
2235 else
2236 check_cursor_col();
2237 changed_cline_bef_curs();
2238 }
2239 invalidate_botline();
2240}
2241
Bram Moolenaar19e60942011-06-19 00:27:51 +02002242/*
2243 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002244 * in Vim format (1-based). The replacement line is given as
2245 * a Python string object. The object is checked for validity
2246 * and correct format. Errors are returned as a value of FAIL.
2247 * The return value is OK on success.
2248 * If OK is returned and len_change is not NULL, *len_change
2249 * is set to the change in the buffer length.
2250 */
2251 static int
2252SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2253{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002254 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002255 * There are three cases:
2256 * 1. NULL, or None - this is a deletion.
2257 * 2. A string - this is a replacement.
2258 * 3. Anything else - this is an error.
2259 */
2260 if (line == Py_None || line == NULL)
2261 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002262 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002263
2264 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002265 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002266
2267 if (u_savedel((linenr_T)n, 1L) == FAIL)
2268 PyErr_SetVim(_("cannot save undo information"));
2269 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2270 PyErr_SetVim(_("cannot delete line"));
2271 else
2272 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002273 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002274 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2275 deleted_lines_mark((linenr_T)n, 1L);
2276 }
2277
Bram Moolenaar105bc352013-05-17 16:03:57 +02002278 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002279
2280 if (PyErr_Occurred() || VimErrorCheck())
2281 return FAIL;
2282
2283 if (len_change)
2284 *len_change = -1;
2285
2286 return OK;
2287 }
2288 else if (PyString_Check(line))
2289 {
2290 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002291 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002292
2293 if (save == NULL)
2294 return FAIL;
2295
2296 /* We do not need to free "save" if ml_replace() consumes it. */
2297 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002298 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002299
2300 if (u_savesub((linenr_T)n) == FAIL)
2301 {
2302 PyErr_SetVim(_("cannot save undo information"));
2303 vim_free(save);
2304 }
2305 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2306 {
2307 PyErr_SetVim(_("cannot replace line"));
2308 vim_free(save);
2309 }
2310 else
2311 changed_bytes((linenr_T)n, 0);
2312
Bram Moolenaar105bc352013-05-17 16:03:57 +02002313 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002314
2315 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002316 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002317 check_cursor_col();
2318
2319 if (PyErr_Occurred() || VimErrorCheck())
2320 return FAIL;
2321
2322 if (len_change)
2323 *len_change = 0;
2324
2325 return OK;
2326 }
2327 else
2328 {
2329 PyErr_BadArgument();
2330 return FAIL;
2331 }
2332}
2333
Bram Moolenaar19e60942011-06-19 00:27:51 +02002334/* Replace a range of lines in the specified buffer. The line numbers are in
2335 * Vim format (1-based). The range is from lo up to, but not including, hi.
2336 * The replacement lines are given as a Python list of string objects. The
2337 * list is checked for validity and correct format. Errors are returned as a
2338 * value of FAIL. The return value is OK on success.
2339 * If OK is returned and len_change is not NULL, *len_change
2340 * is set to the change in the buffer length.
2341 */
2342 static int
2343SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2344{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002345 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002346 * There are three cases:
2347 * 1. NULL, or None - this is a deletion.
2348 * 2. A list - this is a replacement.
2349 * 3. Anything else - this is an error.
2350 */
2351 if (list == Py_None || list == NULL)
2352 {
2353 PyInt i;
2354 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002355 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002356
2357 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002358 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002359
2360 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2361 PyErr_SetVim(_("cannot save undo information"));
2362 else
2363 {
2364 for (i = 0; i < n; ++i)
2365 {
2366 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2367 {
2368 PyErr_SetVim(_("cannot delete line"));
2369 break;
2370 }
2371 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002372 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002373 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2374 deleted_lines_mark((linenr_T)lo, (long)i);
2375 }
2376
Bram Moolenaar105bc352013-05-17 16:03:57 +02002377 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002378
2379 if (PyErr_Occurred() || VimErrorCheck())
2380 return FAIL;
2381
2382 if (len_change)
2383 *len_change = -n;
2384
2385 return OK;
2386 }
2387 else if (PyList_Check(list))
2388 {
2389 PyInt i;
2390 PyInt new_len = PyList_Size(list);
2391 PyInt old_len = hi - lo;
2392 PyInt extra = 0; /* lines added to text, can be negative */
2393 char **array;
2394 buf_T *savebuf;
2395
2396 if (new_len == 0) /* avoid allocating zero bytes */
2397 array = NULL;
2398 else
2399 {
2400 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
2401 if (array == NULL)
2402 {
2403 PyErr_NoMemory();
2404 return FAIL;
2405 }
2406 }
2407
2408 for (i = 0; i < new_len; ++i)
2409 {
2410 PyObject *line = PyList_GetItem(list, i);
2411
2412 array[i] = StringToLine(line);
2413 if (array[i] == NULL)
2414 {
2415 while (i)
2416 vim_free(array[--i]);
2417 vim_free(array);
2418 return FAIL;
2419 }
2420 }
2421
Bram Moolenaar19e60942011-06-19 00:27:51 +02002422 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002423
2424 // START of region without "return". Must call restore_buffer()!
2425 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002426
2427 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2428 PyErr_SetVim(_("cannot save undo information"));
2429
2430 /* If the size of the range is reducing (ie, new_len < old_len) we
2431 * need to delete some old_len. We do this at the start, by
2432 * repeatedly deleting line "lo".
2433 */
2434 if (!PyErr_Occurred())
2435 {
2436 for (i = 0; i < old_len - new_len; ++i)
2437 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2438 {
2439 PyErr_SetVim(_("cannot delete line"));
2440 break;
2441 }
2442 extra -= i;
2443 }
2444
2445 /* For as long as possible, replace the existing old_len with the
2446 * new old_len. This is a more efficient operation, as it requires
2447 * less memory allocation and freeing.
2448 */
2449 if (!PyErr_Occurred())
2450 {
2451 for (i = 0; i < old_len && i < new_len; ++i)
2452 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2453 == FAIL)
2454 {
2455 PyErr_SetVim(_("cannot replace line"));
2456 break;
2457 }
2458 }
2459 else
2460 i = 0;
2461
2462 /* Now we may need to insert the remaining new old_len. If we do, we
2463 * must free the strings as we finish with them (we can't pass the
2464 * responsibility to vim in this case).
2465 */
2466 if (!PyErr_Occurred())
2467 {
2468 while (i < new_len)
2469 {
2470 if (ml_append((linenr_T)(lo + i - 1),
2471 (char_u *)array[i], 0, FALSE) == FAIL)
2472 {
2473 PyErr_SetVim(_("cannot insert line"));
2474 break;
2475 }
2476 vim_free(array[i]);
2477 ++i;
2478 ++extra;
2479 }
2480 }
2481
2482 /* Free any left-over old_len, as a result of an error */
2483 while (i < new_len)
2484 {
2485 vim_free(array[i]);
2486 ++i;
2487 }
2488
2489 /* Free the array of old_len. All of its contents have now
2490 * been dealt with (either freed, or the responsibility passed
2491 * to vim.
2492 */
2493 vim_free(array);
2494
2495 /* Adjust marks. Invalidate any which lie in the
2496 * changed range, and move any in the remainder of the buffer.
2497 */
2498 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2499 (long)MAXLNUM, (long)extra);
2500 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2501
Bram Moolenaar105bc352013-05-17 16:03:57 +02002502 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002503 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2504
Bram Moolenaar105bc352013-05-17 16:03:57 +02002505 // END of region without "return".
2506 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002507
2508 if (PyErr_Occurred() || VimErrorCheck())
2509 return FAIL;
2510
2511 if (len_change)
2512 *len_change = new_len - old_len;
2513
2514 return OK;
2515 }
2516 else
2517 {
2518 PyErr_BadArgument();
2519 return FAIL;
2520 }
2521}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002522
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002523/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002524 * The line number is in Vim format (1-based). The lines to be inserted are
2525 * given as a Python list of string objects or as a single string. The lines
2526 * to be added are checked for validity and correct format. Errors are
2527 * returned as a value of FAIL. The return value is OK on success.
2528 * If OK is returned and len_change is not NULL, *len_change
2529 * is set to the change in the buffer length.
2530 */
2531 static int
2532InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2533{
2534 /* First of all, we check the type of the supplied Python object.
2535 * It must be a string or a list, or the call is in error.
2536 */
2537 if (PyString_Check(lines))
2538 {
2539 char *str = StringToLine(lines);
2540 buf_T *savebuf;
2541
2542 if (str == NULL)
2543 return FAIL;
2544
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002545 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002546 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002547
2548 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2549 PyErr_SetVim(_("cannot save undo information"));
2550 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2551 PyErr_SetVim(_("cannot insert line"));
2552 else
2553 appended_lines_mark((linenr_T)n, 1L);
2554
2555 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002556 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002557 update_screen(VALID);
2558
2559 if (PyErr_Occurred() || VimErrorCheck())
2560 return FAIL;
2561
2562 if (len_change)
2563 *len_change = 1;
2564
2565 return OK;
2566 }
2567 else if (PyList_Check(lines))
2568 {
2569 PyInt i;
2570 PyInt size = PyList_Size(lines);
2571 char **array;
2572 buf_T *savebuf;
2573
2574 array = (char **)alloc((unsigned)(size * sizeof(char *)));
2575 if (array == NULL)
2576 {
2577 PyErr_NoMemory();
2578 return FAIL;
2579 }
2580
2581 for (i = 0; i < size; ++i)
2582 {
2583 PyObject *line = PyList_GetItem(lines, i);
2584 array[i] = StringToLine(line);
2585
2586 if (array[i] == NULL)
2587 {
2588 while (i)
2589 vim_free(array[--i]);
2590 vim_free(array);
2591 return FAIL;
2592 }
2593 }
2594
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002595 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002596 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002597
2598 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2599 PyErr_SetVim(_("cannot save undo information"));
2600 else
2601 {
2602 for (i = 0; i < size; ++i)
2603 {
2604 if (ml_append((linenr_T)(n + i),
2605 (char_u *)array[i], 0, FALSE) == FAIL)
2606 {
2607 PyErr_SetVim(_("cannot insert line"));
2608
2609 /* Free the rest of the lines */
2610 while (i < size)
2611 vim_free(array[i++]);
2612
2613 break;
2614 }
2615 vim_free(array[i]);
2616 }
2617 if (i > 0)
2618 appended_lines_mark((linenr_T)n, (long)i);
2619 }
2620
2621 /* Free the array of lines. All of its contents have now
2622 * been freed.
2623 */
2624 vim_free(array);
2625
Bram Moolenaar105bc352013-05-17 16:03:57 +02002626 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002627 update_screen(VALID);
2628
2629 if (PyErr_Occurred() || VimErrorCheck())
2630 return FAIL;
2631
2632 if (len_change)
2633 *len_change = size;
2634
2635 return OK;
2636 }
2637 else
2638 {
2639 PyErr_BadArgument();
2640 return FAIL;
2641 }
2642}
2643
2644/*
2645 * Common routines for buffers and line ranges
2646 * -------------------------------------------
2647 */
2648
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002649typedef struct
2650{
2651 PyObject_HEAD
2652 buf_T *buf;
2653} BufferObject;
2654
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002655 static int
2656CheckBuffer(BufferObject *this)
2657{
2658 if (this->buf == INVALID_BUFFER_VALUE)
2659 {
2660 PyErr_SetVim(_("attempt to refer to deleted buffer"));
2661 return -1;
2662 }
2663
2664 return 0;
2665}
2666
2667 static PyObject *
2668RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
2669{
2670 if (CheckBuffer(self))
2671 return NULL;
2672
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002673 if (end == -1)
2674 end = self->buf->b_ml.ml_line_count;
2675
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002676 if (n < 0)
2677 n += end - start + 1;
2678
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002679 if (n < 0 || n > end - start)
2680 {
2681 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2682 return NULL;
2683 }
2684
2685 return GetBufferLine(self->buf, n+start);
2686}
2687
2688 static PyObject *
2689RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
2690{
2691 PyInt size;
2692
2693 if (CheckBuffer(self))
2694 return NULL;
2695
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002696 if (end == -1)
2697 end = self->buf->b_ml.ml_line_count;
2698
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002699 size = end - start + 1;
2700
2701 if (lo < 0)
2702 lo = 0;
2703 else if (lo > size)
2704 lo = size;
2705 if (hi < 0)
2706 hi = 0;
2707 if (hi < lo)
2708 hi = lo;
2709 else if (hi > size)
2710 hi = size;
2711
2712 return GetBufferLineList(self->buf, lo+start, hi+start);
2713}
2714
2715 static PyInt
2716RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2717{
2718 PyInt len_change;
2719
2720 if (CheckBuffer(self))
2721 return -1;
2722
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002723 if (end == -1)
2724 end = self->buf->b_ml.ml_line_count;
2725
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002726 if (n < 0)
2727 n += end - start + 1;
2728
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002729 if (n < 0 || n > end - start)
2730 {
2731 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2732 return -1;
2733 }
2734
2735 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
2736 return -1;
2737
2738 if (new_end)
2739 *new_end = end + len_change;
2740
2741 return 0;
2742}
2743
Bram Moolenaar19e60942011-06-19 00:27:51 +02002744 static PyInt
2745RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2746{
2747 PyInt size;
2748 PyInt len_change;
2749
2750 /* Self must be a valid buffer */
2751 if (CheckBuffer(self))
2752 return -1;
2753
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002754 if (end == -1)
2755 end = self->buf->b_ml.ml_line_count;
2756
Bram Moolenaar19e60942011-06-19 00:27:51 +02002757 /* Sort out the slice range */
2758 size = end - start + 1;
2759
2760 if (lo < 0)
2761 lo = 0;
2762 else if (lo > size)
2763 lo = size;
2764 if (hi < 0)
2765 hi = 0;
2766 if (hi < lo)
2767 hi = lo;
2768 else if (hi > size)
2769 hi = size;
2770
2771 if (SetBufferLineList(self->buf, lo + start, hi + start,
2772 val, &len_change) == FAIL)
2773 return -1;
2774
2775 if (new_end)
2776 *new_end = end + len_change;
2777
2778 return 0;
2779}
2780
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002781
2782 static PyObject *
2783RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
2784{
2785 PyObject *lines;
2786 PyInt len_change;
2787 PyInt max;
2788 PyInt n;
2789
2790 if (CheckBuffer(self))
2791 return NULL;
2792
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002793 if (end == -1)
2794 end = self->buf->b_ml.ml_line_count;
2795
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002796 max = n = end - start + 1;
2797
2798 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
2799 return NULL;
2800
2801 if (n < 0 || n > max)
2802 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002803 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002804 return NULL;
2805 }
2806
2807 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
2808 return NULL;
2809
2810 if (new_end)
2811 *new_end = end + len_change;
2812
2813 Py_INCREF(Py_None);
2814 return Py_None;
2815}
2816
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002817/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002818 */
2819
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002820static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002821static PySequenceMethods RangeAsSeq;
2822static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002823
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002824typedef struct
2825{
2826 PyObject_HEAD
2827 BufferObject *buf;
2828 PyInt start;
2829 PyInt end;
2830} RangeObject;
2831
2832 static PyObject *
2833RangeNew(buf_T *buf, PyInt start, PyInt end)
2834{
2835 BufferObject *bufr;
2836 RangeObject *self;
2837 self = PyObject_NEW(RangeObject, &RangeType);
2838 if (self == NULL)
2839 return NULL;
2840
2841 bufr = (BufferObject *)BufferNew(buf);
2842 if (bufr == NULL)
2843 {
2844 Py_DECREF(self);
2845 return NULL;
2846 }
2847 Py_INCREF(bufr);
2848
2849 self->buf = bufr;
2850 self->start = start;
2851 self->end = end;
2852
2853 return (PyObject *)(self);
2854}
2855
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002856 static void
2857RangeDestructor(PyObject *self)
2858{
2859 Py_DECREF(((RangeObject *)(self))->buf);
2860 DESTRUCTOR_FINISH(self);
2861}
2862
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002863 static PyInt
2864RangeLength(PyObject *self)
2865{
2866 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
2867 if (CheckBuffer(((RangeObject *)(self))->buf))
2868 return -1; /* ??? */
2869
2870 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1);
2871}
2872
2873 static PyObject *
2874RangeItem(PyObject *self, PyInt n)
2875{
2876 return RBItem(((RangeObject *)(self))->buf, n,
2877 ((RangeObject *)(self))->start,
2878 ((RangeObject *)(self))->end);
2879}
2880
2881 static PyObject *
2882RangeSlice(PyObject *self, PyInt lo, PyInt hi)
2883{
2884 return RBSlice(((RangeObject *)(self))->buf, lo, hi,
2885 ((RangeObject *)(self))->start,
2886 ((RangeObject *)(self))->end);
2887}
2888
2889 static PyObject *
2890RangeAppend(PyObject *self, PyObject *args)
2891{
2892 return RBAppend(((RangeObject *)(self))->buf, args,
2893 ((RangeObject *)(self))->start,
2894 ((RangeObject *)(self))->end,
2895 &((RangeObject *)(self))->end);
2896}
2897
2898 static PyObject *
2899RangeRepr(PyObject *self)
2900{
2901 static char repr[100];
2902 RangeObject *this = (RangeObject *)(self);
2903
2904 if (this->buf->buf == INVALID_BUFFER_VALUE)
2905 {
2906 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
2907 (self));
2908 return PyString_FromString(repr);
2909 }
2910 else
2911 {
2912 char *name = (char *)this->buf->buf->b_fname;
2913 int len;
2914
2915 if (name == NULL)
2916 name = "";
2917 len = (int)strlen(name);
2918
2919 if (len > 45)
2920 name = name + (45 - len);
2921
2922 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
2923 len > 45 ? "..." : "", name,
2924 this->start, this->end);
2925
2926 return PyString_FromString(repr);
2927 }
2928}
2929
2930static struct PyMethodDef RangeMethods[] = {
2931 /* name, function, calling, documentation */
2932 {"append", RangeAppend, 1, "Append data to the Vim range" },
2933 { NULL, NULL, 0, NULL }
2934};
2935
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002936static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002937static PySequenceMethods BufferAsSeq;
2938static PyMappingMethods BufferAsMapping;
2939
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002940 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02002941BufferNew(buf_T *buf)
2942{
2943 /* We need to handle deletion of buffers underneath us.
2944 * If we add a "b_python*_ref" field to the buf_T structure,
2945 * then we can get at it in buf_freeall() in vim. We then
2946 * need to create only ONE Python object per buffer - if
2947 * we try to create a second, just INCREF the existing one
2948 * and return it. The (single) Python object referring to
2949 * the buffer is stored in "b_python*_ref".
2950 * Question: what to do on a buf_freeall(). We'll probably
2951 * have to either delete the Python object (DECREF it to
2952 * zero - a bad idea, as it leaves dangling refs!) or
2953 * set the buf_T * value to an invalid value (-1?), which
2954 * means we need checks in all access functions... Bah.
2955 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002956 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002957 * b_python_ref and b_python3_ref fields respectively.
2958 */
2959
2960 BufferObject *self;
2961
2962 if (BUF_PYTHON_REF(buf) != NULL)
2963 {
2964 self = BUF_PYTHON_REF(buf);
2965 Py_INCREF(self);
2966 }
2967 else
2968 {
2969 self = PyObject_NEW(BufferObject, &BufferType);
2970 if (self == NULL)
2971 return NULL;
2972 self->buf = buf;
2973 BUF_PYTHON_REF(buf) = self;
2974 }
2975
2976 return (PyObject *)(self);
2977}
2978
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002979 static void
2980BufferDestructor(PyObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002981{
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002982 BufferObject *this = (BufferObject *)(self);
2983
2984 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
2985 BUF_PYTHON_REF(this->buf) = NULL;
2986
2987 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002988}
2989
Bram Moolenaar971db462013-05-12 18:44:48 +02002990 static PyInt
2991BufferLength(PyObject *self)
2992{
2993 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
2994 if (CheckBuffer((BufferObject *)(self)))
2995 return -1; /* ??? */
2996
2997 return (PyInt)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
2998}
2999
3000 static PyObject *
3001BufferItem(PyObject *self, PyInt n)
3002{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003003 return RBItem((BufferObject *)(self), n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003004}
3005
3006 static PyObject *
3007BufferSlice(PyObject *self, PyInt lo, PyInt hi)
3008{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003009 return RBSlice((BufferObject *)(self), lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003010}
3011
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003012 static PyObject *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003013BufferAttr(BufferObject *this, char *name)
3014{
3015 if (strcmp(name, "name") == 0)
3016 return Py_BuildValue("s", this->buf->b_ffname);
3017 else if (strcmp(name, "number") == 0)
3018 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
3019 else if (strcmp(name, "vars") == 0)
3020 return DictionaryNew(this->buf->b_vars);
3021 else if (strcmp(name, "options") == 0)
3022 return OptionsNew(SREQ_BUF, this->buf, (checkfun) CheckBuffer,
3023 (PyObject *) this);
3024 else if (strcmp(name,"__members__") == 0)
3025 return Py_BuildValue("[ssss]", "name", "number", "vars", "options");
3026 else
3027 return NULL;
3028}
3029
3030 static PyObject *
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003031BufferAppend(PyObject *self, PyObject *args)
3032{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003033 return RBAppend((BufferObject *)(self), args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003034}
3035
3036 static PyObject *
3037BufferMark(PyObject *self, PyObject *args)
3038{
3039 pos_T *posp;
3040 char *pmark;
3041 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003042 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003043
3044 if (CheckBuffer((BufferObject *)(self)))
3045 return NULL;
3046
3047 if (!PyArg_ParseTuple(args, "s", &pmark))
3048 return NULL;
3049 mark = *pmark;
3050
Bram Moolenaar105bc352013-05-17 16:03:57 +02003051 switch_buffer(&savebuf, ((BufferObject *)(self))->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003052 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003053 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003054
3055 if (posp == NULL)
3056 {
3057 PyErr_SetVim(_("invalid mark name"));
3058 return NULL;
3059 }
3060
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003061 /* Check for keyboard interrupt */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003062 if (VimErrorCheck())
3063 return NULL;
3064
3065 if (posp->lnum <= 0)
3066 {
3067 /* Or raise an error? */
3068 Py_INCREF(Py_None);
3069 return Py_None;
3070 }
3071
3072 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3073}
3074
3075 static PyObject *
3076BufferRange(PyObject *self, PyObject *args)
3077{
3078 PyInt start;
3079 PyInt end;
3080
3081 if (CheckBuffer((BufferObject *)(self)))
3082 return NULL;
3083
3084 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3085 return NULL;
3086
3087 return RangeNew(((BufferObject *)(self))->buf, start, end);
3088}
3089
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003090 static PyObject *
3091BufferRepr(PyObject *self)
3092{
3093 static char repr[100];
3094 BufferObject *this = (BufferObject *)(self);
3095
3096 if (this->buf == INVALID_BUFFER_VALUE)
3097 {
3098 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3099 return PyString_FromString(repr);
3100 }
3101 else
3102 {
3103 char *name = (char *)this->buf->b_fname;
3104 PyInt len;
3105
3106 if (name == NULL)
3107 name = "";
3108 len = strlen(name);
3109
3110 if (len > 35)
3111 name = name + (35 - len);
3112
3113 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3114
3115 return PyString_FromString(repr);
3116 }
3117}
3118
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003119static struct PyMethodDef BufferMethods[] = {
3120 /* name, function, calling, documentation */
3121 {"append", BufferAppend, 1, "Append data to Vim buffer" },
3122 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" },
3123 {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaar7f85d292012-02-04 20:17:26 +01003124#if PY_VERSION_HEX >= 0x03000000
3125 {"__dir__", BufferDir, 4, "List its attributes" },
3126#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003127 { NULL, NULL, 0, NULL }
3128};
3129
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003130/*
3131 * Buffer list object - Implementation
3132 */
3133
3134static PyTypeObject BufMapType;
3135
3136typedef struct
3137{
3138 PyObject_HEAD
3139} BufMapObject;
3140
3141 static PyInt
3142BufMapLength(PyObject *self UNUSED)
3143{
3144 buf_T *b = firstbuf;
3145 PyInt n = 0;
3146
3147 while (b)
3148 {
3149 ++n;
3150 b = b->b_next;
3151 }
3152
3153 return n;
3154}
3155
3156 static PyObject *
3157BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3158{
3159 buf_T *b;
3160 int bnr;
3161
3162#if PY_MAJOR_VERSION < 3
3163 if (PyInt_Check(keyObject))
3164 bnr = PyInt_AsLong(keyObject);
3165 else
3166#endif
3167 if (PyLong_Check(keyObject))
3168 bnr = PyLong_AsLong(keyObject);
3169 else
3170 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003171 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003172 return NULL;
3173 }
3174
3175 b = buflist_findnr(bnr);
3176
3177 if (b)
3178 return BufferNew(b);
3179 else
3180 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003181 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003182 return NULL;
3183 }
3184}
3185
3186 static void
3187BufMapIterDestruct(PyObject *buffer)
3188{
3189 /* Iteration was stopped before all buffers were processed */
3190 if (buffer)
3191 {
3192 Py_DECREF(buffer);
3193 }
3194}
3195
3196 static PyObject *
3197BufMapIterNext(PyObject **buffer)
3198{
3199 PyObject *next;
3200 PyObject *r;
3201
3202 if (!*buffer)
3203 return NULL;
3204
3205 r = *buffer;
3206
3207 if (CheckBuffer((BufferObject *)(r)))
3208 {
3209 *buffer = NULL;
3210 return NULL;
3211 }
3212
3213 if (!((BufferObject *)(r))->buf->b_next)
3214 next = NULL;
3215 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3216 return NULL;
3217 *buffer = next;
3218 /* Do not increment reference: we no longer hold it (decref), but whoever on
3219 * other side will hold (incref). Decref+incref = nothing.
3220 */
3221 return r;
3222}
3223
3224 static PyObject *
3225BufMapIter(PyObject *self UNUSED)
3226{
3227 PyObject *buffer;
3228
3229 buffer = BufferNew(firstbuf);
3230 return IterNew(buffer,
3231 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext);
3232}
3233
3234static PyMappingMethods BufMapAsMapping = {
3235 (lenfunc) BufMapLength,
3236 (binaryfunc) BufMapItem,
3237 (objobjargproc) 0,
3238};
3239
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003240/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003241 */
3242
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003243 static PyObject *
3244CurrentGetattr(PyObject *self UNUSED, char *name)
3245{
3246 if (strcmp(name, "buffer") == 0)
3247 return (PyObject *)BufferNew(curbuf);
3248 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003249 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003250 else if (strcmp(name, "tabpage") == 0)
3251 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003252 else if (strcmp(name, "line") == 0)
3253 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3254 else if (strcmp(name, "range") == 0)
3255 return RangeNew(curbuf, RangeStart, RangeEnd);
3256 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003257 return Py_BuildValue("[sssss]", "buffer", "window", "line", "range",
3258 "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003259 else
3260 {
3261 PyErr_SetString(PyExc_AttributeError, name);
3262 return NULL;
3263 }
3264}
3265
3266 static int
3267CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3268{
3269 if (strcmp(name, "line") == 0)
3270 {
3271 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3272 return -1;
3273
3274 return 0;
3275 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003276 else if (strcmp(name, "buffer") == 0)
3277 {
3278 int count;
3279
3280 if (value->ob_type != &BufferType)
3281 {
3282 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3283 return -1;
3284 }
3285
3286 if (CheckBuffer((BufferObject *)(value)))
3287 return -1;
3288 count = ((BufferObject *)(value))->buf->b_fnum;
3289
3290 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3291 {
3292 PyErr_SetVim(_("failed to switch to given buffer"));
3293 return -1;
3294 }
3295
3296 return 0;
3297 }
3298 else if (strcmp(name, "window") == 0)
3299 {
3300 int count;
3301
3302 if (value->ob_type != &WindowType)
3303 {
3304 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3305 return -1;
3306 }
3307
3308 if (CheckWindow((WindowObject *)(value)))
3309 return -1;
3310 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3311
3312 if (!count)
3313 {
3314 PyErr_SetString(PyExc_ValueError,
3315 _("failed to find window in the current tab page"));
3316 return -1;
3317 }
3318
3319 win_goto(((WindowObject *)(value))->win);
3320 if (((WindowObject *)(value))->win != curwin)
3321 {
3322 PyErr_SetString(PyExc_RuntimeError,
3323 _("did not switch to the specified window"));
3324 return -1;
3325 }
3326
3327 return 0;
3328 }
3329 else if (strcmp(name, "tabpage") == 0)
3330 {
3331 if (value->ob_type != &TabPageType)
3332 {
3333 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3334 return -1;
3335 }
3336
3337 if (CheckTabPage((TabPageObject *)(value)))
3338 return -1;
3339
3340 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3341 if (((TabPageObject *)(value))->tab != curtab)
3342 {
3343 PyErr_SetString(PyExc_RuntimeError,
3344 _("did not switch to the specified tab page"));
3345 return -1;
3346 }
3347
3348 return 0;
3349 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003350 else
3351 {
3352 PyErr_SetString(PyExc_AttributeError, name);
3353 return -1;
3354 }
3355}
3356
Bram Moolenaardb913952012-06-29 12:54:53 +02003357 static void
3358set_ref_in_py(const int copyID)
3359{
3360 pylinkedlist_T *cur;
3361 dict_T *dd;
3362 list_T *ll;
3363
3364 if (lastdict != NULL)
3365 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3366 {
3367 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3368 if (dd->dv_copyID != copyID)
3369 {
3370 dd->dv_copyID = copyID;
3371 set_ref_in_ht(&dd->dv_hashtab, copyID);
3372 }
3373 }
3374
3375 if (lastlist != NULL)
3376 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3377 {
3378 ll = ((ListObject *) (cur->pll_obj))->list;
3379 if (ll->lv_copyID != copyID)
3380 {
3381 ll->lv_copyID = copyID;
3382 set_ref_in_list(ll, copyID);
3383 }
3384 }
3385}
3386
3387 static int
3388set_string_copy(char_u *str, typval_T *tv)
3389{
3390 tv->vval.v_string = vim_strsave(str);
3391 if (tv->vval.v_string == NULL)
3392 {
3393 PyErr_NoMemory();
3394 return -1;
3395 }
3396 return 0;
3397}
3398
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003399 static int
3400pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3401{
3402 dict_T *d;
3403 char_u *key;
3404 dictitem_T *di;
3405 PyObject *keyObject;
3406 PyObject *valObject;
3407 Py_ssize_t iter = 0;
3408
3409 d = dict_alloc();
3410 if (d == NULL)
3411 {
3412 PyErr_NoMemory();
3413 return -1;
3414 }
3415
3416 tv->v_type = VAR_DICT;
3417 tv->vval.v_dict = d;
3418
3419 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
3420 {
3421 DICTKEY_DECL
3422
3423 if (keyObject == NULL)
3424 return -1;
3425 if (valObject == NULL)
3426 return -1;
3427
3428 DICTKEY_GET_NOTEMPTY(-1)
3429
3430 di = dictitem_alloc(key);
3431
3432 DICTKEY_UNREF
3433
3434 if (di == NULL)
3435 {
3436 PyErr_NoMemory();
3437 return -1;
3438 }
3439 di->di_tv.v_lock = 0;
3440
3441 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3442 {
3443 vim_free(di);
3444 return -1;
3445 }
3446 if (dict_add(d, di) == FAIL)
3447 {
3448 vim_free(di);
3449 PyErr_SetVim(_("failed to add key to dictionary"));
3450 return -1;
3451 }
3452 }
3453 return 0;
3454}
3455
3456 static int
3457pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3458{
3459 dict_T *d;
3460 char_u *key;
3461 dictitem_T *di;
3462 PyObject *list;
3463 PyObject *litem;
3464 PyObject *keyObject;
3465 PyObject *valObject;
3466 Py_ssize_t lsize;
3467
3468 d = dict_alloc();
3469 if (d == NULL)
3470 {
3471 PyErr_NoMemory();
3472 return -1;
3473 }
3474
3475 tv->v_type = VAR_DICT;
3476 tv->vval.v_dict = d;
3477
3478 list = PyMapping_Items(obj);
3479 if (list == NULL)
3480 return -1;
3481 lsize = PyList_Size(list);
3482 while (lsize--)
3483 {
3484 DICTKEY_DECL
3485
3486 litem = PyList_GetItem(list, lsize);
3487 if (litem == NULL)
3488 {
3489 Py_DECREF(list);
3490 return -1;
3491 }
3492
3493 keyObject = PyTuple_GetItem(litem, 0);
3494 if (keyObject == NULL)
3495 {
3496 Py_DECREF(list);
3497 Py_DECREF(litem);
3498 return -1;
3499 }
3500
3501 DICTKEY_GET_NOTEMPTY(-1)
3502
3503 valObject = PyTuple_GetItem(litem, 1);
3504 if (valObject == NULL)
3505 {
3506 Py_DECREF(list);
3507 Py_DECREF(litem);
3508 return -1;
3509 }
3510
3511 di = dictitem_alloc(key);
3512
3513 DICTKEY_UNREF
3514
3515 if (di == NULL)
3516 {
3517 Py_DECREF(list);
3518 Py_DECREF(litem);
3519 PyErr_NoMemory();
3520 return -1;
3521 }
3522 di->di_tv.v_lock = 0;
3523
3524 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3525 {
3526 vim_free(di);
3527 Py_DECREF(list);
3528 Py_DECREF(litem);
3529 return -1;
3530 }
3531 if (dict_add(d, di) == FAIL)
3532 {
3533 vim_free(di);
3534 Py_DECREF(list);
3535 Py_DECREF(litem);
3536 PyErr_SetVim(_("failed to add key to dictionary"));
3537 return -1;
3538 }
3539 Py_DECREF(litem);
3540 }
3541 Py_DECREF(list);
3542 return 0;
3543}
3544
3545 static int
3546pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3547{
3548 list_T *l;
3549
3550 l = list_alloc();
3551 if (l == NULL)
3552 {
3553 PyErr_NoMemory();
3554 return -1;
3555 }
3556
3557 tv->v_type = VAR_LIST;
3558 tv->vval.v_list = l;
3559
3560 if (list_py_concat(l, obj, lookupDict) == -1)
3561 return -1;
3562
3563 return 0;
3564}
3565
3566 static int
3567pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3568{
3569 PyObject *iterator = PyObject_GetIter(obj);
3570 PyObject *item;
3571 list_T *l;
3572 listitem_T *li;
3573
3574 l = list_alloc();
3575
3576 if (l == NULL)
3577 {
3578 PyErr_NoMemory();
3579 return -1;
3580 }
3581
3582 tv->vval.v_list = l;
3583 tv->v_type = VAR_LIST;
3584
3585
3586 if (iterator == NULL)
3587 return -1;
3588
3589 while ((item = PyIter_Next(obj)))
3590 {
3591 li = listitem_alloc();
3592 if (li == NULL)
3593 {
3594 PyErr_NoMemory();
3595 return -1;
3596 }
3597 li->li_tv.v_lock = 0;
3598
3599 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
3600 return -1;
3601
3602 list_append(l, li);
3603
3604 Py_DECREF(item);
3605 }
3606
3607 Py_DECREF(iterator);
3608 return 0;
3609}
3610
Bram Moolenaardb913952012-06-29 12:54:53 +02003611typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
3612
3613 static int
3614convert_dl(PyObject *obj, typval_T *tv,
3615 pytotvfunc py_to_tv, PyObject *lookupDict)
3616{
3617 PyObject *capsule;
3618 char hexBuf[sizeof(void *) * 2 + 3];
3619
3620 sprintf(hexBuf, "%p", obj);
3621
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003622# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003623 capsule = PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003624# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003625 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003626# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02003627 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02003628 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003629# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003630 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02003631# else
3632 capsule = PyCObject_FromVoidPtr(tv, NULL);
3633# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003634 PyDict_SetItemString(lookupDict, hexBuf, capsule);
3635 Py_DECREF(capsule);
3636 if (py_to_tv(obj, tv, lookupDict) == -1)
3637 {
3638 tv->v_type = VAR_UNKNOWN;
3639 return -1;
3640 }
3641 /* As we are not using copy_tv which increments reference count we must
3642 * do it ourself. */
3643 switch(tv->v_type)
3644 {
3645 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
3646 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
3647 }
3648 }
3649 else
3650 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003651 typval_T *v;
3652
3653# ifdef PY_USE_CAPSULE
3654 v = PyCapsule_GetPointer(capsule, NULL);
3655# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003656 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003657# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003658 copy_tv(v, tv);
3659 }
3660 return 0;
3661}
3662
3663 static int
3664ConvertFromPyObject(PyObject *obj, typval_T *tv)
3665{
3666 PyObject *lookup_dict;
3667 int r;
3668
3669 lookup_dict = PyDict_New();
3670 r = _ConvertFromPyObject(obj, tv, lookup_dict);
3671 Py_DECREF(lookup_dict);
3672 return r;
3673}
3674
3675 static int
3676_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3677{
3678 if (obj->ob_type == &DictionaryType)
3679 {
3680 tv->v_type = VAR_DICT;
3681 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
3682 ++tv->vval.v_dict->dv_refcount;
3683 }
3684 else if (obj->ob_type == &ListType)
3685 {
3686 tv->v_type = VAR_LIST;
3687 tv->vval.v_list = (((ListObject *)(obj))->list);
3688 ++tv->vval.v_list->lv_refcount;
3689 }
3690 else if (obj->ob_type == &FunctionType)
3691 {
3692 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
3693 return -1;
3694
3695 tv->v_type = VAR_FUNC;
3696 func_ref(tv->vval.v_string);
3697 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003698 else if (PyBytes_Check(obj))
3699 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003700 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02003701
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003702 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
3703 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003704 if (result == NULL)
3705 return -1;
3706
3707 if (set_string_copy(result, tv) == -1)
3708 return -1;
3709
3710 tv->v_type = VAR_STRING;
3711 }
3712 else if (PyUnicode_Check(obj))
3713 {
3714 PyObject *bytes;
3715 char_u *result;
3716
Bram Moolenaardb913952012-06-29 12:54:53 +02003717 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
3718 if (bytes == NULL)
3719 return -1;
3720
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003721 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
3722 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003723 if (result == NULL)
3724 return -1;
3725
3726 if (set_string_copy(result, tv) == -1)
3727 {
3728 Py_XDECREF(bytes);
3729 return -1;
3730 }
3731 Py_XDECREF(bytes);
3732
3733 tv->v_type = VAR_STRING;
3734 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02003735#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02003736 else if (PyInt_Check(obj))
3737 {
3738 tv->v_type = VAR_NUMBER;
3739 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
3740 }
3741#endif
3742 else if (PyLong_Check(obj))
3743 {
3744 tv->v_type = VAR_NUMBER;
3745 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
3746 }
3747 else if (PyDict_Check(obj))
3748 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
3749#ifdef FEAT_FLOAT
3750 else if (PyFloat_Check(obj))
3751 {
3752 tv->v_type = VAR_FLOAT;
3753 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
3754 }
3755#endif
3756 else if (PyIter_Check(obj))
3757 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
3758 else if (PySequence_Check(obj))
3759 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
3760 else if (PyMapping_Check(obj))
3761 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
3762 else
3763 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003764 PyErr_SetString(PyExc_TypeError,
3765 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02003766 return -1;
3767 }
3768 return 0;
3769}
3770
3771 static PyObject *
3772ConvertToPyObject(typval_T *tv)
3773{
3774 if (tv == NULL)
3775 {
3776 PyErr_SetVim(_("NULL reference passed"));
3777 return NULL;
3778 }
3779 switch (tv->v_type)
3780 {
3781 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02003782 return PyBytes_FromString(tv->vval.v_string == NULL
3783 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02003784 case VAR_NUMBER:
3785 return PyLong_FromLong((long) tv->vval.v_number);
3786#ifdef FEAT_FLOAT
3787 case VAR_FLOAT:
3788 return PyFloat_FromDouble((double) tv->vval.v_float);
3789#endif
3790 case VAR_LIST:
3791 return ListNew(tv->vval.v_list);
3792 case VAR_DICT:
3793 return DictionaryNew(tv->vval.v_dict);
3794 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02003795 return FunctionNew(tv->vval.v_string == NULL
3796 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02003797 case VAR_UNKNOWN:
3798 Py_INCREF(Py_None);
3799 return Py_None;
3800 default:
3801 PyErr_SetVim(_("internal error: invalid value type"));
3802 return NULL;
3803 }
3804}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003805
3806typedef struct
3807{
3808 PyObject_HEAD
3809} CurrentObject;
3810static PyTypeObject CurrentType;
3811
3812 static void
3813init_structs(void)
3814{
3815 vim_memset(&OutputType, 0, sizeof(OutputType));
3816 OutputType.tp_name = "vim.message";
3817 OutputType.tp_basicsize = sizeof(OutputObject);
3818 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
3819 OutputType.tp_doc = "vim message object";
3820 OutputType.tp_methods = OutputMethods;
3821#if PY_MAJOR_VERSION >= 3
3822 OutputType.tp_getattro = OutputGetattro;
3823 OutputType.tp_setattro = OutputSetattro;
3824 OutputType.tp_alloc = call_PyType_GenericAlloc;
3825 OutputType.tp_new = call_PyType_GenericNew;
3826 OutputType.tp_free = call_PyObject_Free;
3827#else
3828 OutputType.tp_getattr = OutputGetattr;
3829 OutputType.tp_setattr = OutputSetattr;
3830#endif
3831
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003832 vim_memset(&IterType, 0, sizeof(IterType));
3833 IterType.tp_name = "vim.iter";
3834 IterType.tp_basicsize = sizeof(IterObject);
3835 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
3836 IterType.tp_doc = "generic iterator object";
3837 IterType.tp_iter = IterIter;
3838 IterType.tp_iternext = IterNext;
Bram Moolenaar2cd73622013-05-15 19:07:47 +02003839 IterType.tp_dealloc = IterDestructor;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003840
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003841 vim_memset(&BufferType, 0, sizeof(BufferType));
3842 BufferType.tp_name = "vim.buffer";
3843 BufferType.tp_basicsize = sizeof(BufferType);
3844 BufferType.tp_dealloc = BufferDestructor;
3845 BufferType.tp_repr = BufferRepr;
3846 BufferType.tp_as_sequence = &BufferAsSeq;
3847 BufferType.tp_as_mapping = &BufferAsMapping;
3848 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
3849 BufferType.tp_doc = "vim buffer object";
3850 BufferType.tp_methods = BufferMethods;
3851#if PY_MAJOR_VERSION >= 3
3852 BufferType.tp_getattro = BufferGetattro;
3853 BufferType.tp_alloc = call_PyType_GenericAlloc;
3854 BufferType.tp_new = call_PyType_GenericNew;
3855 BufferType.tp_free = call_PyObject_Free;
3856#else
3857 BufferType.tp_getattr = BufferGetattr;
3858#endif
3859
3860 vim_memset(&WindowType, 0, sizeof(WindowType));
3861 WindowType.tp_name = "vim.window";
3862 WindowType.tp_basicsize = sizeof(WindowObject);
3863 WindowType.tp_dealloc = WindowDestructor;
3864 WindowType.tp_repr = WindowRepr;
3865 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
3866 WindowType.tp_doc = "vim Window object";
3867 WindowType.tp_methods = WindowMethods;
3868#if PY_MAJOR_VERSION >= 3
3869 WindowType.tp_getattro = WindowGetattro;
3870 WindowType.tp_setattro = WindowSetattro;
3871 WindowType.tp_alloc = call_PyType_GenericAlloc;
3872 WindowType.tp_new = call_PyType_GenericNew;
3873 WindowType.tp_free = call_PyObject_Free;
3874#else
3875 WindowType.tp_getattr = WindowGetattr;
3876 WindowType.tp_setattr = WindowSetattr;
3877#endif
3878
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003879 vim_memset(&TabPageType, 0, sizeof(TabPageType));
3880 TabPageType.tp_name = "vim.tabpage";
3881 TabPageType.tp_basicsize = sizeof(TabPageObject);
3882 TabPageType.tp_dealloc = TabPageDestructor;
3883 TabPageType.tp_repr = TabPageRepr;
3884 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
3885 TabPageType.tp_doc = "vim tab page object";
3886 TabPageType.tp_methods = TabPageMethods;
3887#if PY_MAJOR_VERSION >= 3
3888 TabPageType.tp_getattro = TabPageGetattro;
3889 TabPageType.tp_alloc = call_PyType_GenericAlloc;
3890 TabPageType.tp_new = call_PyType_GenericNew;
3891 TabPageType.tp_free = call_PyObject_Free;
3892#else
3893 TabPageType.tp_getattr = TabPageGetattr;
3894#endif
3895
Bram Moolenaardfa38d42013-05-15 13:38:47 +02003896 vim_memset(&BufMapType, 0, sizeof(BufMapType));
3897 BufMapType.tp_name = "vim.bufferlist";
3898 BufMapType.tp_basicsize = sizeof(BufMapObject);
3899 BufMapType.tp_as_mapping = &BufMapAsMapping;
3900 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003901 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003902 BufferType.tp_doc = "vim buffer list";
3903
3904 vim_memset(&WinListType, 0, sizeof(WinListType));
3905 WinListType.tp_name = "vim.windowlist";
3906 WinListType.tp_basicsize = sizeof(WinListType);
3907 WinListType.tp_as_sequence = &WinListAsSeq;
3908 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
3909 WinListType.tp_doc = "vim window list";
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003910 WinListType.tp_dealloc = WinListDestructor;
3911
3912 vim_memset(&TabListType, 0, sizeof(TabListType));
3913 TabListType.tp_name = "vim.tabpagelist";
3914 TabListType.tp_basicsize = sizeof(TabListType);
3915 TabListType.tp_as_sequence = &TabListAsSeq;
3916 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
3917 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003918
3919 vim_memset(&RangeType, 0, sizeof(RangeType));
3920 RangeType.tp_name = "vim.range";
3921 RangeType.tp_basicsize = sizeof(RangeObject);
3922 RangeType.tp_dealloc = RangeDestructor;
3923 RangeType.tp_repr = RangeRepr;
3924 RangeType.tp_as_sequence = &RangeAsSeq;
3925 RangeType.tp_as_mapping = &RangeAsMapping;
3926 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
3927 RangeType.tp_doc = "vim Range object";
3928 RangeType.tp_methods = RangeMethods;
3929#if PY_MAJOR_VERSION >= 3
3930 RangeType.tp_getattro = RangeGetattro;
3931 RangeType.tp_alloc = call_PyType_GenericAlloc;
3932 RangeType.tp_new = call_PyType_GenericNew;
3933 RangeType.tp_free = call_PyObject_Free;
3934#else
3935 RangeType.tp_getattr = RangeGetattr;
3936#endif
3937
3938 vim_memset(&CurrentType, 0, sizeof(CurrentType));
3939 CurrentType.tp_name = "vim.currentdata";
3940 CurrentType.tp_basicsize = sizeof(CurrentObject);
3941 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
3942 CurrentType.tp_doc = "vim current object";
3943#if PY_MAJOR_VERSION >= 3
3944 CurrentType.tp_getattro = CurrentGetattro;
3945 CurrentType.tp_setattro = CurrentSetattro;
3946#else
3947 CurrentType.tp_getattr = CurrentGetattr;
3948 CurrentType.tp_setattr = CurrentSetattr;
3949#endif
3950
3951 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
3952 DictionaryType.tp_name = "vim.dictionary";
3953 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
3954 DictionaryType.tp_dealloc = DictionaryDestructor;
3955 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
3956 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
3957 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
3958 DictionaryType.tp_methods = DictionaryMethods;
3959#if PY_MAJOR_VERSION >= 3
3960 DictionaryType.tp_getattro = DictionaryGetattro;
3961 DictionaryType.tp_setattro = DictionarySetattro;
3962#else
3963 DictionaryType.tp_getattr = DictionaryGetattr;
3964 DictionaryType.tp_setattr = DictionarySetattr;
3965#endif
3966
3967 vim_memset(&ListType, 0, sizeof(ListType));
3968 ListType.tp_name = "vim.list";
3969 ListType.tp_dealloc = ListDestructor;
3970 ListType.tp_basicsize = sizeof(ListObject);
3971 ListType.tp_as_sequence = &ListAsSeq;
3972 ListType.tp_as_mapping = &ListAsMapping;
3973 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
3974 ListType.tp_doc = "list pushing modifications to vim structure";
3975 ListType.tp_methods = ListMethods;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003976 ListType.tp_iter = ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003977#if PY_MAJOR_VERSION >= 3
3978 ListType.tp_getattro = ListGetattro;
3979 ListType.tp_setattro = ListSetattro;
3980#else
3981 ListType.tp_getattr = ListGetattr;
3982 ListType.tp_setattr = ListSetattr;
3983#endif
3984
3985 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003986 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003987 FunctionType.tp_basicsize = sizeof(FunctionObject);
3988 FunctionType.tp_dealloc = FunctionDestructor;
3989 FunctionType.tp_call = FunctionCall;
3990 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
3991 FunctionType.tp_doc = "object that calls vim function";
3992 FunctionType.tp_methods = FunctionMethods;
3993#if PY_MAJOR_VERSION >= 3
3994 FunctionType.tp_getattro = FunctionGetattro;
3995#else
3996 FunctionType.tp_getattr = FunctionGetattr;
3997#endif
3998
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003999 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4000 OptionsType.tp_name = "vim.options";
4001 OptionsType.tp_basicsize = sizeof(OptionsObject);
4002 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4003 OptionsType.tp_doc = "object for manipulating options";
4004 OptionsType.tp_as_mapping = &OptionsAsMapping;
4005 OptionsType.tp_dealloc = OptionsDestructor;
4006
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004007#if PY_MAJOR_VERSION >= 3
4008 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4009 vimmodule.m_name = "vim";
4010 vimmodule.m_doc = "Vim Python interface\n";
4011 vimmodule.m_size = -1;
4012 vimmodule.m_methods = VimMethods;
4013#endif
4014}