blob: b658ce36780dff3aa838f5472aab96bba00d0a4d [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
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020033typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020034typedef void (*runner)(const char *, void *
35#ifdef PY_CAN_RECURSE
36 , PyGILState_STATE *
37#endif
38 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020039
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020040static int ConvertFromPyObject(PyObject *, typval_T *);
41static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020042static PyObject *WindowNew(win_T *, tabpage_T *);
43static PyObject *BufferNew (buf_T *);
44static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020045
46static PyInt RangeStart;
47static PyInt RangeEnd;
48
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020049static PyObject *globals;
50
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020051/*
52 * obtain a lock on the Vim data structures
53 */
54 static void
55Python_Lock_Vim(void)
56{
57}
58
59/*
60 * release a lock on the Vim data structures
61 */
62 static void
63Python_Release_Vim(void)
64{
65}
66
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020067/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020068 */
69
Bram Moolenaar2eea1982010-09-21 16:49:37 +020070/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020071typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072
73static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020074
75typedef struct
76{
77 PyObject_HEAD
78 long softspace;
79 long error;
80} OutputObject;
81
Bram Moolenaar77045652012-09-21 13:46:06 +020082 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +020083OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +020084{
85 if (val == NULL)
86 {
Bram Moolenaar8661b172013-05-15 15:44:28 +020087 PyErr_SetString(PyExc_AttributeError,
88 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +020089 return -1;
90 }
91
92 if (strcmp(name, "softspace") == 0)
93 {
94 if (!PyInt_Check(val))
95 {
96 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
97 return -1;
98 }
99
Bram Moolenaard6e39182013-05-21 18:30:34 +0200100 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200101 return 0;
102 }
103
104 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
105 return -1;
106}
107
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200108/* Buffer IO, we write one whole line at a time. */
109static garray_T io_ga = {0, 0, 1, 80, NULL};
110static writefn old_fn = NULL;
111
112 static void
113PythonIO_Flush(void)
114{
115 if (old_fn != NULL && io_ga.ga_len > 0)
116 {
117 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
118 old_fn((char_u *)io_ga.ga_data);
119 }
120 io_ga.ga_len = 0;
121}
122
123 static void
124writer(writefn fn, char_u *str, PyInt n)
125{
126 char_u *ptr;
127
128 /* Flush when switching output function. */
129 if (fn != old_fn)
130 PythonIO_Flush();
131 old_fn = fn;
132
133 /* Write each NL separated line. Text after the last NL is kept for
134 * writing later. */
135 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
136 {
137 PyInt len = ptr - str;
138
139 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
140 break;
141
142 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
143 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
144 fn((char_u *)io_ga.ga_data);
145 str = ptr + 1;
146 n -= len + 1;
147 io_ga.ga_len = 0;
148 }
149
150 /* Put the remaining text into io_ga for later printing. */
151 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
152 {
153 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
154 io_ga.ga_len += (int)n;
155 }
156}
157
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200158 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200159OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200160{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200161 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200162 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200163 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200164
Bram Moolenaar27564802011-09-07 19:30:21 +0200165 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200166 return NULL;
167
168 Py_BEGIN_ALLOW_THREADS
169 Python_Lock_Vim();
170 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
171 Python_Release_Vim();
172 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200173 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200174
175 Py_INCREF(Py_None);
176 return Py_None;
177}
178
179 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200180OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200181{
182 PyInt n;
183 PyInt i;
184 PyObject *list;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200185 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200186
187 if (!PyArg_ParseTuple(args, "O", &list))
188 return NULL;
189 Py_INCREF(list);
190
Bram Moolenaardb913952012-06-29 12:54:53 +0200191 if (!PyList_Check(list))
192 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200193 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
194 Py_DECREF(list);
195 return NULL;
196 }
197
198 n = PyList_Size(list);
199
200 for (i = 0; i < n; ++i)
201 {
202 PyObject *line = PyList_GetItem(list, i);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200203 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200204 PyInt len;
205
Bram Moolenaardb913952012-06-29 12:54:53 +0200206 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
207 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200208 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
209 Py_DECREF(list);
210 return NULL;
211 }
212
213 Py_BEGIN_ALLOW_THREADS
214 Python_Lock_Vim();
215 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
216 Python_Release_Vim();
217 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200218 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200219 }
220
221 Py_DECREF(list);
222 Py_INCREF(Py_None);
223 return Py_None;
224}
225
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100226 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200227OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100228{
229 /* do nothing */
230 Py_INCREF(Py_None);
231 return Py_None;
232}
233
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200234/***************/
235
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200236static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200237 /* name, function, calling, doc */
238 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
239 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
240 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
241 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200242};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200243
244static OutputObject Output =
245{
246 PyObject_HEAD_INIT(&OutputType)
247 0,
248 0
249};
250
251static OutputObject Error =
252{
253 PyObject_HEAD_INIT(&OutputType)
254 0,
255 1
256};
257
258 static int
259PythonIO_Init_io(void)
260{
261 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
262 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
263
264 if (PyErr_Occurred())
265 {
266 EMSG(_("E264: Python: Error initialising I/O objects"));
267 return -1;
268 }
269
270 return 0;
271}
272
273
274static PyObject *VimError;
275
276/* Check to see whether a Vim error has been reported, or a keyboard
277 * interrupt has been detected.
278 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200279
280 static void
281VimTryStart(void)
282{
283 ++trylevel;
284}
285
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200286 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200287VimTryEnd(void)
288{
289 --trylevel;
290 if (got_int)
291 {
292 PyErr_SetNone(PyExc_KeyboardInterrupt);
293 return 1;
294 }
295 else if (!did_throw)
296 return 0;
297 else if (PyErr_Occurred())
298 return 1;
299 else
300 {
301 PyErr_SetVim((char *) current_exception->value);
302 discard_current_exception();
303 return 1;
304 }
305}
306
307 static int
308VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200309{
310 if (got_int)
311 {
312 PyErr_SetNone(PyExc_KeyboardInterrupt);
313 return 1;
314 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315 return 0;
316}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200317
318/* Vim module - Implementation
319 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200320
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200321 static PyObject *
322VimCommand(PyObject *self UNUSED, PyObject *args)
323{
324 char *cmd;
325 PyObject *result;
326
327 if (!PyArg_ParseTuple(args, "s", &cmd))
328 return NULL;
329
330 PyErr_Clear();
331
332 Py_BEGIN_ALLOW_THREADS
333 Python_Lock_Vim();
334
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200335 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200336 do_cmdline_cmd((char_u *)cmd);
337 update_screen(VALID);
338
339 Python_Release_Vim();
340 Py_END_ALLOW_THREADS
341
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200342 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200343 result = NULL;
344 else
345 result = Py_None;
346
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200347
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200348 Py_XINCREF(result);
349 return result;
350}
351
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200352/*
353 * Function to translate a typval_T into a PyObject; this will recursively
354 * translate lists/dictionaries into their Python equivalents.
355 *
356 * The depth parameter is to avoid infinite recursion, set it to 1 when
357 * you call VimToPython.
358 */
359 static PyObject *
360VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
361{
362 PyObject *result;
363 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200364 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200365
366 /* Avoid infinite recursion */
367 if (depth > 100)
368 {
369 Py_INCREF(Py_None);
370 result = Py_None;
371 return result;
372 }
373
374 /* Check if we run into a recursive loop. The item must be in lookupDict
375 * then and we can use it again. */
376 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
377 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
378 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200379 sprintf(ptrBuf, "%p",
380 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
381 : (void *)our_tv->vval.v_dict);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200382 result = PyDict_GetItemString(lookupDict, ptrBuf);
383 if (result != NULL)
384 {
385 Py_INCREF(result);
386 return result;
387 }
388 }
389
390 if (our_tv->v_type == VAR_STRING)
391 {
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200392 result = Py_BuildValue("s", our_tv->vval.v_string == NULL
393 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200394 }
395 else if (our_tv->v_type == VAR_NUMBER)
396 {
397 char buf[NUMBUFLEN];
398
399 /* For backwards compatibility numbers are stored as strings. */
400 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
401 result = Py_BuildValue("s", buf);
402 }
403# ifdef FEAT_FLOAT
404 else if (our_tv->v_type == VAR_FLOAT)
405 {
406 char buf[NUMBUFLEN];
407
408 sprintf(buf, "%f", our_tv->vval.v_float);
409 result = Py_BuildValue("s", buf);
410 }
411# endif
412 else if (our_tv->v_type == VAR_LIST)
413 {
414 list_T *list = our_tv->vval.v_list;
415 listitem_T *curr;
416
417 result = PyList_New(0);
418
419 if (list != NULL)
420 {
421 PyDict_SetItemString(lookupDict, ptrBuf, result);
422
423 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
424 {
425 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
426 PyList_Append(result, newObj);
427 Py_DECREF(newObj);
428 }
429 }
430 }
431 else if (our_tv->v_type == VAR_DICT)
432 {
433 result = PyDict_New();
434
435 if (our_tv->vval.v_dict != NULL)
436 {
437 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
438 long_u todo = ht->ht_used;
439 hashitem_T *hi;
440 dictitem_T *di;
441
442 PyDict_SetItemString(lookupDict, ptrBuf, result);
443
444 for (hi = ht->ht_array; todo > 0; ++hi)
445 {
446 if (!HASHITEM_EMPTY(hi))
447 {
448 --todo;
449
450 di = dict_lookup(hi);
451 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
452 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
453 Py_DECREF(newObj);
454 }
455 }
456 }
457 }
458 else
459 {
460 Py_INCREF(Py_None);
461 result = Py_None;
462 }
463
464 return result;
465}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200466
467 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200468VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200469{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200470 char *expr;
471 typval_T *our_tv;
472 PyObject *result;
473 PyObject *lookup_dict;
474
475 if (!PyArg_ParseTuple(args, "s", &expr))
476 return NULL;
477
478 Py_BEGIN_ALLOW_THREADS
479 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200480 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200481 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200482 Python_Release_Vim();
483 Py_END_ALLOW_THREADS
484
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200485 if (VimTryEnd())
486 return NULL;
487
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200488 if (our_tv == NULL)
489 {
490 PyErr_SetVim(_("invalid expression"));
491 return NULL;
492 }
493
494 /* Convert the Vim type into a Python type. Create a dictionary that's
495 * used to check for recursive loops. */
496 lookup_dict = PyDict_New();
497 result = VimToPython(our_tv, 1, lookup_dict);
498 Py_DECREF(lookup_dict);
499
500
501 Py_BEGIN_ALLOW_THREADS
502 Python_Lock_Vim();
503 free_tv(our_tv);
504 Python_Release_Vim();
505 Py_END_ALLOW_THREADS
506
507 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200508}
509
Bram Moolenaardb913952012-06-29 12:54:53 +0200510static PyObject *ConvertToPyObject(typval_T *);
511
512 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200513VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200514{
Bram Moolenaardb913952012-06-29 12:54:53 +0200515 char *expr;
516 typval_T *our_tv;
517 PyObject *result;
518
519 if (!PyArg_ParseTuple(args, "s", &expr))
520 return NULL;
521
522 Py_BEGIN_ALLOW_THREADS
523 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200524 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200525 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200526 Python_Release_Vim();
527 Py_END_ALLOW_THREADS
528
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200529 if (VimTryEnd())
530 return NULL;
531
Bram Moolenaardb913952012-06-29 12:54:53 +0200532 if (our_tv == NULL)
533 {
534 PyErr_SetVim(_("invalid expression"));
535 return NULL;
536 }
537
538 result = ConvertToPyObject(our_tv);
539 Py_BEGIN_ALLOW_THREADS
540 Python_Lock_Vim();
541 free_tv(our_tv);
542 Python_Release_Vim();
543 Py_END_ALLOW_THREADS
544
545 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200546}
547
548 static PyObject *
549VimStrwidth(PyObject *self UNUSED, PyObject *args)
550{
551 char *expr;
552
553 if (!PyArg_ParseTuple(args, "s", &expr))
554 return NULL;
555
Bram Moolenaara54bf402012-12-05 16:30:07 +0100556 return PyLong_FromLong(
557#ifdef FEAT_MBYTE
558 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
559#else
560 STRLEN(expr)
561#endif
562 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200563}
564
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200565/*
566 * Vim module - Definitions
567 */
568
569static struct PyMethodDef VimMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200570 /* name, function, calling, documentation */
571 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
572 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
573 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
574 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
575 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200576};
577
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200578/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200579 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200580 */
581
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200582static PyTypeObject IterType;
583
584typedef PyObject *(*nextfun)(void **);
585typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200586typedef int (*traversefun)(void *, visitproc, void *);
587typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200588
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200589/* Main purpose of this object is removing the need for do python
590 * initialization (i.e. PyType_Ready and setting type attributes) for a big
591 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200592
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200593typedef struct
594{
595 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200596 void *cur;
597 nextfun next;
598 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200599 traversefun traverse;
600 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200601} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200602
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200603 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200604IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
605 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200606{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200607 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200608
Bram Moolenaar774267b2013-05-21 20:51:59 +0200609 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200610 self->cur = start;
611 self->next = next;
612 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200613 self->traverse = traverse;
614 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200615
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200616 return (PyObject *)(self);
617}
618
619 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200620IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200621{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200622 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200623 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200624 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200625}
626
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200627 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200628IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200629{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200630 if (self->traverse != NULL)
631 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200632 else
633 return 0;
634}
635
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200636/* Mac OSX defines clear() somewhere. */
637#ifdef clear
638# undef clear
639#endif
640
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200641 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200642IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200643{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200644 if (self->clear != NULL)
645 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200646 else
647 return 0;
648}
649
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200650 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200651IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200652{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200653 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200654}
655
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200656 static PyObject *
657IterIter(PyObject *self)
658{
659 return self;
660}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200661
Bram Moolenaardb913952012-06-29 12:54:53 +0200662typedef struct pylinkedlist_S {
663 struct pylinkedlist_S *pll_next;
664 struct pylinkedlist_S *pll_prev;
665 PyObject *pll_obj;
666} pylinkedlist_T;
667
668static pylinkedlist_T *lastdict = NULL;
669static pylinkedlist_T *lastlist = NULL;
670
671 static void
672pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
673{
674 if (ref->pll_prev == NULL)
675 {
676 if (ref->pll_next == NULL)
677 {
678 *last = NULL;
679 return;
680 }
681 }
682 else
683 ref->pll_prev->pll_next = ref->pll_next;
684
685 if (ref->pll_next == NULL)
686 *last = ref->pll_prev;
687 else
688 ref->pll_next->pll_prev = ref->pll_prev;
689}
690
691 static void
692pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
693{
694 if (*last == NULL)
695 ref->pll_prev = NULL;
696 else
697 {
698 (*last)->pll_next = ref;
699 ref->pll_prev = *last;
700 }
701 ref->pll_next = NULL;
702 ref->pll_obj = self;
703 *last = ref;
704}
705
706static PyTypeObject DictionaryType;
707
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200708#define DICTKEY_GET_NOTEMPTY(err) \
709 DICTKEY_GET(err) \
710 if (*key == NUL) \
711 { \
712 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
713 return err; \
714 }
715
Bram Moolenaardb913952012-06-29 12:54:53 +0200716typedef struct
717{
718 PyObject_HEAD
719 dict_T *dict;
720 pylinkedlist_T ref;
721} DictionaryObject;
722
723 static PyObject *
724DictionaryNew(dict_T *dict)
725{
726 DictionaryObject *self;
727
728 self = PyObject_NEW(DictionaryObject, &DictionaryType);
729 if (self == NULL)
730 return NULL;
731 self->dict = dict;
732 ++dict->dv_refcount;
733
734 pyll_add((PyObject *)(self), &self->ref, &lastdict);
735
736 return (PyObject *)(self);
737}
738
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200739 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200740DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200741{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200742 pyll_remove(&self->ref, &lastdict);
743 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200744
745 DESTRUCTOR_FINISH(self);
746}
747
Bram Moolenaardb913952012-06-29 12:54:53 +0200748 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200749DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200750{
751 if (val == NULL)
752 {
753 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
754 return -1;
755 }
756
757 if (strcmp(name, "locked") == 0)
758 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200759 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200760 {
761 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
762 return -1;
763 }
764 else
765 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200766 int istrue = PyObject_IsTrue(val);
767 if (istrue == -1)
768 return -1;
769 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200770 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200771 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200772 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200773 }
774 return 0;
775 }
776 else
777 {
778 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
779 return -1;
780 }
781}
782
783 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200784DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200785{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200786 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +0200787}
788
789 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200790DictionaryItem(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200791{
792 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200793 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200794 DICTKEY_DECL
795
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200796 DICTKEY_GET_NOTEMPTY(NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200797
Bram Moolenaard6e39182013-05-21 18:30:34 +0200798 di = dict_find(self->dict, key, -1);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200799
Bram Moolenaar696c2112012-09-21 13:43:14 +0200800 DICTKEY_UNREF
801
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200802 if (di == NULL)
803 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200804 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200805 return NULL;
806 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200807
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200808 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200809}
810
811 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200812DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200813{
814 char_u *key;
815 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200816 dict_T *d = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +0200817 dictitem_T *di;
818 DICTKEY_DECL
819
820 if (d->dv_lock)
821 {
822 PyErr_SetVim(_("dict is locked"));
823 return -1;
824 }
825
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200826 DICTKEY_GET_NOTEMPTY(-1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200827
828 di = dict_find(d, key, -1);
829
830 if (valObject == NULL)
831 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200832 hashitem_T *hi;
833
Bram Moolenaardb913952012-06-29 12:54:53 +0200834 if (di == NULL)
835 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200836 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200837 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +0200838 return -1;
839 }
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200840 hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaardb913952012-06-29 12:54:53 +0200841 hash_remove(&d->dv_hashtab, hi);
842 dictitem_free(di);
843 return 0;
844 }
845
846 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200847 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +0200848
849 if (di == NULL)
850 {
851 di = dictitem_alloc(key);
852 if (di == NULL)
853 {
854 PyErr_NoMemory();
855 return -1;
856 }
857 di->di_tv.v_lock = 0;
858
859 if (dict_add(d, di) == FAIL)
860 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200861 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +0200862 vim_free(di);
863 PyErr_SetVim(_("failed to add key to dictionary"));
864 return -1;
865 }
866 }
867 else
868 clear_tv(&di->di_tv);
869
870 DICTKEY_UNREF
871
872 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +0200873 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200874 return 0;
875}
876
877 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200878DictionaryListKeys(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200879{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200880 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +0200881 long_u todo = dict->dv_hashtab.ht_used;
882 Py_ssize_t i = 0;
883 PyObject *r;
884 hashitem_T *hi;
885
886 r = PyList_New(todo);
887 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
888 {
889 if (!HASHITEM_EMPTY(hi))
890 {
891 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
892 --todo;
893 ++i;
894 }
895 }
896 return r;
897}
898
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200899static PyMappingMethods DictionaryAsMapping = {
900 (lenfunc) DictionaryLength,
901 (binaryfunc) DictionaryItem,
902 (objobjargproc) DictionaryAssItem,
903};
904
Bram Moolenaardb913952012-06-29 12:54:53 +0200905static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200906 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
907 { NULL, NULL, 0, NULL }
Bram Moolenaardb913952012-06-29 12:54:53 +0200908};
909
910static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200911static PySequenceMethods ListAsSeq;
912static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +0200913
914typedef struct
915{
916 PyObject_HEAD
917 list_T *list;
918 pylinkedlist_T ref;
919} ListObject;
920
921 static PyObject *
922ListNew(list_T *list)
923{
924 ListObject *self;
925
926 self = PyObject_NEW(ListObject, &ListType);
927 if (self == NULL)
928 return NULL;
929 self->list = list;
930 ++list->lv_refcount;
931
932 pyll_add((PyObject *)(self), &self->ref, &lastlist);
933
934 return (PyObject *)(self);
935}
936
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200937 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200938ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200939{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200940 pyll_remove(&self->ref, &lastlist);
941 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200942
943 DESTRUCTOR_FINISH(self);
944}
945
Bram Moolenaardb913952012-06-29 12:54:53 +0200946 static int
947list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict)
948{
949 Py_ssize_t i;
950 Py_ssize_t lsize = PySequence_Size(obj);
951 PyObject *litem;
952 listitem_T *li;
953
954 for(i=0; i<lsize; i++)
955 {
956 li = listitem_alloc();
957 if (li == NULL)
958 {
959 PyErr_NoMemory();
960 return -1;
961 }
962 li->li_tv.v_lock = 0;
963
964 litem = PySequence_GetItem(obj, i);
965 if (litem == NULL)
966 return -1;
967 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1)
968 return -1;
969
970 list_append(l, li);
971 }
972 return 0;
973}
974
Bram Moolenaardb913952012-06-29 12:54:53 +0200975 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200976ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200977{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200978 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +0200979}
980
981 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200982ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +0200983{
984 listitem_T *li;
985
Bram Moolenaard6e39182013-05-21 18:30:34 +0200986 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +0200987 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200988 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200989 return NULL;
990 }
Bram Moolenaard6e39182013-05-21 18:30:34 +0200991 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +0200992 if (li == NULL)
993 {
994 PyErr_SetVim(_("internal error: failed to get vim list item"));
995 return NULL;
996 }
997 return ConvertToPyObject(&li->li_tv);
998}
999
1000#define PROC_RANGE \
1001 if (last < 0) {\
1002 if (last < -size) \
1003 last = 0; \
1004 else \
1005 last += size; \
1006 } \
1007 if (first < 0) \
1008 first = 0; \
1009 if (first > size) \
1010 first = size; \
1011 if (last > size) \
1012 last = size;
1013
1014 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001015ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001016{
1017 PyInt i;
1018 PyInt size = ListLength(self);
1019 PyInt n;
1020 PyObject *list;
1021 int reversed = 0;
1022
1023 PROC_RANGE
1024 if (first >= last)
1025 first = last;
1026
1027 n = last-first;
1028 list = PyList_New(n);
1029 if (list == NULL)
1030 return NULL;
1031
1032 for (i = 0; i < n; ++i)
1033 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001034 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001035 if (item == NULL)
1036 {
1037 Py_DECREF(list);
1038 return NULL;
1039 }
1040
1041 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1042 {
1043 Py_DECREF(item);
1044 Py_DECREF(list);
1045 return NULL;
1046 }
1047 }
1048
1049 return list;
1050}
1051
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001052typedef struct
1053{
1054 listwatch_T lw;
1055 list_T *list;
1056} listiterinfo_T;
1057
1058 static void
1059ListIterDestruct(listiterinfo_T *lii)
1060{
1061 list_rem_watch(lii->list, &lii->lw);
1062 PyMem_Free(lii);
1063}
1064
1065 static PyObject *
1066ListIterNext(listiterinfo_T **lii)
1067{
1068 PyObject *r;
1069
1070 if (!((*lii)->lw.lw_item))
1071 return NULL;
1072
1073 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1074 return NULL;
1075
1076 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1077
1078 return r;
1079}
1080
1081 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001082ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001083{
1084 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001085 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001086
1087 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1088 {
1089 PyErr_NoMemory();
1090 return NULL;
1091 }
1092
1093 list_add_watch(l, &lii->lw);
1094 lii->lw.lw_item = l->lv_first;
1095 lii->list = l;
1096
1097 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001098 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1099 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001100}
1101
Bram Moolenaardb913952012-06-29 12:54:53 +02001102 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001103ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001104{
1105 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001106 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001107 listitem_T *li;
1108 Py_ssize_t length = ListLength(self);
1109
1110 if (l->lv_lock)
1111 {
1112 PyErr_SetVim(_("list is locked"));
1113 return -1;
1114 }
1115 if (index>length || (index==length && obj==NULL))
1116 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001117 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001118 return -1;
1119 }
1120
1121 if (obj == NULL)
1122 {
1123 li = list_find(l, (long) index);
1124 list_remove(l, li, li);
1125 clear_tv(&li->li_tv);
1126 vim_free(li);
1127 return 0;
1128 }
1129
1130 if (ConvertFromPyObject(obj, &tv) == -1)
1131 return -1;
1132
1133 if (index == length)
1134 {
1135 if (list_append_tv(l, &tv) == FAIL)
1136 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001137 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001138 PyErr_SetVim(_("Failed to add item to list"));
1139 return -1;
1140 }
1141 }
1142 else
1143 {
1144 li = list_find(l, (long) index);
1145 clear_tv(&li->li_tv);
1146 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001147 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001148 }
1149 return 0;
1150}
1151
1152 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001153ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001154{
1155 PyInt size = ListLength(self);
1156 Py_ssize_t i;
1157 Py_ssize_t lsize;
1158 PyObject *litem;
1159 listitem_T *li;
1160 listitem_T *next;
1161 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001162 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001163
1164 if (l->lv_lock)
1165 {
1166 PyErr_SetVim(_("list is locked"));
1167 return -1;
1168 }
1169
1170 PROC_RANGE
1171
1172 if (first == size)
1173 li = NULL;
1174 else
1175 {
1176 li = list_find(l, (long) first);
1177 if (li == NULL)
1178 {
1179 PyErr_SetVim(_("internal error: no vim list item"));
1180 return -1;
1181 }
1182 if (last > first)
1183 {
1184 i = last - first;
1185 while (i-- && li != NULL)
1186 {
1187 next = li->li_next;
1188 listitem_remove(l, li);
1189 li = next;
1190 }
1191 }
1192 }
1193
1194 if (obj == NULL)
1195 return 0;
1196
1197 if (!PyList_Check(obj))
1198 {
1199 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1200 return -1;
1201 }
1202
1203 lsize = PyList_Size(obj);
1204
1205 for(i=0; i<lsize; i++)
1206 {
1207 litem = PyList_GetItem(obj, i);
1208 if (litem == NULL)
1209 return -1;
1210 if (ConvertFromPyObject(litem, &v) == -1)
1211 return -1;
1212 if (list_insert_tv(l, &v, li) == FAIL)
1213 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001214 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001215 PyErr_SetVim(_("internal error: failed to add item to list"));
1216 return -1;
1217 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001218 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001219 }
1220 return 0;
1221}
1222
1223 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001224ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001225{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001226 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001227 PyObject *lookup_dict;
1228
1229 if (l->lv_lock)
1230 {
1231 PyErr_SetVim(_("list is locked"));
1232 return NULL;
1233 }
1234
1235 if (!PySequence_Check(obj))
1236 {
1237 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1238 return NULL;
1239 }
1240
1241 lookup_dict = PyDict_New();
1242 if (list_py_concat(l, obj, lookup_dict) == -1)
1243 {
1244 Py_DECREF(lookup_dict);
1245 return NULL;
1246 }
1247 Py_DECREF(lookup_dict);
1248
1249 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001250 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001251}
1252
Bram Moolenaar66b79852012-09-21 14:00:35 +02001253 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001254ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001255{
1256 if (val == NULL)
1257 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001258 PyErr_SetString(PyExc_AttributeError,
1259 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001260 return -1;
1261 }
1262
1263 if (strcmp(name, "locked") == 0)
1264 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001265 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001266 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001267 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001268 return -1;
1269 }
1270 else
1271 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001272 int istrue = PyObject_IsTrue(val);
1273 if (istrue == -1)
1274 return -1;
1275 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001276 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001277 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001278 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001279 }
1280 return 0;
1281 }
1282 else
1283 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001284 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001285 return -1;
1286 }
1287}
1288
Bram Moolenaardb913952012-06-29 12:54:53 +02001289static struct PyMethodDef ListMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001290 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1291 { NULL, NULL, 0, NULL }
Bram Moolenaardb913952012-06-29 12:54:53 +02001292};
1293
1294typedef struct
1295{
1296 PyObject_HEAD
1297 char_u *name;
1298} FunctionObject;
1299
1300static PyTypeObject FunctionType;
1301
1302 static PyObject *
1303FunctionNew(char_u *name)
1304{
1305 FunctionObject *self;
1306
1307 self = PyObject_NEW(FunctionObject, &FunctionType);
1308 if (self == NULL)
1309 return NULL;
1310 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1311 if (self->name == NULL)
1312 {
1313 PyErr_NoMemory();
1314 return NULL;
1315 }
1316 STRCPY(self->name, name);
1317 func_ref(name);
1318 return (PyObject *)(self);
1319}
1320
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001321 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001322FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001323{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001324 func_unref(self->name);
1325 PyMem_Free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001326
1327 DESTRUCTOR_FINISH(self);
1328}
1329
Bram Moolenaardb913952012-06-29 12:54:53 +02001330 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001331FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02001332{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001333 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02001334 typval_T args;
1335 typval_T selfdicttv;
1336 typval_T rettv;
1337 dict_T *selfdict = NULL;
1338 PyObject *selfdictObject;
1339 PyObject *result;
1340 int error;
1341
1342 if (ConvertFromPyObject(argsObject, &args) == -1)
1343 return NULL;
1344
1345 if (kwargs != NULL)
1346 {
1347 selfdictObject = PyDict_GetItemString(kwargs, "self");
1348 if (selfdictObject != NULL)
1349 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001350 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001351 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001352 PyErr_SetString(PyExc_TypeError,
1353 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001354 clear_tv(&args);
1355 return NULL;
1356 }
1357 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001358 {
1359 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02001360 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001361 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001362 selfdict = selfdicttv.vval.v_dict;
1363 }
1364 }
1365
Bram Moolenaar71700b82013-05-15 17:49:05 +02001366 Py_BEGIN_ALLOW_THREADS
1367 Python_Lock_Vim();
1368
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001369 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02001370 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001371
1372 Python_Release_Vim();
1373 Py_END_ALLOW_THREADS
1374
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001375 if (VimTryEnd())
1376 result = NULL;
1377 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02001378 {
1379 result = NULL;
1380 PyErr_SetVim(_("failed to run function"));
1381 }
1382 else
1383 result = ConvertToPyObject(&rettv);
1384
Bram Moolenaardb913952012-06-29 12:54:53 +02001385 clear_tv(&args);
1386 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001387 if (selfdict != NULL)
1388 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001389
1390 return result;
1391}
1392
1393static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001394 {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1395 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001396};
1397
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001398/*
1399 * Options object
1400 */
1401
1402static PyTypeObject OptionsType;
1403
1404typedef int (*checkfun)(void *);
1405
1406typedef struct
1407{
1408 PyObject_HEAD
1409 int opt_type;
1410 void *from;
1411 checkfun Check;
1412 PyObject *fromObj;
1413} OptionsObject;
1414
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001415 static int
1416dummy_check(void *arg UNUSED)
1417{
1418 return 0;
1419}
1420
1421 static PyObject *
1422OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1423{
1424 OptionsObject *self;
1425
Bram Moolenaar774267b2013-05-21 20:51:59 +02001426 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001427 if (self == NULL)
1428 return NULL;
1429
1430 self->opt_type = opt_type;
1431 self->from = from;
1432 self->Check = Check;
1433 self->fromObj = fromObj;
1434 if (fromObj)
1435 Py_INCREF(fromObj);
1436
1437 return (PyObject *)(self);
1438}
1439
1440 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001441OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001442{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001443 PyObject_GC_UnTrack((void *)(self));
1444 Py_XDECREF(self->fromObj);
1445 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001446}
1447
1448 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001449OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001450{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001451 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001452 return 0;
1453}
1454
1455 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001456OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001457{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001458 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001459 return 0;
1460}
1461
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001462 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001463OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001464{
1465 char_u *key;
1466 int flags;
1467 long numval;
1468 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001469 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001470
Bram Moolenaard6e39182013-05-21 18:30:34 +02001471 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001472 return NULL;
1473
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001474 DICTKEY_GET_NOTEMPTY(NULL)
1475
1476 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001477 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001478
1479 DICTKEY_UNREF
1480
1481 if (flags == 0)
1482 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001483 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001484 return NULL;
1485 }
1486
1487 if (flags & SOPT_UNSET)
1488 {
1489 Py_INCREF(Py_None);
1490 return Py_None;
1491 }
1492 else if (flags & SOPT_BOOL)
1493 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001494 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001495 r = numval ? Py_True : Py_False;
1496 Py_INCREF(r);
1497 return r;
1498 }
1499 else if (flags & SOPT_NUM)
1500 return PyInt_FromLong(numval);
1501 else if (flags & SOPT_STRING)
1502 {
1503 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001504 {
1505 PyObject *r = PyBytes_FromString((char *) stringval);
1506 vim_free(stringval);
1507 return r;
1508 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001509 else
1510 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001511 PyErr_SetString(PyExc_RuntimeError,
1512 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001513 return NULL;
1514 }
1515 }
1516 else
1517 {
1518 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1519 return NULL;
1520 }
1521}
1522
1523 static int
1524set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1525 char_u *key;
1526 int numval;
1527 char_u *stringval;
1528 int opt_flags;
1529 int opt_type;
1530 void *from;
1531{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001532 win_T *save_curwin = NULL;
1533 tabpage_T *save_curtab = NULL;
1534 buf_T *save_curbuf = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001535
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001536 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001537 switch (opt_type)
1538 {
1539 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001540 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1541 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001542 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001543 if (VimTryEnd())
1544 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001545 PyErr_SetVim("Problem while switching windows.");
1546 return -1;
1547 }
1548 set_option_value(key, numval, stringval, opt_flags);
1549 restore_win(save_curwin, save_curtab);
1550 break;
1551 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001552 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001553 set_option_value(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001554 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001555 break;
1556 case SREQ_GLOBAL:
1557 set_option_value(key, numval, stringval, opt_flags);
1558 break;
1559 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001560 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001561}
1562
1563 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001564OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001565{
1566 char_u *key;
1567 int flags;
1568 int opt_flags;
1569 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001570 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001571
Bram Moolenaard6e39182013-05-21 18:30:34 +02001572 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001573 return -1;
1574
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001575 DICTKEY_GET_NOTEMPTY(-1)
1576
1577 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001578 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001579
1580 DICTKEY_UNREF
1581
1582 if (flags == 0)
1583 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001584 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001585 return -1;
1586 }
1587
1588 if (valObject == NULL)
1589 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001590 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001591 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001592 PyErr_SetString(PyExc_ValueError,
1593 _("unable to unset global option"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001594 return -1;
1595 }
1596 else if (!(flags & SOPT_GLOBAL))
1597 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001598 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1599 "without global value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001600 return -1;
1601 }
1602 else
1603 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001604 unset_global_local_option(key, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001605 return 0;
1606 }
1607 }
1608
Bram Moolenaard6e39182013-05-21 18:30:34 +02001609 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001610
1611 if (flags & SOPT_BOOL)
1612 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001613 int istrue = PyObject_IsTrue(valObject);
1614 if (istrue == -1)
1615 return -1;
1616 r = set_option_value_for(key, istrue, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001617 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001618 }
1619 else if (flags & SOPT_NUM)
1620 {
1621 int val;
1622
1623#if PY_MAJOR_VERSION < 3
1624 if (PyInt_Check(valObject))
1625 val = PyInt_AsLong(valObject);
1626 else
1627#endif
1628 if (PyLong_Check(valObject))
1629 val = PyLong_AsLong(valObject);
1630 else
1631 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001632 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001633 return -1;
1634 }
1635
1636 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001637 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001638 }
1639 else
1640 {
1641 char_u *val;
1642 if (PyBytes_Check(valObject))
1643 {
1644
1645 if (PyString_AsStringAndSize(valObject, (char **) &val, NULL) == -1)
1646 return -1;
1647 if (val == NULL)
1648 return -1;
1649
1650 val = vim_strsave(val);
1651 }
1652 else if (PyUnicode_Check(valObject))
1653 {
1654 PyObject *bytes;
1655
1656 bytes = PyUnicode_AsEncodedString(valObject, (char *)ENC_OPT, NULL);
1657 if (bytes == NULL)
1658 return -1;
1659
1660 if(PyString_AsStringAndSize(bytes, (char **) &val, NULL) == -1)
1661 return -1;
1662 if (val == NULL)
1663 return -1;
1664
1665 val = vim_strsave(val);
1666 Py_XDECREF(bytes);
1667 }
1668 else
1669 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001670 PyErr_SetString(PyExc_TypeError, _("object must be string"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001671 return -1;
1672 }
1673
1674 r = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001675 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001676 vim_free(val);
1677 }
1678
1679 return r;
1680}
1681
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001682static PyMappingMethods OptionsAsMapping = {
1683 (lenfunc) NULL,
1684 (binaryfunc) OptionsItem,
1685 (objobjargproc) OptionsAssItem,
1686};
1687
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001688/* Tabpage object
1689 */
1690
1691typedef struct
1692{
1693 PyObject_HEAD
1694 tabpage_T *tab;
1695} TabPageObject;
1696
1697static PyObject *WinListNew(TabPageObject *tabObject);
1698
1699static PyTypeObject TabPageType;
1700
1701 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001702CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001703{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001704 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001705 {
1706 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1707 return -1;
1708 }
1709
1710 return 0;
1711}
1712
1713 static PyObject *
1714TabPageNew(tabpage_T *tab)
1715{
1716 TabPageObject *self;
1717
1718 if (TAB_PYTHON_REF(tab))
1719 {
1720 self = TAB_PYTHON_REF(tab);
1721 Py_INCREF(self);
1722 }
1723 else
1724 {
1725 self = PyObject_NEW(TabPageObject, &TabPageType);
1726 if (self == NULL)
1727 return NULL;
1728 self->tab = tab;
1729 TAB_PYTHON_REF(tab) = self;
1730 }
1731
1732 return (PyObject *)(self);
1733}
1734
1735 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001736TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001737{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001738 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
1739 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001740
1741 DESTRUCTOR_FINISH(self);
1742}
1743
1744 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001745TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001746{
1747 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001748 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001749 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001750 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001751 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001752 return DictionaryNew(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001753 else if (strcmp(name, "window") == 0)
1754 {
1755 /* For current tab window.c does not bother to set or update tp_curwin
1756 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02001757 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001758 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001759 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001760 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001761 }
1762 return NULL;
1763}
1764
1765 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001766TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001767{
1768 static char repr[100];
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001769
Bram Moolenaard6e39182013-05-21 18:30:34 +02001770 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001771 {
1772 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
1773 return PyString_FromString(repr);
1774 }
1775 else
1776 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001777 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001778
1779 if (t == 0)
1780 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
1781 (self));
1782 else
1783 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
1784
1785 return PyString_FromString(repr);
1786 }
1787}
1788
1789static struct PyMethodDef TabPageMethods[] = {
1790 /* name, function, calling, documentation */
1791 { NULL, NULL, 0, NULL }
1792};
1793
1794/*
1795 * Window list object
1796 */
1797
1798static PyTypeObject TabListType;
1799static PySequenceMethods TabListAsSeq;
1800
1801typedef struct
1802{
1803 PyObject_HEAD
1804} TabListObject;
1805
1806 static PyInt
1807TabListLength(PyObject *self UNUSED)
1808{
1809 tabpage_T *tp = first_tabpage;
1810 PyInt n = 0;
1811
1812 while (tp != NULL)
1813 {
1814 ++n;
1815 tp = tp->tp_next;
1816 }
1817
1818 return n;
1819}
1820
1821 static PyObject *
1822TabListItem(PyObject *self UNUSED, PyInt n)
1823{
1824 tabpage_T *tp;
1825
1826 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
1827 if (n == 0)
1828 return TabPageNew(tp);
1829
1830 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
1831 return NULL;
1832}
1833
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001834/* Window object
1835 */
1836
1837typedef struct
1838{
1839 PyObject_HEAD
1840 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001841 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001842} WindowObject;
1843
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001844static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001845
1846 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001847CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001848{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001849 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001850 {
1851 PyErr_SetVim(_("attempt to refer to deleted window"));
1852 return -1;
1853 }
1854
1855 return 0;
1856}
1857
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001858 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001859WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02001860{
1861 /* We need to handle deletion of windows underneath us.
1862 * If we add a "w_python*_ref" field to the win_T structure,
1863 * then we can get at it in win_free() in vim. We then
1864 * need to create only ONE Python object per window - if
1865 * we try to create a second, just INCREF the existing one
1866 * and return it. The (single) Python object referring to
1867 * the window is stored in "w_python*_ref".
1868 * On a win_free() we set the Python object's win_T* field
1869 * to an invalid value. We trap all uses of a window
1870 * object, and reject them if the win_T* field is invalid.
1871 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001872 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02001873 * w_python_ref and w_python3_ref fields respectively.
1874 */
1875
1876 WindowObject *self;
1877
1878 if (WIN_PYTHON_REF(win))
1879 {
1880 self = WIN_PYTHON_REF(win);
1881 Py_INCREF(self);
1882 }
1883 else
1884 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02001885 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02001886 if (self == NULL)
1887 return NULL;
1888 self->win = win;
1889 WIN_PYTHON_REF(win) = self;
1890 }
1891
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001892 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
1893
Bram Moolenaar971db462013-05-12 18:44:48 +02001894 return (PyObject *)(self);
1895}
1896
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001897 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001898WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001899{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001900 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001901 if (self->win && self->win != INVALID_WINDOW_VALUE)
1902 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02001903 Py_XDECREF(((PyObject *)(self->tabObject)));
1904 PyObject_GC_Del((void *)(self));
1905}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001906
Bram Moolenaar774267b2013-05-21 20:51:59 +02001907 static int
1908WindowTraverse(WindowObject *self, visitproc visit, void *arg)
1909{
1910 Py_VISIT(((PyObject *)(self->tabObject)));
1911 return 0;
1912}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001913
Bram Moolenaar774267b2013-05-21 20:51:59 +02001914 static int
1915WindowClear(WindowObject *self)
1916{
1917 Py_CLEAR(self->tabObject);
1918 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001919}
1920
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001921 static win_T *
1922get_firstwin(TabPageObject *tabObject)
1923{
1924 if (tabObject)
1925 {
1926 if (CheckTabPage(tabObject))
1927 return NULL;
1928 /* For current tab window.c does not bother to set or update tp_firstwin
1929 */
1930 else if (tabObject->tab == curtab)
1931 return firstwin;
1932 else
1933 return tabObject->tab->tp_firstwin;
1934 }
1935 else
1936 return firstwin;
1937}
1938
Bram Moolenaar971db462013-05-12 18:44:48 +02001939 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001940WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001941{
1942 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001943 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001944 else if (strcmp(name, "cursor") == 0)
1945 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001946 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001947
1948 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1949 }
1950 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001951 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001952#ifdef FEAT_WINDOWS
1953 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001954 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001955#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001956#ifdef FEAT_VERTSPLIT
1957 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001958 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001959 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001960 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001961#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001962 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001963 return DictionaryNew(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001964 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001965 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
1966 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02001967 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001968 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001969 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001970 return NULL;
1971 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001972 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001973 }
1974 else if (strcmp(name, "tabpage") == 0)
1975 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001976 Py_INCREF(self->tabObject);
1977 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001978 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001979 else if (strcmp(name,"__members__") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001980 return Py_BuildValue("[sssssssss]", "buffer", "cursor", "height",
1981 "vars", "options", "number", "row", "col", "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001982 else
1983 return NULL;
1984}
1985
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001986 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001987WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001988{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001989 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001990 return -1;
1991
1992 if (strcmp(name, "buffer") == 0)
1993 {
1994 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
1995 return -1;
1996 }
1997 else if (strcmp(name, "cursor") == 0)
1998 {
1999 long lnum;
2000 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002001
2002 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2003 return -1;
2004
Bram Moolenaard6e39182013-05-21 18:30:34 +02002005 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002006 {
2007 PyErr_SetVim(_("cursor position outside buffer"));
2008 return -1;
2009 }
2010
2011 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002012 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002013 return -1;
2014
Bram Moolenaard6e39182013-05-21 18:30:34 +02002015 self->win->w_cursor.lnum = lnum;
2016 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002017#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002018 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002019#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002020 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002021 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002022
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002023 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002024 return 0;
2025 }
2026 else if (strcmp(name, "height") == 0)
2027 {
2028 int height;
2029 win_T *savewin;
2030
2031 if (!PyArg_Parse(val, "i", &height))
2032 return -1;
2033
2034#ifdef FEAT_GUI
2035 need_mouse_correct = TRUE;
2036#endif
2037 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002038 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002039
2040 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002041 win_setheight(height);
2042 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002043 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002044 return -1;
2045
2046 return 0;
2047 }
2048#ifdef FEAT_VERTSPLIT
2049 else if (strcmp(name, "width") == 0)
2050 {
2051 int width;
2052 win_T *savewin;
2053
2054 if (!PyArg_Parse(val, "i", &width))
2055 return -1;
2056
2057#ifdef FEAT_GUI
2058 need_mouse_correct = TRUE;
2059#endif
2060 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002061 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002062
2063 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002064 win_setwidth(width);
2065 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002066 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002067 return -1;
2068
2069 return 0;
2070 }
2071#endif
2072 else
2073 {
2074 PyErr_SetString(PyExc_AttributeError, name);
2075 return -1;
2076 }
2077}
2078
2079 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002080WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002081{
2082 static char repr[100];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002083
Bram Moolenaard6e39182013-05-21 18:30:34 +02002084 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002085 {
2086 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2087 return PyString_FromString(repr);
2088 }
2089 else
2090 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002091 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002092
Bram Moolenaar6d216452013-05-12 19:00:41 +02002093 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002094 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2095 (self));
2096 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002097 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002098
2099 return PyString_FromString(repr);
2100 }
2101}
2102
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002103static struct PyMethodDef WindowMethods[] = {
2104 /* name, function, calling, documentation */
2105 { NULL, NULL, 0, NULL }
2106};
2107
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002108/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002109 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002110 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002111
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002112static PyTypeObject WinListType;
2113static PySequenceMethods WinListAsSeq;
2114
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002115typedef struct
2116{
2117 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002118 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002119} WinListObject;
2120
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002121 static PyObject *
2122WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002123{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002124 WinListObject *self;
2125
2126 self = PyObject_NEW(WinListObject, &WinListType);
2127 self->tabObject = tabObject;
2128 Py_INCREF(tabObject);
2129
2130 return (PyObject *)(self);
2131}
2132
2133 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002134WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002135{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002136 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002137
2138 if (tabObject)
2139 Py_DECREF((PyObject *)(tabObject));
2140
2141 DESTRUCTOR_FINISH(self);
2142}
2143
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002144 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002145WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002146{
2147 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002148 PyInt n = 0;
2149
Bram Moolenaard6e39182013-05-21 18:30:34 +02002150 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002151 return -1;
2152
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002153 while (w != NULL)
2154 {
2155 ++n;
2156 w = W_NEXT(w);
2157 }
2158
2159 return n;
2160}
2161
2162 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002163WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002164{
2165 win_T *w;
2166
Bram Moolenaard6e39182013-05-21 18:30:34 +02002167 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002168 return NULL;
2169
2170 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002171 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002172 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002173
2174 PyErr_SetString(PyExc_IndexError, _("no such window"));
2175 return NULL;
2176}
2177
2178/* Convert a Python string into a Vim line.
2179 *
2180 * The result is in allocated memory. All internal nulls are replaced by
2181 * newline characters. It is an error for the string to contain newline
2182 * characters.
2183 *
2184 * On errors, the Python exception data is set, and NULL is returned.
2185 */
2186 static char *
2187StringToLine(PyObject *obj)
2188{
2189 const char *str;
2190 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002191 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002192 PyInt len;
2193 PyInt i;
2194 char *p;
2195
2196 if (obj == NULL || !PyString_Check(obj))
2197 {
2198 PyErr_BadArgument();
2199 return NULL;
2200 }
2201
Bram Moolenaar19e60942011-06-19 00:27:51 +02002202 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2203 str = PyString_AsString(bytes);
2204 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002205
2206 /*
2207 * Error checking: String must not contain newlines, as we
2208 * are replacing a single line, and we must replace it with
2209 * a single line.
2210 * A trailing newline is removed, so that append(f.readlines()) works.
2211 */
2212 p = memchr(str, '\n', len);
2213 if (p != NULL)
2214 {
2215 if (p == str + len - 1)
2216 --len;
2217 else
2218 {
2219 PyErr_SetVim(_("string cannot contain newlines"));
2220 return NULL;
2221 }
2222 }
2223
2224 /* Create a copy of the string, with internal nulls replaced by
2225 * newline characters, as is the vim convention.
2226 */
2227 save = (char *)alloc((unsigned)(len+1));
2228 if (save == NULL)
2229 {
2230 PyErr_NoMemory();
2231 return NULL;
2232 }
2233
2234 for (i = 0; i < len; ++i)
2235 {
2236 if (str[i] == '\0')
2237 save[i] = '\n';
2238 else
2239 save[i] = str[i];
2240 }
2241
2242 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002243 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002244
2245 return save;
2246}
2247
2248/* Get a line from the specified buffer. The line number is
2249 * in Vim format (1-based). The line is returned as a Python
2250 * string object.
2251 */
2252 static PyObject *
2253GetBufferLine(buf_T *buf, PyInt n)
2254{
2255 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2256}
2257
2258
2259/* Get a list of lines from the specified buffer. The line numbers
2260 * are in Vim format (1-based). The range is from lo up to, but not
2261 * including, hi. The list is returned as a Python list of string objects.
2262 */
2263 static PyObject *
2264GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2265{
2266 PyInt i;
2267 PyInt n = hi - lo;
2268 PyObject *list = PyList_New(n);
2269
2270 if (list == NULL)
2271 return NULL;
2272
2273 for (i = 0; i < n; ++i)
2274 {
2275 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2276
2277 /* Error check - was the Python string creation OK? */
2278 if (str == NULL)
2279 {
2280 Py_DECREF(list);
2281 return NULL;
2282 }
2283
2284 /* Set the list item */
2285 if (PyList_SetItem(list, i, str))
2286 {
2287 Py_DECREF(str);
2288 Py_DECREF(list);
2289 return NULL;
2290 }
2291 }
2292
2293 /* The ownership of the Python list is passed to the caller (ie,
2294 * the caller should Py_DECREF() the object when it is finished
2295 * with it).
2296 */
2297
2298 return list;
2299}
2300
2301/*
2302 * Check if deleting lines made the cursor position invalid.
2303 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2304 * deleted).
2305 */
2306 static void
2307py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2308{
2309 if (curwin->w_cursor.lnum >= lo)
2310 {
2311 /* Adjust the cursor position if it's in/after the changed
2312 * lines. */
2313 if (curwin->w_cursor.lnum >= hi)
2314 {
2315 curwin->w_cursor.lnum += extra;
2316 check_cursor_col();
2317 }
2318 else if (extra < 0)
2319 {
2320 curwin->w_cursor.lnum = lo;
2321 check_cursor();
2322 }
2323 else
2324 check_cursor_col();
2325 changed_cline_bef_curs();
2326 }
2327 invalidate_botline();
2328}
2329
Bram Moolenaar19e60942011-06-19 00:27:51 +02002330/*
2331 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002332 * in Vim format (1-based). The replacement line is given as
2333 * a Python string object. The object is checked for validity
2334 * and correct format. Errors are returned as a value of FAIL.
2335 * The return value is OK on success.
2336 * If OK is returned and len_change is not NULL, *len_change
2337 * is set to the change in the buffer length.
2338 */
2339 static int
2340SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2341{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002342 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002343 * There are three cases:
2344 * 1. NULL, or None - this is a deletion.
2345 * 2. A string - this is a replacement.
2346 * 3. Anything else - this is an error.
2347 */
2348 if (line == Py_None || line == NULL)
2349 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002350 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002351
2352 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002353 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002354
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002355 VimTryStart();
2356
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002357 if (u_savedel((linenr_T)n, 1L) == FAIL)
2358 PyErr_SetVim(_("cannot save undo information"));
2359 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2360 PyErr_SetVim(_("cannot delete line"));
2361 else
2362 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002363 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002364 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2365 deleted_lines_mark((linenr_T)n, 1L);
2366 }
2367
Bram Moolenaar105bc352013-05-17 16:03:57 +02002368 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002369
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002370 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002371 return FAIL;
2372
2373 if (len_change)
2374 *len_change = -1;
2375
2376 return OK;
2377 }
2378 else if (PyString_Check(line))
2379 {
2380 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002381 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002382
2383 if (save == NULL)
2384 return FAIL;
2385
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002386 VimTryStart();
2387
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002388 /* We do not need to free "save" if ml_replace() consumes it. */
2389 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002390 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002391
2392 if (u_savesub((linenr_T)n) == FAIL)
2393 {
2394 PyErr_SetVim(_("cannot save undo information"));
2395 vim_free(save);
2396 }
2397 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2398 {
2399 PyErr_SetVim(_("cannot replace line"));
2400 vim_free(save);
2401 }
2402 else
2403 changed_bytes((linenr_T)n, 0);
2404
Bram Moolenaar105bc352013-05-17 16:03:57 +02002405 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002406
2407 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002408 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002409 check_cursor_col();
2410
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002411 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002412 return FAIL;
2413
2414 if (len_change)
2415 *len_change = 0;
2416
2417 return OK;
2418 }
2419 else
2420 {
2421 PyErr_BadArgument();
2422 return FAIL;
2423 }
2424}
2425
Bram Moolenaar19e60942011-06-19 00:27:51 +02002426/* Replace a range of lines in the specified buffer. The line numbers are in
2427 * Vim format (1-based). The range is from lo up to, but not including, hi.
2428 * The replacement lines are given as a Python list of string objects. The
2429 * list is checked for validity and correct format. Errors are returned as a
2430 * value of FAIL. The return value is OK on success.
2431 * If OK is returned and len_change is not NULL, *len_change
2432 * is set to the change in the buffer length.
2433 */
2434 static int
2435SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2436{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002437 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002438 * There are three cases:
2439 * 1. NULL, or None - this is a deletion.
2440 * 2. A list - this is a replacement.
2441 * 3. Anything else - this is an error.
2442 */
2443 if (list == Py_None || list == NULL)
2444 {
2445 PyInt i;
2446 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002447 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002448
2449 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002450 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002451 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002452
2453 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2454 PyErr_SetVim(_("cannot save undo information"));
2455 else
2456 {
2457 for (i = 0; i < n; ++i)
2458 {
2459 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2460 {
2461 PyErr_SetVim(_("cannot delete line"));
2462 break;
2463 }
2464 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002465 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002466 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2467 deleted_lines_mark((linenr_T)lo, (long)i);
2468 }
2469
Bram Moolenaar105bc352013-05-17 16:03:57 +02002470 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002471
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002472 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002473 return FAIL;
2474
2475 if (len_change)
2476 *len_change = -n;
2477
2478 return OK;
2479 }
2480 else if (PyList_Check(list))
2481 {
2482 PyInt i;
2483 PyInt new_len = PyList_Size(list);
2484 PyInt old_len = hi - lo;
2485 PyInt extra = 0; /* lines added to text, can be negative */
2486 char **array;
2487 buf_T *savebuf;
2488
2489 if (new_len == 0) /* avoid allocating zero bytes */
2490 array = NULL;
2491 else
2492 {
2493 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
2494 if (array == NULL)
2495 {
2496 PyErr_NoMemory();
2497 return FAIL;
2498 }
2499 }
2500
2501 for (i = 0; i < new_len; ++i)
2502 {
2503 PyObject *line = PyList_GetItem(list, i);
2504
2505 array[i] = StringToLine(line);
2506 if (array[i] == NULL)
2507 {
2508 while (i)
2509 vim_free(array[--i]);
2510 vim_free(array);
2511 return FAIL;
2512 }
2513 }
2514
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002515 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02002516 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002517
2518 // START of region without "return". Must call restore_buffer()!
2519 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002520
2521 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2522 PyErr_SetVim(_("cannot save undo information"));
2523
2524 /* If the size of the range is reducing (ie, new_len < old_len) we
2525 * need to delete some old_len. We do this at the start, by
2526 * repeatedly deleting line "lo".
2527 */
2528 if (!PyErr_Occurred())
2529 {
2530 for (i = 0; i < old_len - new_len; ++i)
2531 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2532 {
2533 PyErr_SetVim(_("cannot delete line"));
2534 break;
2535 }
2536 extra -= i;
2537 }
2538
2539 /* For as long as possible, replace the existing old_len with the
2540 * new old_len. This is a more efficient operation, as it requires
2541 * less memory allocation and freeing.
2542 */
2543 if (!PyErr_Occurred())
2544 {
2545 for (i = 0; i < old_len && i < new_len; ++i)
2546 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2547 == FAIL)
2548 {
2549 PyErr_SetVim(_("cannot replace line"));
2550 break;
2551 }
2552 }
2553 else
2554 i = 0;
2555
2556 /* Now we may need to insert the remaining new old_len. If we do, we
2557 * must free the strings as we finish with them (we can't pass the
2558 * responsibility to vim in this case).
2559 */
2560 if (!PyErr_Occurred())
2561 {
2562 while (i < new_len)
2563 {
2564 if (ml_append((linenr_T)(lo + i - 1),
2565 (char_u *)array[i], 0, FALSE) == FAIL)
2566 {
2567 PyErr_SetVim(_("cannot insert line"));
2568 break;
2569 }
2570 vim_free(array[i]);
2571 ++i;
2572 ++extra;
2573 }
2574 }
2575
2576 /* Free any left-over old_len, as a result of an error */
2577 while (i < new_len)
2578 {
2579 vim_free(array[i]);
2580 ++i;
2581 }
2582
2583 /* Free the array of old_len. All of its contents have now
2584 * been dealt with (either freed, or the responsibility passed
2585 * to vim.
2586 */
2587 vim_free(array);
2588
2589 /* Adjust marks. Invalidate any which lie in the
2590 * changed range, and move any in the remainder of the buffer.
2591 */
2592 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2593 (long)MAXLNUM, (long)extra);
2594 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2595
Bram Moolenaar105bc352013-05-17 16:03:57 +02002596 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002597 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2598
Bram Moolenaar105bc352013-05-17 16:03:57 +02002599 // END of region without "return".
2600 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002601
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002602 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002603 return FAIL;
2604
2605 if (len_change)
2606 *len_change = new_len - old_len;
2607
2608 return OK;
2609 }
2610 else
2611 {
2612 PyErr_BadArgument();
2613 return FAIL;
2614 }
2615}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002616
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002617/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002618 * The line number is in Vim format (1-based). The lines to be inserted are
2619 * given as a Python list of string objects or as a single string. The lines
2620 * to be added are checked for validity and correct format. Errors are
2621 * returned as a value of FAIL. The return value is OK on success.
2622 * If OK is returned and len_change is not NULL, *len_change
2623 * is set to the change in the buffer length.
2624 */
2625 static int
2626InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2627{
2628 /* First of all, we check the type of the supplied Python object.
2629 * It must be a string or a list, or the call is in error.
2630 */
2631 if (PyString_Check(lines))
2632 {
2633 char *str = StringToLine(lines);
2634 buf_T *savebuf;
2635
2636 if (str == NULL)
2637 return FAIL;
2638
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002639 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002640 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002641 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002642
2643 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2644 PyErr_SetVim(_("cannot save undo information"));
2645 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2646 PyErr_SetVim(_("cannot insert line"));
2647 else
2648 appended_lines_mark((linenr_T)n, 1L);
2649
2650 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002651 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002652 update_screen(VALID);
2653
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002654 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002655 return FAIL;
2656
2657 if (len_change)
2658 *len_change = 1;
2659
2660 return OK;
2661 }
2662 else if (PyList_Check(lines))
2663 {
2664 PyInt i;
2665 PyInt size = PyList_Size(lines);
2666 char **array;
2667 buf_T *savebuf;
2668
2669 array = (char **)alloc((unsigned)(size * sizeof(char *)));
2670 if (array == NULL)
2671 {
2672 PyErr_NoMemory();
2673 return FAIL;
2674 }
2675
2676 for (i = 0; i < size; ++i)
2677 {
2678 PyObject *line = PyList_GetItem(lines, i);
2679 array[i] = StringToLine(line);
2680
2681 if (array[i] == NULL)
2682 {
2683 while (i)
2684 vim_free(array[--i]);
2685 vim_free(array);
2686 return FAIL;
2687 }
2688 }
2689
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002690 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002691 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002692 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002693
2694 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2695 PyErr_SetVim(_("cannot save undo information"));
2696 else
2697 {
2698 for (i = 0; i < size; ++i)
2699 {
2700 if (ml_append((linenr_T)(n + i),
2701 (char_u *)array[i], 0, FALSE) == FAIL)
2702 {
2703 PyErr_SetVim(_("cannot insert line"));
2704
2705 /* Free the rest of the lines */
2706 while (i < size)
2707 vim_free(array[i++]);
2708
2709 break;
2710 }
2711 vim_free(array[i]);
2712 }
2713 if (i > 0)
2714 appended_lines_mark((linenr_T)n, (long)i);
2715 }
2716
2717 /* Free the array of lines. All of its contents have now
2718 * been freed.
2719 */
2720 vim_free(array);
2721
Bram Moolenaar105bc352013-05-17 16:03:57 +02002722 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002723 update_screen(VALID);
2724
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002725 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002726 return FAIL;
2727
2728 if (len_change)
2729 *len_change = size;
2730
2731 return OK;
2732 }
2733 else
2734 {
2735 PyErr_BadArgument();
2736 return FAIL;
2737 }
2738}
2739
2740/*
2741 * Common routines for buffers and line ranges
2742 * -------------------------------------------
2743 */
2744
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002745typedef struct
2746{
2747 PyObject_HEAD
2748 buf_T *buf;
2749} BufferObject;
2750
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002751 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002752CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002753{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002754 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002755 {
2756 PyErr_SetVim(_("attempt to refer to deleted buffer"));
2757 return -1;
2758 }
2759
2760 return 0;
2761}
2762
2763 static PyObject *
2764RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
2765{
2766 if (CheckBuffer(self))
2767 return NULL;
2768
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002769 if (end == -1)
2770 end = self->buf->b_ml.ml_line_count;
2771
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002772 if (n < 0)
2773 n += end - start + 1;
2774
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002775 if (n < 0 || n > end - start)
2776 {
2777 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2778 return NULL;
2779 }
2780
2781 return GetBufferLine(self->buf, n+start);
2782}
2783
2784 static PyObject *
2785RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
2786{
2787 PyInt size;
2788
2789 if (CheckBuffer(self))
2790 return NULL;
2791
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002792 if (end == -1)
2793 end = self->buf->b_ml.ml_line_count;
2794
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002795 size = end - start + 1;
2796
2797 if (lo < 0)
2798 lo = 0;
2799 else if (lo > size)
2800 lo = size;
2801 if (hi < 0)
2802 hi = 0;
2803 if (hi < lo)
2804 hi = lo;
2805 else if (hi > size)
2806 hi = size;
2807
2808 return GetBufferLineList(self->buf, lo+start, hi+start);
2809}
2810
2811 static PyInt
2812RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2813{
2814 PyInt len_change;
2815
2816 if (CheckBuffer(self))
2817 return -1;
2818
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002819 if (end == -1)
2820 end = self->buf->b_ml.ml_line_count;
2821
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002822 if (n < 0)
2823 n += end - start + 1;
2824
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002825 if (n < 0 || n > end - start)
2826 {
2827 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2828 return -1;
2829 }
2830
2831 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
2832 return -1;
2833
2834 if (new_end)
2835 *new_end = end + len_change;
2836
2837 return 0;
2838}
2839
Bram Moolenaar19e60942011-06-19 00:27:51 +02002840 static PyInt
2841RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2842{
2843 PyInt size;
2844 PyInt len_change;
2845
2846 /* Self must be a valid buffer */
2847 if (CheckBuffer(self))
2848 return -1;
2849
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002850 if (end == -1)
2851 end = self->buf->b_ml.ml_line_count;
2852
Bram Moolenaar19e60942011-06-19 00:27:51 +02002853 /* Sort out the slice range */
2854 size = end - start + 1;
2855
2856 if (lo < 0)
2857 lo = 0;
2858 else if (lo > size)
2859 lo = size;
2860 if (hi < 0)
2861 hi = 0;
2862 if (hi < lo)
2863 hi = lo;
2864 else if (hi > size)
2865 hi = size;
2866
2867 if (SetBufferLineList(self->buf, lo + start, hi + start,
2868 val, &len_change) == FAIL)
2869 return -1;
2870
2871 if (new_end)
2872 *new_end = end + len_change;
2873
2874 return 0;
2875}
2876
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002877
2878 static PyObject *
2879RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
2880{
2881 PyObject *lines;
2882 PyInt len_change;
2883 PyInt max;
2884 PyInt n;
2885
2886 if (CheckBuffer(self))
2887 return NULL;
2888
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002889 if (end == -1)
2890 end = self->buf->b_ml.ml_line_count;
2891
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002892 max = n = end - start + 1;
2893
2894 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
2895 return NULL;
2896
2897 if (n < 0 || n > max)
2898 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002899 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002900 return NULL;
2901 }
2902
2903 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
2904 return NULL;
2905
2906 if (new_end)
2907 *new_end = end + len_change;
2908
2909 Py_INCREF(Py_None);
2910 return Py_None;
2911}
2912
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002913/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002914 */
2915
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002916static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002917static PySequenceMethods RangeAsSeq;
2918static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002919
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002920typedef struct
2921{
2922 PyObject_HEAD
2923 BufferObject *buf;
2924 PyInt start;
2925 PyInt end;
2926} RangeObject;
2927
2928 static PyObject *
2929RangeNew(buf_T *buf, PyInt start, PyInt end)
2930{
2931 BufferObject *bufr;
2932 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002933 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002934 if (self == NULL)
2935 return NULL;
2936
2937 bufr = (BufferObject *)BufferNew(buf);
2938 if (bufr == NULL)
2939 {
2940 Py_DECREF(self);
2941 return NULL;
2942 }
2943 Py_INCREF(bufr);
2944
2945 self->buf = bufr;
2946 self->start = start;
2947 self->end = end;
2948
2949 return (PyObject *)(self);
2950}
2951
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002952 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002953RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002954{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002955 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002956 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02002957 PyObject_GC_Del((void *)(self));
2958}
2959
2960 static int
2961RangeTraverse(RangeObject *self, visitproc visit, void *arg)
2962{
2963 Py_VISIT(((PyObject *)(self->buf)));
2964 return 0;
2965}
2966
2967 static int
2968RangeClear(RangeObject *self)
2969{
2970 Py_CLEAR(self->buf);
2971 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002972}
2973
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002974 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002975RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002976{
2977 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002978 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002979 return -1; /* ??? */
2980
Bram Moolenaard6e39182013-05-21 18:30:34 +02002981 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002982}
2983
2984 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002985RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002986{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002987 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002988}
2989
2990 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002991RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002992{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002993 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002994}
2995
2996 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002997RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002998{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002999 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003000}
3001
3002 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003003RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003004{
3005 static char repr[100];
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003006
Bram Moolenaard6e39182013-05-21 18:30:34 +02003007 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003008 {
3009 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
3010 (self));
3011 return PyString_FromString(repr);
3012 }
3013 else
3014 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003015 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003016 int len;
3017
3018 if (name == NULL)
3019 name = "";
3020 len = (int)strlen(name);
3021
3022 if (len > 45)
3023 name = name + (45 - len);
3024
3025 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
3026 len > 45 ? "..." : "", name,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003027 self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003028
3029 return PyString_FromString(repr);
3030 }
3031}
3032
3033static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003034 /* name, function, calling, documentation */
3035 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
3036 { NULL, NULL, 0, NULL }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003037};
3038
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003039static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003040static PySequenceMethods BufferAsSeq;
3041static PyMappingMethods BufferAsMapping;
3042
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003043 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003044BufferNew(buf_T *buf)
3045{
3046 /* We need to handle deletion of buffers underneath us.
3047 * If we add a "b_python*_ref" field to the buf_T structure,
3048 * then we can get at it in buf_freeall() in vim. We then
3049 * need to create only ONE Python object per buffer - if
3050 * we try to create a second, just INCREF the existing one
3051 * and return it. The (single) Python object referring to
3052 * the buffer is stored in "b_python*_ref".
3053 * Question: what to do on a buf_freeall(). We'll probably
3054 * have to either delete the Python object (DECREF it to
3055 * zero - a bad idea, as it leaves dangling refs!) or
3056 * set the buf_T * value to an invalid value (-1?), which
3057 * means we need checks in all access functions... Bah.
3058 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003059 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003060 * b_python_ref and b_python3_ref fields respectively.
3061 */
3062
3063 BufferObject *self;
3064
3065 if (BUF_PYTHON_REF(buf) != NULL)
3066 {
3067 self = BUF_PYTHON_REF(buf);
3068 Py_INCREF(self);
3069 }
3070 else
3071 {
3072 self = PyObject_NEW(BufferObject, &BufferType);
3073 if (self == NULL)
3074 return NULL;
3075 self->buf = buf;
3076 BUF_PYTHON_REF(buf) = self;
3077 }
3078
3079 return (PyObject *)(self);
3080}
3081
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003082 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003083BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003084{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003085 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3086 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003087
3088 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003089}
3090
Bram Moolenaar971db462013-05-12 18:44:48 +02003091 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003092BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003093{
3094 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003095 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003096 return -1; /* ??? */
3097
Bram Moolenaard6e39182013-05-21 18:30:34 +02003098 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003099}
3100
3101 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003102BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003103{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003104 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003105}
3106
3107 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003108BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003109{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003110 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003111}
3112
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003113 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003114BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003115{
3116 if (strcmp(name, "name") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003117 return Py_BuildValue("s", self->buf->b_ffname);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003118 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003119 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003120 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003121 return DictionaryNew(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003122 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003123 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3124 (PyObject *) self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003125 else if (strcmp(name,"__members__") == 0)
3126 return Py_BuildValue("[ssss]", "name", "number", "vars", "options");
3127 else
3128 return NULL;
3129}
3130
3131 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003132BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003133{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003134 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003135}
3136
3137 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003138BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003139{
3140 pos_T *posp;
3141 char *pmark;
3142 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003143 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003144
Bram Moolenaard6e39182013-05-21 18:30:34 +02003145 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003146 return NULL;
3147
3148 if (!PyArg_ParseTuple(args, "s", &pmark))
3149 return NULL;
3150 mark = *pmark;
3151
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003152 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02003153 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003154 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003155 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003156 if (VimTryEnd())
3157 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003158
3159 if (posp == NULL)
3160 {
3161 PyErr_SetVim(_("invalid mark name"));
3162 return NULL;
3163 }
3164
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003165 if (posp->lnum <= 0)
3166 {
3167 /* Or raise an error? */
3168 Py_INCREF(Py_None);
3169 return Py_None;
3170 }
3171
3172 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3173}
3174
3175 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003176BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003177{
3178 PyInt start;
3179 PyInt end;
3180
Bram Moolenaard6e39182013-05-21 18:30:34 +02003181 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003182 return NULL;
3183
3184 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3185 return NULL;
3186
Bram Moolenaard6e39182013-05-21 18:30:34 +02003187 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003188}
3189
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003190 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003191BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003192{
3193 static char repr[100];
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003194
Bram Moolenaard6e39182013-05-21 18:30:34 +02003195 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003196 {
3197 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3198 return PyString_FromString(repr);
3199 }
3200 else
3201 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003202 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003203 PyInt len;
3204
3205 if (name == NULL)
3206 name = "";
3207 len = strlen(name);
3208
3209 if (len > 35)
3210 name = name + (35 - len);
3211
3212 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3213
3214 return PyString_FromString(repr);
3215 }
3216}
3217
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003218static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003219 /* name, function, calling, documentation */
3220 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
3221 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
3222 {"range", (PyCFunction)BufferRange, METH_VARARGS, "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 +01003223#if PY_VERSION_HEX >= 0x03000000
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003224 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, "List buffer attributes" },
Bram Moolenaar7f85d292012-02-04 20:17:26 +01003225#endif
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003226 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003227};
3228
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003229/*
3230 * Buffer list object - Implementation
3231 */
3232
3233static PyTypeObject BufMapType;
3234
3235typedef struct
3236{
3237 PyObject_HEAD
3238} BufMapObject;
3239
3240 static PyInt
3241BufMapLength(PyObject *self UNUSED)
3242{
3243 buf_T *b = firstbuf;
3244 PyInt n = 0;
3245
3246 while (b)
3247 {
3248 ++n;
3249 b = b->b_next;
3250 }
3251
3252 return n;
3253}
3254
3255 static PyObject *
3256BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3257{
3258 buf_T *b;
3259 int bnr;
3260
3261#if PY_MAJOR_VERSION < 3
3262 if (PyInt_Check(keyObject))
3263 bnr = PyInt_AsLong(keyObject);
3264 else
3265#endif
3266 if (PyLong_Check(keyObject))
3267 bnr = PyLong_AsLong(keyObject);
3268 else
3269 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003270 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003271 return NULL;
3272 }
3273
3274 b = buflist_findnr(bnr);
3275
3276 if (b)
3277 return BufferNew(b);
3278 else
3279 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003280 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003281 return NULL;
3282 }
3283}
3284
3285 static void
3286BufMapIterDestruct(PyObject *buffer)
3287{
3288 /* Iteration was stopped before all buffers were processed */
3289 if (buffer)
3290 {
3291 Py_DECREF(buffer);
3292 }
3293}
3294
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003295 static int
3296BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3297{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003298 if (buffer)
3299 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003300 return 0;
3301}
3302
3303 static int
3304BufMapIterClear(PyObject **buffer)
3305{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003306 if (*buffer)
3307 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003308 return 0;
3309}
3310
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003311 static PyObject *
3312BufMapIterNext(PyObject **buffer)
3313{
3314 PyObject *next;
3315 PyObject *r;
3316
3317 if (!*buffer)
3318 return NULL;
3319
3320 r = *buffer;
3321
3322 if (CheckBuffer((BufferObject *)(r)))
3323 {
3324 *buffer = NULL;
3325 return NULL;
3326 }
3327
3328 if (!((BufferObject *)(r))->buf->b_next)
3329 next = NULL;
3330 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3331 return NULL;
3332 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02003333 /* Do not increment reference: we no longer hold it (decref), but whoever
3334 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003335 return r;
3336}
3337
3338 static PyObject *
3339BufMapIter(PyObject *self UNUSED)
3340{
3341 PyObject *buffer;
3342
3343 buffer = BufferNew(firstbuf);
3344 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003345 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3346 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003347}
3348
3349static PyMappingMethods BufMapAsMapping = {
3350 (lenfunc) BufMapLength,
3351 (binaryfunc) BufMapItem,
3352 (objobjargproc) 0,
3353};
3354
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003355/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003356 */
3357
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003358 static PyObject *
3359CurrentGetattr(PyObject *self UNUSED, char *name)
3360{
3361 if (strcmp(name, "buffer") == 0)
3362 return (PyObject *)BufferNew(curbuf);
3363 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003364 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003365 else if (strcmp(name, "tabpage") == 0)
3366 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003367 else if (strcmp(name, "line") == 0)
3368 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3369 else if (strcmp(name, "range") == 0)
3370 return RangeNew(curbuf, RangeStart, RangeEnd);
3371 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003372 return Py_BuildValue("[sssss]", "buffer", "window", "line", "range",
3373 "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003374 else
3375 {
3376 PyErr_SetString(PyExc_AttributeError, name);
3377 return NULL;
3378 }
3379}
3380
3381 static int
3382CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3383{
3384 if (strcmp(name, "line") == 0)
3385 {
3386 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3387 return -1;
3388
3389 return 0;
3390 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003391 else if (strcmp(name, "buffer") == 0)
3392 {
3393 int count;
3394
3395 if (value->ob_type != &BufferType)
3396 {
3397 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3398 return -1;
3399 }
3400
3401 if (CheckBuffer((BufferObject *)(value)))
3402 return -1;
3403 count = ((BufferObject *)(value))->buf->b_fnum;
3404
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003405 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003406 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3407 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003408 if (VimTryEnd())
3409 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003410 PyErr_SetVim(_("failed to switch to given buffer"));
3411 return -1;
3412 }
3413
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003414 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003415 }
3416 else if (strcmp(name, "window") == 0)
3417 {
3418 int count;
3419
3420 if (value->ob_type != &WindowType)
3421 {
3422 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3423 return -1;
3424 }
3425
3426 if (CheckWindow((WindowObject *)(value)))
3427 return -1;
3428 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3429
3430 if (!count)
3431 {
3432 PyErr_SetString(PyExc_ValueError,
3433 _("failed to find window in the current tab page"));
3434 return -1;
3435 }
3436
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003437 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003438 win_goto(((WindowObject *)(value))->win);
3439 if (((WindowObject *)(value))->win != curwin)
3440 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003441 if (VimTryEnd())
3442 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003443 PyErr_SetString(PyExc_RuntimeError,
3444 _("did not switch to the specified window"));
3445 return -1;
3446 }
3447
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003448 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003449 }
3450 else if (strcmp(name, "tabpage") == 0)
3451 {
3452 if (value->ob_type != &TabPageType)
3453 {
3454 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3455 return -1;
3456 }
3457
3458 if (CheckTabPage((TabPageObject *)(value)))
3459 return -1;
3460
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003461 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003462 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3463 if (((TabPageObject *)(value))->tab != curtab)
3464 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003465 if (VimTryEnd())
3466 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003467 PyErr_SetString(PyExc_RuntimeError,
3468 _("did not switch to the specified tab page"));
3469 return -1;
3470 }
3471
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003472 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003473 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003474 else
3475 {
3476 PyErr_SetString(PyExc_AttributeError, name);
3477 return -1;
3478 }
3479}
3480
Bram Moolenaardb913952012-06-29 12:54:53 +02003481 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003482init_range_cmd(exarg_T *eap)
3483{
3484 RangeStart = eap->line1;
3485 RangeEnd = eap->line2;
3486}
3487
3488 static void
3489init_range_eval(typval_T *rettv UNUSED)
3490{
3491 RangeStart = (PyInt) curwin->w_cursor.lnum;
3492 RangeEnd = RangeStart;
3493}
3494
3495 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003496run_cmd(const char *cmd, void *arg UNUSED
3497#ifdef PY_CAN_RECURSE
3498 , PyGILState_STATE *pygilstate UNUSED
3499#endif
3500 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003501{
3502 PyRun_SimpleString((char *) cmd);
3503}
3504
3505static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
3506static int code_hdr_len = 30;
3507
3508 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003509run_do(const char *cmd, void *arg UNUSED
3510#ifdef PY_CAN_RECURSE
3511 , PyGILState_STATE *pygilstate
3512#endif
3513 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003514{
3515 PyInt lnum;
3516 size_t len;
3517 char *code;
3518 int status;
3519 PyObject *pyfunc, *pymain;
3520
3521 if (u_save(RangeStart - 1, RangeEnd + 1) != OK)
3522 {
3523 EMSG(_("cannot save undo information"));
3524 return;
3525 }
3526
3527 len = code_hdr_len + STRLEN(cmd);
3528 code = PyMem_New(char, len + 1);
3529 memcpy(code, code_hdr, code_hdr_len);
3530 STRCPY(code + code_hdr_len, cmd);
3531 status = PyRun_SimpleString(code);
3532 PyMem_Free(code);
3533
3534 if (status)
3535 {
3536 EMSG(_("failed to run the code"));
3537 return;
3538 }
3539
3540 status = 0;
3541 pymain = PyImport_AddModule("__main__");
3542 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003543#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003544 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003545#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003546
3547 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
3548 {
3549 PyObject *line, *linenr, *ret;
3550
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003551#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003552 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003553#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003554 if (!(line = GetBufferLine(curbuf, lnum)))
3555 goto err;
3556 if (!(linenr = PyInt_FromLong((long) lnum)))
3557 {
3558 Py_DECREF(line);
3559 goto err;
3560 }
3561 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
3562 Py_DECREF(line);
3563 Py_DECREF(linenr);
3564 if (!ret)
3565 goto err;
3566
3567 if (ret != Py_None)
3568 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
3569 goto err;
3570
3571 Py_XDECREF(ret);
3572 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003573#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003574 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003575#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003576 }
3577 goto out;
3578err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003579#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003580 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003581#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003582 PyErr_PrintEx(0);
3583 PythonIO_Flush();
3584 status = 1;
3585out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003586#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003587 if (!status)
3588 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003589#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003590 Py_DECREF(pyfunc);
3591 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
3592 if (status)
3593 return;
3594 check_cursor();
3595 update_curbuf(NOT_VALID);
3596}
3597
3598 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003599run_eval(const char *cmd, typval_T *rettv
3600#ifdef PY_CAN_RECURSE
3601 , PyGILState_STATE *pygilstate UNUSED
3602#endif
3603 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003604{
3605 PyObject *r;
3606
3607 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
3608 if (r == NULL)
3609 {
3610 if (PyErr_Occurred() && !msg_silent)
3611 PyErr_PrintEx(0);
3612 EMSG(_("E858: Eval did not return a valid python object"));
3613 }
3614 else
3615 {
3616 if (ConvertFromPyObject(r, rettv) == -1)
3617 EMSG(_("E859: Failed to convert returned python object to vim value"));
3618 Py_DECREF(r);
3619 }
3620 PyErr_Clear();
3621}
3622
3623 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02003624set_ref_in_py(const int copyID)
3625{
3626 pylinkedlist_T *cur;
3627 dict_T *dd;
3628 list_T *ll;
3629
3630 if (lastdict != NULL)
3631 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3632 {
3633 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3634 if (dd->dv_copyID != copyID)
3635 {
3636 dd->dv_copyID = copyID;
3637 set_ref_in_ht(&dd->dv_hashtab, copyID);
3638 }
3639 }
3640
3641 if (lastlist != NULL)
3642 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3643 {
3644 ll = ((ListObject *) (cur->pll_obj))->list;
3645 if (ll->lv_copyID != copyID)
3646 {
3647 ll->lv_copyID = copyID;
3648 set_ref_in_list(ll, copyID);
3649 }
3650 }
3651}
3652
3653 static int
3654set_string_copy(char_u *str, typval_T *tv)
3655{
3656 tv->vval.v_string = vim_strsave(str);
3657 if (tv->vval.v_string == NULL)
3658 {
3659 PyErr_NoMemory();
3660 return -1;
3661 }
3662 return 0;
3663}
3664
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003665 static int
3666pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3667{
3668 dict_T *d;
3669 char_u *key;
3670 dictitem_T *di;
3671 PyObject *keyObject;
3672 PyObject *valObject;
3673 Py_ssize_t iter = 0;
3674
3675 d = dict_alloc();
3676 if (d == NULL)
3677 {
3678 PyErr_NoMemory();
3679 return -1;
3680 }
3681
3682 tv->v_type = VAR_DICT;
3683 tv->vval.v_dict = d;
3684
3685 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
3686 {
3687 DICTKEY_DECL
3688
3689 if (keyObject == NULL)
3690 return -1;
3691 if (valObject == NULL)
3692 return -1;
3693
3694 DICTKEY_GET_NOTEMPTY(-1)
3695
3696 di = dictitem_alloc(key);
3697
3698 DICTKEY_UNREF
3699
3700 if (di == NULL)
3701 {
3702 PyErr_NoMemory();
3703 return -1;
3704 }
3705 di->di_tv.v_lock = 0;
3706
3707 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3708 {
3709 vim_free(di);
3710 return -1;
3711 }
3712 if (dict_add(d, di) == FAIL)
3713 {
3714 vim_free(di);
3715 PyErr_SetVim(_("failed to add key to dictionary"));
3716 return -1;
3717 }
3718 }
3719 return 0;
3720}
3721
3722 static int
3723pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3724{
3725 dict_T *d;
3726 char_u *key;
3727 dictitem_T *di;
3728 PyObject *list;
3729 PyObject *litem;
3730 PyObject *keyObject;
3731 PyObject *valObject;
3732 Py_ssize_t lsize;
3733
3734 d = dict_alloc();
3735 if (d == NULL)
3736 {
3737 PyErr_NoMemory();
3738 return -1;
3739 }
3740
3741 tv->v_type = VAR_DICT;
3742 tv->vval.v_dict = d;
3743
3744 list = PyMapping_Items(obj);
3745 if (list == NULL)
3746 return -1;
3747 lsize = PyList_Size(list);
3748 while (lsize--)
3749 {
3750 DICTKEY_DECL
3751
3752 litem = PyList_GetItem(list, lsize);
3753 if (litem == NULL)
3754 {
3755 Py_DECREF(list);
3756 return -1;
3757 }
3758
3759 keyObject = PyTuple_GetItem(litem, 0);
3760 if (keyObject == NULL)
3761 {
3762 Py_DECREF(list);
3763 Py_DECREF(litem);
3764 return -1;
3765 }
3766
3767 DICTKEY_GET_NOTEMPTY(-1)
3768
3769 valObject = PyTuple_GetItem(litem, 1);
3770 if (valObject == NULL)
3771 {
3772 Py_DECREF(list);
3773 Py_DECREF(litem);
3774 return -1;
3775 }
3776
3777 di = dictitem_alloc(key);
3778
3779 DICTKEY_UNREF
3780
3781 if (di == NULL)
3782 {
3783 Py_DECREF(list);
3784 Py_DECREF(litem);
3785 PyErr_NoMemory();
3786 return -1;
3787 }
3788 di->di_tv.v_lock = 0;
3789
3790 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3791 {
3792 vim_free(di);
3793 Py_DECREF(list);
3794 Py_DECREF(litem);
3795 return -1;
3796 }
3797 if (dict_add(d, di) == FAIL)
3798 {
3799 vim_free(di);
3800 Py_DECREF(list);
3801 Py_DECREF(litem);
3802 PyErr_SetVim(_("failed to add key to dictionary"));
3803 return -1;
3804 }
3805 Py_DECREF(litem);
3806 }
3807 Py_DECREF(list);
3808 return 0;
3809}
3810
3811 static int
3812pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3813{
3814 list_T *l;
3815
3816 l = list_alloc();
3817 if (l == NULL)
3818 {
3819 PyErr_NoMemory();
3820 return -1;
3821 }
3822
3823 tv->v_type = VAR_LIST;
3824 tv->vval.v_list = l;
3825
3826 if (list_py_concat(l, obj, lookupDict) == -1)
3827 return -1;
3828
3829 return 0;
3830}
3831
3832 static int
3833pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3834{
3835 PyObject *iterator = PyObject_GetIter(obj);
3836 PyObject *item;
3837 list_T *l;
3838 listitem_T *li;
3839
3840 l = list_alloc();
3841
3842 if (l == NULL)
3843 {
3844 PyErr_NoMemory();
3845 return -1;
3846 }
3847
3848 tv->vval.v_list = l;
3849 tv->v_type = VAR_LIST;
3850
3851
3852 if (iterator == NULL)
3853 return -1;
3854
3855 while ((item = PyIter_Next(obj)))
3856 {
3857 li = listitem_alloc();
3858 if (li == NULL)
3859 {
3860 PyErr_NoMemory();
3861 return -1;
3862 }
3863 li->li_tv.v_lock = 0;
3864
3865 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
3866 return -1;
3867
3868 list_append(l, li);
3869
3870 Py_DECREF(item);
3871 }
3872
3873 Py_DECREF(iterator);
3874 return 0;
3875}
3876
Bram Moolenaardb913952012-06-29 12:54:53 +02003877typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
3878
3879 static int
3880convert_dl(PyObject *obj, typval_T *tv,
3881 pytotvfunc py_to_tv, PyObject *lookupDict)
3882{
3883 PyObject *capsule;
3884 char hexBuf[sizeof(void *) * 2 + 3];
3885
3886 sprintf(hexBuf, "%p", obj);
3887
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003888# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003889 capsule = PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003890# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003891 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003892# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02003893 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02003894 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003895# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003896 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02003897# else
3898 capsule = PyCObject_FromVoidPtr(tv, NULL);
3899# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003900 PyDict_SetItemString(lookupDict, hexBuf, capsule);
3901 Py_DECREF(capsule);
3902 if (py_to_tv(obj, tv, lookupDict) == -1)
3903 {
3904 tv->v_type = VAR_UNKNOWN;
3905 return -1;
3906 }
3907 /* As we are not using copy_tv which increments reference count we must
3908 * do it ourself. */
3909 switch(tv->v_type)
3910 {
3911 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
3912 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
3913 }
3914 }
3915 else
3916 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003917 typval_T *v;
3918
3919# ifdef PY_USE_CAPSULE
3920 v = PyCapsule_GetPointer(capsule, NULL);
3921# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003922 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003923# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003924 copy_tv(v, tv);
3925 }
3926 return 0;
3927}
3928
3929 static int
3930ConvertFromPyObject(PyObject *obj, typval_T *tv)
3931{
3932 PyObject *lookup_dict;
3933 int r;
3934
3935 lookup_dict = PyDict_New();
3936 r = _ConvertFromPyObject(obj, tv, lookup_dict);
3937 Py_DECREF(lookup_dict);
3938 return r;
3939}
3940
3941 static int
3942_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3943{
3944 if (obj->ob_type == &DictionaryType)
3945 {
3946 tv->v_type = VAR_DICT;
3947 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
3948 ++tv->vval.v_dict->dv_refcount;
3949 }
3950 else if (obj->ob_type == &ListType)
3951 {
3952 tv->v_type = VAR_LIST;
3953 tv->vval.v_list = (((ListObject *)(obj))->list);
3954 ++tv->vval.v_list->lv_refcount;
3955 }
3956 else if (obj->ob_type == &FunctionType)
3957 {
3958 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
3959 return -1;
3960
3961 tv->v_type = VAR_FUNC;
3962 func_ref(tv->vval.v_string);
3963 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003964 else if (PyBytes_Check(obj))
3965 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003966 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02003967
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003968 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
3969 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003970 if (result == NULL)
3971 return -1;
3972
3973 if (set_string_copy(result, tv) == -1)
3974 return -1;
3975
3976 tv->v_type = VAR_STRING;
3977 }
3978 else if (PyUnicode_Check(obj))
3979 {
3980 PyObject *bytes;
3981 char_u *result;
3982
Bram Moolenaardb913952012-06-29 12:54:53 +02003983 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
3984 if (bytes == NULL)
3985 return -1;
3986
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003987 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
3988 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003989 if (result == NULL)
3990 return -1;
3991
3992 if (set_string_copy(result, tv) == -1)
3993 {
3994 Py_XDECREF(bytes);
3995 return -1;
3996 }
3997 Py_XDECREF(bytes);
3998
3999 tv->v_type = VAR_STRING;
4000 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004001#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004002 else if (PyInt_Check(obj))
4003 {
4004 tv->v_type = VAR_NUMBER;
4005 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4006 }
4007#endif
4008 else if (PyLong_Check(obj))
4009 {
4010 tv->v_type = VAR_NUMBER;
4011 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4012 }
4013 else if (PyDict_Check(obj))
4014 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
4015#ifdef FEAT_FLOAT
4016 else if (PyFloat_Check(obj))
4017 {
4018 tv->v_type = VAR_FLOAT;
4019 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4020 }
4021#endif
4022 else if (PyIter_Check(obj))
4023 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
4024 else if (PySequence_Check(obj))
4025 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
4026 else if (PyMapping_Check(obj))
4027 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
4028 else
4029 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004030 PyErr_SetString(PyExc_TypeError,
4031 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004032 return -1;
4033 }
4034 return 0;
4035}
4036
4037 static PyObject *
4038ConvertToPyObject(typval_T *tv)
4039{
4040 if (tv == NULL)
4041 {
4042 PyErr_SetVim(_("NULL reference passed"));
4043 return NULL;
4044 }
4045 switch (tv->v_type)
4046 {
4047 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004048 return PyBytes_FromString(tv->vval.v_string == NULL
4049 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004050 case VAR_NUMBER:
4051 return PyLong_FromLong((long) tv->vval.v_number);
4052#ifdef FEAT_FLOAT
4053 case VAR_FLOAT:
4054 return PyFloat_FromDouble((double) tv->vval.v_float);
4055#endif
4056 case VAR_LIST:
4057 return ListNew(tv->vval.v_list);
4058 case VAR_DICT:
4059 return DictionaryNew(tv->vval.v_dict);
4060 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004061 return FunctionNew(tv->vval.v_string == NULL
4062 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004063 case VAR_UNKNOWN:
4064 Py_INCREF(Py_None);
4065 return Py_None;
4066 default:
4067 PyErr_SetVim(_("internal error: invalid value type"));
4068 return NULL;
4069 }
4070}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004071
4072typedef struct
4073{
4074 PyObject_HEAD
4075} CurrentObject;
4076static PyTypeObject CurrentType;
4077
4078 static void
4079init_structs(void)
4080{
4081 vim_memset(&OutputType, 0, sizeof(OutputType));
4082 OutputType.tp_name = "vim.message";
4083 OutputType.tp_basicsize = sizeof(OutputObject);
4084 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4085 OutputType.tp_doc = "vim message object";
4086 OutputType.tp_methods = OutputMethods;
4087#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004088 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4089 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004090 OutputType.tp_alloc = call_PyType_GenericAlloc;
4091 OutputType.tp_new = call_PyType_GenericNew;
4092 OutputType.tp_free = call_PyObject_Free;
4093#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004094 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4095 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004096#endif
4097
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004098 vim_memset(&IterType, 0, sizeof(IterType));
4099 IterType.tp_name = "vim.iter";
4100 IterType.tp_basicsize = sizeof(IterObject);
4101 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
4102 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004103 IterType.tp_iter = (getiterfunc)IterIter;
4104 IterType.tp_iternext = (iternextfunc)IterNext;
4105 IterType.tp_dealloc = (destructor)IterDestructor;
4106 IterType.tp_traverse = (traverseproc)IterTraverse;
4107 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004108
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004109 vim_memset(&BufferType, 0, sizeof(BufferType));
4110 BufferType.tp_name = "vim.buffer";
4111 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004112 BufferType.tp_dealloc = (destructor)BufferDestructor;
4113 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004114 BufferType.tp_as_sequence = &BufferAsSeq;
4115 BufferType.tp_as_mapping = &BufferAsMapping;
4116 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4117 BufferType.tp_doc = "vim buffer object";
4118 BufferType.tp_methods = BufferMethods;
4119#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004120 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004121 BufferType.tp_alloc = call_PyType_GenericAlloc;
4122 BufferType.tp_new = call_PyType_GenericNew;
4123 BufferType.tp_free = call_PyObject_Free;
4124#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004125 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004126#endif
4127
4128 vim_memset(&WindowType, 0, sizeof(WindowType));
4129 WindowType.tp_name = "vim.window";
4130 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004131 WindowType.tp_dealloc = (destructor)WindowDestructor;
4132 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004133 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
4134 WindowType.tp_doc = "vim Window object";
4135 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004136 WindowType.tp_traverse = (traverseproc)WindowTraverse;
4137 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004138#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004139 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
4140 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004141 WindowType.tp_alloc = call_PyType_GenericAlloc;
4142 WindowType.tp_new = call_PyType_GenericNew;
4143 WindowType.tp_free = call_PyObject_Free;
4144#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004145 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
4146 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004147#endif
4148
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004149 vim_memset(&TabPageType, 0, sizeof(TabPageType));
4150 TabPageType.tp_name = "vim.tabpage";
4151 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004152 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
4153 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004154 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
4155 TabPageType.tp_doc = "vim tab page object";
4156 TabPageType.tp_methods = TabPageMethods;
4157#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004158 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004159 TabPageType.tp_alloc = call_PyType_GenericAlloc;
4160 TabPageType.tp_new = call_PyType_GenericNew;
4161 TabPageType.tp_free = call_PyObject_Free;
4162#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004163 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004164#endif
4165
Bram Moolenaardfa38d42013-05-15 13:38:47 +02004166 vim_memset(&BufMapType, 0, sizeof(BufMapType));
4167 BufMapType.tp_name = "vim.bufferlist";
4168 BufMapType.tp_basicsize = sizeof(BufMapObject);
4169 BufMapType.tp_as_mapping = &BufMapAsMapping;
4170 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004171 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004172 BufferType.tp_doc = "vim buffer list";
4173
4174 vim_memset(&WinListType, 0, sizeof(WinListType));
4175 WinListType.tp_name = "vim.windowlist";
4176 WinListType.tp_basicsize = sizeof(WinListType);
4177 WinListType.tp_as_sequence = &WinListAsSeq;
4178 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
4179 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004180 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004181
4182 vim_memset(&TabListType, 0, sizeof(TabListType));
4183 TabListType.tp_name = "vim.tabpagelist";
4184 TabListType.tp_basicsize = sizeof(TabListType);
4185 TabListType.tp_as_sequence = &TabListAsSeq;
4186 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
4187 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004188
4189 vim_memset(&RangeType, 0, sizeof(RangeType));
4190 RangeType.tp_name = "vim.range";
4191 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004192 RangeType.tp_dealloc = (destructor)RangeDestructor;
4193 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004194 RangeType.tp_as_sequence = &RangeAsSeq;
4195 RangeType.tp_as_mapping = &RangeAsMapping;
4196 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
4197 RangeType.tp_doc = "vim Range object";
4198 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004199 RangeType.tp_traverse = (traverseproc)RangeTraverse;
4200 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004201#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004202 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004203 RangeType.tp_alloc = call_PyType_GenericAlloc;
4204 RangeType.tp_new = call_PyType_GenericNew;
4205 RangeType.tp_free = call_PyObject_Free;
4206#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004207 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004208#endif
4209
4210 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4211 CurrentType.tp_name = "vim.currentdata";
4212 CurrentType.tp_basicsize = sizeof(CurrentObject);
4213 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4214 CurrentType.tp_doc = "vim current object";
4215#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004216 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
4217 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004218#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004219 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
4220 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004221#endif
4222
4223 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4224 DictionaryType.tp_name = "vim.dictionary";
4225 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004226 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004227 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4228 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4229 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4230 DictionaryType.tp_methods = DictionaryMethods;
4231#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004232 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
4233 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004234#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004235 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
4236 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004237#endif
4238
4239 vim_memset(&ListType, 0, sizeof(ListType));
4240 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004241 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004242 ListType.tp_basicsize = sizeof(ListObject);
4243 ListType.tp_as_sequence = &ListAsSeq;
4244 ListType.tp_as_mapping = &ListAsMapping;
4245 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4246 ListType.tp_doc = "list pushing modifications to vim structure";
4247 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004248 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004249#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004250 ListType.tp_getattro = (getattrofunc)ListGetattro;
4251 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004252#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004253 ListType.tp_getattr = (getattrfunc)ListGetattr;
4254 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004255#endif
4256
4257 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004258 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004259 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004260 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
4261 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004262 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4263 FunctionType.tp_doc = "object that calls vim function";
4264 FunctionType.tp_methods = FunctionMethods;
4265#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004266 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004267#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004268 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004269#endif
4270
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004271 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4272 OptionsType.tp_name = "vim.options";
4273 OptionsType.tp_basicsize = sizeof(OptionsObject);
4274 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4275 OptionsType.tp_doc = "object for manipulating options";
4276 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004277 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
4278 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
4279 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004280
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004281#if PY_MAJOR_VERSION >= 3
4282 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4283 vimmodule.m_name = "vim";
4284 vimmodule.m_doc = "Vim Python interface\n";
4285 vimmodule.m_size = -1;
4286 vimmodule.m_methods = VimMethods;
4287#endif
4288}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004289
4290#define PYTYPE_READY(type) \
4291 if (PyType_Ready(&type)) \
4292 return -1;
4293
4294 static int
4295init_types()
4296{
4297 PYTYPE_READY(IterType);
4298 PYTYPE_READY(BufferType);
4299 PYTYPE_READY(RangeType);
4300 PYTYPE_READY(WindowType);
4301 PYTYPE_READY(TabPageType);
4302 PYTYPE_READY(BufMapType);
4303 PYTYPE_READY(WinListType);
4304 PYTYPE_READY(TabListType);
4305 PYTYPE_READY(CurrentType);
4306 PYTYPE_READY(DictionaryType);
4307 PYTYPE_READY(ListType);
4308 PYTYPE_READY(FunctionType);
4309 PYTYPE_READY(OptionsType);
4310 PYTYPE_READY(OutputType);
4311 return 0;
4312}
4313
4314static BufMapObject TheBufferMap =
4315{
4316 PyObject_HEAD_INIT(&BufMapType)
4317};
4318
4319static WinListObject TheWindowList =
4320{
4321 PyObject_HEAD_INIT(&WinListType)
4322 NULL
4323};
4324
4325static CurrentObject TheCurrent =
4326{
4327 PyObject_HEAD_INIT(&CurrentType)
4328};
4329
4330static TabListObject TheTabPageList =
4331{
4332 PyObject_HEAD_INIT(&TabListType)
4333};
4334
4335static struct numeric_constant {
4336 char *name;
4337 int value;
4338} numeric_constants[] = {
4339 {"VAR_LOCKED", VAR_LOCKED},
4340 {"VAR_FIXED", VAR_FIXED},
4341 {"VAR_SCOPE", VAR_SCOPE},
4342 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
4343};
4344
4345static struct object_constant {
4346 char *name;
4347 PyObject *value;
4348} object_constants[] = {
4349 {"buffers", (PyObject *)(void *)&TheBufferMap},
4350 {"windows", (PyObject *)(void *)&TheWindowList},
4351 {"tabpages", (PyObject *)(void *)&TheTabPageList},
4352 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02004353
4354 {"Buffer", (PyObject *)&BufferType},
4355 {"Range", (PyObject *)&RangeType},
4356 {"Window", (PyObject *)&WindowType},
4357 {"TabPage", (PyObject *)&TabPageType},
4358 {"Dictionary", (PyObject *)&DictionaryType},
4359 {"List", (PyObject *)&ListType},
4360 {"Function", (PyObject *)&FunctionType},
4361 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004362};
4363
4364typedef int (*object_adder)(PyObject *, const char *, PyObject *);
4365
4366#define ADD_OBJECT(m, name, obj) \
4367 if (add_object(m, name, obj)) \
4368 return -1;
4369
4370#define ADD_CHECKED_OBJECT(m, name, obj) \
4371 { \
4372 PyObject *value = obj; \
4373 if (!value) \
4374 return -1; \
4375 ADD_OBJECT(m, name, value); \
4376 }
4377
4378 static int
4379populate_module(PyObject *m, object_adder add_object)
4380{
4381 int i;
4382
4383 for (i = 0; i < (int)(sizeof(numeric_constants)
4384 / sizeof(struct numeric_constant));
4385 ++i)
4386 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
4387 PyInt_FromLong(numeric_constants[i].value));
4388
4389 for (i = 0; i < (int)(sizeof(object_constants)
4390 / sizeof(struct object_constant));
4391 ++i)
4392 {
4393 PyObject *value;
4394
4395 value = object_constants[i].value;
4396 Py_INCREF(value);
4397 ADD_OBJECT(m, object_constants[i].name, value);
4398 }
4399
4400 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
4401 return -1;
4402 ADD_OBJECT(m, "error", VimError);
4403
4404 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict));
4405 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
4406 ADD_CHECKED_OBJECT(m, "options",
4407 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
4408 return 0;
4409}