blob: f982373a104193a06b43bee5d06c8bff37219733 [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 *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200477VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200478{
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
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200550/* Main purpose of this object is removing the need for do python
551 * initialization (i.e. PyType_Ready and setting type attributes) for a big
552 * bunch of objects. */
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
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200601/* Mac OSX defines clear() somewhere. */
602#ifdef clear
603# undef clear
604#endif
605
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200606 static int
607IterClear(PyObject *self)
608{
609 IterObject *this = (IterObject *)(self);
610
611 if (this->clear != NULL)
612 return this->clear(&this->cur);
613 else
614 return 0;
615}
616
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200617 static PyObject *
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200618IterNext(PyObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200619{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200620 IterObject *this = (IterObject *)(self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200621
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200622 return this->next(&this->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200623}
624
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200625 static PyObject *
626IterIter(PyObject *self)
627{
628 return self;
629}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200630
Bram Moolenaardb913952012-06-29 12:54:53 +0200631typedef struct pylinkedlist_S {
632 struct pylinkedlist_S *pll_next;
633 struct pylinkedlist_S *pll_prev;
634 PyObject *pll_obj;
635} pylinkedlist_T;
636
637static pylinkedlist_T *lastdict = NULL;
638static pylinkedlist_T *lastlist = NULL;
639
640 static void
641pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
642{
643 if (ref->pll_prev == NULL)
644 {
645 if (ref->pll_next == NULL)
646 {
647 *last = NULL;
648 return;
649 }
650 }
651 else
652 ref->pll_prev->pll_next = ref->pll_next;
653
654 if (ref->pll_next == NULL)
655 *last = ref->pll_prev;
656 else
657 ref->pll_next->pll_prev = ref->pll_prev;
658}
659
660 static void
661pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
662{
663 if (*last == NULL)
664 ref->pll_prev = NULL;
665 else
666 {
667 (*last)->pll_next = ref;
668 ref->pll_prev = *last;
669 }
670 ref->pll_next = NULL;
671 ref->pll_obj = self;
672 *last = ref;
673}
674
675static PyTypeObject DictionaryType;
676
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200677#define DICTKEY_GET_NOTEMPTY(err) \
678 DICTKEY_GET(err) \
679 if (*key == NUL) \
680 { \
681 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
682 return err; \
683 }
684
Bram Moolenaardb913952012-06-29 12:54:53 +0200685typedef struct
686{
687 PyObject_HEAD
688 dict_T *dict;
689 pylinkedlist_T ref;
690} DictionaryObject;
691
692 static PyObject *
693DictionaryNew(dict_T *dict)
694{
695 DictionaryObject *self;
696
697 self = PyObject_NEW(DictionaryObject, &DictionaryType);
698 if (self == NULL)
699 return NULL;
700 self->dict = dict;
701 ++dict->dv_refcount;
702
703 pyll_add((PyObject *)(self), &self->ref, &lastdict);
704
705 return (PyObject *)(self);
706}
707
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200708 static void
709DictionaryDestructor(PyObject *self)
710{
711 DictionaryObject *this = ((DictionaryObject *) (self));
712
713 pyll_remove(&this->ref, &lastdict);
714 dict_unref(this->dict);
715
716 DESTRUCTOR_FINISH(self);
717}
718
Bram Moolenaardb913952012-06-29 12:54:53 +0200719 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200720DictionarySetattr(PyObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200721{
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200722 DictionaryObject *this = (DictionaryObject *)(self);
723
Bram Moolenaar66b79852012-09-21 14:00:35 +0200724 if (val == NULL)
725 {
726 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
727 return -1;
728 }
729
730 if (strcmp(name, "locked") == 0)
731 {
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200732 if (this->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200733 {
734 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
735 return -1;
736 }
737 else
738 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200739 int istrue = PyObject_IsTrue(val);
740 if (istrue == -1)
741 return -1;
742 else if (istrue)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200743 this->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200744 else
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200745 this->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200746 }
747 return 0;
748 }
749 else
750 {
751 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
752 return -1;
753 }
754}
755
756 static PyInt
Bram Moolenaardb913952012-06-29 12:54:53 +0200757DictionaryLength(PyObject *self)
758{
759 return ((PyInt) ((((DictionaryObject *)(self))->dict->dv_hashtab.ht_used)));
760}
761
762 static PyObject *
763DictionaryItem(PyObject *self, PyObject *keyObject)
764{
765 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200766 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200767 DICTKEY_DECL
768
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200769 DICTKEY_GET_NOTEMPTY(NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200770
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200771 di = dict_find(((DictionaryObject *) (self))->dict, key, -1);
772
Bram Moolenaar696c2112012-09-21 13:43:14 +0200773 DICTKEY_UNREF
774
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200775 if (di == NULL)
776 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200777 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200778 return NULL;
779 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200780
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200781 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200782}
783
784 static PyInt
785DictionaryAssItem(PyObject *self, PyObject *keyObject, PyObject *valObject)
786{
787 char_u *key;
788 typval_T tv;
789 dict_T *d = ((DictionaryObject *)(self))->dict;
790 dictitem_T *di;
791 DICTKEY_DECL
792
793 if (d->dv_lock)
794 {
795 PyErr_SetVim(_("dict is locked"));
796 return -1;
797 }
798
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200799 DICTKEY_GET_NOTEMPTY(-1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200800
801 di = dict_find(d, key, -1);
802
803 if (valObject == NULL)
804 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200805 hashitem_T *hi;
806
Bram Moolenaardb913952012-06-29 12:54:53 +0200807 if (di == NULL)
808 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200809 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200810 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +0200811 return -1;
812 }
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200813 hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaardb913952012-06-29 12:54:53 +0200814 hash_remove(&d->dv_hashtab, hi);
815 dictitem_free(di);
816 return 0;
817 }
818
819 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200820 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +0200821
822 if (di == NULL)
823 {
824 di = dictitem_alloc(key);
825 if (di == NULL)
826 {
827 PyErr_NoMemory();
828 return -1;
829 }
830 di->di_tv.v_lock = 0;
831
832 if (dict_add(d, di) == FAIL)
833 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200834 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +0200835 vim_free(di);
836 PyErr_SetVim(_("failed to add key to dictionary"));
837 return -1;
838 }
839 }
840 else
841 clear_tv(&di->di_tv);
842
843 DICTKEY_UNREF
844
845 copy_tv(&tv, &di->di_tv);
846 return 0;
847}
848
849 static PyObject *
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100850DictionaryListKeys(PyObject *self UNUSED)
Bram Moolenaardb913952012-06-29 12:54:53 +0200851{
852 dict_T *dict = ((DictionaryObject *)(self))->dict;
853 long_u todo = dict->dv_hashtab.ht_used;
854 Py_ssize_t i = 0;
855 PyObject *r;
856 hashitem_T *hi;
857
858 r = PyList_New(todo);
859 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
860 {
861 if (!HASHITEM_EMPTY(hi))
862 {
863 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
864 --todo;
865 ++i;
866 }
867 }
868 return r;
869}
870
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200871static PyMappingMethods DictionaryAsMapping = {
872 (lenfunc) DictionaryLength,
873 (binaryfunc) DictionaryItem,
874 (objobjargproc) DictionaryAssItem,
875};
876
Bram Moolenaardb913952012-06-29 12:54:53 +0200877static struct PyMethodDef DictionaryMethods[] = {
878 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
879 { NULL, NULL, 0, NULL }
880};
881
882static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200883static PySequenceMethods ListAsSeq;
884static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +0200885
886typedef struct
887{
888 PyObject_HEAD
889 list_T *list;
890 pylinkedlist_T ref;
891} ListObject;
892
893 static PyObject *
894ListNew(list_T *list)
895{
896 ListObject *self;
897
898 self = PyObject_NEW(ListObject, &ListType);
899 if (self == NULL)
900 return NULL;
901 self->list = list;
902 ++list->lv_refcount;
903
904 pyll_add((PyObject *)(self), &self->ref, &lastlist);
905
906 return (PyObject *)(self);
907}
908
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200909 static void
910ListDestructor(PyObject *self)
911{
912 ListObject *this = (ListObject *)(self);
913
914 pyll_remove(&this->ref, &lastlist);
915 list_unref(this->list);
916
917 DESTRUCTOR_FINISH(self);
918}
919
Bram Moolenaardb913952012-06-29 12:54:53 +0200920 static int
921list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict)
922{
923 Py_ssize_t i;
924 Py_ssize_t lsize = PySequence_Size(obj);
925 PyObject *litem;
926 listitem_T *li;
927
928 for(i=0; i<lsize; i++)
929 {
930 li = listitem_alloc();
931 if (li == NULL)
932 {
933 PyErr_NoMemory();
934 return -1;
935 }
936 li->li_tv.v_lock = 0;
937
938 litem = PySequence_GetItem(obj, i);
939 if (litem == NULL)
940 return -1;
941 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1)
942 return -1;
943
944 list_append(l, li);
945 }
946 return 0;
947}
948
Bram Moolenaardb913952012-06-29 12:54:53 +0200949 static PyInt
950ListLength(PyObject *self)
951{
952 return ((PyInt) (((ListObject *) (self))->list->lv_len));
953}
954
955 static PyObject *
956ListItem(PyObject *self, Py_ssize_t index)
957{
958 listitem_T *li;
959
960 if (index>=ListLength(self))
961 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200962 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200963 return NULL;
964 }
965 li = list_find(((ListObject *) (self))->list, (long) index);
966 if (li == NULL)
967 {
968 PyErr_SetVim(_("internal error: failed to get vim list item"));
969 return NULL;
970 }
971 return ConvertToPyObject(&li->li_tv);
972}
973
974#define PROC_RANGE \
975 if (last < 0) {\
976 if (last < -size) \
977 last = 0; \
978 else \
979 last += size; \
980 } \
981 if (first < 0) \
982 first = 0; \
983 if (first > size) \
984 first = size; \
985 if (last > size) \
986 last = size;
987
988 static PyObject *
989ListSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last)
990{
991 PyInt i;
992 PyInt size = ListLength(self);
993 PyInt n;
994 PyObject *list;
995 int reversed = 0;
996
997 PROC_RANGE
998 if (first >= last)
999 first = last;
1000
1001 n = last-first;
1002 list = PyList_New(n);
1003 if (list == NULL)
1004 return NULL;
1005
1006 for (i = 0; i < n; ++i)
1007 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001008 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001009 if (item == NULL)
1010 {
1011 Py_DECREF(list);
1012 return NULL;
1013 }
1014
1015 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1016 {
1017 Py_DECREF(item);
1018 Py_DECREF(list);
1019 return NULL;
1020 }
1021 }
1022
1023 return list;
1024}
1025
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001026typedef struct
1027{
1028 listwatch_T lw;
1029 list_T *list;
1030} listiterinfo_T;
1031
1032 static void
1033ListIterDestruct(listiterinfo_T *lii)
1034{
1035 list_rem_watch(lii->list, &lii->lw);
1036 PyMem_Free(lii);
1037}
1038
1039 static PyObject *
1040ListIterNext(listiterinfo_T **lii)
1041{
1042 PyObject *r;
1043
1044 if (!((*lii)->lw.lw_item))
1045 return NULL;
1046
1047 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1048 return NULL;
1049
1050 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1051
1052 return r;
1053}
1054
1055 static PyObject *
1056ListIter(PyObject *self)
1057{
1058 listiterinfo_T *lii;
1059 list_T *l = ((ListObject *) (self))->list;
1060
1061 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1062 {
1063 PyErr_NoMemory();
1064 return NULL;
1065 }
1066
1067 list_add_watch(l, &lii->lw);
1068 lii->lw.lw_item = l->lv_first;
1069 lii->list = l;
1070
1071 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001072 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1073 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001074}
1075
Bram Moolenaardb913952012-06-29 12:54:53 +02001076 static int
1077ListAssItem(PyObject *self, Py_ssize_t index, PyObject *obj)
1078{
1079 typval_T tv;
1080 list_T *l = ((ListObject *) (self))->list;
1081 listitem_T *li;
1082 Py_ssize_t length = ListLength(self);
1083
1084 if (l->lv_lock)
1085 {
1086 PyErr_SetVim(_("list is locked"));
1087 return -1;
1088 }
1089 if (index>length || (index==length && obj==NULL))
1090 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001091 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001092 return -1;
1093 }
1094
1095 if (obj == NULL)
1096 {
1097 li = list_find(l, (long) index);
1098 list_remove(l, li, li);
1099 clear_tv(&li->li_tv);
1100 vim_free(li);
1101 return 0;
1102 }
1103
1104 if (ConvertFromPyObject(obj, &tv) == -1)
1105 return -1;
1106
1107 if (index == length)
1108 {
1109 if (list_append_tv(l, &tv) == FAIL)
1110 {
1111 PyErr_SetVim(_("Failed to add item to list"));
1112 return -1;
1113 }
1114 }
1115 else
1116 {
1117 li = list_find(l, (long) index);
1118 clear_tv(&li->li_tv);
1119 copy_tv(&tv, &li->li_tv);
1120 }
1121 return 0;
1122}
1123
1124 static int
1125ListAssSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
1126{
1127 PyInt size = ListLength(self);
1128 Py_ssize_t i;
1129 Py_ssize_t lsize;
1130 PyObject *litem;
1131 listitem_T *li;
1132 listitem_T *next;
1133 typval_T v;
1134 list_T *l = ((ListObject *) (self))->list;
1135
1136 if (l->lv_lock)
1137 {
1138 PyErr_SetVim(_("list is locked"));
1139 return -1;
1140 }
1141
1142 PROC_RANGE
1143
1144 if (first == size)
1145 li = NULL;
1146 else
1147 {
1148 li = list_find(l, (long) first);
1149 if (li == NULL)
1150 {
1151 PyErr_SetVim(_("internal error: no vim list item"));
1152 return -1;
1153 }
1154 if (last > first)
1155 {
1156 i = last - first;
1157 while (i-- && li != NULL)
1158 {
1159 next = li->li_next;
1160 listitem_remove(l, li);
1161 li = next;
1162 }
1163 }
1164 }
1165
1166 if (obj == NULL)
1167 return 0;
1168
1169 if (!PyList_Check(obj))
1170 {
1171 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1172 return -1;
1173 }
1174
1175 lsize = PyList_Size(obj);
1176
1177 for(i=0; i<lsize; i++)
1178 {
1179 litem = PyList_GetItem(obj, i);
1180 if (litem == NULL)
1181 return -1;
1182 if (ConvertFromPyObject(litem, &v) == -1)
1183 return -1;
1184 if (list_insert_tv(l, &v, li) == FAIL)
1185 {
1186 PyErr_SetVim(_("internal error: failed to add item to list"));
1187 return -1;
1188 }
1189 }
1190 return 0;
1191}
1192
1193 static PyObject *
1194ListConcatInPlace(PyObject *self, PyObject *obj)
1195{
1196 list_T *l = ((ListObject *) (self))->list;
1197 PyObject *lookup_dict;
1198
1199 if (l->lv_lock)
1200 {
1201 PyErr_SetVim(_("list is locked"));
1202 return NULL;
1203 }
1204
1205 if (!PySequence_Check(obj))
1206 {
1207 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1208 return NULL;
1209 }
1210
1211 lookup_dict = PyDict_New();
1212 if (list_py_concat(l, obj, lookup_dict) == -1)
1213 {
1214 Py_DECREF(lookup_dict);
1215 return NULL;
1216 }
1217 Py_DECREF(lookup_dict);
1218
1219 Py_INCREF(self);
1220 return self;
1221}
1222
Bram Moolenaar66b79852012-09-21 14:00:35 +02001223 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001224ListSetattr(PyObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001225{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001226 ListObject *this = (ListObject *)(self);
1227
Bram Moolenaar66b79852012-09-21 14:00:35 +02001228 if (val == NULL)
1229 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001230 PyErr_SetString(PyExc_AttributeError,
1231 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001232 return -1;
1233 }
1234
1235 if (strcmp(name, "locked") == 0)
1236 {
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001237 if (this->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001238 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001239 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001240 return -1;
1241 }
1242 else
1243 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001244 int istrue = PyObject_IsTrue(val);
1245 if (istrue == -1)
1246 return -1;
1247 else if (istrue)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001248 this->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001249 else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001250 this->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001251 }
1252 return 0;
1253 }
1254 else
1255 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001256 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001257 return -1;
1258 }
1259}
1260
Bram Moolenaardb913952012-06-29 12:54:53 +02001261static struct PyMethodDef ListMethods[] = {
1262 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1263 { NULL, NULL, 0, NULL }
1264};
1265
1266typedef struct
1267{
1268 PyObject_HEAD
1269 char_u *name;
1270} FunctionObject;
1271
1272static PyTypeObject FunctionType;
1273
1274 static PyObject *
1275FunctionNew(char_u *name)
1276{
1277 FunctionObject *self;
1278
1279 self = PyObject_NEW(FunctionObject, &FunctionType);
1280 if (self == NULL)
1281 return NULL;
1282 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1283 if (self->name == NULL)
1284 {
1285 PyErr_NoMemory();
1286 return NULL;
1287 }
1288 STRCPY(self->name, name);
1289 func_ref(name);
1290 return (PyObject *)(self);
1291}
1292
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001293 static void
1294FunctionDestructor(PyObject *self)
1295{
1296 FunctionObject *this = (FunctionObject *) (self);
1297
1298 func_unref(this->name);
1299 PyMem_Del(this->name);
1300
1301 DESTRUCTOR_FINISH(self);
1302}
1303
Bram Moolenaardb913952012-06-29 12:54:53 +02001304 static PyObject *
1305FunctionCall(PyObject *self, PyObject *argsObject, PyObject *kwargs)
1306{
1307 FunctionObject *this = (FunctionObject *)(self);
1308 char_u *name = this->name;
1309 typval_T args;
1310 typval_T selfdicttv;
1311 typval_T rettv;
1312 dict_T *selfdict = NULL;
1313 PyObject *selfdictObject;
1314 PyObject *result;
1315 int error;
1316
1317 if (ConvertFromPyObject(argsObject, &args) == -1)
1318 return NULL;
1319
1320 if (kwargs != NULL)
1321 {
1322 selfdictObject = PyDict_GetItemString(kwargs, "self");
1323 if (selfdictObject != NULL)
1324 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001325 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001326 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001327 PyErr_SetString(PyExc_TypeError,
1328 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001329 clear_tv(&args);
1330 return NULL;
1331 }
1332 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
1333 return NULL;
1334 selfdict = selfdicttv.vval.v_dict;
1335 }
1336 }
1337
Bram Moolenaar71700b82013-05-15 17:49:05 +02001338 Py_BEGIN_ALLOW_THREADS
1339 Python_Lock_Vim();
1340
Bram Moolenaardb913952012-06-29 12:54:53 +02001341 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001342
1343 Python_Release_Vim();
1344 Py_END_ALLOW_THREADS
1345
Bram Moolenaardb913952012-06-29 12:54:53 +02001346 if (error != OK)
1347 {
1348 result = NULL;
1349 PyErr_SetVim(_("failed to run function"));
1350 }
1351 else
1352 result = ConvertToPyObject(&rettv);
1353
1354 /* FIXME Check what should really be cleared. */
1355 clear_tv(&args);
1356 clear_tv(&rettv);
1357 /*
1358 * if (selfdict!=NULL)
1359 * clear_tv(selfdicttv);
1360 */
1361
1362 return result;
1363}
1364
1365static struct PyMethodDef FunctionMethods[] = {
1366 {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1367 { NULL, NULL, 0, NULL }
1368};
1369
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001370/*
1371 * Options object
1372 */
1373
1374static PyTypeObject OptionsType;
1375
1376typedef int (*checkfun)(void *);
1377
1378typedef struct
1379{
1380 PyObject_HEAD
1381 int opt_type;
1382 void *from;
1383 checkfun Check;
1384 PyObject *fromObj;
1385} OptionsObject;
1386
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001387 static int
1388dummy_check(void *arg UNUSED)
1389{
1390 return 0;
1391}
1392
1393 static PyObject *
1394OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1395{
1396 OptionsObject *self;
1397
1398 self = PyObject_NEW(OptionsObject, &OptionsType);
1399 if (self == NULL)
1400 return NULL;
1401
1402 self->opt_type = opt_type;
1403 self->from = from;
1404 self->Check = Check;
1405 self->fromObj = fromObj;
1406 if (fromObj)
1407 Py_INCREF(fromObj);
1408
1409 return (PyObject *)(self);
1410}
1411
1412 static void
1413OptionsDestructor(PyObject *self)
1414{
1415 if (((OptionsObject *)(self))->fromObj)
1416 Py_DECREF(((OptionsObject *)(self))->fromObj);
1417 DESTRUCTOR_FINISH(self);
1418}
1419
1420 static int
1421OptionsTraverse(PyObject *self, visitproc visit, void *arg)
1422{
1423 Py_VISIT(((OptionsObject *)(self))->fromObj);
1424 return 0;
1425}
1426
1427 static int
1428OptionsClear(PyObject *self)
1429{
1430 Py_CLEAR(((OptionsObject *)(self))->fromObj);
1431 return 0;
1432}
1433
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001434 static PyObject *
1435OptionsItem(OptionsObject *this, PyObject *keyObject)
1436{
1437 char_u *key;
1438 int flags;
1439 long numval;
1440 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001441 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001442
1443 if (this->Check(this->from))
1444 return NULL;
1445
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001446 DICTKEY_GET_NOTEMPTY(NULL)
1447
1448 flags = get_option_value_strict(key, &numval, &stringval,
1449 this->opt_type, this->from);
1450
1451 DICTKEY_UNREF
1452
1453 if (flags == 0)
1454 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001455 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001456 return NULL;
1457 }
1458
1459 if (flags & SOPT_UNSET)
1460 {
1461 Py_INCREF(Py_None);
1462 return Py_None;
1463 }
1464 else if (flags & SOPT_BOOL)
1465 {
1466 PyObject *r;
1467 r = numval ? Py_True : Py_False;
1468 Py_INCREF(r);
1469 return r;
1470 }
1471 else if (flags & SOPT_NUM)
1472 return PyInt_FromLong(numval);
1473 else if (flags & SOPT_STRING)
1474 {
1475 if (stringval)
1476 return PyBytes_FromString((char *) stringval);
1477 else
1478 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001479 PyErr_SetString(PyExc_RuntimeError,
1480 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001481 return NULL;
1482 }
1483 }
1484 else
1485 {
1486 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1487 return NULL;
1488 }
1489}
1490
1491 static int
1492set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1493 char_u *key;
1494 int numval;
1495 char_u *stringval;
1496 int opt_flags;
1497 int opt_type;
1498 void *from;
1499{
1500 win_T *save_curwin;
1501 tabpage_T *save_curtab;
Bram Moolenaar105bc352013-05-17 16:03:57 +02001502 buf_T *save_curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001503 int r = 0;
1504
1505 switch (opt_type)
1506 {
1507 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001508 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1509 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001510 {
1511 PyErr_SetVim("Problem while switching windows.");
1512 return -1;
1513 }
1514 set_option_value(key, numval, stringval, opt_flags);
1515 restore_win(save_curwin, save_curtab);
1516 break;
1517 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001518 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001519 set_option_value(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001520 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001521 break;
1522 case SREQ_GLOBAL:
1523 set_option_value(key, numval, stringval, opt_flags);
1524 break;
1525 }
1526 return r;
1527}
1528
1529 static int
1530OptionsAssItem(OptionsObject *this, PyObject *keyObject, PyObject *valObject)
1531{
1532 char_u *key;
1533 int flags;
1534 int opt_flags;
1535 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001536 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001537
1538 if (this->Check(this->from))
1539 return -1;
1540
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001541 DICTKEY_GET_NOTEMPTY(-1)
1542
1543 flags = get_option_value_strict(key, NULL, NULL,
1544 this->opt_type, this->from);
1545
1546 DICTKEY_UNREF
1547
1548 if (flags == 0)
1549 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001550 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001551 return -1;
1552 }
1553
1554 if (valObject == NULL)
1555 {
1556 if (this->opt_type == SREQ_GLOBAL)
1557 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001558 PyErr_SetString(PyExc_ValueError,
1559 _("unable to unset global option"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001560 return -1;
1561 }
1562 else if (!(flags & SOPT_GLOBAL))
1563 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001564 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1565 "without global value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001566 return -1;
1567 }
1568 else
1569 {
1570 unset_global_local_option(key, this->from);
1571 return 0;
1572 }
1573 }
1574
1575 opt_flags = (this->opt_type ? OPT_LOCAL : OPT_GLOBAL);
1576
1577 if (flags & SOPT_BOOL)
1578 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001579 int istrue = PyObject_IsTrue(valObject);
1580 if (istrue == -1)
1581 return -1;
1582 r = set_option_value_for(key, istrue, NULL,
Bram Moolenaar03db85b2013-05-15 14:51:35 +02001583 opt_flags, this->opt_type, this->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001584 }
1585 else if (flags & SOPT_NUM)
1586 {
1587 int val;
1588
1589#if PY_MAJOR_VERSION < 3
1590 if (PyInt_Check(valObject))
1591 val = PyInt_AsLong(valObject);
1592 else
1593#endif
1594 if (PyLong_Check(valObject))
1595 val = PyLong_AsLong(valObject);
1596 else
1597 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001598 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001599 return -1;
1600 }
1601
1602 r = set_option_value_for(key, val, NULL, opt_flags,
1603 this->opt_type, this->from);
1604 }
1605 else
1606 {
1607 char_u *val;
1608 if (PyBytes_Check(valObject))
1609 {
1610
1611 if (PyString_AsStringAndSize(valObject, (char **) &val, NULL) == -1)
1612 return -1;
1613 if (val == NULL)
1614 return -1;
1615
1616 val = vim_strsave(val);
1617 }
1618 else if (PyUnicode_Check(valObject))
1619 {
1620 PyObject *bytes;
1621
1622 bytes = PyUnicode_AsEncodedString(valObject, (char *)ENC_OPT, NULL);
1623 if (bytes == NULL)
1624 return -1;
1625
1626 if(PyString_AsStringAndSize(bytes, (char **) &val, NULL) == -1)
1627 return -1;
1628 if (val == NULL)
1629 return -1;
1630
1631 val = vim_strsave(val);
1632 Py_XDECREF(bytes);
1633 }
1634 else
1635 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001636 PyErr_SetString(PyExc_TypeError, _("object must be string"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001637 return -1;
1638 }
1639
1640 r = set_option_value_for(key, 0, val, opt_flags,
1641 this->opt_type, this->from);
1642 vim_free(val);
1643 }
1644
1645 return r;
1646}
1647
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001648static PyMappingMethods OptionsAsMapping = {
1649 (lenfunc) NULL,
1650 (binaryfunc) OptionsItem,
1651 (objobjargproc) OptionsAssItem,
1652};
1653
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001654/* Tabpage object
1655 */
1656
1657typedef struct
1658{
1659 PyObject_HEAD
1660 tabpage_T *tab;
1661} TabPageObject;
1662
1663static PyObject *WinListNew(TabPageObject *tabObject);
1664
1665static PyTypeObject TabPageType;
1666
1667 static int
1668CheckTabPage(TabPageObject *this)
1669{
1670 if (this->tab == INVALID_TABPAGE_VALUE)
1671 {
1672 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1673 return -1;
1674 }
1675
1676 return 0;
1677}
1678
1679 static PyObject *
1680TabPageNew(tabpage_T *tab)
1681{
1682 TabPageObject *self;
1683
1684 if (TAB_PYTHON_REF(tab))
1685 {
1686 self = TAB_PYTHON_REF(tab);
1687 Py_INCREF(self);
1688 }
1689 else
1690 {
1691 self = PyObject_NEW(TabPageObject, &TabPageType);
1692 if (self == NULL)
1693 return NULL;
1694 self->tab = tab;
1695 TAB_PYTHON_REF(tab) = self;
1696 }
1697
1698 return (PyObject *)(self);
1699}
1700
1701 static void
1702TabPageDestructor(PyObject *self)
1703{
1704 TabPageObject *this = (TabPageObject *)(self);
1705
1706 if (this->tab && this->tab != INVALID_TABPAGE_VALUE)
1707 TAB_PYTHON_REF(this->tab) = NULL;
1708
1709 DESTRUCTOR_FINISH(self);
1710}
1711
1712 static PyObject *
1713TabPageAttr(TabPageObject *this, char *name)
1714{
1715 if (strcmp(name, "windows") == 0)
1716 return WinListNew(this);
1717 else if (strcmp(name, "number") == 0)
1718 return PyLong_FromLong((long) get_tab_number(this->tab));
1719 else if (strcmp(name, "vars") == 0)
1720 return DictionaryNew(this->tab->tp_vars);
1721 else if (strcmp(name, "window") == 0)
1722 {
1723 /* For current tab window.c does not bother to set or update tp_curwin
1724 */
1725 if (this->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001726 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001727 else
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001728 return WindowNew(this->tab->tp_curwin, this->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001729 }
1730 return NULL;
1731}
1732
1733 static PyObject *
1734TabPageRepr(PyObject *self)
1735{
1736 static char repr[100];
1737 TabPageObject *this = (TabPageObject *)(self);
1738
1739 if (this->tab == INVALID_TABPAGE_VALUE)
1740 {
1741 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
1742 return PyString_FromString(repr);
1743 }
1744 else
1745 {
1746 int t = get_tab_number(this->tab);
1747
1748 if (t == 0)
1749 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
1750 (self));
1751 else
1752 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
1753
1754 return PyString_FromString(repr);
1755 }
1756}
1757
1758static struct PyMethodDef TabPageMethods[] = {
1759 /* name, function, calling, documentation */
1760 { NULL, NULL, 0, NULL }
1761};
1762
1763/*
1764 * Window list object
1765 */
1766
1767static PyTypeObject TabListType;
1768static PySequenceMethods TabListAsSeq;
1769
1770typedef struct
1771{
1772 PyObject_HEAD
1773} TabListObject;
1774
1775 static PyInt
1776TabListLength(PyObject *self UNUSED)
1777{
1778 tabpage_T *tp = first_tabpage;
1779 PyInt n = 0;
1780
1781 while (tp != NULL)
1782 {
1783 ++n;
1784 tp = tp->tp_next;
1785 }
1786
1787 return n;
1788}
1789
1790 static PyObject *
1791TabListItem(PyObject *self UNUSED, PyInt n)
1792{
1793 tabpage_T *tp;
1794
1795 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
1796 if (n == 0)
1797 return TabPageNew(tp);
1798
1799 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
1800 return NULL;
1801}
1802
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001803/* Window object
1804 */
1805
1806typedef struct
1807{
1808 PyObject_HEAD
1809 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001810 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001811} WindowObject;
1812
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001813static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001814
1815 static int
1816CheckWindow(WindowObject *this)
1817{
1818 if (this->win == INVALID_WINDOW_VALUE)
1819 {
1820 PyErr_SetVim(_("attempt to refer to deleted window"));
1821 return -1;
1822 }
1823
1824 return 0;
1825}
1826
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001827 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001828WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02001829{
1830 /* We need to handle deletion of windows underneath us.
1831 * If we add a "w_python*_ref" field to the win_T structure,
1832 * then we can get at it in win_free() in vim. We then
1833 * need to create only ONE Python object per window - if
1834 * we try to create a second, just INCREF the existing one
1835 * and return it. The (single) Python object referring to
1836 * the window is stored in "w_python*_ref".
1837 * On a win_free() we set the Python object's win_T* field
1838 * to an invalid value. We trap all uses of a window
1839 * object, and reject them if the win_T* field is invalid.
1840 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001841 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02001842 * w_python_ref and w_python3_ref fields respectively.
1843 */
1844
1845 WindowObject *self;
1846
1847 if (WIN_PYTHON_REF(win))
1848 {
1849 self = WIN_PYTHON_REF(win);
1850 Py_INCREF(self);
1851 }
1852 else
1853 {
1854 self = PyObject_NEW(WindowObject, &WindowType);
1855 if (self == NULL)
1856 return NULL;
1857 self->win = win;
1858 WIN_PYTHON_REF(win) = self;
1859 }
1860
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001861 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
1862
Bram Moolenaar971db462013-05-12 18:44:48 +02001863 return (PyObject *)(self);
1864}
1865
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001866 static void
1867WindowDestructor(PyObject *self)
1868{
1869 WindowObject *this = (WindowObject *)(self);
1870
1871 if (this->win && this->win != INVALID_WINDOW_VALUE)
1872 WIN_PYTHON_REF(this->win) = NULL;
1873
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001874 Py_DECREF(((PyObject *)(this->tabObject)));
1875
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001876 DESTRUCTOR_FINISH(self);
1877}
1878
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001879 static win_T *
1880get_firstwin(TabPageObject *tabObject)
1881{
1882 if (tabObject)
1883 {
1884 if (CheckTabPage(tabObject))
1885 return NULL;
1886 /* For current tab window.c does not bother to set or update tp_firstwin
1887 */
1888 else if (tabObject->tab == curtab)
1889 return firstwin;
1890 else
1891 return tabObject->tab->tp_firstwin;
1892 }
1893 else
1894 return firstwin;
1895}
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001896 static int
1897WindowTraverse(PyObject *self, visitproc visit, void *arg)
1898{
1899 Py_VISIT(((PyObject *)(((WindowObject *)(self))->tabObject)));
1900 return 0;
1901}
1902
1903 static int
1904WindowClear(PyObject *self)
1905{
1906 Py_CLEAR((((WindowObject *)(self))->tabObject));
1907 return 0;
1908}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001909
Bram Moolenaar971db462013-05-12 18:44:48 +02001910 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001911WindowAttr(WindowObject *this, char *name)
1912{
1913 if (strcmp(name, "buffer") == 0)
1914 return (PyObject *)BufferNew(this->win->w_buffer);
1915 else if (strcmp(name, "cursor") == 0)
1916 {
1917 pos_T *pos = &this->win->w_cursor;
1918
1919 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1920 }
1921 else if (strcmp(name, "height") == 0)
Bram Moolenaar99add412013-05-12 19:09:51 +02001922 return PyLong_FromLong((long)(this->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001923#ifdef FEAT_WINDOWS
1924 else if (strcmp(name, "row") == 0)
1925 return PyLong_FromLong((long)(this->win->w_winrow));
1926#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001927#ifdef FEAT_VERTSPLIT
1928 else if (strcmp(name, "width") == 0)
Bram Moolenaar99add412013-05-12 19:09:51 +02001929 return PyLong_FromLong((long)(W_WIDTH(this->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001930 else if (strcmp(name, "col") == 0)
1931 return PyLong_FromLong((long)(W_WINCOL(this->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001932#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001933 else if (strcmp(name, "vars") == 0)
1934 return DictionaryNew(this->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001935 else if (strcmp(name, "options") == 0)
1936 return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow,
1937 (PyObject *) this);
Bram Moolenaar6d216452013-05-12 19:00:41 +02001938 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001939 {
1940 if (CheckTabPage(this->tabObject))
1941 return NULL;
1942 return PyLong_FromLong((long)
1943 get_win_number(this->win, get_firstwin(this->tabObject)));
1944 }
1945 else if (strcmp(name, "tabpage") == 0)
1946 {
1947 Py_INCREF(this->tabObject);
1948 return (PyObject *)(this->tabObject);
1949 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001950 else if (strcmp(name,"__members__") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001951 return Py_BuildValue("[sssssssss]", "buffer", "cursor", "height",
1952 "vars", "options", "number", "row", "col", "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001953 else
1954 return NULL;
1955}
1956
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001957 static int
1958WindowSetattr(PyObject *self, char *name, PyObject *val)
1959{
1960 WindowObject *this = (WindowObject *)(self);
1961
1962 if (CheckWindow(this))
1963 return -1;
1964
1965 if (strcmp(name, "buffer") == 0)
1966 {
1967 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
1968 return -1;
1969 }
1970 else if (strcmp(name, "cursor") == 0)
1971 {
1972 long lnum;
1973 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001974
1975 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
1976 return -1;
1977
1978 if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count)
1979 {
1980 PyErr_SetVim(_("cursor position outside buffer"));
1981 return -1;
1982 }
1983
1984 /* Check for keyboard interrupts */
1985 if (VimErrorCheck())
1986 return -1;
1987
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001988 this->win->w_cursor.lnum = lnum;
1989 this->win->w_cursor.col = col;
1990#ifdef FEAT_VIRTUALEDIT
1991 this->win->w_cursor.coladd = 0;
1992#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02001993 /* When column is out of range silently correct it. */
1994 check_cursor_col_win(this->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001995
Bram Moolenaar03a807a2011-07-07 15:08:58 +02001996 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001997 return 0;
1998 }
1999 else if (strcmp(name, "height") == 0)
2000 {
2001 int height;
2002 win_T *savewin;
2003
2004 if (!PyArg_Parse(val, "i", &height))
2005 return -1;
2006
2007#ifdef FEAT_GUI
2008 need_mouse_correct = TRUE;
2009#endif
2010 savewin = curwin;
2011 curwin = this->win;
2012 win_setheight(height);
2013 curwin = savewin;
2014
2015 /* Check for keyboard interrupts */
2016 if (VimErrorCheck())
2017 return -1;
2018
2019 return 0;
2020 }
2021#ifdef FEAT_VERTSPLIT
2022 else if (strcmp(name, "width") == 0)
2023 {
2024 int width;
2025 win_T *savewin;
2026
2027 if (!PyArg_Parse(val, "i", &width))
2028 return -1;
2029
2030#ifdef FEAT_GUI
2031 need_mouse_correct = TRUE;
2032#endif
2033 savewin = curwin;
2034 curwin = this->win;
2035 win_setwidth(width);
2036 curwin = savewin;
2037
2038 /* Check for keyboard interrupts */
2039 if (VimErrorCheck())
2040 return -1;
2041
2042 return 0;
2043 }
2044#endif
2045 else
2046 {
2047 PyErr_SetString(PyExc_AttributeError, name);
2048 return -1;
2049 }
2050}
2051
2052 static PyObject *
2053WindowRepr(PyObject *self)
2054{
2055 static char repr[100];
2056 WindowObject *this = (WindowObject *)(self);
2057
2058 if (this->win == INVALID_WINDOW_VALUE)
2059 {
2060 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2061 return PyString_FromString(repr);
2062 }
2063 else
2064 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002065 int w = get_win_number(this->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002066
Bram Moolenaar6d216452013-05-12 19:00:41 +02002067 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002068 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2069 (self));
2070 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002071 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002072
2073 return PyString_FromString(repr);
2074 }
2075}
2076
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002077static struct PyMethodDef WindowMethods[] = {
2078 /* name, function, calling, documentation */
2079 { NULL, NULL, 0, NULL }
2080};
2081
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002082/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002083 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002084 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002085
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002086static PyTypeObject WinListType;
2087static PySequenceMethods WinListAsSeq;
2088
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002089typedef struct
2090{
2091 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002092 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002093} WinListObject;
2094
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002095 static PyObject *
2096WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002097{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002098 WinListObject *self;
2099
2100 self = PyObject_NEW(WinListObject, &WinListType);
2101 self->tabObject = tabObject;
2102 Py_INCREF(tabObject);
2103
2104 return (PyObject *)(self);
2105}
2106
2107 static void
2108WinListDestructor(PyObject *self)
2109{
2110 TabPageObject *tabObject = ((WinListObject *)(self))->tabObject;
2111
2112 if (tabObject)
2113 Py_DECREF((PyObject *)(tabObject));
2114
2115 DESTRUCTOR_FINISH(self);
2116}
2117
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002118 static PyInt
2119WinListLength(PyObject *self)
2120{
2121 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002122 PyInt n = 0;
2123
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002124 if (!(w = get_firstwin(((WinListObject *)(self))->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002125 return -1;
2126
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002127 while (w != NULL)
2128 {
2129 ++n;
2130 w = W_NEXT(w);
2131 }
2132
2133 return n;
2134}
2135
2136 static PyObject *
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002137WinListItem(PyObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002138{
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002139 WinListObject *this = ((WinListObject *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002140 win_T *w;
2141
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002142 if (!(w = get_firstwin(this->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002143 return NULL;
2144
2145 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002146 if (n == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002147 return WindowNew(w, this->tabObject? this->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002148
2149 PyErr_SetString(PyExc_IndexError, _("no such window"));
2150 return NULL;
2151}
2152
2153/* Convert a Python string into a Vim line.
2154 *
2155 * The result is in allocated memory. All internal nulls are replaced by
2156 * newline characters. It is an error for the string to contain newline
2157 * characters.
2158 *
2159 * On errors, the Python exception data is set, and NULL is returned.
2160 */
2161 static char *
2162StringToLine(PyObject *obj)
2163{
2164 const char *str;
2165 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002166 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002167 PyInt len;
2168 PyInt i;
2169 char *p;
2170
2171 if (obj == NULL || !PyString_Check(obj))
2172 {
2173 PyErr_BadArgument();
2174 return NULL;
2175 }
2176
Bram Moolenaar19e60942011-06-19 00:27:51 +02002177 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2178 str = PyString_AsString(bytes);
2179 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002180
2181 /*
2182 * Error checking: String must not contain newlines, as we
2183 * are replacing a single line, and we must replace it with
2184 * a single line.
2185 * A trailing newline is removed, so that append(f.readlines()) works.
2186 */
2187 p = memchr(str, '\n', len);
2188 if (p != NULL)
2189 {
2190 if (p == str + len - 1)
2191 --len;
2192 else
2193 {
2194 PyErr_SetVim(_("string cannot contain newlines"));
2195 return NULL;
2196 }
2197 }
2198
2199 /* Create a copy of the string, with internal nulls replaced by
2200 * newline characters, as is the vim convention.
2201 */
2202 save = (char *)alloc((unsigned)(len+1));
2203 if (save == NULL)
2204 {
2205 PyErr_NoMemory();
2206 return NULL;
2207 }
2208
2209 for (i = 0; i < len; ++i)
2210 {
2211 if (str[i] == '\0')
2212 save[i] = '\n';
2213 else
2214 save[i] = str[i];
2215 }
2216
2217 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002218 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002219
2220 return save;
2221}
2222
2223/* Get a line from the specified buffer. The line number is
2224 * in Vim format (1-based). The line is returned as a Python
2225 * string object.
2226 */
2227 static PyObject *
2228GetBufferLine(buf_T *buf, PyInt n)
2229{
2230 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2231}
2232
2233
2234/* Get a list of lines from the specified buffer. The line numbers
2235 * are in Vim format (1-based). The range is from lo up to, but not
2236 * including, hi. The list is returned as a Python list of string objects.
2237 */
2238 static PyObject *
2239GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2240{
2241 PyInt i;
2242 PyInt n = hi - lo;
2243 PyObject *list = PyList_New(n);
2244
2245 if (list == NULL)
2246 return NULL;
2247
2248 for (i = 0; i < n; ++i)
2249 {
2250 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2251
2252 /* Error check - was the Python string creation OK? */
2253 if (str == NULL)
2254 {
2255 Py_DECREF(list);
2256 return NULL;
2257 }
2258
2259 /* Set the list item */
2260 if (PyList_SetItem(list, i, str))
2261 {
2262 Py_DECREF(str);
2263 Py_DECREF(list);
2264 return NULL;
2265 }
2266 }
2267
2268 /* The ownership of the Python list is passed to the caller (ie,
2269 * the caller should Py_DECREF() the object when it is finished
2270 * with it).
2271 */
2272
2273 return list;
2274}
2275
2276/*
2277 * Check if deleting lines made the cursor position invalid.
2278 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2279 * deleted).
2280 */
2281 static void
2282py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2283{
2284 if (curwin->w_cursor.lnum >= lo)
2285 {
2286 /* Adjust the cursor position if it's in/after the changed
2287 * lines. */
2288 if (curwin->w_cursor.lnum >= hi)
2289 {
2290 curwin->w_cursor.lnum += extra;
2291 check_cursor_col();
2292 }
2293 else if (extra < 0)
2294 {
2295 curwin->w_cursor.lnum = lo;
2296 check_cursor();
2297 }
2298 else
2299 check_cursor_col();
2300 changed_cline_bef_curs();
2301 }
2302 invalidate_botline();
2303}
2304
Bram Moolenaar19e60942011-06-19 00:27:51 +02002305/*
2306 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002307 * in Vim format (1-based). The replacement line is given as
2308 * a Python string object. The object is checked for validity
2309 * and correct format. Errors are returned as a value of FAIL.
2310 * The return value is OK on success.
2311 * If OK is returned and len_change is not NULL, *len_change
2312 * is set to the change in the buffer length.
2313 */
2314 static int
2315SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2316{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002317 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002318 * There are three cases:
2319 * 1. NULL, or None - this is a deletion.
2320 * 2. A string - this is a replacement.
2321 * 3. Anything else - this is an error.
2322 */
2323 if (line == Py_None || line == NULL)
2324 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002325 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002326
2327 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002328 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002329
2330 if (u_savedel((linenr_T)n, 1L) == FAIL)
2331 PyErr_SetVim(_("cannot save undo information"));
2332 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2333 PyErr_SetVim(_("cannot delete line"));
2334 else
2335 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002336 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002337 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2338 deleted_lines_mark((linenr_T)n, 1L);
2339 }
2340
Bram Moolenaar105bc352013-05-17 16:03:57 +02002341 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002342
2343 if (PyErr_Occurred() || VimErrorCheck())
2344 return FAIL;
2345
2346 if (len_change)
2347 *len_change = -1;
2348
2349 return OK;
2350 }
2351 else if (PyString_Check(line))
2352 {
2353 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002354 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002355
2356 if (save == NULL)
2357 return FAIL;
2358
2359 /* We do not need to free "save" if ml_replace() consumes it. */
2360 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002361 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002362
2363 if (u_savesub((linenr_T)n) == FAIL)
2364 {
2365 PyErr_SetVim(_("cannot save undo information"));
2366 vim_free(save);
2367 }
2368 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2369 {
2370 PyErr_SetVim(_("cannot replace line"));
2371 vim_free(save);
2372 }
2373 else
2374 changed_bytes((linenr_T)n, 0);
2375
Bram Moolenaar105bc352013-05-17 16:03:57 +02002376 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002377
2378 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002379 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002380 check_cursor_col();
2381
2382 if (PyErr_Occurred() || VimErrorCheck())
2383 return FAIL;
2384
2385 if (len_change)
2386 *len_change = 0;
2387
2388 return OK;
2389 }
2390 else
2391 {
2392 PyErr_BadArgument();
2393 return FAIL;
2394 }
2395}
2396
Bram Moolenaar19e60942011-06-19 00:27:51 +02002397/* Replace a range of lines in the specified buffer. The line numbers are in
2398 * Vim format (1-based). The range is from lo up to, but not including, hi.
2399 * The replacement lines are given as a Python list of string objects. The
2400 * list is checked for validity and correct format. Errors are returned as a
2401 * value of FAIL. The return value is OK on success.
2402 * If OK is returned and len_change is not NULL, *len_change
2403 * is set to the change in the buffer length.
2404 */
2405 static int
2406SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2407{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002408 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002409 * There are three cases:
2410 * 1. NULL, or None - this is a deletion.
2411 * 2. A list - this is a replacement.
2412 * 3. Anything else - this is an error.
2413 */
2414 if (list == Py_None || list == NULL)
2415 {
2416 PyInt i;
2417 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002418 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002419
2420 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002421 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002422
2423 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2424 PyErr_SetVim(_("cannot save undo information"));
2425 else
2426 {
2427 for (i = 0; i < n; ++i)
2428 {
2429 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2430 {
2431 PyErr_SetVim(_("cannot delete line"));
2432 break;
2433 }
2434 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002435 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002436 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2437 deleted_lines_mark((linenr_T)lo, (long)i);
2438 }
2439
Bram Moolenaar105bc352013-05-17 16:03:57 +02002440 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002441
2442 if (PyErr_Occurred() || VimErrorCheck())
2443 return FAIL;
2444
2445 if (len_change)
2446 *len_change = -n;
2447
2448 return OK;
2449 }
2450 else if (PyList_Check(list))
2451 {
2452 PyInt i;
2453 PyInt new_len = PyList_Size(list);
2454 PyInt old_len = hi - lo;
2455 PyInt extra = 0; /* lines added to text, can be negative */
2456 char **array;
2457 buf_T *savebuf;
2458
2459 if (new_len == 0) /* avoid allocating zero bytes */
2460 array = NULL;
2461 else
2462 {
2463 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
2464 if (array == NULL)
2465 {
2466 PyErr_NoMemory();
2467 return FAIL;
2468 }
2469 }
2470
2471 for (i = 0; i < new_len; ++i)
2472 {
2473 PyObject *line = PyList_GetItem(list, i);
2474
2475 array[i] = StringToLine(line);
2476 if (array[i] == NULL)
2477 {
2478 while (i)
2479 vim_free(array[--i]);
2480 vim_free(array);
2481 return FAIL;
2482 }
2483 }
2484
Bram Moolenaar19e60942011-06-19 00:27:51 +02002485 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002486
2487 // START of region without "return". Must call restore_buffer()!
2488 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002489
2490 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2491 PyErr_SetVim(_("cannot save undo information"));
2492
2493 /* If the size of the range is reducing (ie, new_len < old_len) we
2494 * need to delete some old_len. We do this at the start, by
2495 * repeatedly deleting line "lo".
2496 */
2497 if (!PyErr_Occurred())
2498 {
2499 for (i = 0; i < old_len - new_len; ++i)
2500 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2501 {
2502 PyErr_SetVim(_("cannot delete line"));
2503 break;
2504 }
2505 extra -= i;
2506 }
2507
2508 /* For as long as possible, replace the existing old_len with the
2509 * new old_len. This is a more efficient operation, as it requires
2510 * less memory allocation and freeing.
2511 */
2512 if (!PyErr_Occurred())
2513 {
2514 for (i = 0; i < old_len && i < new_len; ++i)
2515 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2516 == FAIL)
2517 {
2518 PyErr_SetVim(_("cannot replace line"));
2519 break;
2520 }
2521 }
2522 else
2523 i = 0;
2524
2525 /* Now we may need to insert the remaining new old_len. If we do, we
2526 * must free the strings as we finish with them (we can't pass the
2527 * responsibility to vim in this case).
2528 */
2529 if (!PyErr_Occurred())
2530 {
2531 while (i < new_len)
2532 {
2533 if (ml_append((linenr_T)(lo + i - 1),
2534 (char_u *)array[i], 0, FALSE) == FAIL)
2535 {
2536 PyErr_SetVim(_("cannot insert line"));
2537 break;
2538 }
2539 vim_free(array[i]);
2540 ++i;
2541 ++extra;
2542 }
2543 }
2544
2545 /* Free any left-over old_len, as a result of an error */
2546 while (i < new_len)
2547 {
2548 vim_free(array[i]);
2549 ++i;
2550 }
2551
2552 /* Free the array of old_len. All of its contents have now
2553 * been dealt with (either freed, or the responsibility passed
2554 * to vim.
2555 */
2556 vim_free(array);
2557
2558 /* Adjust marks. Invalidate any which lie in the
2559 * changed range, and move any in the remainder of the buffer.
2560 */
2561 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2562 (long)MAXLNUM, (long)extra);
2563 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2564
Bram Moolenaar105bc352013-05-17 16:03:57 +02002565 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002566 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2567
Bram Moolenaar105bc352013-05-17 16:03:57 +02002568 // END of region without "return".
2569 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002570
2571 if (PyErr_Occurred() || VimErrorCheck())
2572 return FAIL;
2573
2574 if (len_change)
2575 *len_change = new_len - old_len;
2576
2577 return OK;
2578 }
2579 else
2580 {
2581 PyErr_BadArgument();
2582 return FAIL;
2583 }
2584}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002585
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002586/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002587 * The line number is in Vim format (1-based). The lines to be inserted are
2588 * given as a Python list of string objects or as a single string. The lines
2589 * to be added are checked for validity and correct format. Errors are
2590 * returned as a value of FAIL. The return value is OK on success.
2591 * If OK is returned and len_change is not NULL, *len_change
2592 * is set to the change in the buffer length.
2593 */
2594 static int
2595InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2596{
2597 /* First of all, we check the type of the supplied Python object.
2598 * It must be a string or a list, or the call is in error.
2599 */
2600 if (PyString_Check(lines))
2601 {
2602 char *str = StringToLine(lines);
2603 buf_T *savebuf;
2604
2605 if (str == NULL)
2606 return FAIL;
2607
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002608 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002609 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002610
2611 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2612 PyErr_SetVim(_("cannot save undo information"));
2613 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2614 PyErr_SetVim(_("cannot insert line"));
2615 else
2616 appended_lines_mark((linenr_T)n, 1L);
2617
2618 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002619 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002620 update_screen(VALID);
2621
2622 if (PyErr_Occurred() || VimErrorCheck())
2623 return FAIL;
2624
2625 if (len_change)
2626 *len_change = 1;
2627
2628 return OK;
2629 }
2630 else if (PyList_Check(lines))
2631 {
2632 PyInt i;
2633 PyInt size = PyList_Size(lines);
2634 char **array;
2635 buf_T *savebuf;
2636
2637 array = (char **)alloc((unsigned)(size * sizeof(char *)));
2638 if (array == NULL)
2639 {
2640 PyErr_NoMemory();
2641 return FAIL;
2642 }
2643
2644 for (i = 0; i < size; ++i)
2645 {
2646 PyObject *line = PyList_GetItem(lines, i);
2647 array[i] = StringToLine(line);
2648
2649 if (array[i] == NULL)
2650 {
2651 while (i)
2652 vim_free(array[--i]);
2653 vim_free(array);
2654 return FAIL;
2655 }
2656 }
2657
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002658 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002659 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002660
2661 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2662 PyErr_SetVim(_("cannot save undo information"));
2663 else
2664 {
2665 for (i = 0; i < size; ++i)
2666 {
2667 if (ml_append((linenr_T)(n + i),
2668 (char_u *)array[i], 0, FALSE) == FAIL)
2669 {
2670 PyErr_SetVim(_("cannot insert line"));
2671
2672 /* Free the rest of the lines */
2673 while (i < size)
2674 vim_free(array[i++]);
2675
2676 break;
2677 }
2678 vim_free(array[i]);
2679 }
2680 if (i > 0)
2681 appended_lines_mark((linenr_T)n, (long)i);
2682 }
2683
2684 /* Free the array of lines. All of its contents have now
2685 * been freed.
2686 */
2687 vim_free(array);
2688
Bram Moolenaar105bc352013-05-17 16:03:57 +02002689 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002690 update_screen(VALID);
2691
2692 if (PyErr_Occurred() || VimErrorCheck())
2693 return FAIL;
2694
2695 if (len_change)
2696 *len_change = size;
2697
2698 return OK;
2699 }
2700 else
2701 {
2702 PyErr_BadArgument();
2703 return FAIL;
2704 }
2705}
2706
2707/*
2708 * Common routines for buffers and line ranges
2709 * -------------------------------------------
2710 */
2711
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002712typedef struct
2713{
2714 PyObject_HEAD
2715 buf_T *buf;
2716} BufferObject;
2717
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002718 static int
2719CheckBuffer(BufferObject *this)
2720{
2721 if (this->buf == INVALID_BUFFER_VALUE)
2722 {
2723 PyErr_SetVim(_("attempt to refer to deleted buffer"));
2724 return -1;
2725 }
2726
2727 return 0;
2728}
2729
2730 static PyObject *
2731RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
2732{
2733 if (CheckBuffer(self))
2734 return NULL;
2735
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002736 if (end == -1)
2737 end = self->buf->b_ml.ml_line_count;
2738
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002739 if (n < 0)
2740 n += end - start + 1;
2741
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002742 if (n < 0 || n > end - start)
2743 {
2744 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2745 return NULL;
2746 }
2747
2748 return GetBufferLine(self->buf, n+start);
2749}
2750
2751 static PyObject *
2752RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
2753{
2754 PyInt size;
2755
2756 if (CheckBuffer(self))
2757 return NULL;
2758
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002759 if (end == -1)
2760 end = self->buf->b_ml.ml_line_count;
2761
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002762 size = end - start + 1;
2763
2764 if (lo < 0)
2765 lo = 0;
2766 else if (lo > size)
2767 lo = size;
2768 if (hi < 0)
2769 hi = 0;
2770 if (hi < lo)
2771 hi = lo;
2772 else if (hi > size)
2773 hi = size;
2774
2775 return GetBufferLineList(self->buf, lo+start, hi+start);
2776}
2777
2778 static PyInt
2779RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2780{
2781 PyInt len_change;
2782
2783 if (CheckBuffer(self))
2784 return -1;
2785
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002786 if (end == -1)
2787 end = self->buf->b_ml.ml_line_count;
2788
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002789 if (n < 0)
2790 n += end - start + 1;
2791
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002792 if (n < 0 || n > end - start)
2793 {
2794 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2795 return -1;
2796 }
2797
2798 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
2799 return -1;
2800
2801 if (new_end)
2802 *new_end = end + len_change;
2803
2804 return 0;
2805}
2806
Bram Moolenaar19e60942011-06-19 00:27:51 +02002807 static PyInt
2808RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2809{
2810 PyInt size;
2811 PyInt len_change;
2812
2813 /* Self must be a valid buffer */
2814 if (CheckBuffer(self))
2815 return -1;
2816
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002817 if (end == -1)
2818 end = self->buf->b_ml.ml_line_count;
2819
Bram Moolenaar19e60942011-06-19 00:27:51 +02002820 /* Sort out the slice range */
2821 size = end - start + 1;
2822
2823 if (lo < 0)
2824 lo = 0;
2825 else if (lo > size)
2826 lo = size;
2827 if (hi < 0)
2828 hi = 0;
2829 if (hi < lo)
2830 hi = lo;
2831 else if (hi > size)
2832 hi = size;
2833
2834 if (SetBufferLineList(self->buf, lo + start, hi + start,
2835 val, &len_change) == FAIL)
2836 return -1;
2837
2838 if (new_end)
2839 *new_end = end + len_change;
2840
2841 return 0;
2842}
2843
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002844
2845 static PyObject *
2846RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
2847{
2848 PyObject *lines;
2849 PyInt len_change;
2850 PyInt max;
2851 PyInt n;
2852
2853 if (CheckBuffer(self))
2854 return NULL;
2855
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002856 if (end == -1)
2857 end = self->buf->b_ml.ml_line_count;
2858
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002859 max = n = end - start + 1;
2860
2861 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
2862 return NULL;
2863
2864 if (n < 0 || n > max)
2865 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002866 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002867 return NULL;
2868 }
2869
2870 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
2871 return NULL;
2872
2873 if (new_end)
2874 *new_end = end + len_change;
2875
2876 Py_INCREF(Py_None);
2877 return Py_None;
2878}
2879
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002880/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002881 */
2882
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002883static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002884static PySequenceMethods RangeAsSeq;
2885static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002886
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002887typedef struct
2888{
2889 PyObject_HEAD
2890 BufferObject *buf;
2891 PyInt start;
2892 PyInt end;
2893} RangeObject;
2894
2895 static PyObject *
2896RangeNew(buf_T *buf, PyInt start, PyInt end)
2897{
2898 BufferObject *bufr;
2899 RangeObject *self;
2900 self = PyObject_NEW(RangeObject, &RangeType);
2901 if (self == NULL)
2902 return NULL;
2903
2904 bufr = (BufferObject *)BufferNew(buf);
2905 if (bufr == NULL)
2906 {
2907 Py_DECREF(self);
2908 return NULL;
2909 }
2910 Py_INCREF(bufr);
2911
2912 self->buf = bufr;
2913 self->start = start;
2914 self->end = end;
2915
2916 return (PyObject *)(self);
2917}
2918
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002919 static void
2920RangeDestructor(PyObject *self)
2921{
2922 Py_DECREF(((RangeObject *)(self))->buf);
2923 DESTRUCTOR_FINISH(self);
2924}
2925
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002926 static PyInt
2927RangeLength(PyObject *self)
2928{
2929 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
2930 if (CheckBuffer(((RangeObject *)(self))->buf))
2931 return -1; /* ??? */
2932
2933 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1);
2934}
2935
2936 static PyObject *
2937RangeItem(PyObject *self, PyInt n)
2938{
2939 return RBItem(((RangeObject *)(self))->buf, n,
2940 ((RangeObject *)(self))->start,
2941 ((RangeObject *)(self))->end);
2942}
2943
2944 static PyObject *
2945RangeSlice(PyObject *self, PyInt lo, PyInt hi)
2946{
2947 return RBSlice(((RangeObject *)(self))->buf, lo, hi,
2948 ((RangeObject *)(self))->start,
2949 ((RangeObject *)(self))->end);
2950}
2951
2952 static PyObject *
2953RangeAppend(PyObject *self, PyObject *args)
2954{
2955 return RBAppend(((RangeObject *)(self))->buf, args,
2956 ((RangeObject *)(self))->start,
2957 ((RangeObject *)(self))->end,
2958 &((RangeObject *)(self))->end);
2959}
2960
2961 static PyObject *
2962RangeRepr(PyObject *self)
2963{
2964 static char repr[100];
2965 RangeObject *this = (RangeObject *)(self);
2966
2967 if (this->buf->buf == INVALID_BUFFER_VALUE)
2968 {
2969 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
2970 (self));
2971 return PyString_FromString(repr);
2972 }
2973 else
2974 {
2975 char *name = (char *)this->buf->buf->b_fname;
2976 int len;
2977
2978 if (name == NULL)
2979 name = "";
2980 len = (int)strlen(name);
2981
2982 if (len > 45)
2983 name = name + (45 - len);
2984
2985 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
2986 len > 45 ? "..." : "", name,
2987 this->start, this->end);
2988
2989 return PyString_FromString(repr);
2990 }
2991}
2992
2993static struct PyMethodDef RangeMethods[] = {
2994 /* name, function, calling, documentation */
2995 {"append", RangeAppend, 1, "Append data to the Vim range" },
2996 { NULL, NULL, 0, NULL }
2997};
2998
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002999static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003000static PySequenceMethods BufferAsSeq;
3001static PyMappingMethods BufferAsMapping;
3002
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003003 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003004BufferNew(buf_T *buf)
3005{
3006 /* We need to handle deletion of buffers underneath us.
3007 * If we add a "b_python*_ref" field to the buf_T structure,
3008 * then we can get at it in buf_freeall() in vim. We then
3009 * need to create only ONE Python object per buffer - if
3010 * we try to create a second, just INCREF the existing one
3011 * and return it. The (single) Python object referring to
3012 * the buffer is stored in "b_python*_ref".
3013 * Question: what to do on a buf_freeall(). We'll probably
3014 * have to either delete the Python object (DECREF it to
3015 * zero - a bad idea, as it leaves dangling refs!) or
3016 * set the buf_T * value to an invalid value (-1?), which
3017 * means we need checks in all access functions... Bah.
3018 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003019 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003020 * b_python_ref and b_python3_ref fields respectively.
3021 */
3022
3023 BufferObject *self;
3024
3025 if (BUF_PYTHON_REF(buf) != NULL)
3026 {
3027 self = BUF_PYTHON_REF(buf);
3028 Py_INCREF(self);
3029 }
3030 else
3031 {
3032 self = PyObject_NEW(BufferObject, &BufferType);
3033 if (self == NULL)
3034 return NULL;
3035 self->buf = buf;
3036 BUF_PYTHON_REF(buf) = self;
3037 }
3038
3039 return (PyObject *)(self);
3040}
3041
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003042 static void
3043BufferDestructor(PyObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003044{
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003045 BufferObject *this = (BufferObject *)(self);
3046
3047 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
3048 BUF_PYTHON_REF(this->buf) = NULL;
3049
3050 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003051}
3052
Bram Moolenaar971db462013-05-12 18:44:48 +02003053 static PyInt
3054BufferLength(PyObject *self)
3055{
3056 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
3057 if (CheckBuffer((BufferObject *)(self)))
3058 return -1; /* ??? */
3059
3060 return (PyInt)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
3061}
3062
3063 static PyObject *
3064BufferItem(PyObject *self, PyInt n)
3065{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003066 return RBItem((BufferObject *)(self), n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003067}
3068
3069 static PyObject *
3070BufferSlice(PyObject *self, PyInt lo, PyInt hi)
3071{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003072 return RBSlice((BufferObject *)(self), lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003073}
3074
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003075 static PyObject *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003076BufferAttr(BufferObject *this, char *name)
3077{
3078 if (strcmp(name, "name") == 0)
3079 return Py_BuildValue("s", this->buf->b_ffname);
3080 else if (strcmp(name, "number") == 0)
3081 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
3082 else if (strcmp(name, "vars") == 0)
3083 return DictionaryNew(this->buf->b_vars);
3084 else if (strcmp(name, "options") == 0)
3085 return OptionsNew(SREQ_BUF, this->buf, (checkfun) CheckBuffer,
3086 (PyObject *) this);
3087 else if (strcmp(name,"__members__") == 0)
3088 return Py_BuildValue("[ssss]", "name", "number", "vars", "options");
3089 else
3090 return NULL;
3091}
3092
3093 static PyObject *
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003094BufferAppend(PyObject *self, PyObject *args)
3095{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003096 return RBAppend((BufferObject *)(self), args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003097}
3098
3099 static PyObject *
3100BufferMark(PyObject *self, PyObject *args)
3101{
3102 pos_T *posp;
3103 char *pmark;
3104 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003105 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003106
3107 if (CheckBuffer((BufferObject *)(self)))
3108 return NULL;
3109
3110 if (!PyArg_ParseTuple(args, "s", &pmark))
3111 return NULL;
3112 mark = *pmark;
3113
Bram Moolenaar105bc352013-05-17 16:03:57 +02003114 switch_buffer(&savebuf, ((BufferObject *)(self))->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003115 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003116 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003117
3118 if (posp == NULL)
3119 {
3120 PyErr_SetVim(_("invalid mark name"));
3121 return NULL;
3122 }
3123
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003124 /* Check for keyboard interrupt */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003125 if (VimErrorCheck())
3126 return NULL;
3127
3128 if (posp->lnum <= 0)
3129 {
3130 /* Or raise an error? */
3131 Py_INCREF(Py_None);
3132 return Py_None;
3133 }
3134
3135 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3136}
3137
3138 static PyObject *
3139BufferRange(PyObject *self, PyObject *args)
3140{
3141 PyInt start;
3142 PyInt end;
3143
3144 if (CheckBuffer((BufferObject *)(self)))
3145 return NULL;
3146
3147 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3148 return NULL;
3149
3150 return RangeNew(((BufferObject *)(self))->buf, start, end);
3151}
3152
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003153 static PyObject *
3154BufferRepr(PyObject *self)
3155{
3156 static char repr[100];
3157 BufferObject *this = (BufferObject *)(self);
3158
3159 if (this->buf == INVALID_BUFFER_VALUE)
3160 {
3161 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3162 return PyString_FromString(repr);
3163 }
3164 else
3165 {
3166 char *name = (char *)this->buf->b_fname;
3167 PyInt len;
3168
3169 if (name == NULL)
3170 name = "";
3171 len = strlen(name);
3172
3173 if (len > 35)
3174 name = name + (35 - len);
3175
3176 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3177
3178 return PyString_FromString(repr);
3179 }
3180}
3181
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003182static struct PyMethodDef BufferMethods[] = {
3183 /* name, function, calling, documentation */
3184 {"append", BufferAppend, 1, "Append data to Vim buffer" },
3185 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" },
3186 {"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 +01003187#if PY_VERSION_HEX >= 0x03000000
3188 {"__dir__", BufferDir, 4, "List its attributes" },
3189#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003190 { NULL, NULL, 0, NULL }
3191};
3192
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003193/*
3194 * Buffer list object - Implementation
3195 */
3196
3197static PyTypeObject BufMapType;
3198
3199typedef struct
3200{
3201 PyObject_HEAD
3202} BufMapObject;
3203
3204 static PyInt
3205BufMapLength(PyObject *self UNUSED)
3206{
3207 buf_T *b = firstbuf;
3208 PyInt n = 0;
3209
3210 while (b)
3211 {
3212 ++n;
3213 b = b->b_next;
3214 }
3215
3216 return n;
3217}
3218
3219 static PyObject *
3220BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3221{
3222 buf_T *b;
3223 int bnr;
3224
3225#if PY_MAJOR_VERSION < 3
3226 if (PyInt_Check(keyObject))
3227 bnr = PyInt_AsLong(keyObject);
3228 else
3229#endif
3230 if (PyLong_Check(keyObject))
3231 bnr = PyLong_AsLong(keyObject);
3232 else
3233 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003234 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003235 return NULL;
3236 }
3237
3238 b = buflist_findnr(bnr);
3239
3240 if (b)
3241 return BufferNew(b);
3242 else
3243 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003244 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003245 return NULL;
3246 }
3247}
3248
3249 static void
3250BufMapIterDestruct(PyObject *buffer)
3251{
3252 /* Iteration was stopped before all buffers were processed */
3253 if (buffer)
3254 {
3255 Py_DECREF(buffer);
3256 }
3257}
3258
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003259 static int
3260BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3261{
3262 Py_VISIT(buffer);
3263 return 0;
3264}
3265
3266 static int
3267BufMapIterClear(PyObject **buffer)
3268{
3269 Py_CLEAR(*buffer);
3270 return 0;
3271}
3272
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003273 static PyObject *
3274BufMapIterNext(PyObject **buffer)
3275{
3276 PyObject *next;
3277 PyObject *r;
3278
3279 if (!*buffer)
3280 return NULL;
3281
3282 r = *buffer;
3283
3284 if (CheckBuffer((BufferObject *)(r)))
3285 {
3286 *buffer = NULL;
3287 return NULL;
3288 }
3289
3290 if (!((BufferObject *)(r))->buf->b_next)
3291 next = NULL;
3292 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3293 return NULL;
3294 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02003295 /* Do not increment reference: we no longer hold it (decref), but whoever
3296 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003297 return r;
3298}
3299
3300 static PyObject *
3301BufMapIter(PyObject *self UNUSED)
3302{
3303 PyObject *buffer;
3304
3305 buffer = BufferNew(firstbuf);
3306 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003307 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3308 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003309}
3310
3311static PyMappingMethods BufMapAsMapping = {
3312 (lenfunc) BufMapLength,
3313 (binaryfunc) BufMapItem,
3314 (objobjargproc) 0,
3315};
3316
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003317/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003318 */
3319
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003320 static PyObject *
3321CurrentGetattr(PyObject *self UNUSED, char *name)
3322{
3323 if (strcmp(name, "buffer") == 0)
3324 return (PyObject *)BufferNew(curbuf);
3325 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003326 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003327 else if (strcmp(name, "tabpage") == 0)
3328 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003329 else if (strcmp(name, "line") == 0)
3330 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3331 else if (strcmp(name, "range") == 0)
3332 return RangeNew(curbuf, RangeStart, RangeEnd);
3333 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003334 return Py_BuildValue("[sssss]", "buffer", "window", "line", "range",
3335 "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003336 else
3337 {
3338 PyErr_SetString(PyExc_AttributeError, name);
3339 return NULL;
3340 }
3341}
3342
3343 static int
3344CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3345{
3346 if (strcmp(name, "line") == 0)
3347 {
3348 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3349 return -1;
3350
3351 return 0;
3352 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003353 else if (strcmp(name, "buffer") == 0)
3354 {
3355 int count;
3356
3357 if (value->ob_type != &BufferType)
3358 {
3359 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3360 return -1;
3361 }
3362
3363 if (CheckBuffer((BufferObject *)(value)))
3364 return -1;
3365 count = ((BufferObject *)(value))->buf->b_fnum;
3366
3367 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3368 {
3369 PyErr_SetVim(_("failed to switch to given buffer"));
3370 return -1;
3371 }
3372
3373 return 0;
3374 }
3375 else if (strcmp(name, "window") == 0)
3376 {
3377 int count;
3378
3379 if (value->ob_type != &WindowType)
3380 {
3381 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3382 return -1;
3383 }
3384
3385 if (CheckWindow((WindowObject *)(value)))
3386 return -1;
3387 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3388
3389 if (!count)
3390 {
3391 PyErr_SetString(PyExc_ValueError,
3392 _("failed to find window in the current tab page"));
3393 return -1;
3394 }
3395
3396 win_goto(((WindowObject *)(value))->win);
3397 if (((WindowObject *)(value))->win != curwin)
3398 {
3399 PyErr_SetString(PyExc_RuntimeError,
3400 _("did not switch to the specified window"));
3401 return -1;
3402 }
3403
3404 return 0;
3405 }
3406 else if (strcmp(name, "tabpage") == 0)
3407 {
3408 if (value->ob_type != &TabPageType)
3409 {
3410 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3411 return -1;
3412 }
3413
3414 if (CheckTabPage((TabPageObject *)(value)))
3415 return -1;
3416
3417 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3418 if (((TabPageObject *)(value))->tab != curtab)
3419 {
3420 PyErr_SetString(PyExc_RuntimeError,
3421 _("did not switch to the specified tab page"));
3422 return -1;
3423 }
3424
3425 return 0;
3426 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003427 else
3428 {
3429 PyErr_SetString(PyExc_AttributeError, name);
3430 return -1;
3431 }
3432}
3433
Bram Moolenaardb913952012-06-29 12:54:53 +02003434 static void
3435set_ref_in_py(const int copyID)
3436{
3437 pylinkedlist_T *cur;
3438 dict_T *dd;
3439 list_T *ll;
3440
3441 if (lastdict != NULL)
3442 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3443 {
3444 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3445 if (dd->dv_copyID != copyID)
3446 {
3447 dd->dv_copyID = copyID;
3448 set_ref_in_ht(&dd->dv_hashtab, copyID);
3449 }
3450 }
3451
3452 if (lastlist != NULL)
3453 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3454 {
3455 ll = ((ListObject *) (cur->pll_obj))->list;
3456 if (ll->lv_copyID != copyID)
3457 {
3458 ll->lv_copyID = copyID;
3459 set_ref_in_list(ll, copyID);
3460 }
3461 }
3462}
3463
3464 static int
3465set_string_copy(char_u *str, typval_T *tv)
3466{
3467 tv->vval.v_string = vim_strsave(str);
3468 if (tv->vval.v_string == NULL)
3469 {
3470 PyErr_NoMemory();
3471 return -1;
3472 }
3473 return 0;
3474}
3475
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003476 static int
3477pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3478{
3479 dict_T *d;
3480 char_u *key;
3481 dictitem_T *di;
3482 PyObject *keyObject;
3483 PyObject *valObject;
3484 Py_ssize_t iter = 0;
3485
3486 d = dict_alloc();
3487 if (d == NULL)
3488 {
3489 PyErr_NoMemory();
3490 return -1;
3491 }
3492
3493 tv->v_type = VAR_DICT;
3494 tv->vval.v_dict = d;
3495
3496 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
3497 {
3498 DICTKEY_DECL
3499
3500 if (keyObject == NULL)
3501 return -1;
3502 if (valObject == NULL)
3503 return -1;
3504
3505 DICTKEY_GET_NOTEMPTY(-1)
3506
3507 di = dictitem_alloc(key);
3508
3509 DICTKEY_UNREF
3510
3511 if (di == NULL)
3512 {
3513 PyErr_NoMemory();
3514 return -1;
3515 }
3516 di->di_tv.v_lock = 0;
3517
3518 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3519 {
3520 vim_free(di);
3521 return -1;
3522 }
3523 if (dict_add(d, di) == FAIL)
3524 {
3525 vim_free(di);
3526 PyErr_SetVim(_("failed to add key to dictionary"));
3527 return -1;
3528 }
3529 }
3530 return 0;
3531}
3532
3533 static int
3534pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3535{
3536 dict_T *d;
3537 char_u *key;
3538 dictitem_T *di;
3539 PyObject *list;
3540 PyObject *litem;
3541 PyObject *keyObject;
3542 PyObject *valObject;
3543 Py_ssize_t lsize;
3544
3545 d = dict_alloc();
3546 if (d == NULL)
3547 {
3548 PyErr_NoMemory();
3549 return -1;
3550 }
3551
3552 tv->v_type = VAR_DICT;
3553 tv->vval.v_dict = d;
3554
3555 list = PyMapping_Items(obj);
3556 if (list == NULL)
3557 return -1;
3558 lsize = PyList_Size(list);
3559 while (lsize--)
3560 {
3561 DICTKEY_DECL
3562
3563 litem = PyList_GetItem(list, lsize);
3564 if (litem == NULL)
3565 {
3566 Py_DECREF(list);
3567 return -1;
3568 }
3569
3570 keyObject = PyTuple_GetItem(litem, 0);
3571 if (keyObject == NULL)
3572 {
3573 Py_DECREF(list);
3574 Py_DECREF(litem);
3575 return -1;
3576 }
3577
3578 DICTKEY_GET_NOTEMPTY(-1)
3579
3580 valObject = PyTuple_GetItem(litem, 1);
3581 if (valObject == NULL)
3582 {
3583 Py_DECREF(list);
3584 Py_DECREF(litem);
3585 return -1;
3586 }
3587
3588 di = dictitem_alloc(key);
3589
3590 DICTKEY_UNREF
3591
3592 if (di == NULL)
3593 {
3594 Py_DECREF(list);
3595 Py_DECREF(litem);
3596 PyErr_NoMemory();
3597 return -1;
3598 }
3599 di->di_tv.v_lock = 0;
3600
3601 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3602 {
3603 vim_free(di);
3604 Py_DECREF(list);
3605 Py_DECREF(litem);
3606 return -1;
3607 }
3608 if (dict_add(d, di) == FAIL)
3609 {
3610 vim_free(di);
3611 Py_DECREF(list);
3612 Py_DECREF(litem);
3613 PyErr_SetVim(_("failed to add key to dictionary"));
3614 return -1;
3615 }
3616 Py_DECREF(litem);
3617 }
3618 Py_DECREF(list);
3619 return 0;
3620}
3621
3622 static int
3623pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3624{
3625 list_T *l;
3626
3627 l = list_alloc();
3628 if (l == NULL)
3629 {
3630 PyErr_NoMemory();
3631 return -1;
3632 }
3633
3634 tv->v_type = VAR_LIST;
3635 tv->vval.v_list = l;
3636
3637 if (list_py_concat(l, obj, lookupDict) == -1)
3638 return -1;
3639
3640 return 0;
3641}
3642
3643 static int
3644pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3645{
3646 PyObject *iterator = PyObject_GetIter(obj);
3647 PyObject *item;
3648 list_T *l;
3649 listitem_T *li;
3650
3651 l = list_alloc();
3652
3653 if (l == NULL)
3654 {
3655 PyErr_NoMemory();
3656 return -1;
3657 }
3658
3659 tv->vval.v_list = l;
3660 tv->v_type = VAR_LIST;
3661
3662
3663 if (iterator == NULL)
3664 return -1;
3665
3666 while ((item = PyIter_Next(obj)))
3667 {
3668 li = listitem_alloc();
3669 if (li == NULL)
3670 {
3671 PyErr_NoMemory();
3672 return -1;
3673 }
3674 li->li_tv.v_lock = 0;
3675
3676 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
3677 return -1;
3678
3679 list_append(l, li);
3680
3681 Py_DECREF(item);
3682 }
3683
3684 Py_DECREF(iterator);
3685 return 0;
3686}
3687
Bram Moolenaardb913952012-06-29 12:54:53 +02003688typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
3689
3690 static int
3691convert_dl(PyObject *obj, typval_T *tv,
3692 pytotvfunc py_to_tv, PyObject *lookupDict)
3693{
3694 PyObject *capsule;
3695 char hexBuf[sizeof(void *) * 2 + 3];
3696
3697 sprintf(hexBuf, "%p", obj);
3698
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003699# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003700 capsule = PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003701# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003702 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003703# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02003704 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02003705 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003706# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003707 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02003708# else
3709 capsule = PyCObject_FromVoidPtr(tv, NULL);
3710# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003711 PyDict_SetItemString(lookupDict, hexBuf, capsule);
3712 Py_DECREF(capsule);
3713 if (py_to_tv(obj, tv, lookupDict) == -1)
3714 {
3715 tv->v_type = VAR_UNKNOWN;
3716 return -1;
3717 }
3718 /* As we are not using copy_tv which increments reference count we must
3719 * do it ourself. */
3720 switch(tv->v_type)
3721 {
3722 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
3723 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
3724 }
3725 }
3726 else
3727 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003728 typval_T *v;
3729
3730# ifdef PY_USE_CAPSULE
3731 v = PyCapsule_GetPointer(capsule, NULL);
3732# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003733 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003734# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003735 copy_tv(v, tv);
3736 }
3737 return 0;
3738}
3739
3740 static int
3741ConvertFromPyObject(PyObject *obj, typval_T *tv)
3742{
3743 PyObject *lookup_dict;
3744 int r;
3745
3746 lookup_dict = PyDict_New();
3747 r = _ConvertFromPyObject(obj, tv, lookup_dict);
3748 Py_DECREF(lookup_dict);
3749 return r;
3750}
3751
3752 static int
3753_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3754{
3755 if (obj->ob_type == &DictionaryType)
3756 {
3757 tv->v_type = VAR_DICT;
3758 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
3759 ++tv->vval.v_dict->dv_refcount;
3760 }
3761 else if (obj->ob_type == &ListType)
3762 {
3763 tv->v_type = VAR_LIST;
3764 tv->vval.v_list = (((ListObject *)(obj))->list);
3765 ++tv->vval.v_list->lv_refcount;
3766 }
3767 else if (obj->ob_type == &FunctionType)
3768 {
3769 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
3770 return -1;
3771
3772 tv->v_type = VAR_FUNC;
3773 func_ref(tv->vval.v_string);
3774 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003775 else if (PyBytes_Check(obj))
3776 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003777 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02003778
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003779 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
3780 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003781 if (result == NULL)
3782 return -1;
3783
3784 if (set_string_copy(result, tv) == -1)
3785 return -1;
3786
3787 tv->v_type = VAR_STRING;
3788 }
3789 else if (PyUnicode_Check(obj))
3790 {
3791 PyObject *bytes;
3792 char_u *result;
3793
Bram Moolenaardb913952012-06-29 12:54:53 +02003794 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
3795 if (bytes == NULL)
3796 return -1;
3797
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003798 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
3799 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003800 if (result == NULL)
3801 return -1;
3802
3803 if (set_string_copy(result, tv) == -1)
3804 {
3805 Py_XDECREF(bytes);
3806 return -1;
3807 }
3808 Py_XDECREF(bytes);
3809
3810 tv->v_type = VAR_STRING;
3811 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02003812#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02003813 else if (PyInt_Check(obj))
3814 {
3815 tv->v_type = VAR_NUMBER;
3816 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
3817 }
3818#endif
3819 else if (PyLong_Check(obj))
3820 {
3821 tv->v_type = VAR_NUMBER;
3822 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
3823 }
3824 else if (PyDict_Check(obj))
3825 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
3826#ifdef FEAT_FLOAT
3827 else if (PyFloat_Check(obj))
3828 {
3829 tv->v_type = VAR_FLOAT;
3830 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
3831 }
3832#endif
3833 else if (PyIter_Check(obj))
3834 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
3835 else if (PySequence_Check(obj))
3836 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
3837 else if (PyMapping_Check(obj))
3838 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
3839 else
3840 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003841 PyErr_SetString(PyExc_TypeError,
3842 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02003843 return -1;
3844 }
3845 return 0;
3846}
3847
3848 static PyObject *
3849ConvertToPyObject(typval_T *tv)
3850{
3851 if (tv == NULL)
3852 {
3853 PyErr_SetVim(_("NULL reference passed"));
3854 return NULL;
3855 }
3856 switch (tv->v_type)
3857 {
3858 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02003859 return PyBytes_FromString(tv->vval.v_string == NULL
3860 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02003861 case VAR_NUMBER:
3862 return PyLong_FromLong((long) tv->vval.v_number);
3863#ifdef FEAT_FLOAT
3864 case VAR_FLOAT:
3865 return PyFloat_FromDouble((double) tv->vval.v_float);
3866#endif
3867 case VAR_LIST:
3868 return ListNew(tv->vval.v_list);
3869 case VAR_DICT:
3870 return DictionaryNew(tv->vval.v_dict);
3871 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02003872 return FunctionNew(tv->vval.v_string == NULL
3873 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02003874 case VAR_UNKNOWN:
3875 Py_INCREF(Py_None);
3876 return Py_None;
3877 default:
3878 PyErr_SetVim(_("internal error: invalid value type"));
3879 return NULL;
3880 }
3881}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003882
3883typedef struct
3884{
3885 PyObject_HEAD
3886} CurrentObject;
3887static PyTypeObject CurrentType;
3888
3889 static void
3890init_structs(void)
3891{
3892 vim_memset(&OutputType, 0, sizeof(OutputType));
3893 OutputType.tp_name = "vim.message";
3894 OutputType.tp_basicsize = sizeof(OutputObject);
3895 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
3896 OutputType.tp_doc = "vim message object";
3897 OutputType.tp_methods = OutputMethods;
3898#if PY_MAJOR_VERSION >= 3
3899 OutputType.tp_getattro = OutputGetattro;
3900 OutputType.tp_setattro = OutputSetattro;
3901 OutputType.tp_alloc = call_PyType_GenericAlloc;
3902 OutputType.tp_new = call_PyType_GenericNew;
3903 OutputType.tp_free = call_PyObject_Free;
3904#else
3905 OutputType.tp_getattr = OutputGetattr;
3906 OutputType.tp_setattr = OutputSetattr;
3907#endif
3908
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003909 vim_memset(&IterType, 0, sizeof(IterType));
3910 IterType.tp_name = "vim.iter";
3911 IterType.tp_basicsize = sizeof(IterObject);
3912 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
3913 IterType.tp_doc = "generic iterator object";
3914 IterType.tp_iter = IterIter;
3915 IterType.tp_iternext = IterNext;
Bram Moolenaar2cd73622013-05-15 19:07:47 +02003916 IterType.tp_dealloc = IterDestructor;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003917 IterType.tp_traverse = IterTraverse;
3918 IterType.tp_clear = IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003919
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003920 vim_memset(&BufferType, 0, sizeof(BufferType));
3921 BufferType.tp_name = "vim.buffer";
3922 BufferType.tp_basicsize = sizeof(BufferType);
3923 BufferType.tp_dealloc = BufferDestructor;
3924 BufferType.tp_repr = BufferRepr;
3925 BufferType.tp_as_sequence = &BufferAsSeq;
3926 BufferType.tp_as_mapping = &BufferAsMapping;
3927 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
3928 BufferType.tp_doc = "vim buffer object";
3929 BufferType.tp_methods = BufferMethods;
3930#if PY_MAJOR_VERSION >= 3
3931 BufferType.tp_getattro = BufferGetattro;
3932 BufferType.tp_alloc = call_PyType_GenericAlloc;
3933 BufferType.tp_new = call_PyType_GenericNew;
3934 BufferType.tp_free = call_PyObject_Free;
3935#else
3936 BufferType.tp_getattr = BufferGetattr;
3937#endif
3938
3939 vim_memset(&WindowType, 0, sizeof(WindowType));
3940 WindowType.tp_name = "vim.window";
3941 WindowType.tp_basicsize = sizeof(WindowObject);
3942 WindowType.tp_dealloc = WindowDestructor;
3943 WindowType.tp_repr = WindowRepr;
3944 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
3945 WindowType.tp_doc = "vim Window object";
3946 WindowType.tp_methods = WindowMethods;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003947 WindowType.tp_traverse = WindowTraverse;
3948 WindowType.tp_clear = WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003949#if PY_MAJOR_VERSION >= 3
3950 WindowType.tp_getattro = WindowGetattro;
3951 WindowType.tp_setattro = WindowSetattro;
3952 WindowType.tp_alloc = call_PyType_GenericAlloc;
3953 WindowType.tp_new = call_PyType_GenericNew;
3954 WindowType.tp_free = call_PyObject_Free;
3955#else
3956 WindowType.tp_getattr = WindowGetattr;
3957 WindowType.tp_setattr = WindowSetattr;
3958#endif
3959
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003960 vim_memset(&TabPageType, 0, sizeof(TabPageType));
3961 TabPageType.tp_name = "vim.tabpage";
3962 TabPageType.tp_basicsize = sizeof(TabPageObject);
3963 TabPageType.tp_dealloc = TabPageDestructor;
3964 TabPageType.tp_repr = TabPageRepr;
3965 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
3966 TabPageType.tp_doc = "vim tab page object";
3967 TabPageType.tp_methods = TabPageMethods;
3968#if PY_MAJOR_VERSION >= 3
3969 TabPageType.tp_getattro = TabPageGetattro;
3970 TabPageType.tp_alloc = call_PyType_GenericAlloc;
3971 TabPageType.tp_new = call_PyType_GenericNew;
3972 TabPageType.tp_free = call_PyObject_Free;
3973#else
3974 TabPageType.tp_getattr = TabPageGetattr;
3975#endif
3976
Bram Moolenaardfa38d42013-05-15 13:38:47 +02003977 vim_memset(&BufMapType, 0, sizeof(BufMapType));
3978 BufMapType.tp_name = "vim.bufferlist";
3979 BufMapType.tp_basicsize = sizeof(BufMapObject);
3980 BufMapType.tp_as_mapping = &BufMapAsMapping;
3981 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003982 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003983 BufferType.tp_doc = "vim buffer list";
3984
3985 vim_memset(&WinListType, 0, sizeof(WinListType));
3986 WinListType.tp_name = "vim.windowlist";
3987 WinListType.tp_basicsize = sizeof(WinListType);
3988 WinListType.tp_as_sequence = &WinListAsSeq;
3989 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
3990 WinListType.tp_doc = "vim window list";
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003991 WinListType.tp_dealloc = WinListDestructor;
3992
3993 vim_memset(&TabListType, 0, sizeof(TabListType));
3994 TabListType.tp_name = "vim.tabpagelist";
3995 TabListType.tp_basicsize = sizeof(TabListType);
3996 TabListType.tp_as_sequence = &TabListAsSeq;
3997 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
3998 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003999
4000 vim_memset(&RangeType, 0, sizeof(RangeType));
4001 RangeType.tp_name = "vim.range";
4002 RangeType.tp_basicsize = sizeof(RangeObject);
4003 RangeType.tp_dealloc = RangeDestructor;
4004 RangeType.tp_repr = RangeRepr;
4005 RangeType.tp_as_sequence = &RangeAsSeq;
4006 RangeType.tp_as_mapping = &RangeAsMapping;
4007 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
4008 RangeType.tp_doc = "vim Range object";
4009 RangeType.tp_methods = RangeMethods;
4010#if PY_MAJOR_VERSION >= 3
4011 RangeType.tp_getattro = RangeGetattro;
4012 RangeType.tp_alloc = call_PyType_GenericAlloc;
4013 RangeType.tp_new = call_PyType_GenericNew;
4014 RangeType.tp_free = call_PyObject_Free;
4015#else
4016 RangeType.tp_getattr = RangeGetattr;
4017#endif
4018
4019 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4020 CurrentType.tp_name = "vim.currentdata";
4021 CurrentType.tp_basicsize = sizeof(CurrentObject);
4022 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4023 CurrentType.tp_doc = "vim current object";
4024#if PY_MAJOR_VERSION >= 3
4025 CurrentType.tp_getattro = CurrentGetattro;
4026 CurrentType.tp_setattro = CurrentSetattro;
4027#else
4028 CurrentType.tp_getattr = CurrentGetattr;
4029 CurrentType.tp_setattr = CurrentSetattr;
4030#endif
4031
4032 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4033 DictionaryType.tp_name = "vim.dictionary";
4034 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
4035 DictionaryType.tp_dealloc = DictionaryDestructor;
4036 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4037 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4038 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4039 DictionaryType.tp_methods = DictionaryMethods;
4040#if PY_MAJOR_VERSION >= 3
4041 DictionaryType.tp_getattro = DictionaryGetattro;
4042 DictionaryType.tp_setattro = DictionarySetattro;
4043#else
4044 DictionaryType.tp_getattr = DictionaryGetattr;
4045 DictionaryType.tp_setattr = DictionarySetattr;
4046#endif
4047
4048 vim_memset(&ListType, 0, sizeof(ListType));
4049 ListType.tp_name = "vim.list";
4050 ListType.tp_dealloc = ListDestructor;
4051 ListType.tp_basicsize = sizeof(ListObject);
4052 ListType.tp_as_sequence = &ListAsSeq;
4053 ListType.tp_as_mapping = &ListAsMapping;
4054 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4055 ListType.tp_doc = "list pushing modifications to vim structure";
4056 ListType.tp_methods = ListMethods;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004057 ListType.tp_iter = ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004058#if PY_MAJOR_VERSION >= 3
4059 ListType.tp_getattro = ListGetattro;
4060 ListType.tp_setattro = ListSetattro;
4061#else
4062 ListType.tp_getattr = ListGetattr;
4063 ListType.tp_setattr = ListSetattr;
4064#endif
4065
4066 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004067 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004068 FunctionType.tp_basicsize = sizeof(FunctionObject);
4069 FunctionType.tp_dealloc = FunctionDestructor;
4070 FunctionType.tp_call = FunctionCall;
4071 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4072 FunctionType.tp_doc = "object that calls vim function";
4073 FunctionType.tp_methods = FunctionMethods;
4074#if PY_MAJOR_VERSION >= 3
4075 FunctionType.tp_getattro = FunctionGetattro;
4076#else
4077 FunctionType.tp_getattr = FunctionGetattr;
4078#endif
4079
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004080 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4081 OptionsType.tp_name = "vim.options";
4082 OptionsType.tp_basicsize = sizeof(OptionsObject);
4083 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4084 OptionsType.tp_doc = "object for manipulating options";
4085 OptionsType.tp_as_mapping = &OptionsAsMapping;
4086 OptionsType.tp_dealloc = OptionsDestructor;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004087 OptionsType.tp_traverse = OptionsTraverse;
4088 OptionsType.tp_clear = OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004089
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004090#if PY_MAJOR_VERSION >= 3
4091 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4092 vimmodule.m_name = "vim";
4093 vimmodule.m_doc = "Vim Python interface\n";
4094 vimmodule.m_size = -1;
4095 vimmodule.m_methods = VimMethods;
4096#endif
4097}