blob: 280c24841223ee8e7c48fef65bc30215d19a1cb7 [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
Bram Moolenaard620aa92013-05-17 16:40:06 +020025#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020026
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020027#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
28
29#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
30#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020031#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020032
33static int ConvertFromPyObject(PyObject *, typval_T *);
34static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020035static PyObject *WindowNew(win_T *, tabpage_T *);
36static PyObject *BufferNew (buf_T *);
37static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020038
39static PyInt RangeStart;
40static PyInt RangeEnd;
41
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020042/*
43 * obtain a lock on the Vim data structures
44 */
45 static void
46Python_Lock_Vim(void)
47{
48}
49
50/*
51 * release a lock on the Vim data structures
52 */
53 static void
54Python_Release_Vim(void)
55{
56}
57
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020058/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020059 */
60
Bram Moolenaar2eea1982010-09-21 16:49:37 +020061/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020062typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020063
64static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020065
66typedef struct
67{
68 PyObject_HEAD
69 long softspace;
70 long error;
71} OutputObject;
72
Bram Moolenaar77045652012-09-21 13:46:06 +020073 static int
74OutputSetattr(PyObject *self, char *name, PyObject *val)
75{
76 if (val == NULL)
77 {
Bram Moolenaar8661b172013-05-15 15:44:28 +020078 PyErr_SetString(PyExc_AttributeError,
79 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +020080 return -1;
81 }
82
83 if (strcmp(name, "softspace") == 0)
84 {
85 if (!PyInt_Check(val))
86 {
87 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
88 return -1;
89 }
90
91 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
92 return 0;
93 }
94
95 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
96 return -1;
97}
98
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020099/* Buffer IO, we write one whole line at a time. */
100static garray_T io_ga = {0, 0, 1, 80, NULL};
101static writefn old_fn = NULL;
102
103 static void
104PythonIO_Flush(void)
105{
106 if (old_fn != NULL && io_ga.ga_len > 0)
107 {
108 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
109 old_fn((char_u *)io_ga.ga_data);
110 }
111 io_ga.ga_len = 0;
112}
113
114 static void
115writer(writefn fn, char_u *str, PyInt n)
116{
117 char_u *ptr;
118
119 /* Flush when switching output function. */
120 if (fn != old_fn)
121 PythonIO_Flush();
122 old_fn = fn;
123
124 /* Write each NL separated line. Text after the last NL is kept for
125 * writing later. */
126 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
127 {
128 PyInt len = ptr - str;
129
130 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
131 break;
132
133 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
134 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
135 fn((char_u *)io_ga.ga_data);
136 str = ptr + 1;
137 n -= len + 1;
138 io_ga.ga_len = 0;
139 }
140
141 /* Put the remaining text into io_ga for later printing. */
142 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
143 {
144 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
145 io_ga.ga_len += (int)n;
146 }
147}
148
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200149 static PyObject *
150OutputWrite(PyObject *self, PyObject *args)
151{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200152 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200153 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200154 int error = ((OutputObject *)(self))->error;
155
Bram Moolenaar27564802011-09-07 19:30:21 +0200156 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200157 return NULL;
158
159 Py_BEGIN_ALLOW_THREADS
160 Python_Lock_Vim();
161 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
162 Python_Release_Vim();
163 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200164 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200165
166 Py_INCREF(Py_None);
167 return Py_None;
168}
169
170 static PyObject *
171OutputWritelines(PyObject *self, PyObject *args)
172{
173 PyInt n;
174 PyInt i;
175 PyObject *list;
176 int error = ((OutputObject *)(self))->error;
177
178 if (!PyArg_ParseTuple(args, "O", &list))
179 return NULL;
180 Py_INCREF(list);
181
Bram Moolenaardb913952012-06-29 12:54:53 +0200182 if (!PyList_Check(list))
183 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200184 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
185 Py_DECREF(list);
186 return NULL;
187 }
188
189 n = PyList_Size(list);
190
191 for (i = 0; i < n; ++i)
192 {
193 PyObject *line = PyList_GetItem(list, i);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200194 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200195 PyInt len;
196
Bram Moolenaardb913952012-06-29 12:54:53 +0200197 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
198 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200199 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
200 Py_DECREF(list);
201 return NULL;
202 }
203
204 Py_BEGIN_ALLOW_THREADS
205 Python_Lock_Vim();
206 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
207 Python_Release_Vim();
208 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200209 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200210 }
211
212 Py_DECREF(list);
213 Py_INCREF(Py_None);
214 return Py_None;
215}
216
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100217 static PyObject *
218OutputFlush(PyObject *self UNUSED, PyObject *args UNUSED)
219{
220 /* do nothing */
221 Py_INCREF(Py_None);
222 return Py_None;
223}
224
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200225/***************/
226
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200227static struct PyMethodDef OutputMethods[] = {
228 /* name, function, calling, documentation */
229 {"write", OutputWrite, 1, ""},
230 {"writelines", OutputWritelines, 1, ""},
231 {"flush", OutputFlush, 1, ""},
232 { NULL, NULL, 0, NULL}
233};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200234
235static OutputObject Output =
236{
237 PyObject_HEAD_INIT(&OutputType)
238 0,
239 0
240};
241
242static OutputObject Error =
243{
244 PyObject_HEAD_INIT(&OutputType)
245 0,
246 1
247};
248
249 static int
250PythonIO_Init_io(void)
251{
252 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
253 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
254
255 if (PyErr_Occurred())
256 {
257 EMSG(_("E264: Python: Error initialising I/O objects"));
258 return -1;
259 }
260
261 return 0;
262}
263
264
265static PyObject *VimError;
266
267/* Check to see whether a Vim error has been reported, or a keyboard
268 * interrupt has been detected.
269 */
270 static int
271VimErrorCheck(void)
272{
273 if (got_int)
274 {
275 PyErr_SetNone(PyExc_KeyboardInterrupt);
276 return 1;
277 }
278 else if (did_emsg && !PyErr_Occurred())
279 {
280 PyErr_SetNone(VimError);
281 return 1;
282 }
283
284 return 0;
285}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200286
287/* Vim module - Implementation
288 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200289
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200290 static PyObject *
291VimCommand(PyObject *self UNUSED, PyObject *args)
292{
293 char *cmd;
294 PyObject *result;
295
296 if (!PyArg_ParseTuple(args, "s", &cmd))
297 return NULL;
298
299 PyErr_Clear();
300
301 Py_BEGIN_ALLOW_THREADS
302 Python_Lock_Vim();
303
304 do_cmdline_cmd((char_u *)cmd);
305 update_screen(VALID);
306
307 Python_Release_Vim();
308 Py_END_ALLOW_THREADS
309
310 if (VimErrorCheck())
311 result = NULL;
312 else
313 result = Py_None;
314
315 Py_XINCREF(result);
316 return result;
317}
318
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200319/*
320 * Function to translate a typval_T into a PyObject; this will recursively
321 * translate lists/dictionaries into their Python equivalents.
322 *
323 * The depth parameter is to avoid infinite recursion, set it to 1 when
324 * you call VimToPython.
325 */
326 static PyObject *
327VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
328{
329 PyObject *result;
330 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200331 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200332
333 /* Avoid infinite recursion */
334 if (depth > 100)
335 {
336 Py_INCREF(Py_None);
337 result = Py_None;
338 return result;
339 }
340
341 /* Check if we run into a recursive loop. The item must be in lookupDict
342 * then and we can use it again. */
343 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
344 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
345 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200346 sprintf(ptrBuf, "%p",
347 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
348 : (void *)our_tv->vval.v_dict);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200349 result = PyDict_GetItemString(lookupDict, ptrBuf);
350 if (result != NULL)
351 {
352 Py_INCREF(result);
353 return result;
354 }
355 }
356
357 if (our_tv->v_type == VAR_STRING)
358 {
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200359 result = Py_BuildValue("s", our_tv->vval.v_string == NULL
360 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200361 }
362 else if (our_tv->v_type == VAR_NUMBER)
363 {
364 char buf[NUMBUFLEN];
365
366 /* For backwards compatibility numbers are stored as strings. */
367 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
368 result = Py_BuildValue("s", buf);
369 }
370# ifdef FEAT_FLOAT
371 else if (our_tv->v_type == VAR_FLOAT)
372 {
373 char buf[NUMBUFLEN];
374
375 sprintf(buf, "%f", our_tv->vval.v_float);
376 result = Py_BuildValue("s", buf);
377 }
378# endif
379 else if (our_tv->v_type == VAR_LIST)
380 {
381 list_T *list = our_tv->vval.v_list;
382 listitem_T *curr;
383
384 result = PyList_New(0);
385
386 if (list != NULL)
387 {
388 PyDict_SetItemString(lookupDict, ptrBuf, result);
389
390 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
391 {
392 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
393 PyList_Append(result, newObj);
394 Py_DECREF(newObj);
395 }
396 }
397 }
398 else if (our_tv->v_type == VAR_DICT)
399 {
400 result = PyDict_New();
401
402 if (our_tv->vval.v_dict != NULL)
403 {
404 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
405 long_u todo = ht->ht_used;
406 hashitem_T *hi;
407 dictitem_T *di;
408
409 PyDict_SetItemString(lookupDict, ptrBuf, result);
410
411 for (hi = ht->ht_array; todo > 0; ++hi)
412 {
413 if (!HASHITEM_EMPTY(hi))
414 {
415 --todo;
416
417 di = dict_lookup(hi);
418 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
419 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
420 Py_DECREF(newObj);
421 }
422 }
423 }
424 }
425 else
426 {
427 Py_INCREF(Py_None);
428 result = Py_None;
429 }
430
431 return result;
432}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200433
434 static PyObject *
Bram Moolenaar09092152010-08-08 16:38:42 +0200435VimEval(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200436{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200437 char *expr;
438 typval_T *our_tv;
439 PyObject *result;
440 PyObject *lookup_dict;
441
442 if (!PyArg_ParseTuple(args, "s", &expr))
443 return NULL;
444
445 Py_BEGIN_ALLOW_THREADS
446 Python_Lock_Vim();
447 our_tv = eval_expr((char_u *)expr, NULL);
448
449 Python_Release_Vim();
450 Py_END_ALLOW_THREADS
451
452 if (our_tv == NULL)
453 {
454 PyErr_SetVim(_("invalid expression"));
455 return NULL;
456 }
457
458 /* Convert the Vim type into a Python type. Create a dictionary that's
459 * used to check for recursive loops. */
460 lookup_dict = PyDict_New();
461 result = VimToPython(our_tv, 1, lookup_dict);
462 Py_DECREF(lookup_dict);
463
464
465 Py_BEGIN_ALLOW_THREADS
466 Python_Lock_Vim();
467 free_tv(our_tv);
468 Python_Release_Vim();
469 Py_END_ALLOW_THREADS
470
471 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200472}
473
Bram Moolenaardb913952012-06-29 12:54:53 +0200474static PyObject *ConvertToPyObject(typval_T *);
475
476 static PyObject *
477VimEvalPy(PyObject *self UNUSED, PyObject *args UNUSED)
478{
Bram Moolenaardb913952012-06-29 12:54:53 +0200479 char *expr;
480 typval_T *our_tv;
481 PyObject *result;
482
483 if (!PyArg_ParseTuple(args, "s", &expr))
484 return NULL;
485
486 Py_BEGIN_ALLOW_THREADS
487 Python_Lock_Vim();
488 our_tv = eval_expr((char_u *)expr, NULL);
489
490 Python_Release_Vim();
491 Py_END_ALLOW_THREADS
492
493 if (our_tv == NULL)
494 {
495 PyErr_SetVim(_("invalid expression"));
496 return NULL;
497 }
498
499 result = ConvertToPyObject(our_tv);
500 Py_BEGIN_ALLOW_THREADS
501 Python_Lock_Vim();
502 free_tv(our_tv);
503 Python_Release_Vim();
504 Py_END_ALLOW_THREADS
505
506 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200507}
508
509 static PyObject *
510VimStrwidth(PyObject *self UNUSED, PyObject *args)
511{
512 char *expr;
513
514 if (!PyArg_ParseTuple(args, "s", &expr))
515 return NULL;
516
Bram Moolenaara54bf402012-12-05 16:30:07 +0100517 return PyLong_FromLong(
518#ifdef FEAT_MBYTE
519 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
520#else
521 STRLEN(expr)
522#endif
523 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200524}
525
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200526/*
527 * Vim module - Definitions
528 */
529
530static struct PyMethodDef VimMethods[] = {
531 /* name, function, calling, documentation */
532 {"command", VimCommand, 1, "Execute a Vim ex-mode command" },
533 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200534 {"bindeval", VimEvalPy, 1, "Like eval(), but returns objects attached to vim ones"},
535 {"strwidth", VimStrwidth, 1, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200536 { NULL, NULL, 0, NULL }
537};
538
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200539/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200540 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200541 */
542
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200543static PyTypeObject IterType;
544
545typedef PyObject *(*nextfun)(void **);
546typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200547typedef int (*traversefun)(void *, visitproc, void *);
548typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200549
550/* Main purpose of this object is removing the need for do python initialization
551 * (i.e. PyType_Ready and setting type attributes) for a big bunch of objects.
552 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200553
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200554typedef struct
555{
556 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200557 void *cur;
558 nextfun next;
559 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200560 traversefun traverse;
561 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200562} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200563
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200564 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200565IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
566 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200567{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200568 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200569
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200570 self = PyObject_NEW(IterObject, &IterType);
571 self->cur = start;
572 self->next = next;
573 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200574 self->traverse = traverse;
575 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200576
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200577 return (PyObject *)(self);
578}
579
580 static void
581IterDestructor(PyObject *self)
582{
583 IterObject *this = (IterObject *)(self);
584
585 this->destruct(this->cur);
586
587 DESTRUCTOR_FINISH(self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200588}
589
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200590 static int
591IterTraverse(PyObject *self, visitproc visit, void *arg)
592{
593 IterObject *this = (IterObject *)(self);
594
595 if (this->traverse != NULL)
596 return this->traverse(this->cur, visit, arg);
597 else
598 return 0;
599}
600
601 static int
602IterClear(PyObject *self)
603{
604 IterObject *this = (IterObject *)(self);
605
606 if (this->clear != NULL)
607 return this->clear(&this->cur);
608 else
609 return 0;
610}
611
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200612 static PyObject *
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200613IterNext(PyObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200614{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200615 IterObject *this = (IterObject *)(self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200616
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200617 return this->next(&this->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200618}
619
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200620 static PyObject *
621IterIter(PyObject *self)
622{
623 return self;
624}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200625
Bram Moolenaardb913952012-06-29 12:54:53 +0200626typedef struct pylinkedlist_S {
627 struct pylinkedlist_S *pll_next;
628 struct pylinkedlist_S *pll_prev;
629 PyObject *pll_obj;
630} pylinkedlist_T;
631
632static pylinkedlist_T *lastdict = NULL;
633static pylinkedlist_T *lastlist = NULL;
634
635 static void
636pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
637{
638 if (ref->pll_prev == NULL)
639 {
640 if (ref->pll_next == NULL)
641 {
642 *last = NULL;
643 return;
644 }
645 }
646 else
647 ref->pll_prev->pll_next = ref->pll_next;
648
649 if (ref->pll_next == NULL)
650 *last = ref->pll_prev;
651 else
652 ref->pll_next->pll_prev = ref->pll_prev;
653}
654
655 static void
656pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
657{
658 if (*last == NULL)
659 ref->pll_prev = NULL;
660 else
661 {
662 (*last)->pll_next = ref;
663 ref->pll_prev = *last;
664 }
665 ref->pll_next = NULL;
666 ref->pll_obj = self;
667 *last = ref;
668}
669
670static PyTypeObject DictionaryType;
671
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200672#define DICTKEY_GET_NOTEMPTY(err) \
673 DICTKEY_GET(err) \
674 if (*key == NUL) \
675 { \
676 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
677 return err; \
678 }
679
Bram Moolenaardb913952012-06-29 12:54:53 +0200680typedef struct
681{
682 PyObject_HEAD
683 dict_T *dict;
684 pylinkedlist_T ref;
685} DictionaryObject;
686
687 static PyObject *
688DictionaryNew(dict_T *dict)
689{
690 DictionaryObject *self;
691
692 self = PyObject_NEW(DictionaryObject, &DictionaryType);
693 if (self == NULL)
694 return NULL;
695 self->dict = dict;
696 ++dict->dv_refcount;
697
698 pyll_add((PyObject *)(self), &self->ref, &lastdict);
699
700 return (PyObject *)(self);
701}
702
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200703 static void
704DictionaryDestructor(PyObject *self)
705{
706 DictionaryObject *this = ((DictionaryObject *) (self));
707
708 pyll_remove(&this->ref, &lastdict);
709 dict_unref(this->dict);
710
711 DESTRUCTOR_FINISH(self);
712}
713
Bram Moolenaardb913952012-06-29 12:54:53 +0200714 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200715DictionarySetattr(PyObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200716{
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200717 DictionaryObject *this = (DictionaryObject *)(self);
718
Bram Moolenaar66b79852012-09-21 14:00:35 +0200719 if (val == NULL)
720 {
721 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
722 return -1;
723 }
724
725 if (strcmp(name, "locked") == 0)
726 {
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200727 if (this->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200728 {
729 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
730 return -1;
731 }
732 else
733 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200734 int istrue = PyObject_IsTrue(val);
735 if (istrue == -1)
736 return -1;
737 else if (istrue)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200738 this->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200739 else
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200740 this->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200741 }
742 return 0;
743 }
744 else
745 {
746 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
747 return -1;
748 }
749}
750
751 static PyInt
Bram Moolenaardb913952012-06-29 12:54:53 +0200752DictionaryLength(PyObject *self)
753{
754 return ((PyInt) ((((DictionaryObject *)(self))->dict->dv_hashtab.ht_used)));
755}
756
757 static PyObject *
758DictionaryItem(PyObject *self, PyObject *keyObject)
759{
760 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200761 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200762 DICTKEY_DECL
763
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200764 DICTKEY_GET_NOTEMPTY(NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200765
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200766 di = dict_find(((DictionaryObject *) (self))->dict, key, -1);
767
Bram Moolenaar696c2112012-09-21 13:43:14 +0200768 DICTKEY_UNREF
769
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200770 if (di == NULL)
771 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200772 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200773 return NULL;
774 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200775
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200776 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200777}
778
779 static PyInt
780DictionaryAssItem(PyObject *self, PyObject *keyObject, PyObject *valObject)
781{
782 char_u *key;
783 typval_T tv;
784 dict_T *d = ((DictionaryObject *)(self))->dict;
785 dictitem_T *di;
786 DICTKEY_DECL
787
788 if (d->dv_lock)
789 {
790 PyErr_SetVim(_("dict is locked"));
791 return -1;
792 }
793
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200794 DICTKEY_GET_NOTEMPTY(-1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200795
796 di = dict_find(d, key, -1);
797
798 if (valObject == NULL)
799 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200800 hashitem_T *hi;
801
Bram Moolenaardb913952012-06-29 12:54:53 +0200802 if (di == NULL)
803 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200804 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200805 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +0200806 return -1;
807 }
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200808 hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaardb913952012-06-29 12:54:53 +0200809 hash_remove(&d->dv_hashtab, hi);
810 dictitem_free(di);
811 return 0;
812 }
813
814 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200815 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +0200816
817 if (di == NULL)
818 {
819 di = dictitem_alloc(key);
820 if (di == NULL)
821 {
822 PyErr_NoMemory();
823 return -1;
824 }
825 di->di_tv.v_lock = 0;
826
827 if (dict_add(d, di) == FAIL)
828 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200829 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +0200830 vim_free(di);
831 PyErr_SetVim(_("failed to add key to dictionary"));
832 return -1;
833 }
834 }
835 else
836 clear_tv(&di->di_tv);
837
838 DICTKEY_UNREF
839
840 copy_tv(&tv, &di->di_tv);
841 return 0;
842}
843
844 static PyObject *
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100845DictionaryListKeys(PyObject *self UNUSED)
Bram Moolenaardb913952012-06-29 12:54:53 +0200846{
847 dict_T *dict = ((DictionaryObject *)(self))->dict;
848 long_u todo = dict->dv_hashtab.ht_used;
849 Py_ssize_t i = 0;
850 PyObject *r;
851 hashitem_T *hi;
852
853 r = PyList_New(todo);
854 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
855 {
856 if (!HASHITEM_EMPTY(hi))
857 {
858 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
859 --todo;
860 ++i;
861 }
862 }
863 return r;
864}
865
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200866static PyMappingMethods DictionaryAsMapping = {
867 (lenfunc) DictionaryLength,
868 (binaryfunc) DictionaryItem,
869 (objobjargproc) DictionaryAssItem,
870};
871
Bram Moolenaardb913952012-06-29 12:54:53 +0200872static struct PyMethodDef DictionaryMethods[] = {
873 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
874 { NULL, NULL, 0, NULL }
875};
876
877static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200878static PySequenceMethods ListAsSeq;
879static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +0200880
881typedef struct
882{
883 PyObject_HEAD
884 list_T *list;
885 pylinkedlist_T ref;
886} ListObject;
887
888 static PyObject *
889ListNew(list_T *list)
890{
891 ListObject *self;
892
893 self = PyObject_NEW(ListObject, &ListType);
894 if (self == NULL)
895 return NULL;
896 self->list = list;
897 ++list->lv_refcount;
898
899 pyll_add((PyObject *)(self), &self->ref, &lastlist);
900
901 return (PyObject *)(self);
902}
903
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200904 static void
905ListDestructor(PyObject *self)
906{
907 ListObject *this = (ListObject *)(self);
908
909 pyll_remove(&this->ref, &lastlist);
910 list_unref(this->list);
911
912 DESTRUCTOR_FINISH(self);
913}
914
Bram Moolenaardb913952012-06-29 12:54:53 +0200915 static int
916list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict)
917{
918 Py_ssize_t i;
919 Py_ssize_t lsize = PySequence_Size(obj);
920 PyObject *litem;
921 listitem_T *li;
922
923 for(i=0; i<lsize; i++)
924 {
925 li = listitem_alloc();
926 if (li == NULL)
927 {
928 PyErr_NoMemory();
929 return -1;
930 }
931 li->li_tv.v_lock = 0;
932
933 litem = PySequence_GetItem(obj, i);
934 if (litem == NULL)
935 return -1;
936 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1)
937 return -1;
938
939 list_append(l, li);
940 }
941 return 0;
942}
943
Bram Moolenaardb913952012-06-29 12:54:53 +0200944 static PyInt
945ListLength(PyObject *self)
946{
947 return ((PyInt) (((ListObject *) (self))->list->lv_len));
948}
949
950 static PyObject *
951ListItem(PyObject *self, Py_ssize_t index)
952{
953 listitem_T *li;
954
955 if (index>=ListLength(self))
956 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200957 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200958 return NULL;
959 }
960 li = list_find(((ListObject *) (self))->list, (long) index);
961 if (li == NULL)
962 {
963 PyErr_SetVim(_("internal error: failed to get vim list item"));
964 return NULL;
965 }
966 return ConvertToPyObject(&li->li_tv);
967}
968
969#define PROC_RANGE \
970 if (last < 0) {\
971 if (last < -size) \
972 last = 0; \
973 else \
974 last += size; \
975 } \
976 if (first < 0) \
977 first = 0; \
978 if (first > size) \
979 first = size; \
980 if (last > size) \
981 last = size;
982
983 static PyObject *
984ListSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last)
985{
986 PyInt i;
987 PyInt size = ListLength(self);
988 PyInt n;
989 PyObject *list;
990 int reversed = 0;
991
992 PROC_RANGE
993 if (first >= last)
994 first = last;
995
996 n = last-first;
997 list = PyList_New(n);
998 if (list == NULL)
999 return NULL;
1000
1001 for (i = 0; i < n; ++i)
1002 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001003 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001004 if (item == NULL)
1005 {
1006 Py_DECREF(list);
1007 return NULL;
1008 }
1009
1010 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1011 {
1012 Py_DECREF(item);
1013 Py_DECREF(list);
1014 return NULL;
1015 }
1016 }
1017
1018 return list;
1019}
1020
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001021typedef struct
1022{
1023 listwatch_T lw;
1024 list_T *list;
1025} listiterinfo_T;
1026
1027 static void
1028ListIterDestruct(listiterinfo_T *lii)
1029{
1030 list_rem_watch(lii->list, &lii->lw);
1031 PyMem_Free(lii);
1032}
1033
1034 static PyObject *
1035ListIterNext(listiterinfo_T **lii)
1036{
1037 PyObject *r;
1038
1039 if (!((*lii)->lw.lw_item))
1040 return NULL;
1041
1042 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1043 return NULL;
1044
1045 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1046
1047 return r;
1048}
1049
1050 static PyObject *
1051ListIter(PyObject *self)
1052{
1053 listiterinfo_T *lii;
1054 list_T *l = ((ListObject *) (self))->list;
1055
1056 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1057 {
1058 PyErr_NoMemory();
1059 return NULL;
1060 }
1061
1062 list_add_watch(l, &lii->lw);
1063 lii->lw.lw_item = l->lv_first;
1064 lii->list = l;
1065
1066 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001067 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1068 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001069}
1070
Bram Moolenaardb913952012-06-29 12:54:53 +02001071 static int
1072ListAssItem(PyObject *self, Py_ssize_t index, PyObject *obj)
1073{
1074 typval_T tv;
1075 list_T *l = ((ListObject *) (self))->list;
1076 listitem_T *li;
1077 Py_ssize_t length = ListLength(self);
1078
1079 if (l->lv_lock)
1080 {
1081 PyErr_SetVim(_("list is locked"));
1082 return -1;
1083 }
1084 if (index>length || (index==length && obj==NULL))
1085 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001086 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001087 return -1;
1088 }
1089
1090 if (obj == NULL)
1091 {
1092 li = list_find(l, (long) index);
1093 list_remove(l, li, li);
1094 clear_tv(&li->li_tv);
1095 vim_free(li);
1096 return 0;
1097 }
1098
1099 if (ConvertFromPyObject(obj, &tv) == -1)
1100 return -1;
1101
1102 if (index == length)
1103 {
1104 if (list_append_tv(l, &tv) == FAIL)
1105 {
1106 PyErr_SetVim(_("Failed to add item to list"));
1107 return -1;
1108 }
1109 }
1110 else
1111 {
1112 li = list_find(l, (long) index);
1113 clear_tv(&li->li_tv);
1114 copy_tv(&tv, &li->li_tv);
1115 }
1116 return 0;
1117}
1118
1119 static int
1120ListAssSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
1121{
1122 PyInt size = ListLength(self);
1123 Py_ssize_t i;
1124 Py_ssize_t lsize;
1125 PyObject *litem;
1126 listitem_T *li;
1127 listitem_T *next;
1128 typval_T v;
1129 list_T *l = ((ListObject *) (self))->list;
1130
1131 if (l->lv_lock)
1132 {
1133 PyErr_SetVim(_("list is locked"));
1134 return -1;
1135 }
1136
1137 PROC_RANGE
1138
1139 if (first == size)
1140 li = NULL;
1141 else
1142 {
1143 li = list_find(l, (long) first);
1144 if (li == NULL)
1145 {
1146 PyErr_SetVim(_("internal error: no vim list item"));
1147 return -1;
1148 }
1149 if (last > first)
1150 {
1151 i = last - first;
1152 while (i-- && li != NULL)
1153 {
1154 next = li->li_next;
1155 listitem_remove(l, li);
1156 li = next;
1157 }
1158 }
1159 }
1160
1161 if (obj == NULL)
1162 return 0;
1163
1164 if (!PyList_Check(obj))
1165 {
1166 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1167 return -1;
1168 }
1169
1170 lsize = PyList_Size(obj);
1171
1172 for(i=0; i<lsize; i++)
1173 {
1174 litem = PyList_GetItem(obj, i);
1175 if (litem == NULL)
1176 return -1;
1177 if (ConvertFromPyObject(litem, &v) == -1)
1178 return -1;
1179 if (list_insert_tv(l, &v, li) == FAIL)
1180 {
1181 PyErr_SetVim(_("internal error: failed to add item to list"));
1182 return -1;
1183 }
1184 }
1185 return 0;
1186}
1187
1188 static PyObject *
1189ListConcatInPlace(PyObject *self, PyObject *obj)
1190{
1191 list_T *l = ((ListObject *) (self))->list;
1192 PyObject *lookup_dict;
1193
1194 if (l->lv_lock)
1195 {
1196 PyErr_SetVim(_("list is locked"));
1197 return NULL;
1198 }
1199
1200 if (!PySequence_Check(obj))
1201 {
1202 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1203 return NULL;
1204 }
1205
1206 lookup_dict = PyDict_New();
1207 if (list_py_concat(l, obj, lookup_dict) == -1)
1208 {
1209 Py_DECREF(lookup_dict);
1210 return NULL;
1211 }
1212 Py_DECREF(lookup_dict);
1213
1214 Py_INCREF(self);
1215 return self;
1216}
1217
Bram Moolenaar66b79852012-09-21 14:00:35 +02001218 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001219ListSetattr(PyObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001220{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001221 ListObject *this = (ListObject *)(self);
1222
Bram Moolenaar66b79852012-09-21 14:00:35 +02001223 if (val == NULL)
1224 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001225 PyErr_SetString(PyExc_AttributeError,
1226 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001227 return -1;
1228 }
1229
1230 if (strcmp(name, "locked") == 0)
1231 {
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001232 if (this->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001233 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001234 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001235 return -1;
1236 }
1237 else
1238 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001239 int istrue = PyObject_IsTrue(val);
1240 if (istrue == -1)
1241 return -1;
1242 else if (istrue)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001243 this->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001244 else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001245 this->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001246 }
1247 return 0;
1248 }
1249 else
1250 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001251 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001252 return -1;
1253 }
1254}
1255
Bram Moolenaardb913952012-06-29 12:54:53 +02001256static struct PyMethodDef ListMethods[] = {
1257 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1258 { NULL, NULL, 0, NULL }
1259};
1260
1261typedef struct
1262{
1263 PyObject_HEAD
1264 char_u *name;
1265} FunctionObject;
1266
1267static PyTypeObject FunctionType;
1268
1269 static PyObject *
1270FunctionNew(char_u *name)
1271{
1272 FunctionObject *self;
1273
1274 self = PyObject_NEW(FunctionObject, &FunctionType);
1275 if (self == NULL)
1276 return NULL;
1277 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1278 if (self->name == NULL)
1279 {
1280 PyErr_NoMemory();
1281 return NULL;
1282 }
1283 STRCPY(self->name, name);
1284 func_ref(name);
1285 return (PyObject *)(self);
1286}
1287
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001288 static void
1289FunctionDestructor(PyObject *self)
1290{
1291 FunctionObject *this = (FunctionObject *) (self);
1292
1293 func_unref(this->name);
1294 PyMem_Del(this->name);
1295
1296 DESTRUCTOR_FINISH(self);
1297}
1298
Bram Moolenaardb913952012-06-29 12:54:53 +02001299 static PyObject *
1300FunctionCall(PyObject *self, PyObject *argsObject, PyObject *kwargs)
1301{
1302 FunctionObject *this = (FunctionObject *)(self);
1303 char_u *name = this->name;
1304 typval_T args;
1305 typval_T selfdicttv;
1306 typval_T rettv;
1307 dict_T *selfdict = NULL;
1308 PyObject *selfdictObject;
1309 PyObject *result;
1310 int error;
1311
1312 if (ConvertFromPyObject(argsObject, &args) == -1)
1313 return NULL;
1314
1315 if (kwargs != NULL)
1316 {
1317 selfdictObject = PyDict_GetItemString(kwargs, "self");
1318 if (selfdictObject != NULL)
1319 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001320 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001321 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001322 PyErr_SetString(PyExc_TypeError,
1323 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001324 clear_tv(&args);
1325 return NULL;
1326 }
1327 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
1328 return NULL;
1329 selfdict = selfdicttv.vval.v_dict;
1330 }
1331 }
1332
Bram Moolenaar71700b82013-05-15 17:49:05 +02001333 Py_BEGIN_ALLOW_THREADS
1334 Python_Lock_Vim();
1335
Bram Moolenaardb913952012-06-29 12:54:53 +02001336 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001337
1338 Python_Release_Vim();
1339 Py_END_ALLOW_THREADS
1340
Bram Moolenaardb913952012-06-29 12:54:53 +02001341 if (error != OK)
1342 {
1343 result = NULL;
1344 PyErr_SetVim(_("failed to run function"));
1345 }
1346 else
1347 result = ConvertToPyObject(&rettv);
1348
1349 /* FIXME Check what should really be cleared. */
1350 clear_tv(&args);
1351 clear_tv(&rettv);
1352 /*
1353 * if (selfdict!=NULL)
1354 * clear_tv(selfdicttv);
1355 */
1356
1357 return result;
1358}
1359
1360static struct PyMethodDef FunctionMethods[] = {
1361 {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1362 { NULL, NULL, 0, NULL }
1363};
1364
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001365/*
1366 * Options object
1367 */
1368
1369static PyTypeObject OptionsType;
1370
1371typedef int (*checkfun)(void *);
1372
1373typedef struct
1374{
1375 PyObject_HEAD
1376 int opt_type;
1377 void *from;
1378 checkfun Check;
1379 PyObject *fromObj;
1380} OptionsObject;
1381
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001382 static int
1383dummy_check(void *arg UNUSED)
1384{
1385 return 0;
1386}
1387
1388 static PyObject *
1389OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1390{
1391 OptionsObject *self;
1392
1393 self = PyObject_NEW(OptionsObject, &OptionsType);
1394 if (self == NULL)
1395 return NULL;
1396
1397 self->opt_type = opt_type;
1398 self->from = from;
1399 self->Check = Check;
1400 self->fromObj = fromObj;
1401 if (fromObj)
1402 Py_INCREF(fromObj);
1403
1404 return (PyObject *)(self);
1405}
1406
1407 static void
1408OptionsDestructor(PyObject *self)
1409{
1410 if (((OptionsObject *)(self))->fromObj)
1411 Py_DECREF(((OptionsObject *)(self))->fromObj);
1412 DESTRUCTOR_FINISH(self);
1413}
1414
1415 static int
1416OptionsTraverse(PyObject *self, visitproc visit, void *arg)
1417{
1418 Py_VISIT(((OptionsObject *)(self))->fromObj);
1419 return 0;
1420}
1421
1422 static int
1423OptionsClear(PyObject *self)
1424{
1425 Py_CLEAR(((OptionsObject *)(self))->fromObj);
1426 return 0;
1427}
1428
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001429 static PyObject *
1430OptionsItem(OptionsObject *this, PyObject *keyObject)
1431{
1432 char_u *key;
1433 int flags;
1434 long numval;
1435 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001436 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001437
1438 if (this->Check(this->from))
1439 return NULL;
1440
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001441 DICTKEY_GET_NOTEMPTY(NULL)
1442
1443 flags = get_option_value_strict(key, &numval, &stringval,
1444 this->opt_type, this->from);
1445
1446 DICTKEY_UNREF
1447
1448 if (flags == 0)
1449 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001450 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001451 return NULL;
1452 }
1453
1454 if (flags & SOPT_UNSET)
1455 {
1456 Py_INCREF(Py_None);
1457 return Py_None;
1458 }
1459 else if (flags & SOPT_BOOL)
1460 {
1461 PyObject *r;
1462 r = numval ? Py_True : Py_False;
1463 Py_INCREF(r);
1464 return r;
1465 }
1466 else if (flags & SOPT_NUM)
1467 return PyInt_FromLong(numval);
1468 else if (flags & SOPT_STRING)
1469 {
1470 if (stringval)
1471 return PyBytes_FromString((char *) stringval);
1472 else
1473 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001474 PyErr_SetString(PyExc_RuntimeError,
1475 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001476 return NULL;
1477 }
1478 }
1479 else
1480 {
1481 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1482 return NULL;
1483 }
1484}
1485
1486 static int
1487set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1488 char_u *key;
1489 int numval;
1490 char_u *stringval;
1491 int opt_flags;
1492 int opt_type;
1493 void *from;
1494{
1495 win_T *save_curwin;
1496 tabpage_T *save_curtab;
Bram Moolenaar105bc352013-05-17 16:03:57 +02001497 buf_T *save_curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001498 int r = 0;
1499
1500 switch (opt_type)
1501 {
1502 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001503 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1504 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001505 {
1506 PyErr_SetVim("Problem while switching windows.");
1507 return -1;
1508 }
1509 set_option_value(key, numval, stringval, opt_flags);
1510 restore_win(save_curwin, save_curtab);
1511 break;
1512 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001513 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001514 set_option_value(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001515 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001516 break;
1517 case SREQ_GLOBAL:
1518 set_option_value(key, numval, stringval, opt_flags);
1519 break;
1520 }
1521 return r;
1522}
1523
1524 static int
1525OptionsAssItem(OptionsObject *this, PyObject *keyObject, PyObject *valObject)
1526{
1527 char_u *key;
1528 int flags;
1529 int opt_flags;
1530 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001531 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001532
1533 if (this->Check(this->from))
1534 return -1;
1535
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001536 DICTKEY_GET_NOTEMPTY(-1)
1537
1538 flags = get_option_value_strict(key, NULL, NULL,
1539 this->opt_type, this->from);
1540
1541 DICTKEY_UNREF
1542
1543 if (flags == 0)
1544 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001545 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001546 return -1;
1547 }
1548
1549 if (valObject == NULL)
1550 {
1551 if (this->opt_type == SREQ_GLOBAL)
1552 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001553 PyErr_SetString(PyExc_ValueError,
1554 _("unable to unset global option"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001555 return -1;
1556 }
1557 else if (!(flags & SOPT_GLOBAL))
1558 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001559 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1560 "without global value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001561 return -1;
1562 }
1563 else
1564 {
1565 unset_global_local_option(key, this->from);
1566 return 0;
1567 }
1568 }
1569
1570 opt_flags = (this->opt_type ? OPT_LOCAL : OPT_GLOBAL);
1571
1572 if (flags & SOPT_BOOL)
1573 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001574 int istrue = PyObject_IsTrue(valObject);
1575 if (istrue == -1)
1576 return -1;
1577 r = set_option_value_for(key, istrue, NULL,
Bram Moolenaar03db85b2013-05-15 14:51:35 +02001578 opt_flags, this->opt_type, this->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001579 }
1580 else if (flags & SOPT_NUM)
1581 {
1582 int val;
1583
1584#if PY_MAJOR_VERSION < 3
1585 if (PyInt_Check(valObject))
1586 val = PyInt_AsLong(valObject);
1587 else
1588#endif
1589 if (PyLong_Check(valObject))
1590 val = PyLong_AsLong(valObject);
1591 else
1592 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001593 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001594 return -1;
1595 }
1596
1597 r = set_option_value_for(key, val, NULL, opt_flags,
1598 this->opt_type, this->from);
1599 }
1600 else
1601 {
1602 char_u *val;
1603 if (PyBytes_Check(valObject))
1604 {
1605
1606 if (PyString_AsStringAndSize(valObject, (char **) &val, NULL) == -1)
1607 return -1;
1608 if (val == NULL)
1609 return -1;
1610
1611 val = vim_strsave(val);
1612 }
1613 else if (PyUnicode_Check(valObject))
1614 {
1615 PyObject *bytes;
1616
1617 bytes = PyUnicode_AsEncodedString(valObject, (char *)ENC_OPT, NULL);
1618 if (bytes == NULL)
1619 return -1;
1620
1621 if(PyString_AsStringAndSize(bytes, (char **) &val, NULL) == -1)
1622 return -1;
1623 if (val == NULL)
1624 return -1;
1625
1626 val = vim_strsave(val);
1627 Py_XDECREF(bytes);
1628 }
1629 else
1630 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001631 PyErr_SetString(PyExc_TypeError, _("object must be string"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001632 return -1;
1633 }
1634
1635 r = set_option_value_for(key, 0, val, opt_flags,
1636 this->opt_type, this->from);
1637 vim_free(val);
1638 }
1639
1640 return r;
1641}
1642
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001643static PyMappingMethods OptionsAsMapping = {
1644 (lenfunc) NULL,
1645 (binaryfunc) OptionsItem,
1646 (objobjargproc) OptionsAssItem,
1647};
1648
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001649/* Tabpage object
1650 */
1651
1652typedef struct
1653{
1654 PyObject_HEAD
1655 tabpage_T *tab;
1656} TabPageObject;
1657
1658static PyObject *WinListNew(TabPageObject *tabObject);
1659
1660static PyTypeObject TabPageType;
1661
1662 static int
1663CheckTabPage(TabPageObject *this)
1664{
1665 if (this->tab == INVALID_TABPAGE_VALUE)
1666 {
1667 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1668 return -1;
1669 }
1670
1671 return 0;
1672}
1673
1674 static PyObject *
1675TabPageNew(tabpage_T *tab)
1676{
1677 TabPageObject *self;
1678
1679 if (TAB_PYTHON_REF(tab))
1680 {
1681 self = TAB_PYTHON_REF(tab);
1682 Py_INCREF(self);
1683 }
1684 else
1685 {
1686 self = PyObject_NEW(TabPageObject, &TabPageType);
1687 if (self == NULL)
1688 return NULL;
1689 self->tab = tab;
1690 TAB_PYTHON_REF(tab) = self;
1691 }
1692
1693 return (PyObject *)(self);
1694}
1695
1696 static void
1697TabPageDestructor(PyObject *self)
1698{
1699 TabPageObject *this = (TabPageObject *)(self);
1700
1701 if (this->tab && this->tab != INVALID_TABPAGE_VALUE)
1702 TAB_PYTHON_REF(this->tab) = NULL;
1703
1704 DESTRUCTOR_FINISH(self);
1705}
1706
1707 static PyObject *
1708TabPageAttr(TabPageObject *this, char *name)
1709{
1710 if (strcmp(name, "windows") == 0)
1711 return WinListNew(this);
1712 else if (strcmp(name, "number") == 0)
1713 return PyLong_FromLong((long) get_tab_number(this->tab));
1714 else if (strcmp(name, "vars") == 0)
1715 return DictionaryNew(this->tab->tp_vars);
1716 else if (strcmp(name, "window") == 0)
1717 {
1718 /* For current tab window.c does not bother to set or update tp_curwin
1719 */
1720 if (this->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001721 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001722 else
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001723 return WindowNew(this->tab->tp_curwin, this->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001724 }
1725 return NULL;
1726}
1727
1728 static PyObject *
1729TabPageRepr(PyObject *self)
1730{
1731 static char repr[100];
1732 TabPageObject *this = (TabPageObject *)(self);
1733
1734 if (this->tab == INVALID_TABPAGE_VALUE)
1735 {
1736 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
1737 return PyString_FromString(repr);
1738 }
1739 else
1740 {
1741 int t = get_tab_number(this->tab);
1742
1743 if (t == 0)
1744 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
1745 (self));
1746 else
1747 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
1748
1749 return PyString_FromString(repr);
1750 }
1751}
1752
1753static struct PyMethodDef TabPageMethods[] = {
1754 /* name, function, calling, documentation */
1755 { NULL, NULL, 0, NULL }
1756};
1757
1758/*
1759 * Window list object
1760 */
1761
1762static PyTypeObject TabListType;
1763static PySequenceMethods TabListAsSeq;
1764
1765typedef struct
1766{
1767 PyObject_HEAD
1768} TabListObject;
1769
1770 static PyInt
1771TabListLength(PyObject *self UNUSED)
1772{
1773 tabpage_T *tp = first_tabpage;
1774 PyInt n = 0;
1775
1776 while (tp != NULL)
1777 {
1778 ++n;
1779 tp = tp->tp_next;
1780 }
1781
1782 return n;
1783}
1784
1785 static PyObject *
1786TabListItem(PyObject *self UNUSED, PyInt n)
1787{
1788 tabpage_T *tp;
1789
1790 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
1791 if (n == 0)
1792 return TabPageNew(tp);
1793
1794 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
1795 return NULL;
1796}
1797
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001798/* Window object
1799 */
1800
1801typedef struct
1802{
1803 PyObject_HEAD
1804 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001805 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001806} WindowObject;
1807
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001808static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001809
1810 static int
1811CheckWindow(WindowObject *this)
1812{
1813 if (this->win == INVALID_WINDOW_VALUE)
1814 {
1815 PyErr_SetVim(_("attempt to refer to deleted window"));
1816 return -1;
1817 }
1818
1819 return 0;
1820}
1821
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001822 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001823WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02001824{
1825 /* We need to handle deletion of windows underneath us.
1826 * If we add a "w_python*_ref" field to the win_T structure,
1827 * then we can get at it in win_free() in vim. We then
1828 * need to create only ONE Python object per window - if
1829 * we try to create a second, just INCREF the existing one
1830 * and return it. The (single) Python object referring to
1831 * the window is stored in "w_python*_ref".
1832 * On a win_free() we set the Python object's win_T* field
1833 * to an invalid value. We trap all uses of a window
1834 * object, and reject them if the win_T* field is invalid.
1835 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001836 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02001837 * w_python_ref and w_python3_ref fields respectively.
1838 */
1839
1840 WindowObject *self;
1841
1842 if (WIN_PYTHON_REF(win))
1843 {
1844 self = WIN_PYTHON_REF(win);
1845 Py_INCREF(self);
1846 }
1847 else
1848 {
1849 self = PyObject_NEW(WindowObject, &WindowType);
1850 if (self == NULL)
1851 return NULL;
1852 self->win = win;
1853 WIN_PYTHON_REF(win) = self;
1854 }
1855
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001856 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
1857
Bram Moolenaar971db462013-05-12 18:44:48 +02001858 return (PyObject *)(self);
1859}
1860
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001861 static void
1862WindowDestructor(PyObject *self)
1863{
1864 WindowObject *this = (WindowObject *)(self);
1865
1866 if (this->win && this->win != INVALID_WINDOW_VALUE)
1867 WIN_PYTHON_REF(this->win) = NULL;
1868
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001869 Py_DECREF(((PyObject *)(this->tabObject)));
1870
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001871 DESTRUCTOR_FINISH(self);
1872}
1873
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001874 static win_T *
1875get_firstwin(TabPageObject *tabObject)
1876{
1877 if (tabObject)
1878 {
1879 if (CheckTabPage(tabObject))
1880 return NULL;
1881 /* For current tab window.c does not bother to set or update tp_firstwin
1882 */
1883 else if (tabObject->tab == curtab)
1884 return firstwin;
1885 else
1886 return tabObject->tab->tp_firstwin;
1887 }
1888 else
1889 return firstwin;
1890}
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001891 static int
1892WindowTraverse(PyObject *self, visitproc visit, void *arg)
1893{
1894 Py_VISIT(((PyObject *)(((WindowObject *)(self))->tabObject)));
1895 return 0;
1896}
1897
1898 static int
1899WindowClear(PyObject *self)
1900{
1901 Py_CLEAR((((WindowObject *)(self))->tabObject));
1902 return 0;
1903}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001904
Bram Moolenaar971db462013-05-12 18:44:48 +02001905 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001906WindowAttr(WindowObject *this, char *name)
1907{
1908 if (strcmp(name, "buffer") == 0)
1909 return (PyObject *)BufferNew(this->win->w_buffer);
1910 else if (strcmp(name, "cursor") == 0)
1911 {
1912 pos_T *pos = &this->win->w_cursor;
1913
1914 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1915 }
1916 else if (strcmp(name, "height") == 0)
Bram Moolenaar99add412013-05-12 19:09:51 +02001917 return PyLong_FromLong((long)(this->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001918#ifdef FEAT_WINDOWS
1919 else if (strcmp(name, "row") == 0)
1920 return PyLong_FromLong((long)(this->win->w_winrow));
1921#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001922#ifdef FEAT_VERTSPLIT
1923 else if (strcmp(name, "width") == 0)
Bram Moolenaar99add412013-05-12 19:09:51 +02001924 return PyLong_FromLong((long)(W_WIDTH(this->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001925 else if (strcmp(name, "col") == 0)
1926 return PyLong_FromLong((long)(W_WINCOL(this->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001927#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001928 else if (strcmp(name, "vars") == 0)
1929 return DictionaryNew(this->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001930 else if (strcmp(name, "options") == 0)
1931 return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow,
1932 (PyObject *) this);
Bram Moolenaar6d216452013-05-12 19:00:41 +02001933 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001934 {
1935 if (CheckTabPage(this->tabObject))
1936 return NULL;
1937 return PyLong_FromLong((long)
1938 get_win_number(this->win, get_firstwin(this->tabObject)));
1939 }
1940 else if (strcmp(name, "tabpage") == 0)
1941 {
1942 Py_INCREF(this->tabObject);
1943 return (PyObject *)(this->tabObject);
1944 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001945 else if (strcmp(name,"__members__") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001946 return Py_BuildValue("[sssssssss]", "buffer", "cursor", "height",
1947 "vars", "options", "number", "row", "col", "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001948 else
1949 return NULL;
1950}
1951
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001952 static int
1953WindowSetattr(PyObject *self, char *name, PyObject *val)
1954{
1955 WindowObject *this = (WindowObject *)(self);
1956
1957 if (CheckWindow(this))
1958 return -1;
1959
1960 if (strcmp(name, "buffer") == 0)
1961 {
1962 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
1963 return -1;
1964 }
1965 else if (strcmp(name, "cursor") == 0)
1966 {
1967 long lnum;
1968 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001969
1970 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
1971 return -1;
1972
1973 if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count)
1974 {
1975 PyErr_SetVim(_("cursor position outside buffer"));
1976 return -1;
1977 }
1978
1979 /* Check for keyboard interrupts */
1980 if (VimErrorCheck())
1981 return -1;
1982
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001983 this->win->w_cursor.lnum = lnum;
1984 this->win->w_cursor.col = col;
1985#ifdef FEAT_VIRTUALEDIT
1986 this->win->w_cursor.coladd = 0;
1987#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02001988 /* When column is out of range silently correct it. */
1989 check_cursor_col_win(this->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001990
Bram Moolenaar03a807a2011-07-07 15:08:58 +02001991 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001992 return 0;
1993 }
1994 else if (strcmp(name, "height") == 0)
1995 {
1996 int height;
1997 win_T *savewin;
1998
1999 if (!PyArg_Parse(val, "i", &height))
2000 return -1;
2001
2002#ifdef FEAT_GUI
2003 need_mouse_correct = TRUE;
2004#endif
2005 savewin = curwin;
2006 curwin = this->win;
2007 win_setheight(height);
2008 curwin = savewin;
2009
2010 /* Check for keyboard interrupts */
2011 if (VimErrorCheck())
2012 return -1;
2013
2014 return 0;
2015 }
2016#ifdef FEAT_VERTSPLIT
2017 else if (strcmp(name, "width") == 0)
2018 {
2019 int width;
2020 win_T *savewin;
2021
2022 if (!PyArg_Parse(val, "i", &width))
2023 return -1;
2024
2025#ifdef FEAT_GUI
2026 need_mouse_correct = TRUE;
2027#endif
2028 savewin = curwin;
2029 curwin = this->win;
2030 win_setwidth(width);
2031 curwin = savewin;
2032
2033 /* Check for keyboard interrupts */
2034 if (VimErrorCheck())
2035 return -1;
2036
2037 return 0;
2038 }
2039#endif
2040 else
2041 {
2042 PyErr_SetString(PyExc_AttributeError, name);
2043 return -1;
2044 }
2045}
2046
2047 static PyObject *
2048WindowRepr(PyObject *self)
2049{
2050 static char repr[100];
2051 WindowObject *this = (WindowObject *)(self);
2052
2053 if (this->win == INVALID_WINDOW_VALUE)
2054 {
2055 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2056 return PyString_FromString(repr);
2057 }
2058 else
2059 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002060 int w = get_win_number(this->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002061
Bram Moolenaar6d216452013-05-12 19:00:41 +02002062 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002063 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2064 (self));
2065 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002066 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002067
2068 return PyString_FromString(repr);
2069 }
2070}
2071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002072static struct PyMethodDef WindowMethods[] = {
2073 /* name, function, calling, documentation */
2074 { NULL, NULL, 0, NULL }
2075};
2076
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002077/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002078 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002079 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002080
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002081static PyTypeObject WinListType;
2082static PySequenceMethods WinListAsSeq;
2083
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002084typedef struct
2085{
2086 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002087 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002088} WinListObject;
2089
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002090 static PyObject *
2091WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002092{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002093 WinListObject *self;
2094
2095 self = PyObject_NEW(WinListObject, &WinListType);
2096 self->tabObject = tabObject;
2097 Py_INCREF(tabObject);
2098
2099 return (PyObject *)(self);
2100}
2101
2102 static void
2103WinListDestructor(PyObject *self)
2104{
2105 TabPageObject *tabObject = ((WinListObject *)(self))->tabObject;
2106
2107 if (tabObject)
2108 Py_DECREF((PyObject *)(tabObject));
2109
2110 DESTRUCTOR_FINISH(self);
2111}
2112
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002113 static PyInt
2114WinListLength(PyObject *self)
2115{
2116 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002117 PyInt n = 0;
2118
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002119 if (!(w = get_firstwin(((WinListObject *)(self))->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002120 return -1;
2121
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002122 while (w != NULL)
2123 {
2124 ++n;
2125 w = W_NEXT(w);
2126 }
2127
2128 return n;
2129}
2130
2131 static PyObject *
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002132WinListItem(PyObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002133{
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002134 WinListObject *this = ((WinListObject *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002135 win_T *w;
2136
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002137 if (!(w = get_firstwin(this->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002138 return NULL;
2139
2140 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002141 if (n == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002142 return WindowNew(w, this->tabObject? this->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002143
2144 PyErr_SetString(PyExc_IndexError, _("no such window"));
2145 return NULL;
2146}
2147
2148/* Convert a Python string into a Vim line.
2149 *
2150 * The result is in allocated memory. All internal nulls are replaced by
2151 * newline characters. It is an error for the string to contain newline
2152 * characters.
2153 *
2154 * On errors, the Python exception data is set, and NULL is returned.
2155 */
2156 static char *
2157StringToLine(PyObject *obj)
2158{
2159 const char *str;
2160 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002161 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002162 PyInt len;
2163 PyInt i;
2164 char *p;
2165
2166 if (obj == NULL || !PyString_Check(obj))
2167 {
2168 PyErr_BadArgument();
2169 return NULL;
2170 }
2171
Bram Moolenaar19e60942011-06-19 00:27:51 +02002172 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2173 str = PyString_AsString(bytes);
2174 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002175
2176 /*
2177 * Error checking: String must not contain newlines, as we
2178 * are replacing a single line, and we must replace it with
2179 * a single line.
2180 * A trailing newline is removed, so that append(f.readlines()) works.
2181 */
2182 p = memchr(str, '\n', len);
2183 if (p != NULL)
2184 {
2185 if (p == str + len - 1)
2186 --len;
2187 else
2188 {
2189 PyErr_SetVim(_("string cannot contain newlines"));
2190 return NULL;
2191 }
2192 }
2193
2194 /* Create a copy of the string, with internal nulls replaced by
2195 * newline characters, as is the vim convention.
2196 */
2197 save = (char *)alloc((unsigned)(len+1));
2198 if (save == NULL)
2199 {
2200 PyErr_NoMemory();
2201 return NULL;
2202 }
2203
2204 for (i = 0; i < len; ++i)
2205 {
2206 if (str[i] == '\0')
2207 save[i] = '\n';
2208 else
2209 save[i] = str[i];
2210 }
2211
2212 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002213 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002214
2215 return save;
2216}
2217
2218/* Get a line from the specified buffer. The line number is
2219 * in Vim format (1-based). The line is returned as a Python
2220 * string object.
2221 */
2222 static PyObject *
2223GetBufferLine(buf_T *buf, PyInt n)
2224{
2225 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2226}
2227
2228
2229/* Get a list of lines from the specified buffer. The line numbers
2230 * are in Vim format (1-based). The range is from lo up to, but not
2231 * including, hi. The list is returned as a Python list of string objects.
2232 */
2233 static PyObject *
2234GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2235{
2236 PyInt i;
2237 PyInt n = hi - lo;
2238 PyObject *list = PyList_New(n);
2239
2240 if (list == NULL)
2241 return NULL;
2242
2243 for (i = 0; i < n; ++i)
2244 {
2245 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2246
2247 /* Error check - was the Python string creation OK? */
2248 if (str == NULL)
2249 {
2250 Py_DECREF(list);
2251 return NULL;
2252 }
2253
2254 /* Set the list item */
2255 if (PyList_SetItem(list, i, str))
2256 {
2257 Py_DECREF(str);
2258 Py_DECREF(list);
2259 return NULL;
2260 }
2261 }
2262
2263 /* The ownership of the Python list is passed to the caller (ie,
2264 * the caller should Py_DECREF() the object when it is finished
2265 * with it).
2266 */
2267
2268 return list;
2269}
2270
2271/*
2272 * Check if deleting lines made the cursor position invalid.
2273 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2274 * deleted).
2275 */
2276 static void
2277py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2278{
2279 if (curwin->w_cursor.lnum >= lo)
2280 {
2281 /* Adjust the cursor position if it's in/after the changed
2282 * lines. */
2283 if (curwin->w_cursor.lnum >= hi)
2284 {
2285 curwin->w_cursor.lnum += extra;
2286 check_cursor_col();
2287 }
2288 else if (extra < 0)
2289 {
2290 curwin->w_cursor.lnum = lo;
2291 check_cursor();
2292 }
2293 else
2294 check_cursor_col();
2295 changed_cline_bef_curs();
2296 }
2297 invalidate_botline();
2298}
2299
Bram Moolenaar19e60942011-06-19 00:27:51 +02002300/*
2301 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002302 * in Vim format (1-based). The replacement line is given as
2303 * a Python string object. The object is checked for validity
2304 * and correct format. Errors are returned as a value of FAIL.
2305 * The return value is OK on success.
2306 * If OK is returned and len_change is not NULL, *len_change
2307 * is set to the change in the buffer length.
2308 */
2309 static int
2310SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2311{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002312 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002313 * There are three cases:
2314 * 1. NULL, or None - this is a deletion.
2315 * 2. A string - this is a replacement.
2316 * 3. Anything else - this is an error.
2317 */
2318 if (line == Py_None || line == NULL)
2319 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002320 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002321
2322 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002323 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002324
2325 if (u_savedel((linenr_T)n, 1L) == FAIL)
2326 PyErr_SetVim(_("cannot save undo information"));
2327 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2328 PyErr_SetVim(_("cannot delete line"));
2329 else
2330 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002331 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002332 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2333 deleted_lines_mark((linenr_T)n, 1L);
2334 }
2335
Bram Moolenaar105bc352013-05-17 16:03:57 +02002336 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002337
2338 if (PyErr_Occurred() || VimErrorCheck())
2339 return FAIL;
2340
2341 if (len_change)
2342 *len_change = -1;
2343
2344 return OK;
2345 }
2346 else if (PyString_Check(line))
2347 {
2348 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002349 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002350
2351 if (save == NULL)
2352 return FAIL;
2353
2354 /* We do not need to free "save" if ml_replace() consumes it. */
2355 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002356 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002357
2358 if (u_savesub((linenr_T)n) == FAIL)
2359 {
2360 PyErr_SetVim(_("cannot save undo information"));
2361 vim_free(save);
2362 }
2363 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2364 {
2365 PyErr_SetVim(_("cannot replace line"));
2366 vim_free(save);
2367 }
2368 else
2369 changed_bytes((linenr_T)n, 0);
2370
Bram Moolenaar105bc352013-05-17 16:03:57 +02002371 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002372
2373 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002374 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002375 check_cursor_col();
2376
2377 if (PyErr_Occurred() || VimErrorCheck())
2378 return FAIL;
2379
2380 if (len_change)
2381 *len_change = 0;
2382
2383 return OK;
2384 }
2385 else
2386 {
2387 PyErr_BadArgument();
2388 return FAIL;
2389 }
2390}
2391
Bram Moolenaar19e60942011-06-19 00:27:51 +02002392/* Replace a range of lines in the specified buffer. The line numbers are in
2393 * Vim format (1-based). The range is from lo up to, but not including, hi.
2394 * The replacement lines are given as a Python list of string objects. The
2395 * list is checked for validity and correct format. Errors are returned as a
2396 * value of FAIL. The return value is OK on success.
2397 * If OK is returned and len_change is not NULL, *len_change
2398 * is set to the change in the buffer length.
2399 */
2400 static int
2401SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2402{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002403 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002404 * There are three cases:
2405 * 1. NULL, or None - this is a deletion.
2406 * 2. A list - this is a replacement.
2407 * 3. Anything else - this is an error.
2408 */
2409 if (list == Py_None || list == NULL)
2410 {
2411 PyInt i;
2412 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002413 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002414
2415 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002416 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002417
2418 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2419 PyErr_SetVim(_("cannot save undo information"));
2420 else
2421 {
2422 for (i = 0; i < n; ++i)
2423 {
2424 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2425 {
2426 PyErr_SetVim(_("cannot delete line"));
2427 break;
2428 }
2429 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002430 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002431 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2432 deleted_lines_mark((linenr_T)lo, (long)i);
2433 }
2434
Bram Moolenaar105bc352013-05-17 16:03:57 +02002435 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002436
2437 if (PyErr_Occurred() || VimErrorCheck())
2438 return FAIL;
2439
2440 if (len_change)
2441 *len_change = -n;
2442
2443 return OK;
2444 }
2445 else if (PyList_Check(list))
2446 {
2447 PyInt i;
2448 PyInt new_len = PyList_Size(list);
2449 PyInt old_len = hi - lo;
2450 PyInt extra = 0; /* lines added to text, can be negative */
2451 char **array;
2452 buf_T *savebuf;
2453
2454 if (new_len == 0) /* avoid allocating zero bytes */
2455 array = NULL;
2456 else
2457 {
2458 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
2459 if (array == NULL)
2460 {
2461 PyErr_NoMemory();
2462 return FAIL;
2463 }
2464 }
2465
2466 for (i = 0; i < new_len; ++i)
2467 {
2468 PyObject *line = PyList_GetItem(list, i);
2469
2470 array[i] = StringToLine(line);
2471 if (array[i] == NULL)
2472 {
2473 while (i)
2474 vim_free(array[--i]);
2475 vim_free(array);
2476 return FAIL;
2477 }
2478 }
2479
Bram Moolenaar19e60942011-06-19 00:27:51 +02002480 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002481
2482 // START of region without "return". Must call restore_buffer()!
2483 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002484
2485 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2486 PyErr_SetVim(_("cannot save undo information"));
2487
2488 /* If the size of the range is reducing (ie, new_len < old_len) we
2489 * need to delete some old_len. We do this at the start, by
2490 * repeatedly deleting line "lo".
2491 */
2492 if (!PyErr_Occurred())
2493 {
2494 for (i = 0; i < old_len - new_len; ++i)
2495 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2496 {
2497 PyErr_SetVim(_("cannot delete line"));
2498 break;
2499 }
2500 extra -= i;
2501 }
2502
2503 /* For as long as possible, replace the existing old_len with the
2504 * new old_len. This is a more efficient operation, as it requires
2505 * less memory allocation and freeing.
2506 */
2507 if (!PyErr_Occurred())
2508 {
2509 for (i = 0; i < old_len && i < new_len; ++i)
2510 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2511 == FAIL)
2512 {
2513 PyErr_SetVim(_("cannot replace line"));
2514 break;
2515 }
2516 }
2517 else
2518 i = 0;
2519
2520 /* Now we may need to insert the remaining new old_len. If we do, we
2521 * must free the strings as we finish with them (we can't pass the
2522 * responsibility to vim in this case).
2523 */
2524 if (!PyErr_Occurred())
2525 {
2526 while (i < new_len)
2527 {
2528 if (ml_append((linenr_T)(lo + i - 1),
2529 (char_u *)array[i], 0, FALSE) == FAIL)
2530 {
2531 PyErr_SetVim(_("cannot insert line"));
2532 break;
2533 }
2534 vim_free(array[i]);
2535 ++i;
2536 ++extra;
2537 }
2538 }
2539
2540 /* Free any left-over old_len, as a result of an error */
2541 while (i < new_len)
2542 {
2543 vim_free(array[i]);
2544 ++i;
2545 }
2546
2547 /* Free the array of old_len. All of its contents have now
2548 * been dealt with (either freed, or the responsibility passed
2549 * to vim.
2550 */
2551 vim_free(array);
2552
2553 /* Adjust marks. Invalidate any which lie in the
2554 * changed range, and move any in the remainder of the buffer.
2555 */
2556 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2557 (long)MAXLNUM, (long)extra);
2558 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2559
Bram Moolenaar105bc352013-05-17 16:03:57 +02002560 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002561 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2562
Bram Moolenaar105bc352013-05-17 16:03:57 +02002563 // END of region without "return".
2564 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002565
2566 if (PyErr_Occurred() || VimErrorCheck())
2567 return FAIL;
2568
2569 if (len_change)
2570 *len_change = new_len - old_len;
2571
2572 return OK;
2573 }
2574 else
2575 {
2576 PyErr_BadArgument();
2577 return FAIL;
2578 }
2579}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002580
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002581/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002582 * The line number is in Vim format (1-based). The lines to be inserted are
2583 * given as a Python list of string objects or as a single string. The lines
2584 * to be added are checked for validity and correct format. Errors are
2585 * returned as a value of FAIL. The return value is OK on success.
2586 * If OK is returned and len_change is not NULL, *len_change
2587 * is set to the change in the buffer length.
2588 */
2589 static int
2590InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2591{
2592 /* First of all, we check the type of the supplied Python object.
2593 * It must be a string or a list, or the call is in error.
2594 */
2595 if (PyString_Check(lines))
2596 {
2597 char *str = StringToLine(lines);
2598 buf_T *savebuf;
2599
2600 if (str == NULL)
2601 return FAIL;
2602
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002603 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002604 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002605
2606 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2607 PyErr_SetVim(_("cannot save undo information"));
2608 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2609 PyErr_SetVim(_("cannot insert line"));
2610 else
2611 appended_lines_mark((linenr_T)n, 1L);
2612
2613 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002614 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002615 update_screen(VALID);
2616
2617 if (PyErr_Occurred() || VimErrorCheck())
2618 return FAIL;
2619
2620 if (len_change)
2621 *len_change = 1;
2622
2623 return OK;
2624 }
2625 else if (PyList_Check(lines))
2626 {
2627 PyInt i;
2628 PyInt size = PyList_Size(lines);
2629 char **array;
2630 buf_T *savebuf;
2631
2632 array = (char **)alloc((unsigned)(size * sizeof(char *)));
2633 if (array == NULL)
2634 {
2635 PyErr_NoMemory();
2636 return FAIL;
2637 }
2638
2639 for (i = 0; i < size; ++i)
2640 {
2641 PyObject *line = PyList_GetItem(lines, i);
2642 array[i] = StringToLine(line);
2643
2644 if (array[i] == NULL)
2645 {
2646 while (i)
2647 vim_free(array[--i]);
2648 vim_free(array);
2649 return FAIL;
2650 }
2651 }
2652
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002653 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002654 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002655
2656 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2657 PyErr_SetVim(_("cannot save undo information"));
2658 else
2659 {
2660 for (i = 0; i < size; ++i)
2661 {
2662 if (ml_append((linenr_T)(n + i),
2663 (char_u *)array[i], 0, FALSE) == FAIL)
2664 {
2665 PyErr_SetVim(_("cannot insert line"));
2666
2667 /* Free the rest of the lines */
2668 while (i < size)
2669 vim_free(array[i++]);
2670
2671 break;
2672 }
2673 vim_free(array[i]);
2674 }
2675 if (i > 0)
2676 appended_lines_mark((linenr_T)n, (long)i);
2677 }
2678
2679 /* Free the array of lines. All of its contents have now
2680 * been freed.
2681 */
2682 vim_free(array);
2683
Bram Moolenaar105bc352013-05-17 16:03:57 +02002684 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002685 update_screen(VALID);
2686
2687 if (PyErr_Occurred() || VimErrorCheck())
2688 return FAIL;
2689
2690 if (len_change)
2691 *len_change = size;
2692
2693 return OK;
2694 }
2695 else
2696 {
2697 PyErr_BadArgument();
2698 return FAIL;
2699 }
2700}
2701
2702/*
2703 * Common routines for buffers and line ranges
2704 * -------------------------------------------
2705 */
2706
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002707typedef struct
2708{
2709 PyObject_HEAD
2710 buf_T *buf;
2711} BufferObject;
2712
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002713 static int
2714CheckBuffer(BufferObject *this)
2715{
2716 if (this->buf == INVALID_BUFFER_VALUE)
2717 {
2718 PyErr_SetVim(_("attempt to refer to deleted buffer"));
2719 return -1;
2720 }
2721
2722 return 0;
2723}
2724
2725 static PyObject *
2726RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
2727{
2728 if (CheckBuffer(self))
2729 return NULL;
2730
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002731 if (end == -1)
2732 end = self->buf->b_ml.ml_line_count;
2733
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002734 if (n < 0)
2735 n += end - start + 1;
2736
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002737 if (n < 0 || n > end - start)
2738 {
2739 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2740 return NULL;
2741 }
2742
2743 return GetBufferLine(self->buf, n+start);
2744}
2745
2746 static PyObject *
2747RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
2748{
2749 PyInt size;
2750
2751 if (CheckBuffer(self))
2752 return NULL;
2753
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002754 if (end == -1)
2755 end = self->buf->b_ml.ml_line_count;
2756
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002757 size = end - start + 1;
2758
2759 if (lo < 0)
2760 lo = 0;
2761 else if (lo > size)
2762 lo = size;
2763 if (hi < 0)
2764 hi = 0;
2765 if (hi < lo)
2766 hi = lo;
2767 else if (hi > size)
2768 hi = size;
2769
2770 return GetBufferLineList(self->buf, lo+start, hi+start);
2771}
2772
2773 static PyInt
2774RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2775{
2776 PyInt len_change;
2777
2778 if (CheckBuffer(self))
2779 return -1;
2780
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002781 if (end == -1)
2782 end = self->buf->b_ml.ml_line_count;
2783
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002784 if (n < 0)
2785 n += end - start + 1;
2786
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002787 if (n < 0 || n > end - start)
2788 {
2789 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2790 return -1;
2791 }
2792
2793 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
2794 return -1;
2795
2796 if (new_end)
2797 *new_end = end + len_change;
2798
2799 return 0;
2800}
2801
Bram Moolenaar19e60942011-06-19 00:27:51 +02002802 static PyInt
2803RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2804{
2805 PyInt size;
2806 PyInt len_change;
2807
2808 /* Self must be a valid buffer */
2809 if (CheckBuffer(self))
2810 return -1;
2811
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002812 if (end == -1)
2813 end = self->buf->b_ml.ml_line_count;
2814
Bram Moolenaar19e60942011-06-19 00:27:51 +02002815 /* Sort out the slice range */
2816 size = end - start + 1;
2817
2818 if (lo < 0)
2819 lo = 0;
2820 else if (lo > size)
2821 lo = size;
2822 if (hi < 0)
2823 hi = 0;
2824 if (hi < lo)
2825 hi = lo;
2826 else if (hi > size)
2827 hi = size;
2828
2829 if (SetBufferLineList(self->buf, lo + start, hi + start,
2830 val, &len_change) == FAIL)
2831 return -1;
2832
2833 if (new_end)
2834 *new_end = end + len_change;
2835
2836 return 0;
2837}
2838
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002839
2840 static PyObject *
2841RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
2842{
2843 PyObject *lines;
2844 PyInt len_change;
2845 PyInt max;
2846 PyInt n;
2847
2848 if (CheckBuffer(self))
2849 return NULL;
2850
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002851 if (end == -1)
2852 end = self->buf->b_ml.ml_line_count;
2853
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002854 max = n = end - start + 1;
2855
2856 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
2857 return NULL;
2858
2859 if (n < 0 || n > max)
2860 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002861 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002862 return NULL;
2863 }
2864
2865 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
2866 return NULL;
2867
2868 if (new_end)
2869 *new_end = end + len_change;
2870
2871 Py_INCREF(Py_None);
2872 return Py_None;
2873}
2874
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002875/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002876 */
2877
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002878static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002879static PySequenceMethods RangeAsSeq;
2880static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002881
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002882typedef struct
2883{
2884 PyObject_HEAD
2885 BufferObject *buf;
2886 PyInt start;
2887 PyInt end;
2888} RangeObject;
2889
2890 static PyObject *
2891RangeNew(buf_T *buf, PyInt start, PyInt end)
2892{
2893 BufferObject *bufr;
2894 RangeObject *self;
2895 self = PyObject_NEW(RangeObject, &RangeType);
2896 if (self == NULL)
2897 return NULL;
2898
2899 bufr = (BufferObject *)BufferNew(buf);
2900 if (bufr == NULL)
2901 {
2902 Py_DECREF(self);
2903 return NULL;
2904 }
2905 Py_INCREF(bufr);
2906
2907 self->buf = bufr;
2908 self->start = start;
2909 self->end = end;
2910
2911 return (PyObject *)(self);
2912}
2913
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002914 static void
2915RangeDestructor(PyObject *self)
2916{
2917 Py_DECREF(((RangeObject *)(self))->buf);
2918 DESTRUCTOR_FINISH(self);
2919}
2920
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002921 static PyInt
2922RangeLength(PyObject *self)
2923{
2924 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
2925 if (CheckBuffer(((RangeObject *)(self))->buf))
2926 return -1; /* ??? */
2927
2928 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1);
2929}
2930
2931 static PyObject *
2932RangeItem(PyObject *self, PyInt n)
2933{
2934 return RBItem(((RangeObject *)(self))->buf, n,
2935 ((RangeObject *)(self))->start,
2936 ((RangeObject *)(self))->end);
2937}
2938
2939 static PyObject *
2940RangeSlice(PyObject *self, PyInt lo, PyInt hi)
2941{
2942 return RBSlice(((RangeObject *)(self))->buf, lo, hi,
2943 ((RangeObject *)(self))->start,
2944 ((RangeObject *)(self))->end);
2945}
2946
2947 static PyObject *
2948RangeAppend(PyObject *self, PyObject *args)
2949{
2950 return RBAppend(((RangeObject *)(self))->buf, args,
2951 ((RangeObject *)(self))->start,
2952 ((RangeObject *)(self))->end,
2953 &((RangeObject *)(self))->end);
2954}
2955
2956 static PyObject *
2957RangeRepr(PyObject *self)
2958{
2959 static char repr[100];
2960 RangeObject *this = (RangeObject *)(self);
2961
2962 if (this->buf->buf == INVALID_BUFFER_VALUE)
2963 {
2964 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
2965 (self));
2966 return PyString_FromString(repr);
2967 }
2968 else
2969 {
2970 char *name = (char *)this->buf->buf->b_fname;
2971 int len;
2972
2973 if (name == NULL)
2974 name = "";
2975 len = (int)strlen(name);
2976
2977 if (len > 45)
2978 name = name + (45 - len);
2979
2980 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
2981 len > 45 ? "..." : "", name,
2982 this->start, this->end);
2983
2984 return PyString_FromString(repr);
2985 }
2986}
2987
2988static struct PyMethodDef RangeMethods[] = {
2989 /* name, function, calling, documentation */
2990 {"append", RangeAppend, 1, "Append data to the Vim range" },
2991 { NULL, NULL, 0, NULL }
2992};
2993
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002994static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002995static PySequenceMethods BufferAsSeq;
2996static PyMappingMethods BufferAsMapping;
2997
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002998 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02002999BufferNew(buf_T *buf)
3000{
3001 /* We need to handle deletion of buffers underneath us.
3002 * If we add a "b_python*_ref" field to the buf_T structure,
3003 * then we can get at it in buf_freeall() in vim. We then
3004 * need to create only ONE Python object per buffer - if
3005 * we try to create a second, just INCREF the existing one
3006 * and return it. The (single) Python object referring to
3007 * the buffer is stored in "b_python*_ref".
3008 * Question: what to do on a buf_freeall(). We'll probably
3009 * have to either delete the Python object (DECREF it to
3010 * zero - a bad idea, as it leaves dangling refs!) or
3011 * set the buf_T * value to an invalid value (-1?), which
3012 * means we need checks in all access functions... Bah.
3013 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003014 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003015 * b_python_ref and b_python3_ref fields respectively.
3016 */
3017
3018 BufferObject *self;
3019
3020 if (BUF_PYTHON_REF(buf) != NULL)
3021 {
3022 self = BUF_PYTHON_REF(buf);
3023 Py_INCREF(self);
3024 }
3025 else
3026 {
3027 self = PyObject_NEW(BufferObject, &BufferType);
3028 if (self == NULL)
3029 return NULL;
3030 self->buf = buf;
3031 BUF_PYTHON_REF(buf) = self;
3032 }
3033
3034 return (PyObject *)(self);
3035}
3036
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003037 static void
3038BufferDestructor(PyObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003039{
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003040 BufferObject *this = (BufferObject *)(self);
3041
3042 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
3043 BUF_PYTHON_REF(this->buf) = NULL;
3044
3045 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003046}
3047
Bram Moolenaar971db462013-05-12 18:44:48 +02003048 static PyInt
3049BufferLength(PyObject *self)
3050{
3051 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
3052 if (CheckBuffer((BufferObject *)(self)))
3053 return -1; /* ??? */
3054
3055 return (PyInt)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
3056}
3057
3058 static PyObject *
3059BufferItem(PyObject *self, PyInt n)
3060{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003061 return RBItem((BufferObject *)(self), n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003062}
3063
3064 static PyObject *
3065BufferSlice(PyObject *self, PyInt lo, PyInt hi)
3066{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003067 return RBSlice((BufferObject *)(self), lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003068}
3069
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003070 static PyObject *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003071BufferAttr(BufferObject *this, char *name)
3072{
3073 if (strcmp(name, "name") == 0)
3074 return Py_BuildValue("s", this->buf->b_ffname);
3075 else if (strcmp(name, "number") == 0)
3076 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
3077 else if (strcmp(name, "vars") == 0)
3078 return DictionaryNew(this->buf->b_vars);
3079 else if (strcmp(name, "options") == 0)
3080 return OptionsNew(SREQ_BUF, this->buf, (checkfun) CheckBuffer,
3081 (PyObject *) this);
3082 else if (strcmp(name,"__members__") == 0)
3083 return Py_BuildValue("[ssss]", "name", "number", "vars", "options");
3084 else
3085 return NULL;
3086}
3087
3088 static PyObject *
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003089BufferAppend(PyObject *self, PyObject *args)
3090{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003091 return RBAppend((BufferObject *)(self), args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003092}
3093
3094 static PyObject *
3095BufferMark(PyObject *self, PyObject *args)
3096{
3097 pos_T *posp;
3098 char *pmark;
3099 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003100 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003101
3102 if (CheckBuffer((BufferObject *)(self)))
3103 return NULL;
3104
3105 if (!PyArg_ParseTuple(args, "s", &pmark))
3106 return NULL;
3107 mark = *pmark;
3108
Bram Moolenaar105bc352013-05-17 16:03:57 +02003109 switch_buffer(&savebuf, ((BufferObject *)(self))->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003110 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003111 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003112
3113 if (posp == NULL)
3114 {
3115 PyErr_SetVim(_("invalid mark name"));
3116 return NULL;
3117 }
3118
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003119 /* Check for keyboard interrupt */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003120 if (VimErrorCheck())
3121 return NULL;
3122
3123 if (posp->lnum <= 0)
3124 {
3125 /* Or raise an error? */
3126 Py_INCREF(Py_None);
3127 return Py_None;
3128 }
3129
3130 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3131}
3132
3133 static PyObject *
3134BufferRange(PyObject *self, PyObject *args)
3135{
3136 PyInt start;
3137 PyInt end;
3138
3139 if (CheckBuffer((BufferObject *)(self)))
3140 return NULL;
3141
3142 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3143 return NULL;
3144
3145 return RangeNew(((BufferObject *)(self))->buf, start, end);
3146}
3147
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003148 static PyObject *
3149BufferRepr(PyObject *self)
3150{
3151 static char repr[100];
3152 BufferObject *this = (BufferObject *)(self);
3153
3154 if (this->buf == INVALID_BUFFER_VALUE)
3155 {
3156 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3157 return PyString_FromString(repr);
3158 }
3159 else
3160 {
3161 char *name = (char *)this->buf->b_fname;
3162 PyInt len;
3163
3164 if (name == NULL)
3165 name = "";
3166 len = strlen(name);
3167
3168 if (len > 35)
3169 name = name + (35 - len);
3170
3171 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3172
3173 return PyString_FromString(repr);
3174 }
3175}
3176
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003177static struct PyMethodDef BufferMethods[] = {
3178 /* name, function, calling, documentation */
3179 {"append", BufferAppend, 1, "Append data to Vim buffer" },
3180 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" },
3181 {"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 +01003182#if PY_VERSION_HEX >= 0x03000000
3183 {"__dir__", BufferDir, 4, "List its attributes" },
3184#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003185 { NULL, NULL, 0, NULL }
3186};
3187
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003188/*
3189 * Buffer list object - Implementation
3190 */
3191
3192static PyTypeObject BufMapType;
3193
3194typedef struct
3195{
3196 PyObject_HEAD
3197} BufMapObject;
3198
3199 static PyInt
3200BufMapLength(PyObject *self UNUSED)
3201{
3202 buf_T *b = firstbuf;
3203 PyInt n = 0;
3204
3205 while (b)
3206 {
3207 ++n;
3208 b = b->b_next;
3209 }
3210
3211 return n;
3212}
3213
3214 static PyObject *
3215BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3216{
3217 buf_T *b;
3218 int bnr;
3219
3220#if PY_MAJOR_VERSION < 3
3221 if (PyInt_Check(keyObject))
3222 bnr = PyInt_AsLong(keyObject);
3223 else
3224#endif
3225 if (PyLong_Check(keyObject))
3226 bnr = PyLong_AsLong(keyObject);
3227 else
3228 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003229 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003230 return NULL;
3231 }
3232
3233 b = buflist_findnr(bnr);
3234
3235 if (b)
3236 return BufferNew(b);
3237 else
3238 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003239 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003240 return NULL;
3241 }
3242}
3243
3244 static void
3245BufMapIterDestruct(PyObject *buffer)
3246{
3247 /* Iteration was stopped before all buffers were processed */
3248 if (buffer)
3249 {
3250 Py_DECREF(buffer);
3251 }
3252}
3253
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003254 static int
3255BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3256{
3257 Py_VISIT(buffer);
3258 return 0;
3259}
3260
3261 static int
3262BufMapIterClear(PyObject **buffer)
3263{
3264 Py_CLEAR(*buffer);
3265 return 0;
3266}
3267
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003268 static PyObject *
3269BufMapIterNext(PyObject **buffer)
3270{
3271 PyObject *next;
3272 PyObject *r;
3273
3274 if (!*buffer)
3275 return NULL;
3276
3277 r = *buffer;
3278
3279 if (CheckBuffer((BufferObject *)(r)))
3280 {
3281 *buffer = NULL;
3282 return NULL;
3283 }
3284
3285 if (!((BufferObject *)(r))->buf->b_next)
3286 next = NULL;
3287 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3288 return NULL;
3289 *buffer = next;
3290 /* Do not increment reference: we no longer hold it (decref), but whoever on
3291 * other side will hold (incref). Decref+incref = nothing.
3292 */
3293 return r;
3294}
3295
3296 static PyObject *
3297BufMapIter(PyObject *self UNUSED)
3298{
3299 PyObject *buffer;
3300
3301 buffer = BufferNew(firstbuf);
3302 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003303 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3304 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003305}
3306
3307static PyMappingMethods BufMapAsMapping = {
3308 (lenfunc) BufMapLength,
3309 (binaryfunc) BufMapItem,
3310 (objobjargproc) 0,
3311};
3312
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003313/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003314 */
3315
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003316 static PyObject *
3317CurrentGetattr(PyObject *self UNUSED, char *name)
3318{
3319 if (strcmp(name, "buffer") == 0)
3320 return (PyObject *)BufferNew(curbuf);
3321 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003322 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003323 else if (strcmp(name, "tabpage") == 0)
3324 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003325 else if (strcmp(name, "line") == 0)
3326 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3327 else if (strcmp(name, "range") == 0)
3328 return RangeNew(curbuf, RangeStart, RangeEnd);
3329 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003330 return Py_BuildValue("[sssss]", "buffer", "window", "line", "range",
3331 "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003332 else
3333 {
3334 PyErr_SetString(PyExc_AttributeError, name);
3335 return NULL;
3336 }
3337}
3338
3339 static int
3340CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3341{
3342 if (strcmp(name, "line") == 0)
3343 {
3344 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3345 return -1;
3346
3347 return 0;
3348 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003349 else if (strcmp(name, "buffer") == 0)
3350 {
3351 int count;
3352
3353 if (value->ob_type != &BufferType)
3354 {
3355 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3356 return -1;
3357 }
3358
3359 if (CheckBuffer((BufferObject *)(value)))
3360 return -1;
3361 count = ((BufferObject *)(value))->buf->b_fnum;
3362
3363 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3364 {
3365 PyErr_SetVim(_("failed to switch to given buffer"));
3366 return -1;
3367 }
3368
3369 return 0;
3370 }
3371 else if (strcmp(name, "window") == 0)
3372 {
3373 int count;
3374
3375 if (value->ob_type != &WindowType)
3376 {
3377 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3378 return -1;
3379 }
3380
3381 if (CheckWindow((WindowObject *)(value)))
3382 return -1;
3383 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3384
3385 if (!count)
3386 {
3387 PyErr_SetString(PyExc_ValueError,
3388 _("failed to find window in the current tab page"));
3389 return -1;
3390 }
3391
3392 win_goto(((WindowObject *)(value))->win);
3393 if (((WindowObject *)(value))->win != curwin)
3394 {
3395 PyErr_SetString(PyExc_RuntimeError,
3396 _("did not switch to the specified window"));
3397 return -1;
3398 }
3399
3400 return 0;
3401 }
3402 else if (strcmp(name, "tabpage") == 0)
3403 {
3404 if (value->ob_type != &TabPageType)
3405 {
3406 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3407 return -1;
3408 }
3409
3410 if (CheckTabPage((TabPageObject *)(value)))
3411 return -1;
3412
3413 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3414 if (((TabPageObject *)(value))->tab != curtab)
3415 {
3416 PyErr_SetString(PyExc_RuntimeError,
3417 _("did not switch to the specified tab page"));
3418 return -1;
3419 }
3420
3421 return 0;
3422 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003423 else
3424 {
3425 PyErr_SetString(PyExc_AttributeError, name);
3426 return -1;
3427 }
3428}
3429
Bram Moolenaardb913952012-06-29 12:54:53 +02003430 static void
3431set_ref_in_py(const int copyID)
3432{
3433 pylinkedlist_T *cur;
3434 dict_T *dd;
3435 list_T *ll;
3436
3437 if (lastdict != NULL)
3438 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3439 {
3440 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3441 if (dd->dv_copyID != copyID)
3442 {
3443 dd->dv_copyID = copyID;
3444 set_ref_in_ht(&dd->dv_hashtab, copyID);
3445 }
3446 }
3447
3448 if (lastlist != NULL)
3449 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3450 {
3451 ll = ((ListObject *) (cur->pll_obj))->list;
3452 if (ll->lv_copyID != copyID)
3453 {
3454 ll->lv_copyID = copyID;
3455 set_ref_in_list(ll, copyID);
3456 }
3457 }
3458}
3459
3460 static int
3461set_string_copy(char_u *str, typval_T *tv)
3462{
3463 tv->vval.v_string = vim_strsave(str);
3464 if (tv->vval.v_string == NULL)
3465 {
3466 PyErr_NoMemory();
3467 return -1;
3468 }
3469 return 0;
3470}
3471
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003472 static int
3473pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3474{
3475 dict_T *d;
3476 char_u *key;
3477 dictitem_T *di;
3478 PyObject *keyObject;
3479 PyObject *valObject;
3480 Py_ssize_t iter = 0;
3481
3482 d = dict_alloc();
3483 if (d == NULL)
3484 {
3485 PyErr_NoMemory();
3486 return -1;
3487 }
3488
3489 tv->v_type = VAR_DICT;
3490 tv->vval.v_dict = d;
3491
3492 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
3493 {
3494 DICTKEY_DECL
3495
3496 if (keyObject == NULL)
3497 return -1;
3498 if (valObject == NULL)
3499 return -1;
3500
3501 DICTKEY_GET_NOTEMPTY(-1)
3502
3503 di = dictitem_alloc(key);
3504
3505 DICTKEY_UNREF
3506
3507 if (di == NULL)
3508 {
3509 PyErr_NoMemory();
3510 return -1;
3511 }
3512 di->di_tv.v_lock = 0;
3513
3514 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3515 {
3516 vim_free(di);
3517 return -1;
3518 }
3519 if (dict_add(d, di) == FAIL)
3520 {
3521 vim_free(di);
3522 PyErr_SetVim(_("failed to add key to dictionary"));
3523 return -1;
3524 }
3525 }
3526 return 0;
3527}
3528
3529 static int
3530pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3531{
3532 dict_T *d;
3533 char_u *key;
3534 dictitem_T *di;
3535 PyObject *list;
3536 PyObject *litem;
3537 PyObject *keyObject;
3538 PyObject *valObject;
3539 Py_ssize_t lsize;
3540
3541 d = dict_alloc();
3542 if (d == NULL)
3543 {
3544 PyErr_NoMemory();
3545 return -1;
3546 }
3547
3548 tv->v_type = VAR_DICT;
3549 tv->vval.v_dict = d;
3550
3551 list = PyMapping_Items(obj);
3552 if (list == NULL)
3553 return -1;
3554 lsize = PyList_Size(list);
3555 while (lsize--)
3556 {
3557 DICTKEY_DECL
3558
3559 litem = PyList_GetItem(list, lsize);
3560 if (litem == NULL)
3561 {
3562 Py_DECREF(list);
3563 return -1;
3564 }
3565
3566 keyObject = PyTuple_GetItem(litem, 0);
3567 if (keyObject == NULL)
3568 {
3569 Py_DECREF(list);
3570 Py_DECREF(litem);
3571 return -1;
3572 }
3573
3574 DICTKEY_GET_NOTEMPTY(-1)
3575
3576 valObject = PyTuple_GetItem(litem, 1);
3577 if (valObject == NULL)
3578 {
3579 Py_DECREF(list);
3580 Py_DECREF(litem);
3581 return -1;
3582 }
3583
3584 di = dictitem_alloc(key);
3585
3586 DICTKEY_UNREF
3587
3588 if (di == NULL)
3589 {
3590 Py_DECREF(list);
3591 Py_DECREF(litem);
3592 PyErr_NoMemory();
3593 return -1;
3594 }
3595 di->di_tv.v_lock = 0;
3596
3597 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3598 {
3599 vim_free(di);
3600 Py_DECREF(list);
3601 Py_DECREF(litem);
3602 return -1;
3603 }
3604 if (dict_add(d, di) == FAIL)
3605 {
3606 vim_free(di);
3607 Py_DECREF(list);
3608 Py_DECREF(litem);
3609 PyErr_SetVim(_("failed to add key to dictionary"));
3610 return -1;
3611 }
3612 Py_DECREF(litem);
3613 }
3614 Py_DECREF(list);
3615 return 0;
3616}
3617
3618 static int
3619pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3620{
3621 list_T *l;
3622
3623 l = list_alloc();
3624 if (l == NULL)
3625 {
3626 PyErr_NoMemory();
3627 return -1;
3628 }
3629
3630 tv->v_type = VAR_LIST;
3631 tv->vval.v_list = l;
3632
3633 if (list_py_concat(l, obj, lookupDict) == -1)
3634 return -1;
3635
3636 return 0;
3637}
3638
3639 static int
3640pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3641{
3642 PyObject *iterator = PyObject_GetIter(obj);
3643 PyObject *item;
3644 list_T *l;
3645 listitem_T *li;
3646
3647 l = list_alloc();
3648
3649 if (l == NULL)
3650 {
3651 PyErr_NoMemory();
3652 return -1;
3653 }
3654
3655 tv->vval.v_list = l;
3656 tv->v_type = VAR_LIST;
3657
3658
3659 if (iterator == NULL)
3660 return -1;
3661
3662 while ((item = PyIter_Next(obj)))
3663 {
3664 li = listitem_alloc();
3665 if (li == NULL)
3666 {
3667 PyErr_NoMemory();
3668 return -1;
3669 }
3670 li->li_tv.v_lock = 0;
3671
3672 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
3673 return -1;
3674
3675 list_append(l, li);
3676
3677 Py_DECREF(item);
3678 }
3679
3680 Py_DECREF(iterator);
3681 return 0;
3682}
3683
Bram Moolenaardb913952012-06-29 12:54:53 +02003684typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
3685
3686 static int
3687convert_dl(PyObject *obj, typval_T *tv,
3688 pytotvfunc py_to_tv, PyObject *lookupDict)
3689{
3690 PyObject *capsule;
3691 char hexBuf[sizeof(void *) * 2 + 3];
3692
3693 sprintf(hexBuf, "%p", obj);
3694
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003695# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003696 capsule = PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003697# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003698 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003699# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02003700 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02003701 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003702# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003703 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02003704# else
3705 capsule = PyCObject_FromVoidPtr(tv, NULL);
3706# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003707 PyDict_SetItemString(lookupDict, hexBuf, capsule);
3708 Py_DECREF(capsule);
3709 if (py_to_tv(obj, tv, lookupDict) == -1)
3710 {
3711 tv->v_type = VAR_UNKNOWN;
3712 return -1;
3713 }
3714 /* As we are not using copy_tv which increments reference count we must
3715 * do it ourself. */
3716 switch(tv->v_type)
3717 {
3718 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
3719 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
3720 }
3721 }
3722 else
3723 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003724 typval_T *v;
3725
3726# ifdef PY_USE_CAPSULE
3727 v = PyCapsule_GetPointer(capsule, NULL);
3728# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003729 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003730# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003731 copy_tv(v, tv);
3732 }
3733 return 0;
3734}
3735
3736 static int
3737ConvertFromPyObject(PyObject *obj, typval_T *tv)
3738{
3739 PyObject *lookup_dict;
3740 int r;
3741
3742 lookup_dict = PyDict_New();
3743 r = _ConvertFromPyObject(obj, tv, lookup_dict);
3744 Py_DECREF(lookup_dict);
3745 return r;
3746}
3747
3748 static int
3749_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3750{
3751 if (obj->ob_type == &DictionaryType)
3752 {
3753 tv->v_type = VAR_DICT;
3754 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
3755 ++tv->vval.v_dict->dv_refcount;
3756 }
3757 else if (obj->ob_type == &ListType)
3758 {
3759 tv->v_type = VAR_LIST;
3760 tv->vval.v_list = (((ListObject *)(obj))->list);
3761 ++tv->vval.v_list->lv_refcount;
3762 }
3763 else if (obj->ob_type == &FunctionType)
3764 {
3765 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
3766 return -1;
3767
3768 tv->v_type = VAR_FUNC;
3769 func_ref(tv->vval.v_string);
3770 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003771 else if (PyBytes_Check(obj))
3772 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003773 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02003774
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003775 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
3776 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003777 if (result == NULL)
3778 return -1;
3779
3780 if (set_string_copy(result, tv) == -1)
3781 return -1;
3782
3783 tv->v_type = VAR_STRING;
3784 }
3785 else if (PyUnicode_Check(obj))
3786 {
3787 PyObject *bytes;
3788 char_u *result;
3789
Bram Moolenaardb913952012-06-29 12:54:53 +02003790 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
3791 if (bytes == NULL)
3792 return -1;
3793
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003794 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
3795 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003796 if (result == NULL)
3797 return -1;
3798
3799 if (set_string_copy(result, tv) == -1)
3800 {
3801 Py_XDECREF(bytes);
3802 return -1;
3803 }
3804 Py_XDECREF(bytes);
3805
3806 tv->v_type = VAR_STRING;
3807 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02003808#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02003809 else if (PyInt_Check(obj))
3810 {
3811 tv->v_type = VAR_NUMBER;
3812 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
3813 }
3814#endif
3815 else if (PyLong_Check(obj))
3816 {
3817 tv->v_type = VAR_NUMBER;
3818 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
3819 }
3820 else if (PyDict_Check(obj))
3821 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
3822#ifdef FEAT_FLOAT
3823 else if (PyFloat_Check(obj))
3824 {
3825 tv->v_type = VAR_FLOAT;
3826 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
3827 }
3828#endif
3829 else if (PyIter_Check(obj))
3830 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
3831 else if (PySequence_Check(obj))
3832 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
3833 else if (PyMapping_Check(obj))
3834 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
3835 else
3836 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003837 PyErr_SetString(PyExc_TypeError,
3838 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02003839 return -1;
3840 }
3841 return 0;
3842}
3843
3844 static PyObject *
3845ConvertToPyObject(typval_T *tv)
3846{
3847 if (tv == NULL)
3848 {
3849 PyErr_SetVim(_("NULL reference passed"));
3850 return NULL;
3851 }
3852 switch (tv->v_type)
3853 {
3854 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02003855 return PyBytes_FromString(tv->vval.v_string == NULL
3856 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02003857 case VAR_NUMBER:
3858 return PyLong_FromLong((long) tv->vval.v_number);
3859#ifdef FEAT_FLOAT
3860 case VAR_FLOAT:
3861 return PyFloat_FromDouble((double) tv->vval.v_float);
3862#endif
3863 case VAR_LIST:
3864 return ListNew(tv->vval.v_list);
3865 case VAR_DICT:
3866 return DictionaryNew(tv->vval.v_dict);
3867 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02003868 return FunctionNew(tv->vval.v_string == NULL
3869 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02003870 case VAR_UNKNOWN:
3871 Py_INCREF(Py_None);
3872 return Py_None;
3873 default:
3874 PyErr_SetVim(_("internal error: invalid value type"));
3875 return NULL;
3876 }
3877}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003878
3879typedef struct
3880{
3881 PyObject_HEAD
3882} CurrentObject;
3883static PyTypeObject CurrentType;
3884
3885 static void
3886init_structs(void)
3887{
3888 vim_memset(&OutputType, 0, sizeof(OutputType));
3889 OutputType.tp_name = "vim.message";
3890 OutputType.tp_basicsize = sizeof(OutputObject);
3891 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
3892 OutputType.tp_doc = "vim message object";
3893 OutputType.tp_methods = OutputMethods;
3894#if PY_MAJOR_VERSION >= 3
3895 OutputType.tp_getattro = OutputGetattro;
3896 OutputType.tp_setattro = OutputSetattro;
3897 OutputType.tp_alloc = call_PyType_GenericAlloc;
3898 OutputType.tp_new = call_PyType_GenericNew;
3899 OutputType.tp_free = call_PyObject_Free;
3900#else
3901 OutputType.tp_getattr = OutputGetattr;
3902 OutputType.tp_setattr = OutputSetattr;
3903#endif
3904
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003905 vim_memset(&IterType, 0, sizeof(IterType));
3906 IterType.tp_name = "vim.iter";
3907 IterType.tp_basicsize = sizeof(IterObject);
3908 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
3909 IterType.tp_doc = "generic iterator object";
3910 IterType.tp_iter = IterIter;
3911 IterType.tp_iternext = IterNext;
Bram Moolenaar2cd73622013-05-15 19:07:47 +02003912 IterType.tp_dealloc = IterDestructor;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003913 IterType.tp_traverse = IterTraverse;
3914 IterType.tp_clear = IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003915
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003916 vim_memset(&BufferType, 0, sizeof(BufferType));
3917 BufferType.tp_name = "vim.buffer";
3918 BufferType.tp_basicsize = sizeof(BufferType);
3919 BufferType.tp_dealloc = BufferDestructor;
3920 BufferType.tp_repr = BufferRepr;
3921 BufferType.tp_as_sequence = &BufferAsSeq;
3922 BufferType.tp_as_mapping = &BufferAsMapping;
3923 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
3924 BufferType.tp_doc = "vim buffer object";
3925 BufferType.tp_methods = BufferMethods;
3926#if PY_MAJOR_VERSION >= 3
3927 BufferType.tp_getattro = BufferGetattro;
3928 BufferType.tp_alloc = call_PyType_GenericAlloc;
3929 BufferType.tp_new = call_PyType_GenericNew;
3930 BufferType.tp_free = call_PyObject_Free;
3931#else
3932 BufferType.tp_getattr = BufferGetattr;
3933#endif
3934
3935 vim_memset(&WindowType, 0, sizeof(WindowType));
3936 WindowType.tp_name = "vim.window";
3937 WindowType.tp_basicsize = sizeof(WindowObject);
3938 WindowType.tp_dealloc = WindowDestructor;
3939 WindowType.tp_repr = WindowRepr;
3940 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
3941 WindowType.tp_doc = "vim Window object";
3942 WindowType.tp_methods = WindowMethods;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003943 WindowType.tp_traverse = WindowTraverse;
3944 WindowType.tp_clear = WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003945#if PY_MAJOR_VERSION >= 3
3946 WindowType.tp_getattro = WindowGetattro;
3947 WindowType.tp_setattro = WindowSetattro;
3948 WindowType.tp_alloc = call_PyType_GenericAlloc;
3949 WindowType.tp_new = call_PyType_GenericNew;
3950 WindowType.tp_free = call_PyObject_Free;
3951#else
3952 WindowType.tp_getattr = WindowGetattr;
3953 WindowType.tp_setattr = WindowSetattr;
3954#endif
3955
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003956 vim_memset(&TabPageType, 0, sizeof(TabPageType));
3957 TabPageType.tp_name = "vim.tabpage";
3958 TabPageType.tp_basicsize = sizeof(TabPageObject);
3959 TabPageType.tp_dealloc = TabPageDestructor;
3960 TabPageType.tp_repr = TabPageRepr;
3961 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
3962 TabPageType.tp_doc = "vim tab page object";
3963 TabPageType.tp_methods = TabPageMethods;
3964#if PY_MAJOR_VERSION >= 3
3965 TabPageType.tp_getattro = TabPageGetattro;
3966 TabPageType.tp_alloc = call_PyType_GenericAlloc;
3967 TabPageType.tp_new = call_PyType_GenericNew;
3968 TabPageType.tp_free = call_PyObject_Free;
3969#else
3970 TabPageType.tp_getattr = TabPageGetattr;
3971#endif
3972
Bram Moolenaardfa38d42013-05-15 13:38:47 +02003973 vim_memset(&BufMapType, 0, sizeof(BufMapType));
3974 BufMapType.tp_name = "vim.bufferlist";
3975 BufMapType.tp_basicsize = sizeof(BufMapObject);
3976 BufMapType.tp_as_mapping = &BufMapAsMapping;
3977 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003978 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003979 BufferType.tp_doc = "vim buffer list";
3980
3981 vim_memset(&WinListType, 0, sizeof(WinListType));
3982 WinListType.tp_name = "vim.windowlist";
3983 WinListType.tp_basicsize = sizeof(WinListType);
3984 WinListType.tp_as_sequence = &WinListAsSeq;
3985 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
3986 WinListType.tp_doc = "vim window list";
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003987 WinListType.tp_dealloc = WinListDestructor;
3988
3989 vim_memset(&TabListType, 0, sizeof(TabListType));
3990 TabListType.tp_name = "vim.tabpagelist";
3991 TabListType.tp_basicsize = sizeof(TabListType);
3992 TabListType.tp_as_sequence = &TabListAsSeq;
3993 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
3994 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003995
3996 vim_memset(&RangeType, 0, sizeof(RangeType));
3997 RangeType.tp_name = "vim.range";
3998 RangeType.tp_basicsize = sizeof(RangeObject);
3999 RangeType.tp_dealloc = RangeDestructor;
4000 RangeType.tp_repr = RangeRepr;
4001 RangeType.tp_as_sequence = &RangeAsSeq;
4002 RangeType.tp_as_mapping = &RangeAsMapping;
4003 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
4004 RangeType.tp_doc = "vim Range object";
4005 RangeType.tp_methods = RangeMethods;
4006#if PY_MAJOR_VERSION >= 3
4007 RangeType.tp_getattro = RangeGetattro;
4008 RangeType.tp_alloc = call_PyType_GenericAlloc;
4009 RangeType.tp_new = call_PyType_GenericNew;
4010 RangeType.tp_free = call_PyObject_Free;
4011#else
4012 RangeType.tp_getattr = RangeGetattr;
4013#endif
4014
4015 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4016 CurrentType.tp_name = "vim.currentdata";
4017 CurrentType.tp_basicsize = sizeof(CurrentObject);
4018 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4019 CurrentType.tp_doc = "vim current object";
4020#if PY_MAJOR_VERSION >= 3
4021 CurrentType.tp_getattro = CurrentGetattro;
4022 CurrentType.tp_setattro = CurrentSetattro;
4023#else
4024 CurrentType.tp_getattr = CurrentGetattr;
4025 CurrentType.tp_setattr = CurrentSetattr;
4026#endif
4027
4028 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4029 DictionaryType.tp_name = "vim.dictionary";
4030 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
4031 DictionaryType.tp_dealloc = DictionaryDestructor;
4032 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4033 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4034 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4035 DictionaryType.tp_methods = DictionaryMethods;
4036#if PY_MAJOR_VERSION >= 3
4037 DictionaryType.tp_getattro = DictionaryGetattro;
4038 DictionaryType.tp_setattro = DictionarySetattro;
4039#else
4040 DictionaryType.tp_getattr = DictionaryGetattr;
4041 DictionaryType.tp_setattr = DictionarySetattr;
4042#endif
4043
4044 vim_memset(&ListType, 0, sizeof(ListType));
4045 ListType.tp_name = "vim.list";
4046 ListType.tp_dealloc = ListDestructor;
4047 ListType.tp_basicsize = sizeof(ListObject);
4048 ListType.tp_as_sequence = &ListAsSeq;
4049 ListType.tp_as_mapping = &ListAsMapping;
4050 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4051 ListType.tp_doc = "list pushing modifications to vim structure";
4052 ListType.tp_methods = ListMethods;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004053 ListType.tp_iter = ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004054#if PY_MAJOR_VERSION >= 3
4055 ListType.tp_getattro = ListGetattro;
4056 ListType.tp_setattro = ListSetattro;
4057#else
4058 ListType.tp_getattr = ListGetattr;
4059 ListType.tp_setattr = ListSetattr;
4060#endif
4061
4062 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004063 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004064 FunctionType.tp_basicsize = sizeof(FunctionObject);
4065 FunctionType.tp_dealloc = FunctionDestructor;
4066 FunctionType.tp_call = FunctionCall;
4067 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4068 FunctionType.tp_doc = "object that calls vim function";
4069 FunctionType.tp_methods = FunctionMethods;
4070#if PY_MAJOR_VERSION >= 3
4071 FunctionType.tp_getattro = FunctionGetattro;
4072#else
4073 FunctionType.tp_getattr = FunctionGetattr;
4074#endif
4075
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004076 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4077 OptionsType.tp_name = "vim.options";
4078 OptionsType.tp_basicsize = sizeof(OptionsObject);
4079 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4080 OptionsType.tp_doc = "object for manipulating options";
4081 OptionsType.tp_as_mapping = &OptionsAsMapping;
4082 OptionsType.tp_dealloc = OptionsDestructor;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004083 OptionsType.tp_traverse = OptionsTraverse;
4084 OptionsType.tp_clear = OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004085
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004086#if PY_MAJOR_VERSION >= 3
4087 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4088 vimmodule.m_name = "vim";
4089 vimmodule.m_doc = "Vim Python interface\n";
4090 vimmodule.m_size = -1;
4091 vimmodule.m_methods = VimMethods;
4092#endif
4093}