blob: 777e7dae049abdf318afee56c7c41b6fbaacde9b [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
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001524set_option_value_err(key, numval, stringval, opt_flags)
1525 char_u *key;
1526 int numval;
1527 char_u *stringval;
1528 int opt_flags;
1529{
1530 char_u *errmsg;
1531
1532 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
1533 {
1534 if (VimTryEnd())
1535 return FAIL;
1536 PyErr_SetVim((char *)errmsg);
1537 return FAIL;
1538 }
1539 return OK;
1540}
1541
1542 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001543set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1544 char_u *key;
1545 int numval;
1546 char_u *stringval;
1547 int opt_flags;
1548 int opt_type;
1549 void *from;
1550{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001551 win_T *save_curwin = NULL;
1552 tabpage_T *save_curtab = NULL;
1553 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001554 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001555
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001556 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001557 switch (opt_type)
1558 {
1559 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001560 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1561 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001562 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001563 if (VimTryEnd())
1564 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001565 PyErr_SetVim("Problem while switching windows.");
1566 return -1;
1567 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001568 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001569 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001570 if (r == FAIL)
1571 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001572 break;
1573 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001574 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001575 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001576 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001577 if (r == FAIL)
1578 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001579 break;
1580 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001581 r = set_option_value_err(key, numval, stringval, opt_flags);
1582 if (r == FAIL)
1583 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001584 break;
1585 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001586 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001587}
1588
1589 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001590OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001591{
1592 char_u *key;
1593 int flags;
1594 int opt_flags;
1595 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001596 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001597
Bram Moolenaard6e39182013-05-21 18:30:34 +02001598 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001599 return -1;
1600
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001601 DICTKEY_GET_NOTEMPTY(-1)
1602
1603 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001604 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001605
1606 DICTKEY_UNREF
1607
1608 if (flags == 0)
1609 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001610 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001611 return -1;
1612 }
1613
1614 if (valObject == NULL)
1615 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001616 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001617 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001618 PyErr_SetString(PyExc_ValueError,
1619 _("unable to unset global option"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001620 return -1;
1621 }
1622 else if (!(flags & SOPT_GLOBAL))
1623 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001624 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1625 "without global value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001626 return -1;
1627 }
1628 else
1629 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001630 unset_global_local_option(key, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001631 return 0;
1632 }
1633 }
1634
Bram Moolenaard6e39182013-05-21 18:30:34 +02001635 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001636
1637 if (flags & SOPT_BOOL)
1638 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001639 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001640
Bram Moolenaarb983f752013-05-15 16:11:50 +02001641 if (istrue == -1)
1642 return -1;
1643 r = set_option_value_for(key, istrue, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001644 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001645 }
1646 else if (flags & SOPT_NUM)
1647 {
1648 int val;
1649
1650#if PY_MAJOR_VERSION < 3
1651 if (PyInt_Check(valObject))
1652 val = PyInt_AsLong(valObject);
1653 else
1654#endif
1655 if (PyLong_Check(valObject))
1656 val = PyLong_AsLong(valObject);
1657 else
1658 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001659 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001660 return -1;
1661 }
1662
1663 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001664 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001665 }
1666 else
1667 {
1668 char_u *val;
1669 if (PyBytes_Check(valObject))
1670 {
1671
1672 if (PyString_AsStringAndSize(valObject, (char **) &val, NULL) == -1)
1673 return -1;
1674 if (val == NULL)
1675 return -1;
1676
1677 val = vim_strsave(val);
1678 }
1679 else if (PyUnicode_Check(valObject))
1680 {
1681 PyObject *bytes;
1682
1683 bytes = PyUnicode_AsEncodedString(valObject, (char *)ENC_OPT, NULL);
1684 if (bytes == NULL)
1685 return -1;
1686
1687 if(PyString_AsStringAndSize(bytes, (char **) &val, NULL) == -1)
1688 return -1;
1689 if (val == NULL)
1690 return -1;
1691
1692 val = vim_strsave(val);
1693 Py_XDECREF(bytes);
1694 }
1695 else
1696 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001697 PyErr_SetString(PyExc_TypeError, _("object must be string"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001698 return -1;
1699 }
1700
1701 r = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001702 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001703 vim_free(val);
1704 }
1705
1706 return r;
1707}
1708
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001709static PyMappingMethods OptionsAsMapping = {
1710 (lenfunc) NULL,
1711 (binaryfunc) OptionsItem,
1712 (objobjargproc) OptionsAssItem,
1713};
1714
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001715/* Tabpage object
1716 */
1717
1718typedef struct
1719{
1720 PyObject_HEAD
1721 tabpage_T *tab;
1722} TabPageObject;
1723
1724static PyObject *WinListNew(TabPageObject *tabObject);
1725
1726static PyTypeObject TabPageType;
1727
1728 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001729CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001730{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001731 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001732 {
1733 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1734 return -1;
1735 }
1736
1737 return 0;
1738}
1739
1740 static PyObject *
1741TabPageNew(tabpage_T *tab)
1742{
1743 TabPageObject *self;
1744
1745 if (TAB_PYTHON_REF(tab))
1746 {
1747 self = TAB_PYTHON_REF(tab);
1748 Py_INCREF(self);
1749 }
1750 else
1751 {
1752 self = PyObject_NEW(TabPageObject, &TabPageType);
1753 if (self == NULL)
1754 return NULL;
1755 self->tab = tab;
1756 TAB_PYTHON_REF(tab) = self;
1757 }
1758
1759 return (PyObject *)(self);
1760}
1761
1762 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001763TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001764{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001765 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
1766 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001767
1768 DESTRUCTOR_FINISH(self);
1769}
1770
1771 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001772TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001773{
1774 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001775 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001776 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001777 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001778 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001779 return DictionaryNew(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001780 else if (strcmp(name, "window") == 0)
1781 {
1782 /* For current tab window.c does not bother to set or update tp_curwin
1783 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02001784 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001785 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001786 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001787 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001788 }
1789 return NULL;
1790}
1791
1792 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001793TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001794{
1795 static char repr[100];
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001796
Bram Moolenaard6e39182013-05-21 18:30:34 +02001797 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001798 {
1799 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
1800 return PyString_FromString(repr);
1801 }
1802 else
1803 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001804 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001805
1806 if (t == 0)
1807 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
1808 (self));
1809 else
1810 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
1811
1812 return PyString_FromString(repr);
1813 }
1814}
1815
1816static struct PyMethodDef TabPageMethods[] = {
1817 /* name, function, calling, documentation */
1818 { NULL, NULL, 0, NULL }
1819};
1820
1821/*
1822 * Window list object
1823 */
1824
1825static PyTypeObject TabListType;
1826static PySequenceMethods TabListAsSeq;
1827
1828typedef struct
1829{
1830 PyObject_HEAD
1831} TabListObject;
1832
1833 static PyInt
1834TabListLength(PyObject *self UNUSED)
1835{
1836 tabpage_T *tp = first_tabpage;
1837 PyInt n = 0;
1838
1839 while (tp != NULL)
1840 {
1841 ++n;
1842 tp = tp->tp_next;
1843 }
1844
1845 return n;
1846}
1847
1848 static PyObject *
1849TabListItem(PyObject *self UNUSED, PyInt n)
1850{
1851 tabpage_T *tp;
1852
1853 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
1854 if (n == 0)
1855 return TabPageNew(tp);
1856
1857 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
1858 return NULL;
1859}
1860
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001861/* Window object
1862 */
1863
1864typedef struct
1865{
1866 PyObject_HEAD
1867 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001868 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001869} WindowObject;
1870
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001871static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001872
1873 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001874CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001875{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001876 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001877 {
1878 PyErr_SetVim(_("attempt to refer to deleted window"));
1879 return -1;
1880 }
1881
1882 return 0;
1883}
1884
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001885 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001886WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02001887{
1888 /* We need to handle deletion of windows underneath us.
1889 * If we add a "w_python*_ref" field to the win_T structure,
1890 * then we can get at it in win_free() in vim. We then
1891 * need to create only ONE Python object per window - if
1892 * we try to create a second, just INCREF the existing one
1893 * and return it. The (single) Python object referring to
1894 * the window is stored in "w_python*_ref".
1895 * On a win_free() we set the Python object's win_T* field
1896 * to an invalid value. We trap all uses of a window
1897 * object, and reject them if the win_T* field is invalid.
1898 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001899 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02001900 * w_python_ref and w_python3_ref fields respectively.
1901 */
1902
1903 WindowObject *self;
1904
1905 if (WIN_PYTHON_REF(win))
1906 {
1907 self = WIN_PYTHON_REF(win);
1908 Py_INCREF(self);
1909 }
1910 else
1911 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02001912 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02001913 if (self == NULL)
1914 return NULL;
1915 self->win = win;
1916 WIN_PYTHON_REF(win) = self;
1917 }
1918
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001919 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
1920
Bram Moolenaar971db462013-05-12 18:44:48 +02001921 return (PyObject *)(self);
1922}
1923
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001924 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001925WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001926{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001927 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001928 if (self->win && self->win != INVALID_WINDOW_VALUE)
1929 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02001930 Py_XDECREF(((PyObject *)(self->tabObject)));
1931 PyObject_GC_Del((void *)(self));
1932}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001933
Bram Moolenaar774267b2013-05-21 20:51:59 +02001934 static int
1935WindowTraverse(WindowObject *self, visitproc visit, void *arg)
1936{
1937 Py_VISIT(((PyObject *)(self->tabObject)));
1938 return 0;
1939}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001940
Bram Moolenaar774267b2013-05-21 20:51:59 +02001941 static int
1942WindowClear(WindowObject *self)
1943{
1944 Py_CLEAR(self->tabObject);
1945 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001946}
1947
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001948 static win_T *
1949get_firstwin(TabPageObject *tabObject)
1950{
1951 if (tabObject)
1952 {
1953 if (CheckTabPage(tabObject))
1954 return NULL;
1955 /* For current tab window.c does not bother to set or update tp_firstwin
1956 */
1957 else if (tabObject->tab == curtab)
1958 return firstwin;
1959 else
1960 return tabObject->tab->tp_firstwin;
1961 }
1962 else
1963 return firstwin;
1964}
1965
Bram Moolenaar971db462013-05-12 18:44:48 +02001966 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001967WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001968{
1969 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001970 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001971 else if (strcmp(name, "cursor") == 0)
1972 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001973 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001974
1975 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1976 }
1977 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001978 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001979#ifdef FEAT_WINDOWS
1980 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001981 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001982#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001983#ifdef FEAT_VERTSPLIT
1984 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001985 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001986 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001987 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001988#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001989 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001990 return DictionaryNew(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001991 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001992 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
1993 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02001994 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001995 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001996 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001997 return NULL;
1998 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001999 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002000 }
2001 else if (strcmp(name, "tabpage") == 0)
2002 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002003 Py_INCREF(self->tabObject);
2004 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002005 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002006 else if (strcmp(name,"__members__") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002007 return Py_BuildValue("[sssssssss]", "buffer", "cursor", "height",
2008 "vars", "options", "number", "row", "col", "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002009 else
2010 return NULL;
2011}
2012
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002013 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002014WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002015{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002016 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002017 return -1;
2018
2019 if (strcmp(name, "buffer") == 0)
2020 {
2021 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2022 return -1;
2023 }
2024 else if (strcmp(name, "cursor") == 0)
2025 {
2026 long lnum;
2027 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002028
2029 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2030 return -1;
2031
Bram Moolenaard6e39182013-05-21 18:30:34 +02002032 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002033 {
2034 PyErr_SetVim(_("cursor position outside buffer"));
2035 return -1;
2036 }
2037
2038 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002039 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002040 return -1;
2041
Bram Moolenaard6e39182013-05-21 18:30:34 +02002042 self->win->w_cursor.lnum = lnum;
2043 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002044#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002045 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002046#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002047 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002048 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002049
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002050 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002051 return 0;
2052 }
2053 else if (strcmp(name, "height") == 0)
2054 {
2055 int height;
2056 win_T *savewin;
2057
2058 if (!PyArg_Parse(val, "i", &height))
2059 return -1;
2060
2061#ifdef FEAT_GUI
2062 need_mouse_correct = TRUE;
2063#endif
2064 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002065 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002066
2067 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002068 win_setheight(height);
2069 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002070 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002071 return -1;
2072
2073 return 0;
2074 }
2075#ifdef FEAT_VERTSPLIT
2076 else if (strcmp(name, "width") == 0)
2077 {
2078 int width;
2079 win_T *savewin;
2080
2081 if (!PyArg_Parse(val, "i", &width))
2082 return -1;
2083
2084#ifdef FEAT_GUI
2085 need_mouse_correct = TRUE;
2086#endif
2087 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002088 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002089
2090 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002091 win_setwidth(width);
2092 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002093 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002094 return -1;
2095
2096 return 0;
2097 }
2098#endif
2099 else
2100 {
2101 PyErr_SetString(PyExc_AttributeError, name);
2102 return -1;
2103 }
2104}
2105
2106 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002107WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002108{
2109 static char repr[100];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002110
Bram Moolenaard6e39182013-05-21 18:30:34 +02002111 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002112 {
2113 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2114 return PyString_FromString(repr);
2115 }
2116 else
2117 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002118 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002119
Bram Moolenaar6d216452013-05-12 19:00:41 +02002120 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002121 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2122 (self));
2123 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002124 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002125
2126 return PyString_FromString(repr);
2127 }
2128}
2129
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002130static struct PyMethodDef WindowMethods[] = {
2131 /* name, function, calling, documentation */
2132 { NULL, NULL, 0, NULL }
2133};
2134
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002135/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002136 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002137 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002138
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002139static PyTypeObject WinListType;
2140static PySequenceMethods WinListAsSeq;
2141
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002142typedef struct
2143{
2144 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002145 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002146} WinListObject;
2147
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002148 static PyObject *
2149WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002150{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002151 WinListObject *self;
2152
2153 self = PyObject_NEW(WinListObject, &WinListType);
2154 self->tabObject = tabObject;
2155 Py_INCREF(tabObject);
2156
2157 return (PyObject *)(self);
2158}
2159
2160 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002161WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002162{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002163 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002164
2165 if (tabObject)
2166 Py_DECREF((PyObject *)(tabObject));
2167
2168 DESTRUCTOR_FINISH(self);
2169}
2170
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002171 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002172WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002173{
2174 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002175 PyInt n = 0;
2176
Bram Moolenaard6e39182013-05-21 18:30:34 +02002177 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002178 return -1;
2179
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002180 while (w != NULL)
2181 {
2182 ++n;
2183 w = W_NEXT(w);
2184 }
2185
2186 return n;
2187}
2188
2189 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002190WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002191{
2192 win_T *w;
2193
Bram Moolenaard6e39182013-05-21 18:30:34 +02002194 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002195 return NULL;
2196
2197 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002198 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002199 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002200
2201 PyErr_SetString(PyExc_IndexError, _("no such window"));
2202 return NULL;
2203}
2204
2205/* Convert a Python string into a Vim line.
2206 *
2207 * The result is in allocated memory. All internal nulls are replaced by
2208 * newline characters. It is an error for the string to contain newline
2209 * characters.
2210 *
2211 * On errors, the Python exception data is set, and NULL is returned.
2212 */
2213 static char *
2214StringToLine(PyObject *obj)
2215{
2216 const char *str;
2217 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002218 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002219 PyInt len;
2220 PyInt i;
2221 char *p;
2222
2223 if (obj == NULL || !PyString_Check(obj))
2224 {
2225 PyErr_BadArgument();
2226 return NULL;
2227 }
2228
Bram Moolenaar19e60942011-06-19 00:27:51 +02002229 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2230 str = PyString_AsString(bytes);
2231 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002232
2233 /*
2234 * Error checking: String must not contain newlines, as we
2235 * are replacing a single line, and we must replace it with
2236 * a single line.
2237 * A trailing newline is removed, so that append(f.readlines()) works.
2238 */
2239 p = memchr(str, '\n', len);
2240 if (p != NULL)
2241 {
2242 if (p == str + len - 1)
2243 --len;
2244 else
2245 {
2246 PyErr_SetVim(_("string cannot contain newlines"));
2247 return NULL;
2248 }
2249 }
2250
2251 /* Create a copy of the string, with internal nulls replaced by
2252 * newline characters, as is the vim convention.
2253 */
2254 save = (char *)alloc((unsigned)(len+1));
2255 if (save == NULL)
2256 {
2257 PyErr_NoMemory();
2258 return NULL;
2259 }
2260
2261 for (i = 0; i < len; ++i)
2262 {
2263 if (str[i] == '\0')
2264 save[i] = '\n';
2265 else
2266 save[i] = str[i];
2267 }
2268
2269 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002270 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002271
2272 return save;
2273}
2274
2275/* Get a line from the specified buffer. The line number is
2276 * in Vim format (1-based). The line is returned as a Python
2277 * string object.
2278 */
2279 static PyObject *
2280GetBufferLine(buf_T *buf, PyInt n)
2281{
2282 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2283}
2284
2285
2286/* Get a list of lines from the specified buffer. The line numbers
2287 * are in Vim format (1-based). The range is from lo up to, but not
2288 * including, hi. The list is returned as a Python list of string objects.
2289 */
2290 static PyObject *
2291GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2292{
2293 PyInt i;
2294 PyInt n = hi - lo;
2295 PyObject *list = PyList_New(n);
2296
2297 if (list == NULL)
2298 return NULL;
2299
2300 for (i = 0; i < n; ++i)
2301 {
2302 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2303
2304 /* Error check - was the Python string creation OK? */
2305 if (str == NULL)
2306 {
2307 Py_DECREF(list);
2308 return NULL;
2309 }
2310
2311 /* Set the list item */
2312 if (PyList_SetItem(list, i, str))
2313 {
2314 Py_DECREF(str);
2315 Py_DECREF(list);
2316 return NULL;
2317 }
2318 }
2319
2320 /* The ownership of the Python list is passed to the caller (ie,
2321 * the caller should Py_DECREF() the object when it is finished
2322 * with it).
2323 */
2324
2325 return list;
2326}
2327
2328/*
2329 * Check if deleting lines made the cursor position invalid.
2330 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2331 * deleted).
2332 */
2333 static void
2334py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2335{
2336 if (curwin->w_cursor.lnum >= lo)
2337 {
2338 /* Adjust the cursor position if it's in/after the changed
2339 * lines. */
2340 if (curwin->w_cursor.lnum >= hi)
2341 {
2342 curwin->w_cursor.lnum += extra;
2343 check_cursor_col();
2344 }
2345 else if (extra < 0)
2346 {
2347 curwin->w_cursor.lnum = lo;
2348 check_cursor();
2349 }
2350 else
2351 check_cursor_col();
2352 changed_cline_bef_curs();
2353 }
2354 invalidate_botline();
2355}
2356
Bram Moolenaar19e60942011-06-19 00:27:51 +02002357/*
2358 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002359 * in Vim format (1-based). The replacement line is given as
2360 * a Python string object. The object is checked for validity
2361 * and correct format. Errors are returned as a value of FAIL.
2362 * The return value is OK on success.
2363 * If OK is returned and len_change is not NULL, *len_change
2364 * is set to the change in the buffer length.
2365 */
2366 static int
2367SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2368{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002369 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002370 * There are three cases:
2371 * 1. NULL, or None - this is a deletion.
2372 * 2. A string - this is a replacement.
2373 * 3. Anything else - this is an error.
2374 */
2375 if (line == Py_None || line == NULL)
2376 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002377 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002378
2379 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002380 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002381
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002382 VimTryStart();
2383
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002384 if (u_savedel((linenr_T)n, 1L) == FAIL)
2385 PyErr_SetVim(_("cannot save undo information"));
2386 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2387 PyErr_SetVim(_("cannot delete line"));
2388 else
2389 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002390 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002391 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2392 deleted_lines_mark((linenr_T)n, 1L);
2393 }
2394
Bram Moolenaar105bc352013-05-17 16:03:57 +02002395 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002396
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002397 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002398 return FAIL;
2399
2400 if (len_change)
2401 *len_change = -1;
2402
2403 return OK;
2404 }
2405 else if (PyString_Check(line))
2406 {
2407 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002408 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002409
2410 if (save == NULL)
2411 return FAIL;
2412
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002413 VimTryStart();
2414
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002415 /* We do not need to free "save" if ml_replace() consumes it. */
2416 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002417 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002418
2419 if (u_savesub((linenr_T)n) == FAIL)
2420 {
2421 PyErr_SetVim(_("cannot save undo information"));
2422 vim_free(save);
2423 }
2424 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2425 {
2426 PyErr_SetVim(_("cannot replace line"));
2427 vim_free(save);
2428 }
2429 else
2430 changed_bytes((linenr_T)n, 0);
2431
Bram Moolenaar105bc352013-05-17 16:03:57 +02002432 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002433
2434 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002435 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002436 check_cursor_col();
2437
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002438 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002439 return FAIL;
2440
2441 if (len_change)
2442 *len_change = 0;
2443
2444 return OK;
2445 }
2446 else
2447 {
2448 PyErr_BadArgument();
2449 return FAIL;
2450 }
2451}
2452
Bram Moolenaar19e60942011-06-19 00:27:51 +02002453/* Replace a range of lines in the specified buffer. The line numbers are in
2454 * Vim format (1-based). The range is from lo up to, but not including, hi.
2455 * The replacement lines are given as a Python list of string objects. The
2456 * list is checked for validity and correct format. Errors are returned as a
2457 * value of FAIL. The return value is OK on success.
2458 * If OK is returned and len_change is not NULL, *len_change
2459 * is set to the change in the buffer length.
2460 */
2461 static int
2462SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2463{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002464 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002465 * There are three cases:
2466 * 1. NULL, or None - this is a deletion.
2467 * 2. A list - this is a replacement.
2468 * 3. Anything else - this is an error.
2469 */
2470 if (list == Py_None || list == NULL)
2471 {
2472 PyInt i;
2473 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002474 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002475
2476 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002477 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002478 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002479
2480 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2481 PyErr_SetVim(_("cannot save undo information"));
2482 else
2483 {
2484 for (i = 0; i < n; ++i)
2485 {
2486 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2487 {
2488 PyErr_SetVim(_("cannot delete line"));
2489 break;
2490 }
2491 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002492 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002493 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2494 deleted_lines_mark((linenr_T)lo, (long)i);
2495 }
2496
Bram Moolenaar105bc352013-05-17 16:03:57 +02002497 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002498
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002499 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002500 return FAIL;
2501
2502 if (len_change)
2503 *len_change = -n;
2504
2505 return OK;
2506 }
2507 else if (PyList_Check(list))
2508 {
2509 PyInt i;
2510 PyInt new_len = PyList_Size(list);
2511 PyInt old_len = hi - lo;
2512 PyInt extra = 0; /* lines added to text, can be negative */
2513 char **array;
2514 buf_T *savebuf;
2515
2516 if (new_len == 0) /* avoid allocating zero bytes */
2517 array = NULL;
2518 else
2519 {
2520 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
2521 if (array == NULL)
2522 {
2523 PyErr_NoMemory();
2524 return FAIL;
2525 }
2526 }
2527
2528 for (i = 0; i < new_len; ++i)
2529 {
2530 PyObject *line = PyList_GetItem(list, i);
2531
2532 array[i] = StringToLine(line);
2533 if (array[i] == NULL)
2534 {
2535 while (i)
2536 vim_free(array[--i]);
2537 vim_free(array);
2538 return FAIL;
2539 }
2540 }
2541
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002542 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02002543 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002544
2545 // START of region without "return". Must call restore_buffer()!
2546 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002547
2548 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2549 PyErr_SetVim(_("cannot save undo information"));
2550
2551 /* If the size of the range is reducing (ie, new_len < old_len) we
2552 * need to delete some old_len. We do this at the start, by
2553 * repeatedly deleting line "lo".
2554 */
2555 if (!PyErr_Occurred())
2556 {
2557 for (i = 0; i < old_len - new_len; ++i)
2558 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2559 {
2560 PyErr_SetVim(_("cannot delete line"));
2561 break;
2562 }
2563 extra -= i;
2564 }
2565
2566 /* For as long as possible, replace the existing old_len with the
2567 * new old_len. This is a more efficient operation, as it requires
2568 * less memory allocation and freeing.
2569 */
2570 if (!PyErr_Occurred())
2571 {
2572 for (i = 0; i < old_len && i < new_len; ++i)
2573 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2574 == FAIL)
2575 {
2576 PyErr_SetVim(_("cannot replace line"));
2577 break;
2578 }
2579 }
2580 else
2581 i = 0;
2582
2583 /* Now we may need to insert the remaining new old_len. If we do, we
2584 * must free the strings as we finish with them (we can't pass the
2585 * responsibility to vim in this case).
2586 */
2587 if (!PyErr_Occurred())
2588 {
2589 while (i < new_len)
2590 {
2591 if (ml_append((linenr_T)(lo + i - 1),
2592 (char_u *)array[i], 0, FALSE) == FAIL)
2593 {
2594 PyErr_SetVim(_("cannot insert line"));
2595 break;
2596 }
2597 vim_free(array[i]);
2598 ++i;
2599 ++extra;
2600 }
2601 }
2602
2603 /* Free any left-over old_len, as a result of an error */
2604 while (i < new_len)
2605 {
2606 vim_free(array[i]);
2607 ++i;
2608 }
2609
2610 /* Free the array of old_len. All of its contents have now
2611 * been dealt with (either freed, or the responsibility passed
2612 * to vim.
2613 */
2614 vim_free(array);
2615
2616 /* Adjust marks. Invalidate any which lie in the
2617 * changed range, and move any in the remainder of the buffer.
2618 */
2619 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2620 (long)MAXLNUM, (long)extra);
2621 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2622
Bram Moolenaar105bc352013-05-17 16:03:57 +02002623 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002624 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2625
Bram Moolenaar105bc352013-05-17 16:03:57 +02002626 // END of region without "return".
2627 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002628
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002629 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002630 return FAIL;
2631
2632 if (len_change)
2633 *len_change = new_len - old_len;
2634
2635 return OK;
2636 }
2637 else
2638 {
2639 PyErr_BadArgument();
2640 return FAIL;
2641 }
2642}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002643
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002644/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002645 * The line number is in Vim format (1-based). The lines to be inserted are
2646 * given as a Python list of string objects or as a single string. The lines
2647 * to be added are checked for validity and correct format. Errors are
2648 * returned as a value of FAIL. The return value is OK on success.
2649 * If OK is returned and len_change is not NULL, *len_change
2650 * is set to the change in the buffer length.
2651 */
2652 static int
2653InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2654{
2655 /* First of all, we check the type of the supplied Python object.
2656 * It must be a string or a list, or the call is in error.
2657 */
2658 if (PyString_Check(lines))
2659 {
2660 char *str = StringToLine(lines);
2661 buf_T *savebuf;
2662
2663 if (str == NULL)
2664 return FAIL;
2665
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002666 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002667 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002668 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002669
2670 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2671 PyErr_SetVim(_("cannot save undo information"));
2672 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2673 PyErr_SetVim(_("cannot insert line"));
2674 else
2675 appended_lines_mark((linenr_T)n, 1L);
2676
2677 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002678 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002679 update_screen(VALID);
2680
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002681 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002682 return FAIL;
2683
2684 if (len_change)
2685 *len_change = 1;
2686
2687 return OK;
2688 }
2689 else if (PyList_Check(lines))
2690 {
2691 PyInt i;
2692 PyInt size = PyList_Size(lines);
2693 char **array;
2694 buf_T *savebuf;
2695
2696 array = (char **)alloc((unsigned)(size * sizeof(char *)));
2697 if (array == NULL)
2698 {
2699 PyErr_NoMemory();
2700 return FAIL;
2701 }
2702
2703 for (i = 0; i < size; ++i)
2704 {
2705 PyObject *line = PyList_GetItem(lines, i);
2706 array[i] = StringToLine(line);
2707
2708 if (array[i] == NULL)
2709 {
2710 while (i)
2711 vim_free(array[--i]);
2712 vim_free(array);
2713 return FAIL;
2714 }
2715 }
2716
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002717 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002718 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002719 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002720
2721 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2722 PyErr_SetVim(_("cannot save undo information"));
2723 else
2724 {
2725 for (i = 0; i < size; ++i)
2726 {
2727 if (ml_append((linenr_T)(n + i),
2728 (char_u *)array[i], 0, FALSE) == FAIL)
2729 {
2730 PyErr_SetVim(_("cannot insert line"));
2731
2732 /* Free the rest of the lines */
2733 while (i < size)
2734 vim_free(array[i++]);
2735
2736 break;
2737 }
2738 vim_free(array[i]);
2739 }
2740 if (i > 0)
2741 appended_lines_mark((linenr_T)n, (long)i);
2742 }
2743
2744 /* Free the array of lines. All of its contents have now
2745 * been freed.
2746 */
2747 vim_free(array);
2748
Bram Moolenaar105bc352013-05-17 16:03:57 +02002749 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002750 update_screen(VALID);
2751
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002752 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002753 return FAIL;
2754
2755 if (len_change)
2756 *len_change = size;
2757
2758 return OK;
2759 }
2760 else
2761 {
2762 PyErr_BadArgument();
2763 return FAIL;
2764 }
2765}
2766
2767/*
2768 * Common routines for buffers and line ranges
2769 * -------------------------------------------
2770 */
2771
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002772typedef struct
2773{
2774 PyObject_HEAD
2775 buf_T *buf;
2776} BufferObject;
2777
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002778 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002779CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002780{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002781 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002782 {
2783 PyErr_SetVim(_("attempt to refer to deleted buffer"));
2784 return -1;
2785 }
2786
2787 return 0;
2788}
2789
2790 static PyObject *
2791RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
2792{
2793 if (CheckBuffer(self))
2794 return NULL;
2795
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002796 if (end == -1)
2797 end = self->buf->b_ml.ml_line_count;
2798
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002799 if (n < 0)
2800 n += end - start + 1;
2801
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002802 if (n < 0 || n > end - start)
2803 {
2804 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2805 return NULL;
2806 }
2807
2808 return GetBufferLine(self->buf, n+start);
2809}
2810
2811 static PyObject *
2812RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
2813{
2814 PyInt size;
2815
2816 if (CheckBuffer(self))
2817 return NULL;
2818
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002819 if (end == -1)
2820 end = self->buf->b_ml.ml_line_count;
2821
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002822 size = end - start + 1;
2823
2824 if (lo < 0)
2825 lo = 0;
2826 else if (lo > size)
2827 lo = size;
2828 if (hi < 0)
2829 hi = 0;
2830 if (hi < lo)
2831 hi = lo;
2832 else if (hi > size)
2833 hi = size;
2834
2835 return GetBufferLineList(self->buf, lo+start, hi+start);
2836}
2837
2838 static PyInt
2839RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2840{
2841 PyInt len_change;
2842
2843 if (CheckBuffer(self))
2844 return -1;
2845
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002846 if (end == -1)
2847 end = self->buf->b_ml.ml_line_count;
2848
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002849 if (n < 0)
2850 n += end - start + 1;
2851
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002852 if (n < 0 || n > end - start)
2853 {
2854 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2855 return -1;
2856 }
2857
2858 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
2859 return -1;
2860
2861 if (new_end)
2862 *new_end = end + len_change;
2863
2864 return 0;
2865}
2866
Bram Moolenaar19e60942011-06-19 00:27:51 +02002867 static PyInt
2868RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2869{
2870 PyInt size;
2871 PyInt len_change;
2872
2873 /* Self must be a valid buffer */
2874 if (CheckBuffer(self))
2875 return -1;
2876
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002877 if (end == -1)
2878 end = self->buf->b_ml.ml_line_count;
2879
Bram Moolenaar19e60942011-06-19 00:27:51 +02002880 /* Sort out the slice range */
2881 size = end - start + 1;
2882
2883 if (lo < 0)
2884 lo = 0;
2885 else if (lo > size)
2886 lo = size;
2887 if (hi < 0)
2888 hi = 0;
2889 if (hi < lo)
2890 hi = lo;
2891 else if (hi > size)
2892 hi = size;
2893
2894 if (SetBufferLineList(self->buf, lo + start, hi + start,
2895 val, &len_change) == FAIL)
2896 return -1;
2897
2898 if (new_end)
2899 *new_end = end + len_change;
2900
2901 return 0;
2902}
2903
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002904
2905 static PyObject *
2906RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
2907{
2908 PyObject *lines;
2909 PyInt len_change;
2910 PyInt max;
2911 PyInt n;
2912
2913 if (CheckBuffer(self))
2914 return NULL;
2915
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002916 if (end == -1)
2917 end = self->buf->b_ml.ml_line_count;
2918
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002919 max = n = end - start + 1;
2920
2921 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
2922 return NULL;
2923
2924 if (n < 0 || n > max)
2925 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002926 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002927 return NULL;
2928 }
2929
2930 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
2931 return NULL;
2932
2933 if (new_end)
2934 *new_end = end + len_change;
2935
2936 Py_INCREF(Py_None);
2937 return Py_None;
2938}
2939
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002940/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002941 */
2942
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002943static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002944static PySequenceMethods RangeAsSeq;
2945static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002946
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002947typedef struct
2948{
2949 PyObject_HEAD
2950 BufferObject *buf;
2951 PyInt start;
2952 PyInt end;
2953} RangeObject;
2954
2955 static PyObject *
2956RangeNew(buf_T *buf, PyInt start, PyInt end)
2957{
2958 BufferObject *bufr;
2959 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002960 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002961 if (self == NULL)
2962 return NULL;
2963
2964 bufr = (BufferObject *)BufferNew(buf);
2965 if (bufr == NULL)
2966 {
2967 Py_DECREF(self);
2968 return NULL;
2969 }
2970 Py_INCREF(bufr);
2971
2972 self->buf = bufr;
2973 self->start = start;
2974 self->end = end;
2975
2976 return (PyObject *)(self);
2977}
2978
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002979 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002980RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002981{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002982 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002983 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02002984 PyObject_GC_Del((void *)(self));
2985}
2986
2987 static int
2988RangeTraverse(RangeObject *self, visitproc visit, void *arg)
2989{
2990 Py_VISIT(((PyObject *)(self->buf)));
2991 return 0;
2992}
2993
2994 static int
2995RangeClear(RangeObject *self)
2996{
2997 Py_CLEAR(self->buf);
2998 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002999}
3000
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003001 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003002RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003003{
3004 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003005 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003006 return -1; /* ??? */
3007
Bram Moolenaard6e39182013-05-21 18:30:34 +02003008 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003009}
3010
3011 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003012RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003013{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003014 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003015}
3016
3017 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003018RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003019{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003020 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003021}
3022
3023 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003024RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003025{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003026 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003027}
3028
3029 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003030RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003031{
3032 static char repr[100];
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003033
Bram Moolenaard6e39182013-05-21 18:30:34 +02003034 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003035 {
3036 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
3037 (self));
3038 return PyString_FromString(repr);
3039 }
3040 else
3041 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003042 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003043 int len;
3044
3045 if (name == NULL)
3046 name = "";
3047 len = (int)strlen(name);
3048
3049 if (len > 45)
3050 name = name + (45 - len);
3051
3052 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
3053 len > 45 ? "..." : "", name,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003054 self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003055
3056 return PyString_FromString(repr);
3057 }
3058}
3059
3060static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003061 /* name, function, calling, documentation */
3062 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
3063 { NULL, NULL, 0, NULL }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003064};
3065
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003066static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003067static PySequenceMethods BufferAsSeq;
3068static PyMappingMethods BufferAsMapping;
3069
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003070 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003071BufferNew(buf_T *buf)
3072{
3073 /* We need to handle deletion of buffers underneath us.
3074 * If we add a "b_python*_ref" field to the buf_T structure,
3075 * then we can get at it in buf_freeall() in vim. We then
3076 * need to create only ONE Python object per buffer - if
3077 * we try to create a second, just INCREF the existing one
3078 * and return it. The (single) Python object referring to
3079 * the buffer is stored in "b_python*_ref".
3080 * Question: what to do on a buf_freeall(). We'll probably
3081 * have to either delete the Python object (DECREF it to
3082 * zero - a bad idea, as it leaves dangling refs!) or
3083 * set the buf_T * value to an invalid value (-1?), which
3084 * means we need checks in all access functions... Bah.
3085 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003086 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003087 * b_python_ref and b_python3_ref fields respectively.
3088 */
3089
3090 BufferObject *self;
3091
3092 if (BUF_PYTHON_REF(buf) != NULL)
3093 {
3094 self = BUF_PYTHON_REF(buf);
3095 Py_INCREF(self);
3096 }
3097 else
3098 {
3099 self = PyObject_NEW(BufferObject, &BufferType);
3100 if (self == NULL)
3101 return NULL;
3102 self->buf = buf;
3103 BUF_PYTHON_REF(buf) = self;
3104 }
3105
3106 return (PyObject *)(self);
3107}
3108
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003109 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003110BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003111{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003112 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3113 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003114
3115 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003116}
3117
Bram Moolenaar971db462013-05-12 18:44:48 +02003118 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003119BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003120{
3121 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003122 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003123 return -1; /* ??? */
3124
Bram Moolenaard6e39182013-05-21 18:30:34 +02003125 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003126}
3127
3128 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003129BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003130{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003131 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003132}
3133
3134 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003135BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003136{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003137 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003138}
3139
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003140 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003141BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003142{
3143 if (strcmp(name, "name") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003144 return Py_BuildValue("s", self->buf->b_ffname);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003145 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003146 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003147 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003148 return DictionaryNew(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003149 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003150 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3151 (PyObject *) self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003152 else if (strcmp(name,"__members__") == 0)
3153 return Py_BuildValue("[ssss]", "name", "number", "vars", "options");
3154 else
3155 return NULL;
3156}
3157
3158 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003159BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003160{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003161 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003162}
3163
3164 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003165BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003166{
3167 pos_T *posp;
3168 char *pmark;
3169 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003170 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003171
Bram Moolenaard6e39182013-05-21 18:30:34 +02003172 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003173 return NULL;
3174
3175 if (!PyArg_ParseTuple(args, "s", &pmark))
3176 return NULL;
3177 mark = *pmark;
3178
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003179 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02003180 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003181 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003182 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003183 if (VimTryEnd())
3184 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003185
3186 if (posp == NULL)
3187 {
3188 PyErr_SetVim(_("invalid mark name"));
3189 return NULL;
3190 }
3191
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003192 if (posp->lnum <= 0)
3193 {
3194 /* Or raise an error? */
3195 Py_INCREF(Py_None);
3196 return Py_None;
3197 }
3198
3199 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3200}
3201
3202 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003203BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003204{
3205 PyInt start;
3206 PyInt end;
3207
Bram Moolenaard6e39182013-05-21 18:30:34 +02003208 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003209 return NULL;
3210
3211 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3212 return NULL;
3213
Bram Moolenaard6e39182013-05-21 18:30:34 +02003214 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003215}
3216
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003217 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003218BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003219{
3220 static char repr[100];
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003221
Bram Moolenaard6e39182013-05-21 18:30:34 +02003222 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003223 {
3224 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3225 return PyString_FromString(repr);
3226 }
3227 else
3228 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003229 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003230 PyInt len;
3231
3232 if (name == NULL)
3233 name = "";
3234 len = strlen(name);
3235
3236 if (len > 35)
3237 name = name + (35 - len);
3238
3239 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3240
3241 return PyString_FromString(repr);
3242 }
3243}
3244
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003245static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003246 /* name, function, calling, documentation */
3247 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
3248 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
3249 {"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 +01003250#if PY_VERSION_HEX >= 0x03000000
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003251 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, "List buffer attributes" },
Bram Moolenaar7f85d292012-02-04 20:17:26 +01003252#endif
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003253 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003254};
3255
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003256/*
3257 * Buffer list object - Implementation
3258 */
3259
3260static PyTypeObject BufMapType;
3261
3262typedef struct
3263{
3264 PyObject_HEAD
3265} BufMapObject;
3266
3267 static PyInt
3268BufMapLength(PyObject *self UNUSED)
3269{
3270 buf_T *b = firstbuf;
3271 PyInt n = 0;
3272
3273 while (b)
3274 {
3275 ++n;
3276 b = b->b_next;
3277 }
3278
3279 return n;
3280}
3281
3282 static PyObject *
3283BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3284{
3285 buf_T *b;
3286 int bnr;
3287
3288#if PY_MAJOR_VERSION < 3
3289 if (PyInt_Check(keyObject))
3290 bnr = PyInt_AsLong(keyObject);
3291 else
3292#endif
3293 if (PyLong_Check(keyObject))
3294 bnr = PyLong_AsLong(keyObject);
3295 else
3296 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003297 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003298 return NULL;
3299 }
3300
3301 b = buflist_findnr(bnr);
3302
3303 if (b)
3304 return BufferNew(b);
3305 else
3306 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003307 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003308 return NULL;
3309 }
3310}
3311
3312 static void
3313BufMapIterDestruct(PyObject *buffer)
3314{
3315 /* Iteration was stopped before all buffers were processed */
3316 if (buffer)
3317 {
3318 Py_DECREF(buffer);
3319 }
3320}
3321
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003322 static int
3323BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3324{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003325 if (buffer)
3326 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003327 return 0;
3328}
3329
3330 static int
3331BufMapIterClear(PyObject **buffer)
3332{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003333 if (*buffer)
3334 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003335 return 0;
3336}
3337
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003338 static PyObject *
3339BufMapIterNext(PyObject **buffer)
3340{
3341 PyObject *next;
3342 PyObject *r;
3343
3344 if (!*buffer)
3345 return NULL;
3346
3347 r = *buffer;
3348
3349 if (CheckBuffer((BufferObject *)(r)))
3350 {
3351 *buffer = NULL;
3352 return NULL;
3353 }
3354
3355 if (!((BufferObject *)(r))->buf->b_next)
3356 next = NULL;
3357 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3358 return NULL;
3359 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02003360 /* Do not increment reference: we no longer hold it (decref), but whoever
3361 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003362 return r;
3363}
3364
3365 static PyObject *
3366BufMapIter(PyObject *self UNUSED)
3367{
3368 PyObject *buffer;
3369
3370 buffer = BufferNew(firstbuf);
3371 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003372 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3373 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003374}
3375
3376static PyMappingMethods BufMapAsMapping = {
3377 (lenfunc) BufMapLength,
3378 (binaryfunc) BufMapItem,
3379 (objobjargproc) 0,
3380};
3381
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003382/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003383 */
3384
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003385 static PyObject *
3386CurrentGetattr(PyObject *self UNUSED, char *name)
3387{
3388 if (strcmp(name, "buffer") == 0)
3389 return (PyObject *)BufferNew(curbuf);
3390 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003391 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003392 else if (strcmp(name, "tabpage") == 0)
3393 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003394 else if (strcmp(name, "line") == 0)
3395 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3396 else if (strcmp(name, "range") == 0)
3397 return RangeNew(curbuf, RangeStart, RangeEnd);
3398 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003399 return Py_BuildValue("[sssss]", "buffer", "window", "line", "range",
3400 "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003401 else
3402 {
3403 PyErr_SetString(PyExc_AttributeError, name);
3404 return NULL;
3405 }
3406}
3407
3408 static int
3409CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3410{
3411 if (strcmp(name, "line") == 0)
3412 {
3413 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3414 return -1;
3415
3416 return 0;
3417 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003418 else if (strcmp(name, "buffer") == 0)
3419 {
3420 int count;
3421
3422 if (value->ob_type != &BufferType)
3423 {
3424 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3425 return -1;
3426 }
3427
3428 if (CheckBuffer((BufferObject *)(value)))
3429 return -1;
3430 count = ((BufferObject *)(value))->buf->b_fnum;
3431
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003432 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003433 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3434 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003435 if (VimTryEnd())
3436 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003437 PyErr_SetVim(_("failed to switch to given buffer"));
3438 return -1;
3439 }
3440
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003441 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003442 }
3443 else if (strcmp(name, "window") == 0)
3444 {
3445 int count;
3446
3447 if (value->ob_type != &WindowType)
3448 {
3449 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3450 return -1;
3451 }
3452
3453 if (CheckWindow((WindowObject *)(value)))
3454 return -1;
3455 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3456
3457 if (!count)
3458 {
3459 PyErr_SetString(PyExc_ValueError,
3460 _("failed to find window in the current tab page"));
3461 return -1;
3462 }
3463
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003464 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003465 win_goto(((WindowObject *)(value))->win);
3466 if (((WindowObject *)(value))->win != curwin)
3467 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003468 if (VimTryEnd())
3469 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003470 PyErr_SetString(PyExc_RuntimeError,
3471 _("did not switch to the specified window"));
3472 return -1;
3473 }
3474
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003475 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003476 }
3477 else if (strcmp(name, "tabpage") == 0)
3478 {
3479 if (value->ob_type != &TabPageType)
3480 {
3481 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3482 return -1;
3483 }
3484
3485 if (CheckTabPage((TabPageObject *)(value)))
3486 return -1;
3487
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003488 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003489 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3490 if (((TabPageObject *)(value))->tab != curtab)
3491 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003492 if (VimTryEnd())
3493 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003494 PyErr_SetString(PyExc_RuntimeError,
3495 _("did not switch to the specified tab page"));
3496 return -1;
3497 }
3498
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003499 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003500 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003501 else
3502 {
3503 PyErr_SetString(PyExc_AttributeError, name);
3504 return -1;
3505 }
3506}
3507
Bram Moolenaardb913952012-06-29 12:54:53 +02003508 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003509init_range_cmd(exarg_T *eap)
3510{
3511 RangeStart = eap->line1;
3512 RangeEnd = eap->line2;
3513}
3514
3515 static void
3516init_range_eval(typval_T *rettv UNUSED)
3517{
3518 RangeStart = (PyInt) curwin->w_cursor.lnum;
3519 RangeEnd = RangeStart;
3520}
3521
3522 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003523run_cmd(const char *cmd, void *arg UNUSED
3524#ifdef PY_CAN_RECURSE
3525 , PyGILState_STATE *pygilstate UNUSED
3526#endif
3527 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003528{
3529 PyRun_SimpleString((char *) cmd);
3530}
3531
3532static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
3533static int code_hdr_len = 30;
3534
3535 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003536run_do(const char *cmd, void *arg UNUSED
3537#ifdef PY_CAN_RECURSE
3538 , PyGILState_STATE *pygilstate
3539#endif
3540 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003541{
3542 PyInt lnum;
3543 size_t len;
3544 char *code;
3545 int status;
3546 PyObject *pyfunc, *pymain;
3547
3548 if (u_save(RangeStart - 1, RangeEnd + 1) != OK)
3549 {
3550 EMSG(_("cannot save undo information"));
3551 return;
3552 }
3553
3554 len = code_hdr_len + STRLEN(cmd);
3555 code = PyMem_New(char, len + 1);
3556 memcpy(code, code_hdr, code_hdr_len);
3557 STRCPY(code + code_hdr_len, cmd);
3558 status = PyRun_SimpleString(code);
3559 PyMem_Free(code);
3560
3561 if (status)
3562 {
3563 EMSG(_("failed to run the code"));
3564 return;
3565 }
3566
3567 status = 0;
3568 pymain = PyImport_AddModule("__main__");
3569 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003570#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003571 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003572#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003573
3574 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
3575 {
3576 PyObject *line, *linenr, *ret;
3577
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003578#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003579 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003580#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003581 if (!(line = GetBufferLine(curbuf, lnum)))
3582 goto err;
3583 if (!(linenr = PyInt_FromLong((long) lnum)))
3584 {
3585 Py_DECREF(line);
3586 goto err;
3587 }
3588 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
3589 Py_DECREF(line);
3590 Py_DECREF(linenr);
3591 if (!ret)
3592 goto err;
3593
3594 if (ret != Py_None)
3595 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
3596 goto err;
3597
3598 Py_XDECREF(ret);
3599 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003600#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003601 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003602#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003603 }
3604 goto out;
3605err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003606#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003607 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003608#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003609 PyErr_PrintEx(0);
3610 PythonIO_Flush();
3611 status = 1;
3612out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003613#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003614 if (!status)
3615 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003616#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003617 Py_DECREF(pyfunc);
3618 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
3619 if (status)
3620 return;
3621 check_cursor();
3622 update_curbuf(NOT_VALID);
3623}
3624
3625 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003626run_eval(const char *cmd, typval_T *rettv
3627#ifdef PY_CAN_RECURSE
3628 , PyGILState_STATE *pygilstate UNUSED
3629#endif
3630 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003631{
3632 PyObject *r;
3633
3634 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
3635 if (r == NULL)
3636 {
3637 if (PyErr_Occurred() && !msg_silent)
3638 PyErr_PrintEx(0);
3639 EMSG(_("E858: Eval did not return a valid python object"));
3640 }
3641 else
3642 {
3643 if (ConvertFromPyObject(r, rettv) == -1)
3644 EMSG(_("E859: Failed to convert returned python object to vim value"));
3645 Py_DECREF(r);
3646 }
3647 PyErr_Clear();
3648}
3649
3650 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02003651set_ref_in_py(const int copyID)
3652{
3653 pylinkedlist_T *cur;
3654 dict_T *dd;
3655 list_T *ll;
3656
3657 if (lastdict != NULL)
3658 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3659 {
3660 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3661 if (dd->dv_copyID != copyID)
3662 {
3663 dd->dv_copyID = copyID;
3664 set_ref_in_ht(&dd->dv_hashtab, copyID);
3665 }
3666 }
3667
3668 if (lastlist != NULL)
3669 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3670 {
3671 ll = ((ListObject *) (cur->pll_obj))->list;
3672 if (ll->lv_copyID != copyID)
3673 {
3674 ll->lv_copyID = copyID;
3675 set_ref_in_list(ll, copyID);
3676 }
3677 }
3678}
3679
3680 static int
3681set_string_copy(char_u *str, typval_T *tv)
3682{
3683 tv->vval.v_string = vim_strsave(str);
3684 if (tv->vval.v_string == NULL)
3685 {
3686 PyErr_NoMemory();
3687 return -1;
3688 }
3689 return 0;
3690}
3691
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003692 static int
3693pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3694{
3695 dict_T *d;
3696 char_u *key;
3697 dictitem_T *di;
3698 PyObject *keyObject;
3699 PyObject *valObject;
3700 Py_ssize_t iter = 0;
3701
3702 d = dict_alloc();
3703 if (d == NULL)
3704 {
3705 PyErr_NoMemory();
3706 return -1;
3707 }
3708
3709 tv->v_type = VAR_DICT;
3710 tv->vval.v_dict = d;
3711
3712 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
3713 {
3714 DICTKEY_DECL
3715
3716 if (keyObject == NULL)
3717 return -1;
3718 if (valObject == NULL)
3719 return -1;
3720
3721 DICTKEY_GET_NOTEMPTY(-1)
3722
3723 di = dictitem_alloc(key);
3724
3725 DICTKEY_UNREF
3726
3727 if (di == NULL)
3728 {
3729 PyErr_NoMemory();
3730 return -1;
3731 }
3732 di->di_tv.v_lock = 0;
3733
3734 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3735 {
3736 vim_free(di);
3737 return -1;
3738 }
3739 if (dict_add(d, di) == FAIL)
3740 {
3741 vim_free(di);
3742 PyErr_SetVim(_("failed to add key to dictionary"));
3743 return -1;
3744 }
3745 }
3746 return 0;
3747}
3748
3749 static int
3750pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3751{
3752 dict_T *d;
3753 char_u *key;
3754 dictitem_T *di;
3755 PyObject *list;
3756 PyObject *litem;
3757 PyObject *keyObject;
3758 PyObject *valObject;
3759 Py_ssize_t lsize;
3760
3761 d = dict_alloc();
3762 if (d == NULL)
3763 {
3764 PyErr_NoMemory();
3765 return -1;
3766 }
3767
3768 tv->v_type = VAR_DICT;
3769 tv->vval.v_dict = d;
3770
3771 list = PyMapping_Items(obj);
3772 if (list == NULL)
3773 return -1;
3774 lsize = PyList_Size(list);
3775 while (lsize--)
3776 {
3777 DICTKEY_DECL
3778
3779 litem = PyList_GetItem(list, lsize);
3780 if (litem == NULL)
3781 {
3782 Py_DECREF(list);
3783 return -1;
3784 }
3785
3786 keyObject = PyTuple_GetItem(litem, 0);
3787 if (keyObject == NULL)
3788 {
3789 Py_DECREF(list);
3790 Py_DECREF(litem);
3791 return -1;
3792 }
3793
3794 DICTKEY_GET_NOTEMPTY(-1)
3795
3796 valObject = PyTuple_GetItem(litem, 1);
3797 if (valObject == NULL)
3798 {
3799 Py_DECREF(list);
3800 Py_DECREF(litem);
3801 return -1;
3802 }
3803
3804 di = dictitem_alloc(key);
3805
3806 DICTKEY_UNREF
3807
3808 if (di == NULL)
3809 {
3810 Py_DECREF(list);
3811 Py_DECREF(litem);
3812 PyErr_NoMemory();
3813 return -1;
3814 }
3815 di->di_tv.v_lock = 0;
3816
3817 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3818 {
3819 vim_free(di);
3820 Py_DECREF(list);
3821 Py_DECREF(litem);
3822 return -1;
3823 }
3824 if (dict_add(d, di) == FAIL)
3825 {
3826 vim_free(di);
3827 Py_DECREF(list);
3828 Py_DECREF(litem);
3829 PyErr_SetVim(_("failed to add key to dictionary"));
3830 return -1;
3831 }
3832 Py_DECREF(litem);
3833 }
3834 Py_DECREF(list);
3835 return 0;
3836}
3837
3838 static int
3839pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3840{
3841 list_T *l;
3842
3843 l = list_alloc();
3844 if (l == NULL)
3845 {
3846 PyErr_NoMemory();
3847 return -1;
3848 }
3849
3850 tv->v_type = VAR_LIST;
3851 tv->vval.v_list = l;
3852
3853 if (list_py_concat(l, obj, lookupDict) == -1)
3854 return -1;
3855
3856 return 0;
3857}
3858
3859 static int
3860pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3861{
3862 PyObject *iterator = PyObject_GetIter(obj);
3863 PyObject *item;
3864 list_T *l;
3865 listitem_T *li;
3866
3867 l = list_alloc();
3868
3869 if (l == NULL)
3870 {
3871 PyErr_NoMemory();
3872 return -1;
3873 }
3874
3875 tv->vval.v_list = l;
3876 tv->v_type = VAR_LIST;
3877
3878
3879 if (iterator == NULL)
3880 return -1;
3881
3882 while ((item = PyIter_Next(obj)))
3883 {
3884 li = listitem_alloc();
3885 if (li == NULL)
3886 {
3887 PyErr_NoMemory();
3888 return -1;
3889 }
3890 li->li_tv.v_lock = 0;
3891
3892 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
3893 return -1;
3894
3895 list_append(l, li);
3896
3897 Py_DECREF(item);
3898 }
3899
3900 Py_DECREF(iterator);
3901 return 0;
3902}
3903
Bram Moolenaardb913952012-06-29 12:54:53 +02003904typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
3905
3906 static int
3907convert_dl(PyObject *obj, typval_T *tv,
3908 pytotvfunc py_to_tv, PyObject *lookupDict)
3909{
3910 PyObject *capsule;
3911 char hexBuf[sizeof(void *) * 2 + 3];
3912
3913 sprintf(hexBuf, "%p", obj);
3914
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003915# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003916 capsule = PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003917# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003918 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003919# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02003920 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02003921 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003922# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003923 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02003924# else
3925 capsule = PyCObject_FromVoidPtr(tv, NULL);
3926# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003927 PyDict_SetItemString(lookupDict, hexBuf, capsule);
3928 Py_DECREF(capsule);
3929 if (py_to_tv(obj, tv, lookupDict) == -1)
3930 {
3931 tv->v_type = VAR_UNKNOWN;
3932 return -1;
3933 }
3934 /* As we are not using copy_tv which increments reference count we must
3935 * do it ourself. */
3936 switch(tv->v_type)
3937 {
3938 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
3939 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
3940 }
3941 }
3942 else
3943 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003944 typval_T *v;
3945
3946# ifdef PY_USE_CAPSULE
3947 v = PyCapsule_GetPointer(capsule, NULL);
3948# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003949 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003950# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003951 copy_tv(v, tv);
3952 }
3953 return 0;
3954}
3955
3956 static int
3957ConvertFromPyObject(PyObject *obj, typval_T *tv)
3958{
3959 PyObject *lookup_dict;
3960 int r;
3961
3962 lookup_dict = PyDict_New();
3963 r = _ConvertFromPyObject(obj, tv, lookup_dict);
3964 Py_DECREF(lookup_dict);
3965 return r;
3966}
3967
3968 static int
3969_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3970{
3971 if (obj->ob_type == &DictionaryType)
3972 {
3973 tv->v_type = VAR_DICT;
3974 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
3975 ++tv->vval.v_dict->dv_refcount;
3976 }
3977 else if (obj->ob_type == &ListType)
3978 {
3979 tv->v_type = VAR_LIST;
3980 tv->vval.v_list = (((ListObject *)(obj))->list);
3981 ++tv->vval.v_list->lv_refcount;
3982 }
3983 else if (obj->ob_type == &FunctionType)
3984 {
3985 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
3986 return -1;
3987
3988 tv->v_type = VAR_FUNC;
3989 func_ref(tv->vval.v_string);
3990 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003991 else if (PyBytes_Check(obj))
3992 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003993 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02003994
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003995 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
3996 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003997 if (result == NULL)
3998 return -1;
3999
4000 if (set_string_copy(result, tv) == -1)
4001 return -1;
4002
4003 tv->v_type = VAR_STRING;
4004 }
4005 else if (PyUnicode_Check(obj))
4006 {
4007 PyObject *bytes;
4008 char_u *result;
4009
Bram Moolenaardb913952012-06-29 12:54:53 +02004010 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4011 if (bytes == NULL)
4012 return -1;
4013
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004014 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4015 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004016 if (result == NULL)
4017 return -1;
4018
4019 if (set_string_copy(result, tv) == -1)
4020 {
4021 Py_XDECREF(bytes);
4022 return -1;
4023 }
4024 Py_XDECREF(bytes);
4025
4026 tv->v_type = VAR_STRING;
4027 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004028#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004029 else if (PyInt_Check(obj))
4030 {
4031 tv->v_type = VAR_NUMBER;
4032 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4033 }
4034#endif
4035 else if (PyLong_Check(obj))
4036 {
4037 tv->v_type = VAR_NUMBER;
4038 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4039 }
4040 else if (PyDict_Check(obj))
4041 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
4042#ifdef FEAT_FLOAT
4043 else if (PyFloat_Check(obj))
4044 {
4045 tv->v_type = VAR_FLOAT;
4046 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4047 }
4048#endif
4049 else if (PyIter_Check(obj))
4050 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
4051 else if (PySequence_Check(obj))
4052 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
4053 else if (PyMapping_Check(obj))
4054 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
4055 else
4056 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004057 PyErr_SetString(PyExc_TypeError,
4058 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004059 return -1;
4060 }
4061 return 0;
4062}
4063
4064 static PyObject *
4065ConvertToPyObject(typval_T *tv)
4066{
4067 if (tv == NULL)
4068 {
4069 PyErr_SetVim(_("NULL reference passed"));
4070 return NULL;
4071 }
4072 switch (tv->v_type)
4073 {
4074 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004075 return PyBytes_FromString(tv->vval.v_string == NULL
4076 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004077 case VAR_NUMBER:
4078 return PyLong_FromLong((long) tv->vval.v_number);
4079#ifdef FEAT_FLOAT
4080 case VAR_FLOAT:
4081 return PyFloat_FromDouble((double) tv->vval.v_float);
4082#endif
4083 case VAR_LIST:
4084 return ListNew(tv->vval.v_list);
4085 case VAR_DICT:
4086 return DictionaryNew(tv->vval.v_dict);
4087 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004088 return FunctionNew(tv->vval.v_string == NULL
4089 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004090 case VAR_UNKNOWN:
4091 Py_INCREF(Py_None);
4092 return Py_None;
4093 default:
4094 PyErr_SetVim(_("internal error: invalid value type"));
4095 return NULL;
4096 }
4097}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004098
4099typedef struct
4100{
4101 PyObject_HEAD
4102} CurrentObject;
4103static PyTypeObject CurrentType;
4104
4105 static void
4106init_structs(void)
4107{
4108 vim_memset(&OutputType, 0, sizeof(OutputType));
4109 OutputType.tp_name = "vim.message";
4110 OutputType.tp_basicsize = sizeof(OutputObject);
4111 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4112 OutputType.tp_doc = "vim message object";
4113 OutputType.tp_methods = OutputMethods;
4114#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004115 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4116 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004117 OutputType.tp_alloc = call_PyType_GenericAlloc;
4118 OutputType.tp_new = call_PyType_GenericNew;
4119 OutputType.tp_free = call_PyObject_Free;
4120#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004121 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4122 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004123#endif
4124
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004125 vim_memset(&IterType, 0, sizeof(IterType));
4126 IterType.tp_name = "vim.iter";
4127 IterType.tp_basicsize = sizeof(IterObject);
4128 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
4129 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004130 IterType.tp_iter = (getiterfunc)IterIter;
4131 IterType.tp_iternext = (iternextfunc)IterNext;
4132 IterType.tp_dealloc = (destructor)IterDestructor;
4133 IterType.tp_traverse = (traverseproc)IterTraverse;
4134 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004135
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004136 vim_memset(&BufferType, 0, sizeof(BufferType));
4137 BufferType.tp_name = "vim.buffer";
4138 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004139 BufferType.tp_dealloc = (destructor)BufferDestructor;
4140 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004141 BufferType.tp_as_sequence = &BufferAsSeq;
4142 BufferType.tp_as_mapping = &BufferAsMapping;
4143 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4144 BufferType.tp_doc = "vim buffer object";
4145 BufferType.tp_methods = BufferMethods;
4146#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004147 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004148 BufferType.tp_alloc = call_PyType_GenericAlloc;
4149 BufferType.tp_new = call_PyType_GenericNew;
4150 BufferType.tp_free = call_PyObject_Free;
4151#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004152 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004153#endif
4154
4155 vim_memset(&WindowType, 0, sizeof(WindowType));
4156 WindowType.tp_name = "vim.window";
4157 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004158 WindowType.tp_dealloc = (destructor)WindowDestructor;
4159 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004160 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
4161 WindowType.tp_doc = "vim Window object";
4162 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004163 WindowType.tp_traverse = (traverseproc)WindowTraverse;
4164 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004165#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004166 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
4167 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004168 WindowType.tp_alloc = call_PyType_GenericAlloc;
4169 WindowType.tp_new = call_PyType_GenericNew;
4170 WindowType.tp_free = call_PyObject_Free;
4171#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004172 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
4173 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004174#endif
4175
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004176 vim_memset(&TabPageType, 0, sizeof(TabPageType));
4177 TabPageType.tp_name = "vim.tabpage";
4178 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004179 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
4180 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004181 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
4182 TabPageType.tp_doc = "vim tab page object";
4183 TabPageType.tp_methods = TabPageMethods;
4184#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004185 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004186 TabPageType.tp_alloc = call_PyType_GenericAlloc;
4187 TabPageType.tp_new = call_PyType_GenericNew;
4188 TabPageType.tp_free = call_PyObject_Free;
4189#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004190 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004191#endif
4192
Bram Moolenaardfa38d42013-05-15 13:38:47 +02004193 vim_memset(&BufMapType, 0, sizeof(BufMapType));
4194 BufMapType.tp_name = "vim.bufferlist";
4195 BufMapType.tp_basicsize = sizeof(BufMapObject);
4196 BufMapType.tp_as_mapping = &BufMapAsMapping;
4197 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004198 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004199 BufferType.tp_doc = "vim buffer list";
4200
4201 vim_memset(&WinListType, 0, sizeof(WinListType));
4202 WinListType.tp_name = "vim.windowlist";
4203 WinListType.tp_basicsize = sizeof(WinListType);
4204 WinListType.tp_as_sequence = &WinListAsSeq;
4205 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
4206 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004207 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004208
4209 vim_memset(&TabListType, 0, sizeof(TabListType));
4210 TabListType.tp_name = "vim.tabpagelist";
4211 TabListType.tp_basicsize = sizeof(TabListType);
4212 TabListType.tp_as_sequence = &TabListAsSeq;
4213 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
4214 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004215
4216 vim_memset(&RangeType, 0, sizeof(RangeType));
4217 RangeType.tp_name = "vim.range";
4218 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004219 RangeType.tp_dealloc = (destructor)RangeDestructor;
4220 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004221 RangeType.tp_as_sequence = &RangeAsSeq;
4222 RangeType.tp_as_mapping = &RangeAsMapping;
4223 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
4224 RangeType.tp_doc = "vim Range object";
4225 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004226 RangeType.tp_traverse = (traverseproc)RangeTraverse;
4227 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004228#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004229 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004230 RangeType.tp_alloc = call_PyType_GenericAlloc;
4231 RangeType.tp_new = call_PyType_GenericNew;
4232 RangeType.tp_free = call_PyObject_Free;
4233#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004234 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004235#endif
4236
4237 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4238 CurrentType.tp_name = "vim.currentdata";
4239 CurrentType.tp_basicsize = sizeof(CurrentObject);
4240 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4241 CurrentType.tp_doc = "vim current object";
4242#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004243 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
4244 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004245#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004246 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
4247 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004248#endif
4249
4250 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4251 DictionaryType.tp_name = "vim.dictionary";
4252 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004253 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004254 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4255 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4256 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4257 DictionaryType.tp_methods = DictionaryMethods;
4258#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004259 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
4260 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004261#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004262 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
4263 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004264#endif
4265
4266 vim_memset(&ListType, 0, sizeof(ListType));
4267 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004268 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004269 ListType.tp_basicsize = sizeof(ListObject);
4270 ListType.tp_as_sequence = &ListAsSeq;
4271 ListType.tp_as_mapping = &ListAsMapping;
4272 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4273 ListType.tp_doc = "list pushing modifications to vim structure";
4274 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004275 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004276#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004277 ListType.tp_getattro = (getattrofunc)ListGetattro;
4278 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004279#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004280 ListType.tp_getattr = (getattrfunc)ListGetattr;
4281 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004282#endif
4283
4284 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004285 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004286 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004287 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
4288 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004289 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4290 FunctionType.tp_doc = "object that calls vim function";
4291 FunctionType.tp_methods = FunctionMethods;
4292#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004293 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004294#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004295 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004296#endif
4297
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004298 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4299 OptionsType.tp_name = "vim.options";
4300 OptionsType.tp_basicsize = sizeof(OptionsObject);
4301 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4302 OptionsType.tp_doc = "object for manipulating options";
4303 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004304 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
4305 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
4306 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004307
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004308#if PY_MAJOR_VERSION >= 3
4309 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4310 vimmodule.m_name = "vim";
4311 vimmodule.m_doc = "Vim Python interface\n";
4312 vimmodule.m_size = -1;
4313 vimmodule.m_methods = VimMethods;
4314#endif
4315}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004316
4317#define PYTYPE_READY(type) \
4318 if (PyType_Ready(&type)) \
4319 return -1;
4320
4321 static int
4322init_types()
4323{
4324 PYTYPE_READY(IterType);
4325 PYTYPE_READY(BufferType);
4326 PYTYPE_READY(RangeType);
4327 PYTYPE_READY(WindowType);
4328 PYTYPE_READY(TabPageType);
4329 PYTYPE_READY(BufMapType);
4330 PYTYPE_READY(WinListType);
4331 PYTYPE_READY(TabListType);
4332 PYTYPE_READY(CurrentType);
4333 PYTYPE_READY(DictionaryType);
4334 PYTYPE_READY(ListType);
4335 PYTYPE_READY(FunctionType);
4336 PYTYPE_READY(OptionsType);
4337 PYTYPE_READY(OutputType);
4338 return 0;
4339}
4340
4341static BufMapObject TheBufferMap =
4342{
4343 PyObject_HEAD_INIT(&BufMapType)
4344};
4345
4346static WinListObject TheWindowList =
4347{
4348 PyObject_HEAD_INIT(&WinListType)
4349 NULL
4350};
4351
4352static CurrentObject TheCurrent =
4353{
4354 PyObject_HEAD_INIT(&CurrentType)
4355};
4356
4357static TabListObject TheTabPageList =
4358{
4359 PyObject_HEAD_INIT(&TabListType)
4360};
4361
4362static struct numeric_constant {
4363 char *name;
4364 int value;
4365} numeric_constants[] = {
4366 {"VAR_LOCKED", VAR_LOCKED},
4367 {"VAR_FIXED", VAR_FIXED},
4368 {"VAR_SCOPE", VAR_SCOPE},
4369 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
4370};
4371
4372static struct object_constant {
4373 char *name;
4374 PyObject *value;
4375} object_constants[] = {
4376 {"buffers", (PyObject *)(void *)&TheBufferMap},
4377 {"windows", (PyObject *)(void *)&TheWindowList},
4378 {"tabpages", (PyObject *)(void *)&TheTabPageList},
4379 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02004380
4381 {"Buffer", (PyObject *)&BufferType},
4382 {"Range", (PyObject *)&RangeType},
4383 {"Window", (PyObject *)&WindowType},
4384 {"TabPage", (PyObject *)&TabPageType},
4385 {"Dictionary", (PyObject *)&DictionaryType},
4386 {"List", (PyObject *)&ListType},
4387 {"Function", (PyObject *)&FunctionType},
4388 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004389};
4390
4391typedef int (*object_adder)(PyObject *, const char *, PyObject *);
4392
4393#define ADD_OBJECT(m, name, obj) \
4394 if (add_object(m, name, obj)) \
4395 return -1;
4396
4397#define ADD_CHECKED_OBJECT(m, name, obj) \
4398 { \
4399 PyObject *value = obj; \
4400 if (!value) \
4401 return -1; \
4402 ADD_OBJECT(m, name, value); \
4403 }
4404
4405 static int
4406populate_module(PyObject *m, object_adder add_object)
4407{
4408 int i;
4409
4410 for (i = 0; i < (int)(sizeof(numeric_constants)
4411 / sizeof(struct numeric_constant));
4412 ++i)
4413 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
4414 PyInt_FromLong(numeric_constants[i].value));
4415
4416 for (i = 0; i < (int)(sizeof(object_constants)
4417 / sizeof(struct object_constant));
4418 ++i)
4419 {
4420 PyObject *value;
4421
4422 value = object_constants[i].value;
4423 Py_INCREF(value);
4424 ADD_OBJECT(m, object_constants[i].name, value);
4425 }
4426
4427 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
4428 return -1;
4429 ADD_OBJECT(m, "error", VimError);
4430
4431 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict));
4432 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
4433 ADD_CHECKED_OBJECT(m, "options",
4434 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
4435 return 0;
4436}