blob: 384aea9360cdaef7cdb59d1c20f17cfe456e19c6 [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 *);
34typedef void (*runner)(const char *, void *, PyGILState_STATE *);
35
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020036static int ConvertFromPyObject(PyObject *, typval_T *);
37static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020038static PyObject *WindowNew(win_T *, tabpage_T *);
39static PyObject *BufferNew (buf_T *);
40static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020041
42static PyInt RangeStart;
43static PyInt RangeEnd;
44
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020045static PyObject *globals;
46
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020047/*
48 * obtain a lock on the Vim data structures
49 */
50 static void
51Python_Lock_Vim(void)
52{
53}
54
55/*
56 * release a lock on the Vim data structures
57 */
58 static void
59Python_Release_Vim(void)
60{
61}
62
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020063/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020064 */
65
Bram Moolenaar2eea1982010-09-21 16:49:37 +020066/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020067typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020068
69static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020070
71typedef struct
72{
73 PyObject_HEAD
74 long softspace;
75 long error;
76} OutputObject;
77
Bram Moolenaar77045652012-09-21 13:46:06 +020078 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +020079OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +020080{
81 if (val == NULL)
82 {
Bram Moolenaar8661b172013-05-15 15:44:28 +020083 PyErr_SetString(PyExc_AttributeError,
84 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +020085 return -1;
86 }
87
88 if (strcmp(name, "softspace") == 0)
89 {
90 if (!PyInt_Check(val))
91 {
92 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
93 return -1;
94 }
95
Bram Moolenaard6e39182013-05-21 18:30:34 +020096 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +020097 return 0;
98 }
99
100 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
101 return -1;
102}
103
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200104/* Buffer IO, we write one whole line at a time. */
105static garray_T io_ga = {0, 0, 1, 80, NULL};
106static writefn old_fn = NULL;
107
108 static void
109PythonIO_Flush(void)
110{
111 if (old_fn != NULL && io_ga.ga_len > 0)
112 {
113 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
114 old_fn((char_u *)io_ga.ga_data);
115 }
116 io_ga.ga_len = 0;
117}
118
119 static void
120writer(writefn fn, char_u *str, PyInt n)
121{
122 char_u *ptr;
123
124 /* Flush when switching output function. */
125 if (fn != old_fn)
126 PythonIO_Flush();
127 old_fn = fn;
128
129 /* Write each NL separated line. Text after the last NL is kept for
130 * writing later. */
131 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
132 {
133 PyInt len = ptr - str;
134
135 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
136 break;
137
138 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
139 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
140 fn((char_u *)io_ga.ga_data);
141 str = ptr + 1;
142 n -= len + 1;
143 io_ga.ga_len = 0;
144 }
145
146 /* Put the remaining text into io_ga for later printing. */
147 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
148 {
149 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
150 io_ga.ga_len += (int)n;
151 }
152}
153
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200154 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200155OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200156{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200157 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200158 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200159 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200160
Bram Moolenaar27564802011-09-07 19:30:21 +0200161 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200162 return NULL;
163
164 Py_BEGIN_ALLOW_THREADS
165 Python_Lock_Vim();
166 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
167 Python_Release_Vim();
168 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200169 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200170
171 Py_INCREF(Py_None);
172 return Py_None;
173}
174
175 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200176OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200177{
178 PyInt n;
179 PyInt i;
180 PyObject *list;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200181 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200182
183 if (!PyArg_ParseTuple(args, "O", &list))
184 return NULL;
185 Py_INCREF(list);
186
Bram Moolenaardb913952012-06-29 12:54:53 +0200187 if (!PyList_Check(list))
188 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200189 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
190 Py_DECREF(list);
191 return NULL;
192 }
193
194 n = PyList_Size(list);
195
196 for (i = 0; i < n; ++i)
197 {
198 PyObject *line = PyList_GetItem(list, i);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200199 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200200 PyInt len;
201
Bram Moolenaardb913952012-06-29 12:54:53 +0200202 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
203 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200204 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
205 Py_DECREF(list);
206 return NULL;
207 }
208
209 Py_BEGIN_ALLOW_THREADS
210 Python_Lock_Vim();
211 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
212 Python_Release_Vim();
213 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200214 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200215 }
216
217 Py_DECREF(list);
218 Py_INCREF(Py_None);
219 return Py_None;
220}
221
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100222 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200223OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100224{
225 /* do nothing */
226 Py_INCREF(Py_None);
227 return Py_None;
228}
229
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200230/***************/
231
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200232static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200233 /* name, function, calling, doc */
234 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
235 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
236 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
237 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200238};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200239
240static OutputObject Output =
241{
242 PyObject_HEAD_INIT(&OutputType)
243 0,
244 0
245};
246
247static OutputObject Error =
248{
249 PyObject_HEAD_INIT(&OutputType)
250 0,
251 1
252};
253
254 static int
255PythonIO_Init_io(void)
256{
257 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
258 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
259
260 if (PyErr_Occurred())
261 {
262 EMSG(_("E264: Python: Error initialising I/O objects"));
263 return -1;
264 }
265
266 return 0;
267}
268
269
270static PyObject *VimError;
271
272/* Check to see whether a Vim error has been reported, or a keyboard
273 * interrupt has been detected.
274 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200275
276 static void
277VimTryStart(void)
278{
279 ++trylevel;
280}
281
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200282 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200283VimTryEnd(void)
284{
285 --trylevel;
286 if (got_int)
287 {
288 PyErr_SetNone(PyExc_KeyboardInterrupt);
289 return 1;
290 }
291 else if (!did_throw)
292 return 0;
293 else if (PyErr_Occurred())
294 return 1;
295 else
296 {
297 PyErr_SetVim((char *) current_exception->value);
298 discard_current_exception();
299 return 1;
300 }
301}
302
303 static int
304VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200305{
306 if (got_int)
307 {
308 PyErr_SetNone(PyExc_KeyboardInterrupt);
309 return 1;
310 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200311 return 0;
312}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200313
314/* Vim module - Implementation
315 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200316
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200317 static PyObject *
318VimCommand(PyObject *self UNUSED, PyObject *args)
319{
320 char *cmd;
321 PyObject *result;
322
323 if (!PyArg_ParseTuple(args, "s", &cmd))
324 return NULL;
325
326 PyErr_Clear();
327
328 Py_BEGIN_ALLOW_THREADS
329 Python_Lock_Vim();
330
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200331 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200332 do_cmdline_cmd((char_u *)cmd);
333 update_screen(VALID);
334
335 Python_Release_Vim();
336 Py_END_ALLOW_THREADS
337
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200338 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200339 result = NULL;
340 else
341 result = Py_None;
342
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200343
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200344 Py_XINCREF(result);
345 return result;
346}
347
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200348/*
349 * Function to translate a typval_T into a PyObject; this will recursively
350 * translate lists/dictionaries into their Python equivalents.
351 *
352 * The depth parameter is to avoid infinite recursion, set it to 1 when
353 * you call VimToPython.
354 */
355 static PyObject *
356VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
357{
358 PyObject *result;
359 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200360 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200361
362 /* Avoid infinite recursion */
363 if (depth > 100)
364 {
365 Py_INCREF(Py_None);
366 result = Py_None;
367 return result;
368 }
369
370 /* Check if we run into a recursive loop. The item must be in lookupDict
371 * then and we can use it again. */
372 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
373 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
374 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200375 sprintf(ptrBuf, "%p",
376 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
377 : (void *)our_tv->vval.v_dict);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200378 result = PyDict_GetItemString(lookupDict, ptrBuf);
379 if (result != NULL)
380 {
381 Py_INCREF(result);
382 return result;
383 }
384 }
385
386 if (our_tv->v_type == VAR_STRING)
387 {
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200388 result = Py_BuildValue("s", our_tv->vval.v_string == NULL
389 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200390 }
391 else if (our_tv->v_type == VAR_NUMBER)
392 {
393 char buf[NUMBUFLEN];
394
395 /* For backwards compatibility numbers are stored as strings. */
396 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
397 result = Py_BuildValue("s", buf);
398 }
399# ifdef FEAT_FLOAT
400 else if (our_tv->v_type == VAR_FLOAT)
401 {
402 char buf[NUMBUFLEN];
403
404 sprintf(buf, "%f", our_tv->vval.v_float);
405 result = Py_BuildValue("s", buf);
406 }
407# endif
408 else if (our_tv->v_type == VAR_LIST)
409 {
410 list_T *list = our_tv->vval.v_list;
411 listitem_T *curr;
412
413 result = PyList_New(0);
414
415 if (list != NULL)
416 {
417 PyDict_SetItemString(lookupDict, ptrBuf, result);
418
419 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
420 {
421 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
422 PyList_Append(result, newObj);
423 Py_DECREF(newObj);
424 }
425 }
426 }
427 else if (our_tv->v_type == VAR_DICT)
428 {
429 result = PyDict_New();
430
431 if (our_tv->vval.v_dict != NULL)
432 {
433 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
434 long_u todo = ht->ht_used;
435 hashitem_T *hi;
436 dictitem_T *di;
437
438 PyDict_SetItemString(lookupDict, ptrBuf, result);
439
440 for (hi = ht->ht_array; todo > 0; ++hi)
441 {
442 if (!HASHITEM_EMPTY(hi))
443 {
444 --todo;
445
446 di = dict_lookup(hi);
447 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
448 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
449 Py_DECREF(newObj);
450 }
451 }
452 }
453 }
454 else
455 {
456 Py_INCREF(Py_None);
457 result = Py_None;
458 }
459
460 return result;
461}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200462
463 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200464VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200465{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200466 char *expr;
467 typval_T *our_tv;
468 PyObject *result;
469 PyObject *lookup_dict;
470
471 if (!PyArg_ParseTuple(args, "s", &expr))
472 return NULL;
473
474 Py_BEGIN_ALLOW_THREADS
475 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200476 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200477 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200478 Python_Release_Vim();
479 Py_END_ALLOW_THREADS
480
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200481 if (VimTryEnd())
482 return NULL;
483
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200484 if (our_tv == NULL)
485 {
486 PyErr_SetVim(_("invalid expression"));
487 return NULL;
488 }
489
490 /* Convert the Vim type into a Python type. Create a dictionary that's
491 * used to check for recursive loops. */
492 lookup_dict = PyDict_New();
493 result = VimToPython(our_tv, 1, lookup_dict);
494 Py_DECREF(lookup_dict);
495
496
497 Py_BEGIN_ALLOW_THREADS
498 Python_Lock_Vim();
499 free_tv(our_tv);
500 Python_Release_Vim();
501 Py_END_ALLOW_THREADS
502
503 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200504}
505
Bram Moolenaardb913952012-06-29 12:54:53 +0200506static PyObject *ConvertToPyObject(typval_T *);
507
508 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200509VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200510{
Bram Moolenaardb913952012-06-29 12:54:53 +0200511 char *expr;
512 typval_T *our_tv;
513 PyObject *result;
514
515 if (!PyArg_ParseTuple(args, "s", &expr))
516 return NULL;
517
518 Py_BEGIN_ALLOW_THREADS
519 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200520 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200521 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200522 Python_Release_Vim();
523 Py_END_ALLOW_THREADS
524
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200525 if (VimTryEnd())
526 return NULL;
527
Bram Moolenaardb913952012-06-29 12:54:53 +0200528 if (our_tv == NULL)
529 {
530 PyErr_SetVim(_("invalid expression"));
531 return NULL;
532 }
533
534 result = ConvertToPyObject(our_tv);
535 Py_BEGIN_ALLOW_THREADS
536 Python_Lock_Vim();
537 free_tv(our_tv);
538 Python_Release_Vim();
539 Py_END_ALLOW_THREADS
540
541 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200542}
543
544 static PyObject *
545VimStrwidth(PyObject *self UNUSED, PyObject *args)
546{
547 char *expr;
548
549 if (!PyArg_ParseTuple(args, "s", &expr))
550 return NULL;
551
Bram Moolenaara54bf402012-12-05 16:30:07 +0100552 return PyLong_FromLong(
553#ifdef FEAT_MBYTE
554 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
555#else
556 STRLEN(expr)
557#endif
558 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200559}
560
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200561/*
562 * Vim module - Definitions
563 */
564
565static struct PyMethodDef VimMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200566 /* name, function, calling, documentation */
567 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
568 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
569 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
570 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
571 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200572};
573
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200574/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200575 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200576 */
577
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200578static PyTypeObject IterType;
579
580typedef PyObject *(*nextfun)(void **);
581typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200582typedef int (*traversefun)(void *, visitproc, void *);
583typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200584
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200585/* Main purpose of this object is removing the need for do python
586 * initialization (i.e. PyType_Ready and setting type attributes) for a big
587 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200588
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200589typedef struct
590{
591 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200592 void *cur;
593 nextfun next;
594 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200595 traversefun traverse;
596 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200597} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200598
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200599 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200600IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
601 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200602{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200603 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200604
Bram Moolenaar774267b2013-05-21 20:51:59 +0200605 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200606 self->cur = start;
607 self->next = next;
608 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200609 self->traverse = traverse;
610 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200611
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200612 return (PyObject *)(self);
613}
614
615 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200616IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200617{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200618 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200619 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200620 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200621}
622
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200623 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200624IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200625{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200626 if (self->traverse != NULL)
627 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200628 else
629 return 0;
630}
631
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200632/* Mac OSX defines clear() somewhere. */
633#ifdef clear
634# undef clear
635#endif
636
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200637 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200638IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200639{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200640 if (self->clear != NULL)
641 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200642 else
643 return 0;
644}
645
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200647IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200648{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200649 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200650}
651
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200652 static PyObject *
653IterIter(PyObject *self)
654{
655 return self;
656}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200657
Bram Moolenaardb913952012-06-29 12:54:53 +0200658typedef struct pylinkedlist_S {
659 struct pylinkedlist_S *pll_next;
660 struct pylinkedlist_S *pll_prev;
661 PyObject *pll_obj;
662} pylinkedlist_T;
663
664static pylinkedlist_T *lastdict = NULL;
665static pylinkedlist_T *lastlist = NULL;
666
667 static void
668pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
669{
670 if (ref->pll_prev == NULL)
671 {
672 if (ref->pll_next == NULL)
673 {
674 *last = NULL;
675 return;
676 }
677 }
678 else
679 ref->pll_prev->pll_next = ref->pll_next;
680
681 if (ref->pll_next == NULL)
682 *last = ref->pll_prev;
683 else
684 ref->pll_next->pll_prev = ref->pll_prev;
685}
686
687 static void
688pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
689{
690 if (*last == NULL)
691 ref->pll_prev = NULL;
692 else
693 {
694 (*last)->pll_next = ref;
695 ref->pll_prev = *last;
696 }
697 ref->pll_next = NULL;
698 ref->pll_obj = self;
699 *last = ref;
700}
701
702static PyTypeObject DictionaryType;
703
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200704#define DICTKEY_GET_NOTEMPTY(err) \
705 DICTKEY_GET(err) \
706 if (*key == NUL) \
707 { \
708 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
709 return err; \
710 }
711
Bram Moolenaardb913952012-06-29 12:54:53 +0200712typedef struct
713{
714 PyObject_HEAD
715 dict_T *dict;
716 pylinkedlist_T ref;
717} DictionaryObject;
718
719 static PyObject *
720DictionaryNew(dict_T *dict)
721{
722 DictionaryObject *self;
723
724 self = PyObject_NEW(DictionaryObject, &DictionaryType);
725 if (self == NULL)
726 return NULL;
727 self->dict = dict;
728 ++dict->dv_refcount;
729
730 pyll_add((PyObject *)(self), &self->ref, &lastdict);
731
732 return (PyObject *)(self);
733}
734
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200735 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200736DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200737{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200738 pyll_remove(&self->ref, &lastdict);
739 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200740
741 DESTRUCTOR_FINISH(self);
742}
743
Bram Moolenaardb913952012-06-29 12:54:53 +0200744 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200745DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200746{
747 if (val == NULL)
748 {
749 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
750 return -1;
751 }
752
753 if (strcmp(name, "locked") == 0)
754 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200755 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200756 {
757 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
758 return -1;
759 }
760 else
761 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200762 int istrue = PyObject_IsTrue(val);
763 if (istrue == -1)
764 return -1;
765 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200766 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200767 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200768 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200769 }
770 return 0;
771 }
772 else
773 {
774 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
775 return -1;
776 }
777}
778
779 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200780DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200781{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200782 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +0200783}
784
785 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200786DictionaryItem(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200787{
788 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200789 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200790 DICTKEY_DECL
791
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200792 DICTKEY_GET_NOTEMPTY(NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200793
Bram Moolenaard6e39182013-05-21 18:30:34 +0200794 di = dict_find(self->dict, key, -1);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200795
Bram Moolenaar696c2112012-09-21 13:43:14 +0200796 DICTKEY_UNREF
797
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200798 if (di == NULL)
799 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200800 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200801 return NULL;
802 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200803
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200804 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200805}
806
807 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200808DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200809{
810 char_u *key;
811 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200812 dict_T *d = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +0200813 dictitem_T *di;
814 DICTKEY_DECL
815
816 if (d->dv_lock)
817 {
818 PyErr_SetVim(_("dict is locked"));
819 return -1;
820 }
821
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200822 DICTKEY_GET_NOTEMPTY(-1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200823
824 di = dict_find(d, key, -1);
825
826 if (valObject == NULL)
827 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200828 hashitem_T *hi;
829
Bram Moolenaardb913952012-06-29 12:54:53 +0200830 if (di == NULL)
831 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200832 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200833 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +0200834 return -1;
835 }
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200836 hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaardb913952012-06-29 12:54:53 +0200837 hash_remove(&d->dv_hashtab, hi);
838 dictitem_free(di);
839 return 0;
840 }
841
842 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200843 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +0200844
845 if (di == NULL)
846 {
847 di = dictitem_alloc(key);
848 if (di == NULL)
849 {
850 PyErr_NoMemory();
851 return -1;
852 }
853 di->di_tv.v_lock = 0;
854
855 if (dict_add(d, di) == FAIL)
856 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200857 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +0200858 vim_free(di);
859 PyErr_SetVim(_("failed to add key to dictionary"));
860 return -1;
861 }
862 }
863 else
864 clear_tv(&di->di_tv);
865
866 DICTKEY_UNREF
867
868 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +0200869 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200870 return 0;
871}
872
873 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200874DictionaryListKeys(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200875{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200876 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +0200877 long_u todo = dict->dv_hashtab.ht_used;
878 Py_ssize_t i = 0;
879 PyObject *r;
880 hashitem_T *hi;
881
882 r = PyList_New(todo);
883 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
884 {
885 if (!HASHITEM_EMPTY(hi))
886 {
887 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
888 --todo;
889 ++i;
890 }
891 }
892 return r;
893}
894
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200895static PyMappingMethods DictionaryAsMapping = {
896 (lenfunc) DictionaryLength,
897 (binaryfunc) DictionaryItem,
898 (objobjargproc) DictionaryAssItem,
899};
900
Bram Moolenaardb913952012-06-29 12:54:53 +0200901static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200902 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
903 { NULL, NULL, 0, NULL }
Bram Moolenaardb913952012-06-29 12:54:53 +0200904};
905
906static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200907static PySequenceMethods ListAsSeq;
908static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +0200909
910typedef struct
911{
912 PyObject_HEAD
913 list_T *list;
914 pylinkedlist_T ref;
915} ListObject;
916
917 static PyObject *
918ListNew(list_T *list)
919{
920 ListObject *self;
921
922 self = PyObject_NEW(ListObject, &ListType);
923 if (self == NULL)
924 return NULL;
925 self->list = list;
926 ++list->lv_refcount;
927
928 pyll_add((PyObject *)(self), &self->ref, &lastlist);
929
930 return (PyObject *)(self);
931}
932
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200933 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200934ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200935{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200936 pyll_remove(&self->ref, &lastlist);
937 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200938
939 DESTRUCTOR_FINISH(self);
940}
941
Bram Moolenaardb913952012-06-29 12:54:53 +0200942 static int
943list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict)
944{
945 Py_ssize_t i;
946 Py_ssize_t lsize = PySequence_Size(obj);
947 PyObject *litem;
948 listitem_T *li;
949
950 for(i=0; i<lsize; i++)
951 {
952 li = listitem_alloc();
953 if (li == NULL)
954 {
955 PyErr_NoMemory();
956 return -1;
957 }
958 li->li_tv.v_lock = 0;
959
960 litem = PySequence_GetItem(obj, i);
961 if (litem == NULL)
962 return -1;
963 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1)
964 return -1;
965
966 list_append(l, li);
967 }
968 return 0;
969}
970
Bram Moolenaardb913952012-06-29 12:54:53 +0200971 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200972ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200973{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200974 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +0200975}
976
977 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200978ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +0200979{
980 listitem_T *li;
981
Bram Moolenaard6e39182013-05-21 18:30:34 +0200982 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +0200983 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200984 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200985 return NULL;
986 }
Bram Moolenaard6e39182013-05-21 18:30:34 +0200987 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +0200988 if (li == NULL)
989 {
990 PyErr_SetVim(_("internal error: failed to get vim list item"));
991 return NULL;
992 }
993 return ConvertToPyObject(&li->li_tv);
994}
995
996#define PROC_RANGE \
997 if (last < 0) {\
998 if (last < -size) \
999 last = 0; \
1000 else \
1001 last += size; \
1002 } \
1003 if (first < 0) \
1004 first = 0; \
1005 if (first > size) \
1006 first = size; \
1007 if (last > size) \
1008 last = size;
1009
1010 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001011ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001012{
1013 PyInt i;
1014 PyInt size = ListLength(self);
1015 PyInt n;
1016 PyObject *list;
1017 int reversed = 0;
1018
1019 PROC_RANGE
1020 if (first >= last)
1021 first = last;
1022
1023 n = last-first;
1024 list = PyList_New(n);
1025 if (list == NULL)
1026 return NULL;
1027
1028 for (i = 0; i < n; ++i)
1029 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001030 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001031 if (item == NULL)
1032 {
1033 Py_DECREF(list);
1034 return NULL;
1035 }
1036
1037 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1038 {
1039 Py_DECREF(item);
1040 Py_DECREF(list);
1041 return NULL;
1042 }
1043 }
1044
1045 return list;
1046}
1047
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001048typedef struct
1049{
1050 listwatch_T lw;
1051 list_T *list;
1052} listiterinfo_T;
1053
1054 static void
1055ListIterDestruct(listiterinfo_T *lii)
1056{
1057 list_rem_watch(lii->list, &lii->lw);
1058 PyMem_Free(lii);
1059}
1060
1061 static PyObject *
1062ListIterNext(listiterinfo_T **lii)
1063{
1064 PyObject *r;
1065
1066 if (!((*lii)->lw.lw_item))
1067 return NULL;
1068
1069 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1070 return NULL;
1071
1072 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1073
1074 return r;
1075}
1076
1077 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001078ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001079{
1080 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001081 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001082
1083 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1084 {
1085 PyErr_NoMemory();
1086 return NULL;
1087 }
1088
1089 list_add_watch(l, &lii->lw);
1090 lii->lw.lw_item = l->lv_first;
1091 lii->list = l;
1092
1093 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001094 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1095 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001096}
1097
Bram Moolenaardb913952012-06-29 12:54:53 +02001098 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001099ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001100{
1101 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001102 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001103 listitem_T *li;
1104 Py_ssize_t length = ListLength(self);
1105
1106 if (l->lv_lock)
1107 {
1108 PyErr_SetVim(_("list is locked"));
1109 return -1;
1110 }
1111 if (index>length || (index==length && obj==NULL))
1112 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001113 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001114 return -1;
1115 }
1116
1117 if (obj == NULL)
1118 {
1119 li = list_find(l, (long) index);
1120 list_remove(l, li, li);
1121 clear_tv(&li->li_tv);
1122 vim_free(li);
1123 return 0;
1124 }
1125
1126 if (ConvertFromPyObject(obj, &tv) == -1)
1127 return -1;
1128
1129 if (index == length)
1130 {
1131 if (list_append_tv(l, &tv) == FAIL)
1132 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001133 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001134 PyErr_SetVim(_("Failed to add item to list"));
1135 return -1;
1136 }
1137 }
1138 else
1139 {
1140 li = list_find(l, (long) index);
1141 clear_tv(&li->li_tv);
1142 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001143 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001144 }
1145 return 0;
1146}
1147
1148 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001149ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001150{
1151 PyInt size = ListLength(self);
1152 Py_ssize_t i;
1153 Py_ssize_t lsize;
1154 PyObject *litem;
1155 listitem_T *li;
1156 listitem_T *next;
1157 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001158 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001159
1160 if (l->lv_lock)
1161 {
1162 PyErr_SetVim(_("list is locked"));
1163 return -1;
1164 }
1165
1166 PROC_RANGE
1167
1168 if (first == size)
1169 li = NULL;
1170 else
1171 {
1172 li = list_find(l, (long) first);
1173 if (li == NULL)
1174 {
1175 PyErr_SetVim(_("internal error: no vim list item"));
1176 return -1;
1177 }
1178 if (last > first)
1179 {
1180 i = last - first;
1181 while (i-- && li != NULL)
1182 {
1183 next = li->li_next;
1184 listitem_remove(l, li);
1185 li = next;
1186 }
1187 }
1188 }
1189
1190 if (obj == NULL)
1191 return 0;
1192
1193 if (!PyList_Check(obj))
1194 {
1195 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1196 return -1;
1197 }
1198
1199 lsize = PyList_Size(obj);
1200
1201 for(i=0; i<lsize; i++)
1202 {
1203 litem = PyList_GetItem(obj, i);
1204 if (litem == NULL)
1205 return -1;
1206 if (ConvertFromPyObject(litem, &v) == -1)
1207 return -1;
1208 if (list_insert_tv(l, &v, li) == FAIL)
1209 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001210 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001211 PyErr_SetVim(_("internal error: failed to add item to list"));
1212 return -1;
1213 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001214 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001215 }
1216 return 0;
1217}
1218
1219 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001220ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001221{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001222 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001223 PyObject *lookup_dict;
1224
1225 if (l->lv_lock)
1226 {
1227 PyErr_SetVim(_("list is locked"));
1228 return NULL;
1229 }
1230
1231 if (!PySequence_Check(obj))
1232 {
1233 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1234 return NULL;
1235 }
1236
1237 lookup_dict = PyDict_New();
1238 if (list_py_concat(l, obj, lookup_dict) == -1)
1239 {
1240 Py_DECREF(lookup_dict);
1241 return NULL;
1242 }
1243 Py_DECREF(lookup_dict);
1244
1245 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001246 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001247}
1248
Bram Moolenaar66b79852012-09-21 14:00:35 +02001249 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001250ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001251{
1252 if (val == NULL)
1253 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001254 PyErr_SetString(PyExc_AttributeError,
1255 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001256 return -1;
1257 }
1258
1259 if (strcmp(name, "locked") == 0)
1260 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001261 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001262 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001263 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001264 return -1;
1265 }
1266 else
1267 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001268 int istrue = PyObject_IsTrue(val);
1269 if (istrue == -1)
1270 return -1;
1271 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001272 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001273 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001274 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001275 }
1276 return 0;
1277 }
1278 else
1279 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001280 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001281 return -1;
1282 }
1283}
1284
Bram Moolenaardb913952012-06-29 12:54:53 +02001285static struct PyMethodDef ListMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001286 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1287 { NULL, NULL, 0, NULL }
Bram Moolenaardb913952012-06-29 12:54:53 +02001288};
1289
1290typedef struct
1291{
1292 PyObject_HEAD
1293 char_u *name;
1294} FunctionObject;
1295
1296static PyTypeObject FunctionType;
1297
1298 static PyObject *
1299FunctionNew(char_u *name)
1300{
1301 FunctionObject *self;
1302
1303 self = PyObject_NEW(FunctionObject, &FunctionType);
1304 if (self == NULL)
1305 return NULL;
1306 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1307 if (self->name == NULL)
1308 {
1309 PyErr_NoMemory();
1310 return NULL;
1311 }
1312 STRCPY(self->name, name);
1313 func_ref(name);
1314 return (PyObject *)(self);
1315}
1316
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001317 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001318FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001319{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001320 func_unref(self->name);
1321 PyMem_Free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001322
1323 DESTRUCTOR_FINISH(self);
1324}
1325
Bram Moolenaardb913952012-06-29 12:54:53 +02001326 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001327FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02001328{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001329 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02001330 typval_T args;
1331 typval_T selfdicttv;
1332 typval_T rettv;
1333 dict_T *selfdict = NULL;
1334 PyObject *selfdictObject;
1335 PyObject *result;
1336 int error;
1337
1338 if (ConvertFromPyObject(argsObject, &args) == -1)
1339 return NULL;
1340
1341 if (kwargs != NULL)
1342 {
1343 selfdictObject = PyDict_GetItemString(kwargs, "self");
1344 if (selfdictObject != NULL)
1345 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001346 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001347 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001348 PyErr_SetString(PyExc_TypeError,
1349 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001350 clear_tv(&args);
1351 return NULL;
1352 }
1353 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001354 {
1355 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02001356 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001357 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001358 selfdict = selfdicttv.vval.v_dict;
1359 }
1360 }
1361
Bram Moolenaar71700b82013-05-15 17:49:05 +02001362 Py_BEGIN_ALLOW_THREADS
1363 Python_Lock_Vim();
1364
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001365 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02001366 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001367
1368 Python_Release_Vim();
1369 Py_END_ALLOW_THREADS
1370
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001371 if (VimTryEnd())
1372 result = NULL;
1373 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02001374 {
1375 result = NULL;
1376 PyErr_SetVim(_("failed to run function"));
1377 }
1378 else
1379 result = ConvertToPyObject(&rettv);
1380
Bram Moolenaardb913952012-06-29 12:54:53 +02001381 clear_tv(&args);
1382 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001383 if (selfdict != NULL)
1384 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001385
1386 return result;
1387}
1388
1389static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001390 {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1391 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001392};
1393
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001394/*
1395 * Options object
1396 */
1397
1398static PyTypeObject OptionsType;
1399
1400typedef int (*checkfun)(void *);
1401
1402typedef struct
1403{
1404 PyObject_HEAD
1405 int opt_type;
1406 void *from;
1407 checkfun Check;
1408 PyObject *fromObj;
1409} OptionsObject;
1410
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001411 static int
1412dummy_check(void *arg UNUSED)
1413{
1414 return 0;
1415}
1416
1417 static PyObject *
1418OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1419{
1420 OptionsObject *self;
1421
Bram Moolenaar774267b2013-05-21 20:51:59 +02001422 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001423 if (self == NULL)
1424 return NULL;
1425
1426 self->opt_type = opt_type;
1427 self->from = from;
1428 self->Check = Check;
1429 self->fromObj = fromObj;
1430 if (fromObj)
1431 Py_INCREF(fromObj);
1432
1433 return (PyObject *)(self);
1434}
1435
1436 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001437OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001438{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001439 PyObject_GC_UnTrack((void *)(self));
1440 Py_XDECREF(self->fromObj);
1441 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001442}
1443
1444 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001445OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001446{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001447 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001448 return 0;
1449}
1450
1451 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001452OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001453{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001454 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001455 return 0;
1456}
1457
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001458 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001459OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001460{
1461 char_u *key;
1462 int flags;
1463 long numval;
1464 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001465 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001466
Bram Moolenaard6e39182013-05-21 18:30:34 +02001467 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001468 return NULL;
1469
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001470 DICTKEY_GET_NOTEMPTY(NULL)
1471
1472 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001473 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001474
1475 DICTKEY_UNREF
1476
1477 if (flags == 0)
1478 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001479 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001480 return NULL;
1481 }
1482
1483 if (flags & SOPT_UNSET)
1484 {
1485 Py_INCREF(Py_None);
1486 return Py_None;
1487 }
1488 else if (flags & SOPT_BOOL)
1489 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001490 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001491 r = numval ? Py_True : Py_False;
1492 Py_INCREF(r);
1493 return r;
1494 }
1495 else if (flags & SOPT_NUM)
1496 return PyInt_FromLong(numval);
1497 else if (flags & SOPT_STRING)
1498 {
1499 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001500 {
1501 PyObject *r = PyBytes_FromString((char *) stringval);
1502 vim_free(stringval);
1503 return r;
1504 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001505 else
1506 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001507 PyErr_SetString(PyExc_RuntimeError,
1508 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001509 return NULL;
1510 }
1511 }
1512 else
1513 {
1514 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1515 return NULL;
1516 }
1517}
1518
1519 static int
1520set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1521 char_u *key;
1522 int numval;
1523 char_u *stringval;
1524 int opt_flags;
1525 int opt_type;
1526 void *from;
1527{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001528 win_T *save_curwin = NULL;
1529 tabpage_T *save_curtab = NULL;
1530 buf_T *save_curbuf = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001531
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001532 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001533 switch (opt_type)
1534 {
1535 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001536 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1537 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001538 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001539 if (VimTryEnd())
1540 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001541 PyErr_SetVim("Problem while switching windows.");
1542 return -1;
1543 }
1544 set_option_value(key, numval, stringval, opt_flags);
1545 restore_win(save_curwin, save_curtab);
1546 break;
1547 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001548 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001549 set_option_value(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001550 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001551 break;
1552 case SREQ_GLOBAL:
1553 set_option_value(key, numval, stringval, opt_flags);
1554 break;
1555 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001556 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001557}
1558
1559 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001560OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001561{
1562 char_u *key;
1563 int flags;
1564 int opt_flags;
1565 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001566 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001567
Bram Moolenaard6e39182013-05-21 18:30:34 +02001568 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001569 return -1;
1570
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001571 DICTKEY_GET_NOTEMPTY(-1)
1572
1573 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001574 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001575
1576 DICTKEY_UNREF
1577
1578 if (flags == 0)
1579 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001580 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001581 return -1;
1582 }
1583
1584 if (valObject == NULL)
1585 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001586 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001587 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001588 PyErr_SetString(PyExc_ValueError,
1589 _("unable to unset global option"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001590 return -1;
1591 }
1592 else if (!(flags & SOPT_GLOBAL))
1593 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001594 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1595 "without global value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001596 return -1;
1597 }
1598 else
1599 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001600 unset_global_local_option(key, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001601 return 0;
1602 }
1603 }
1604
Bram Moolenaard6e39182013-05-21 18:30:34 +02001605 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001606
1607 if (flags & SOPT_BOOL)
1608 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001609 int istrue = PyObject_IsTrue(valObject);
1610 if (istrue == -1)
1611 return -1;
1612 r = set_option_value_for(key, istrue, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001613 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001614 }
1615 else if (flags & SOPT_NUM)
1616 {
1617 int val;
1618
1619#if PY_MAJOR_VERSION < 3
1620 if (PyInt_Check(valObject))
1621 val = PyInt_AsLong(valObject);
1622 else
1623#endif
1624 if (PyLong_Check(valObject))
1625 val = PyLong_AsLong(valObject);
1626 else
1627 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001628 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001629 return -1;
1630 }
1631
1632 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001633 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001634 }
1635 else
1636 {
1637 char_u *val;
1638 if (PyBytes_Check(valObject))
1639 {
1640
1641 if (PyString_AsStringAndSize(valObject, (char **) &val, NULL) == -1)
1642 return -1;
1643 if (val == NULL)
1644 return -1;
1645
1646 val = vim_strsave(val);
1647 }
1648 else if (PyUnicode_Check(valObject))
1649 {
1650 PyObject *bytes;
1651
1652 bytes = PyUnicode_AsEncodedString(valObject, (char *)ENC_OPT, NULL);
1653 if (bytes == NULL)
1654 return -1;
1655
1656 if(PyString_AsStringAndSize(bytes, (char **) &val, NULL) == -1)
1657 return -1;
1658 if (val == NULL)
1659 return -1;
1660
1661 val = vim_strsave(val);
1662 Py_XDECREF(bytes);
1663 }
1664 else
1665 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001666 PyErr_SetString(PyExc_TypeError, _("object must be string"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001667 return -1;
1668 }
1669
1670 r = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001671 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001672 vim_free(val);
1673 }
1674
1675 return r;
1676}
1677
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001678static PyMappingMethods OptionsAsMapping = {
1679 (lenfunc) NULL,
1680 (binaryfunc) OptionsItem,
1681 (objobjargproc) OptionsAssItem,
1682};
1683
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001684/* Tabpage object
1685 */
1686
1687typedef struct
1688{
1689 PyObject_HEAD
1690 tabpage_T *tab;
1691} TabPageObject;
1692
1693static PyObject *WinListNew(TabPageObject *tabObject);
1694
1695static PyTypeObject TabPageType;
1696
1697 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001698CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001699{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001700 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001701 {
1702 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1703 return -1;
1704 }
1705
1706 return 0;
1707}
1708
1709 static PyObject *
1710TabPageNew(tabpage_T *tab)
1711{
1712 TabPageObject *self;
1713
1714 if (TAB_PYTHON_REF(tab))
1715 {
1716 self = TAB_PYTHON_REF(tab);
1717 Py_INCREF(self);
1718 }
1719 else
1720 {
1721 self = PyObject_NEW(TabPageObject, &TabPageType);
1722 if (self == NULL)
1723 return NULL;
1724 self->tab = tab;
1725 TAB_PYTHON_REF(tab) = self;
1726 }
1727
1728 return (PyObject *)(self);
1729}
1730
1731 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001732TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001733{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001734 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
1735 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001736
1737 DESTRUCTOR_FINISH(self);
1738}
1739
1740 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001741TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001742{
1743 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001744 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001745 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001746 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001747 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001748 return DictionaryNew(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001749 else if (strcmp(name, "window") == 0)
1750 {
1751 /* For current tab window.c does not bother to set or update tp_curwin
1752 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02001753 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001754 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001755 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001756 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001757 }
1758 return NULL;
1759}
1760
1761 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001762TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001763{
1764 static char repr[100];
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001765
Bram Moolenaard6e39182013-05-21 18:30:34 +02001766 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001767 {
1768 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
1769 return PyString_FromString(repr);
1770 }
1771 else
1772 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001773 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001774
1775 if (t == 0)
1776 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
1777 (self));
1778 else
1779 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
1780
1781 return PyString_FromString(repr);
1782 }
1783}
1784
1785static struct PyMethodDef TabPageMethods[] = {
1786 /* name, function, calling, documentation */
1787 { NULL, NULL, 0, NULL }
1788};
1789
1790/*
1791 * Window list object
1792 */
1793
1794static PyTypeObject TabListType;
1795static PySequenceMethods TabListAsSeq;
1796
1797typedef struct
1798{
1799 PyObject_HEAD
1800} TabListObject;
1801
1802 static PyInt
1803TabListLength(PyObject *self UNUSED)
1804{
1805 tabpage_T *tp = first_tabpage;
1806 PyInt n = 0;
1807
1808 while (tp != NULL)
1809 {
1810 ++n;
1811 tp = tp->tp_next;
1812 }
1813
1814 return n;
1815}
1816
1817 static PyObject *
1818TabListItem(PyObject *self UNUSED, PyInt n)
1819{
1820 tabpage_T *tp;
1821
1822 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
1823 if (n == 0)
1824 return TabPageNew(tp);
1825
1826 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
1827 return NULL;
1828}
1829
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001830/* Window object
1831 */
1832
1833typedef struct
1834{
1835 PyObject_HEAD
1836 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001837 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001838} WindowObject;
1839
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001840static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001841
1842 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001843CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001844{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001845 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001846 {
1847 PyErr_SetVim(_("attempt to refer to deleted window"));
1848 return -1;
1849 }
1850
1851 return 0;
1852}
1853
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001854 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001855WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02001856{
1857 /* We need to handle deletion of windows underneath us.
1858 * If we add a "w_python*_ref" field to the win_T structure,
1859 * then we can get at it in win_free() in vim. We then
1860 * need to create only ONE Python object per window - if
1861 * we try to create a second, just INCREF the existing one
1862 * and return it. The (single) Python object referring to
1863 * the window is stored in "w_python*_ref".
1864 * On a win_free() we set the Python object's win_T* field
1865 * to an invalid value. We trap all uses of a window
1866 * object, and reject them if the win_T* field is invalid.
1867 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001868 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02001869 * w_python_ref and w_python3_ref fields respectively.
1870 */
1871
1872 WindowObject *self;
1873
1874 if (WIN_PYTHON_REF(win))
1875 {
1876 self = WIN_PYTHON_REF(win);
1877 Py_INCREF(self);
1878 }
1879 else
1880 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02001881 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02001882 if (self == NULL)
1883 return NULL;
1884 self->win = win;
1885 WIN_PYTHON_REF(win) = self;
1886 }
1887
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001888 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
1889
Bram Moolenaar971db462013-05-12 18:44:48 +02001890 return (PyObject *)(self);
1891}
1892
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001893 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001894WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001895{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001896 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001897 if (self->win && self->win != INVALID_WINDOW_VALUE)
1898 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02001899 Py_XDECREF(((PyObject *)(self->tabObject)));
1900 PyObject_GC_Del((void *)(self));
1901}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001902
Bram Moolenaar774267b2013-05-21 20:51:59 +02001903 static int
1904WindowTraverse(WindowObject *self, visitproc visit, void *arg)
1905{
1906 Py_VISIT(((PyObject *)(self->tabObject)));
1907 return 0;
1908}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001909
Bram Moolenaar774267b2013-05-21 20:51:59 +02001910 static int
1911WindowClear(WindowObject *self)
1912{
1913 Py_CLEAR(self->tabObject);
1914 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001915}
1916
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001917 static win_T *
1918get_firstwin(TabPageObject *tabObject)
1919{
1920 if (tabObject)
1921 {
1922 if (CheckTabPage(tabObject))
1923 return NULL;
1924 /* For current tab window.c does not bother to set or update tp_firstwin
1925 */
1926 else if (tabObject->tab == curtab)
1927 return firstwin;
1928 else
1929 return tabObject->tab->tp_firstwin;
1930 }
1931 else
1932 return firstwin;
1933}
1934
Bram Moolenaar971db462013-05-12 18:44:48 +02001935 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001936WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001937{
1938 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001939 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001940 else if (strcmp(name, "cursor") == 0)
1941 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001942 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001943
1944 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1945 }
1946 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001947 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001948#ifdef FEAT_WINDOWS
1949 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001950 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001951#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001952#ifdef FEAT_VERTSPLIT
1953 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001954 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001955 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001956 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001957#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001958 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001959 return DictionaryNew(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001960 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001961 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
1962 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02001963 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001964 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001965 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001966 return NULL;
1967 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001968 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001969 }
1970 else if (strcmp(name, "tabpage") == 0)
1971 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001972 Py_INCREF(self->tabObject);
1973 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001974 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001975 else if (strcmp(name,"__members__") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001976 return Py_BuildValue("[sssssssss]", "buffer", "cursor", "height",
1977 "vars", "options", "number", "row", "col", "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001978 else
1979 return NULL;
1980}
1981
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001982 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001983WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001984{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001985 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001986 return -1;
1987
1988 if (strcmp(name, "buffer") == 0)
1989 {
1990 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
1991 return -1;
1992 }
1993 else if (strcmp(name, "cursor") == 0)
1994 {
1995 long lnum;
1996 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001997
1998 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
1999 return -1;
2000
Bram Moolenaard6e39182013-05-21 18:30:34 +02002001 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002002 {
2003 PyErr_SetVim(_("cursor position outside buffer"));
2004 return -1;
2005 }
2006
2007 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002008 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002009 return -1;
2010
Bram Moolenaard6e39182013-05-21 18:30:34 +02002011 self->win->w_cursor.lnum = lnum;
2012 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002013#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002014 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002015#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002016 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002017 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002018
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002019 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002020 return 0;
2021 }
2022 else if (strcmp(name, "height") == 0)
2023 {
2024 int height;
2025 win_T *savewin;
2026
2027 if (!PyArg_Parse(val, "i", &height))
2028 return -1;
2029
2030#ifdef FEAT_GUI
2031 need_mouse_correct = TRUE;
2032#endif
2033 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002034 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002035
2036 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002037 win_setheight(height);
2038 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002039 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002040 return -1;
2041
2042 return 0;
2043 }
2044#ifdef FEAT_VERTSPLIT
2045 else if (strcmp(name, "width") == 0)
2046 {
2047 int width;
2048 win_T *savewin;
2049
2050 if (!PyArg_Parse(val, "i", &width))
2051 return -1;
2052
2053#ifdef FEAT_GUI
2054 need_mouse_correct = TRUE;
2055#endif
2056 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002057 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002058
2059 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002060 win_setwidth(width);
2061 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002062 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002063 return -1;
2064
2065 return 0;
2066 }
2067#endif
2068 else
2069 {
2070 PyErr_SetString(PyExc_AttributeError, name);
2071 return -1;
2072 }
2073}
2074
2075 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002076WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002077{
2078 static char repr[100];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002079
Bram Moolenaard6e39182013-05-21 18:30:34 +02002080 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002081 {
2082 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2083 return PyString_FromString(repr);
2084 }
2085 else
2086 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002087 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002088
Bram Moolenaar6d216452013-05-12 19:00:41 +02002089 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002090 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2091 (self));
2092 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002093 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002094
2095 return PyString_FromString(repr);
2096 }
2097}
2098
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002099static struct PyMethodDef WindowMethods[] = {
2100 /* name, function, calling, documentation */
2101 { NULL, NULL, 0, NULL }
2102};
2103
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002104/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002105 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002106 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002107
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002108static PyTypeObject WinListType;
2109static PySequenceMethods WinListAsSeq;
2110
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002111typedef struct
2112{
2113 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002114 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002115} WinListObject;
2116
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002117 static PyObject *
2118WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002119{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002120 WinListObject *self;
2121
2122 self = PyObject_NEW(WinListObject, &WinListType);
2123 self->tabObject = tabObject;
2124 Py_INCREF(tabObject);
2125
2126 return (PyObject *)(self);
2127}
2128
2129 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002130WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002131{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002132 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002133
2134 if (tabObject)
2135 Py_DECREF((PyObject *)(tabObject));
2136
2137 DESTRUCTOR_FINISH(self);
2138}
2139
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002140 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002141WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002142{
2143 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002144 PyInt n = 0;
2145
Bram Moolenaard6e39182013-05-21 18:30:34 +02002146 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002147 return -1;
2148
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002149 while (w != NULL)
2150 {
2151 ++n;
2152 w = W_NEXT(w);
2153 }
2154
2155 return n;
2156}
2157
2158 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002159WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002160{
2161 win_T *w;
2162
Bram Moolenaard6e39182013-05-21 18:30:34 +02002163 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002164 return NULL;
2165
2166 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002167 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002168 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002169
2170 PyErr_SetString(PyExc_IndexError, _("no such window"));
2171 return NULL;
2172}
2173
2174/* Convert a Python string into a Vim line.
2175 *
2176 * The result is in allocated memory. All internal nulls are replaced by
2177 * newline characters. It is an error for the string to contain newline
2178 * characters.
2179 *
2180 * On errors, the Python exception data is set, and NULL is returned.
2181 */
2182 static char *
2183StringToLine(PyObject *obj)
2184{
2185 const char *str;
2186 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002187 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002188 PyInt len;
2189 PyInt i;
2190 char *p;
2191
2192 if (obj == NULL || !PyString_Check(obj))
2193 {
2194 PyErr_BadArgument();
2195 return NULL;
2196 }
2197
Bram Moolenaar19e60942011-06-19 00:27:51 +02002198 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2199 str = PyString_AsString(bytes);
2200 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002201
2202 /*
2203 * Error checking: String must not contain newlines, as we
2204 * are replacing a single line, and we must replace it with
2205 * a single line.
2206 * A trailing newline is removed, so that append(f.readlines()) works.
2207 */
2208 p = memchr(str, '\n', len);
2209 if (p != NULL)
2210 {
2211 if (p == str + len - 1)
2212 --len;
2213 else
2214 {
2215 PyErr_SetVim(_("string cannot contain newlines"));
2216 return NULL;
2217 }
2218 }
2219
2220 /* Create a copy of the string, with internal nulls replaced by
2221 * newline characters, as is the vim convention.
2222 */
2223 save = (char *)alloc((unsigned)(len+1));
2224 if (save == NULL)
2225 {
2226 PyErr_NoMemory();
2227 return NULL;
2228 }
2229
2230 for (i = 0; i < len; ++i)
2231 {
2232 if (str[i] == '\0')
2233 save[i] = '\n';
2234 else
2235 save[i] = str[i];
2236 }
2237
2238 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002239 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002240
2241 return save;
2242}
2243
2244/* Get a line from the specified buffer. The line number is
2245 * in Vim format (1-based). The line is returned as a Python
2246 * string object.
2247 */
2248 static PyObject *
2249GetBufferLine(buf_T *buf, PyInt n)
2250{
2251 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2252}
2253
2254
2255/* Get a list of lines from the specified buffer. The line numbers
2256 * are in Vim format (1-based). The range is from lo up to, but not
2257 * including, hi. The list is returned as a Python list of string objects.
2258 */
2259 static PyObject *
2260GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2261{
2262 PyInt i;
2263 PyInt n = hi - lo;
2264 PyObject *list = PyList_New(n);
2265
2266 if (list == NULL)
2267 return NULL;
2268
2269 for (i = 0; i < n; ++i)
2270 {
2271 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2272
2273 /* Error check - was the Python string creation OK? */
2274 if (str == NULL)
2275 {
2276 Py_DECREF(list);
2277 return NULL;
2278 }
2279
2280 /* Set the list item */
2281 if (PyList_SetItem(list, i, str))
2282 {
2283 Py_DECREF(str);
2284 Py_DECREF(list);
2285 return NULL;
2286 }
2287 }
2288
2289 /* The ownership of the Python list is passed to the caller (ie,
2290 * the caller should Py_DECREF() the object when it is finished
2291 * with it).
2292 */
2293
2294 return list;
2295}
2296
2297/*
2298 * Check if deleting lines made the cursor position invalid.
2299 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2300 * deleted).
2301 */
2302 static void
2303py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2304{
2305 if (curwin->w_cursor.lnum >= lo)
2306 {
2307 /* Adjust the cursor position if it's in/after the changed
2308 * lines. */
2309 if (curwin->w_cursor.lnum >= hi)
2310 {
2311 curwin->w_cursor.lnum += extra;
2312 check_cursor_col();
2313 }
2314 else if (extra < 0)
2315 {
2316 curwin->w_cursor.lnum = lo;
2317 check_cursor();
2318 }
2319 else
2320 check_cursor_col();
2321 changed_cline_bef_curs();
2322 }
2323 invalidate_botline();
2324}
2325
Bram Moolenaar19e60942011-06-19 00:27:51 +02002326/*
2327 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002328 * in Vim format (1-based). The replacement line is given as
2329 * a Python string object. The object is checked for validity
2330 * and correct format. Errors are returned as a value of FAIL.
2331 * The return value is OK on success.
2332 * If OK is returned and len_change is not NULL, *len_change
2333 * is set to the change in the buffer length.
2334 */
2335 static int
2336SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2337{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002338 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002339 * There are three cases:
2340 * 1. NULL, or None - this is a deletion.
2341 * 2. A string - this is a replacement.
2342 * 3. Anything else - this is an error.
2343 */
2344 if (line == Py_None || line == NULL)
2345 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002346 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002347
2348 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002349 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002350
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002351 VimTryStart();
2352
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002353 if (u_savedel((linenr_T)n, 1L) == FAIL)
2354 PyErr_SetVim(_("cannot save undo information"));
2355 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2356 PyErr_SetVim(_("cannot delete line"));
2357 else
2358 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002359 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002360 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2361 deleted_lines_mark((linenr_T)n, 1L);
2362 }
2363
Bram Moolenaar105bc352013-05-17 16:03:57 +02002364 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002365
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002366 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002367 return FAIL;
2368
2369 if (len_change)
2370 *len_change = -1;
2371
2372 return OK;
2373 }
2374 else if (PyString_Check(line))
2375 {
2376 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002377 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002378
2379 if (save == NULL)
2380 return FAIL;
2381
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002382 VimTryStart();
2383
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002384 /* We do not need to free "save" if ml_replace() consumes it. */
2385 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002386 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002387
2388 if (u_savesub((linenr_T)n) == FAIL)
2389 {
2390 PyErr_SetVim(_("cannot save undo information"));
2391 vim_free(save);
2392 }
2393 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2394 {
2395 PyErr_SetVim(_("cannot replace line"));
2396 vim_free(save);
2397 }
2398 else
2399 changed_bytes((linenr_T)n, 0);
2400
Bram Moolenaar105bc352013-05-17 16:03:57 +02002401 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002402
2403 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002404 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002405 check_cursor_col();
2406
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002407 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002408 return FAIL;
2409
2410 if (len_change)
2411 *len_change = 0;
2412
2413 return OK;
2414 }
2415 else
2416 {
2417 PyErr_BadArgument();
2418 return FAIL;
2419 }
2420}
2421
Bram Moolenaar19e60942011-06-19 00:27:51 +02002422/* Replace a range of lines in the specified buffer. The line numbers are in
2423 * Vim format (1-based). The range is from lo up to, but not including, hi.
2424 * The replacement lines are given as a Python list of string objects. The
2425 * list is checked for validity and correct format. Errors are returned as a
2426 * value of FAIL. The return value is OK on success.
2427 * If OK is returned and len_change is not NULL, *len_change
2428 * is set to the change in the buffer length.
2429 */
2430 static int
2431SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2432{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002433 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002434 * There are three cases:
2435 * 1. NULL, or None - this is a deletion.
2436 * 2. A list - this is a replacement.
2437 * 3. Anything else - this is an error.
2438 */
2439 if (list == Py_None || list == NULL)
2440 {
2441 PyInt i;
2442 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002443 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002444
2445 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002446 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002447 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002448
2449 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2450 PyErr_SetVim(_("cannot save undo information"));
2451 else
2452 {
2453 for (i = 0; i < n; ++i)
2454 {
2455 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2456 {
2457 PyErr_SetVim(_("cannot delete line"));
2458 break;
2459 }
2460 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002461 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002462 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2463 deleted_lines_mark((linenr_T)lo, (long)i);
2464 }
2465
Bram Moolenaar105bc352013-05-17 16:03:57 +02002466 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002467
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002468 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002469 return FAIL;
2470
2471 if (len_change)
2472 *len_change = -n;
2473
2474 return OK;
2475 }
2476 else if (PyList_Check(list))
2477 {
2478 PyInt i;
2479 PyInt new_len = PyList_Size(list);
2480 PyInt old_len = hi - lo;
2481 PyInt extra = 0; /* lines added to text, can be negative */
2482 char **array;
2483 buf_T *savebuf;
2484
2485 if (new_len == 0) /* avoid allocating zero bytes */
2486 array = NULL;
2487 else
2488 {
2489 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
2490 if (array == NULL)
2491 {
2492 PyErr_NoMemory();
2493 return FAIL;
2494 }
2495 }
2496
2497 for (i = 0; i < new_len; ++i)
2498 {
2499 PyObject *line = PyList_GetItem(list, i);
2500
2501 array[i] = StringToLine(line);
2502 if (array[i] == NULL)
2503 {
2504 while (i)
2505 vim_free(array[--i]);
2506 vim_free(array);
2507 return FAIL;
2508 }
2509 }
2510
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002511 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02002512 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002513
2514 // START of region without "return". Must call restore_buffer()!
2515 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002516
2517 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2518 PyErr_SetVim(_("cannot save undo information"));
2519
2520 /* If the size of the range is reducing (ie, new_len < old_len) we
2521 * need to delete some old_len. We do this at the start, by
2522 * repeatedly deleting line "lo".
2523 */
2524 if (!PyErr_Occurred())
2525 {
2526 for (i = 0; i < old_len - new_len; ++i)
2527 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2528 {
2529 PyErr_SetVim(_("cannot delete line"));
2530 break;
2531 }
2532 extra -= i;
2533 }
2534
2535 /* For as long as possible, replace the existing old_len with the
2536 * new old_len. This is a more efficient operation, as it requires
2537 * less memory allocation and freeing.
2538 */
2539 if (!PyErr_Occurred())
2540 {
2541 for (i = 0; i < old_len && i < new_len; ++i)
2542 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2543 == FAIL)
2544 {
2545 PyErr_SetVim(_("cannot replace line"));
2546 break;
2547 }
2548 }
2549 else
2550 i = 0;
2551
2552 /* Now we may need to insert the remaining new old_len. If we do, we
2553 * must free the strings as we finish with them (we can't pass the
2554 * responsibility to vim in this case).
2555 */
2556 if (!PyErr_Occurred())
2557 {
2558 while (i < new_len)
2559 {
2560 if (ml_append((linenr_T)(lo + i - 1),
2561 (char_u *)array[i], 0, FALSE) == FAIL)
2562 {
2563 PyErr_SetVim(_("cannot insert line"));
2564 break;
2565 }
2566 vim_free(array[i]);
2567 ++i;
2568 ++extra;
2569 }
2570 }
2571
2572 /* Free any left-over old_len, as a result of an error */
2573 while (i < new_len)
2574 {
2575 vim_free(array[i]);
2576 ++i;
2577 }
2578
2579 /* Free the array of old_len. All of its contents have now
2580 * been dealt with (either freed, or the responsibility passed
2581 * to vim.
2582 */
2583 vim_free(array);
2584
2585 /* Adjust marks. Invalidate any which lie in the
2586 * changed range, and move any in the remainder of the buffer.
2587 */
2588 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2589 (long)MAXLNUM, (long)extra);
2590 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2591
Bram Moolenaar105bc352013-05-17 16:03:57 +02002592 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002593 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2594
Bram Moolenaar105bc352013-05-17 16:03:57 +02002595 // END of region without "return".
2596 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002597
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002598 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002599 return FAIL;
2600
2601 if (len_change)
2602 *len_change = new_len - old_len;
2603
2604 return OK;
2605 }
2606 else
2607 {
2608 PyErr_BadArgument();
2609 return FAIL;
2610 }
2611}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002612
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002613/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002614 * The line number is in Vim format (1-based). The lines to be inserted are
2615 * given as a Python list of string objects or as a single string. The lines
2616 * to be added are checked for validity and correct format. Errors are
2617 * returned as a value of FAIL. The return value is OK on success.
2618 * If OK is returned and len_change is not NULL, *len_change
2619 * is set to the change in the buffer length.
2620 */
2621 static int
2622InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2623{
2624 /* First of all, we check the type of the supplied Python object.
2625 * It must be a string or a list, or the call is in error.
2626 */
2627 if (PyString_Check(lines))
2628 {
2629 char *str = StringToLine(lines);
2630 buf_T *savebuf;
2631
2632 if (str == NULL)
2633 return FAIL;
2634
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002635 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002636 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002637 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002638
2639 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2640 PyErr_SetVim(_("cannot save undo information"));
2641 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2642 PyErr_SetVim(_("cannot insert line"));
2643 else
2644 appended_lines_mark((linenr_T)n, 1L);
2645
2646 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002647 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002648 update_screen(VALID);
2649
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002650 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002651 return FAIL;
2652
2653 if (len_change)
2654 *len_change = 1;
2655
2656 return OK;
2657 }
2658 else if (PyList_Check(lines))
2659 {
2660 PyInt i;
2661 PyInt size = PyList_Size(lines);
2662 char **array;
2663 buf_T *savebuf;
2664
2665 array = (char **)alloc((unsigned)(size * sizeof(char *)));
2666 if (array == NULL)
2667 {
2668 PyErr_NoMemory();
2669 return FAIL;
2670 }
2671
2672 for (i = 0; i < size; ++i)
2673 {
2674 PyObject *line = PyList_GetItem(lines, i);
2675 array[i] = StringToLine(line);
2676
2677 if (array[i] == NULL)
2678 {
2679 while (i)
2680 vim_free(array[--i]);
2681 vim_free(array);
2682 return FAIL;
2683 }
2684 }
2685
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002686 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002687 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002688 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002689
2690 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2691 PyErr_SetVim(_("cannot save undo information"));
2692 else
2693 {
2694 for (i = 0; i < size; ++i)
2695 {
2696 if (ml_append((linenr_T)(n + i),
2697 (char_u *)array[i], 0, FALSE) == FAIL)
2698 {
2699 PyErr_SetVim(_("cannot insert line"));
2700
2701 /* Free the rest of the lines */
2702 while (i < size)
2703 vim_free(array[i++]);
2704
2705 break;
2706 }
2707 vim_free(array[i]);
2708 }
2709 if (i > 0)
2710 appended_lines_mark((linenr_T)n, (long)i);
2711 }
2712
2713 /* Free the array of lines. All of its contents have now
2714 * been freed.
2715 */
2716 vim_free(array);
2717
Bram Moolenaar105bc352013-05-17 16:03:57 +02002718 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002719 update_screen(VALID);
2720
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002721 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002722 return FAIL;
2723
2724 if (len_change)
2725 *len_change = size;
2726
2727 return OK;
2728 }
2729 else
2730 {
2731 PyErr_BadArgument();
2732 return FAIL;
2733 }
2734}
2735
2736/*
2737 * Common routines for buffers and line ranges
2738 * -------------------------------------------
2739 */
2740
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002741typedef struct
2742{
2743 PyObject_HEAD
2744 buf_T *buf;
2745} BufferObject;
2746
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002747 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002748CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002749{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002750 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002751 {
2752 PyErr_SetVim(_("attempt to refer to deleted buffer"));
2753 return -1;
2754 }
2755
2756 return 0;
2757}
2758
2759 static PyObject *
2760RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
2761{
2762 if (CheckBuffer(self))
2763 return NULL;
2764
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002765 if (end == -1)
2766 end = self->buf->b_ml.ml_line_count;
2767
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002768 if (n < 0)
2769 n += end - start + 1;
2770
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002771 if (n < 0 || n > end - start)
2772 {
2773 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2774 return NULL;
2775 }
2776
2777 return GetBufferLine(self->buf, n+start);
2778}
2779
2780 static PyObject *
2781RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
2782{
2783 PyInt size;
2784
2785 if (CheckBuffer(self))
2786 return NULL;
2787
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002788 if (end == -1)
2789 end = self->buf->b_ml.ml_line_count;
2790
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002791 size = end - start + 1;
2792
2793 if (lo < 0)
2794 lo = 0;
2795 else if (lo > size)
2796 lo = size;
2797 if (hi < 0)
2798 hi = 0;
2799 if (hi < lo)
2800 hi = lo;
2801 else if (hi > size)
2802 hi = size;
2803
2804 return GetBufferLineList(self->buf, lo+start, hi+start);
2805}
2806
2807 static PyInt
2808RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2809{
2810 PyInt len_change;
2811
2812 if (CheckBuffer(self))
2813 return -1;
2814
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002815 if (end == -1)
2816 end = self->buf->b_ml.ml_line_count;
2817
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002818 if (n < 0)
2819 n += end - start + 1;
2820
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002821 if (n < 0 || n > end - start)
2822 {
2823 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2824 return -1;
2825 }
2826
2827 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
2828 return -1;
2829
2830 if (new_end)
2831 *new_end = end + len_change;
2832
2833 return 0;
2834}
2835
Bram Moolenaar19e60942011-06-19 00:27:51 +02002836 static PyInt
2837RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2838{
2839 PyInt size;
2840 PyInt len_change;
2841
2842 /* Self must be a valid buffer */
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 Moolenaar19e60942011-06-19 00:27:51 +02002849 /* Sort out the slice range */
2850 size = end - start + 1;
2851
2852 if (lo < 0)
2853 lo = 0;
2854 else if (lo > size)
2855 lo = size;
2856 if (hi < 0)
2857 hi = 0;
2858 if (hi < lo)
2859 hi = lo;
2860 else if (hi > size)
2861 hi = size;
2862
2863 if (SetBufferLineList(self->buf, lo + start, hi + start,
2864 val, &len_change) == FAIL)
2865 return -1;
2866
2867 if (new_end)
2868 *new_end = end + len_change;
2869
2870 return 0;
2871}
2872
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002873
2874 static PyObject *
2875RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
2876{
2877 PyObject *lines;
2878 PyInt len_change;
2879 PyInt max;
2880 PyInt n;
2881
2882 if (CheckBuffer(self))
2883 return NULL;
2884
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002885 if (end == -1)
2886 end = self->buf->b_ml.ml_line_count;
2887
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002888 max = n = end - start + 1;
2889
2890 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
2891 return NULL;
2892
2893 if (n < 0 || n > max)
2894 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002895 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002896 return NULL;
2897 }
2898
2899 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
2900 return NULL;
2901
2902 if (new_end)
2903 *new_end = end + len_change;
2904
2905 Py_INCREF(Py_None);
2906 return Py_None;
2907}
2908
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002909/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002910 */
2911
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002912static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002913static PySequenceMethods RangeAsSeq;
2914static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002915
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002916typedef struct
2917{
2918 PyObject_HEAD
2919 BufferObject *buf;
2920 PyInt start;
2921 PyInt end;
2922} RangeObject;
2923
2924 static PyObject *
2925RangeNew(buf_T *buf, PyInt start, PyInt end)
2926{
2927 BufferObject *bufr;
2928 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002929 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002930 if (self == NULL)
2931 return NULL;
2932
2933 bufr = (BufferObject *)BufferNew(buf);
2934 if (bufr == NULL)
2935 {
2936 Py_DECREF(self);
2937 return NULL;
2938 }
2939 Py_INCREF(bufr);
2940
2941 self->buf = bufr;
2942 self->start = start;
2943 self->end = end;
2944
2945 return (PyObject *)(self);
2946}
2947
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002948 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002949RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002950{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002951 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002952 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02002953 PyObject_GC_Del((void *)(self));
2954}
2955
2956 static int
2957RangeTraverse(RangeObject *self, visitproc visit, void *arg)
2958{
2959 Py_VISIT(((PyObject *)(self->buf)));
2960 return 0;
2961}
2962
2963 static int
2964RangeClear(RangeObject *self)
2965{
2966 Py_CLEAR(self->buf);
2967 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002968}
2969
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002970 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002971RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002972{
2973 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002974 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002975 return -1; /* ??? */
2976
Bram Moolenaard6e39182013-05-21 18:30:34 +02002977 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002978}
2979
2980 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002981RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002982{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002983 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002984}
2985
2986 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002987RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002988{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002989 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002990}
2991
2992 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002993RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002994{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002995 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002996}
2997
2998 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002999RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003000{
3001 static char repr[100];
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003002
Bram Moolenaard6e39182013-05-21 18:30:34 +02003003 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003004 {
3005 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
3006 (self));
3007 return PyString_FromString(repr);
3008 }
3009 else
3010 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003011 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003012 int len;
3013
3014 if (name == NULL)
3015 name = "";
3016 len = (int)strlen(name);
3017
3018 if (len > 45)
3019 name = name + (45 - len);
3020
3021 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
3022 len > 45 ? "..." : "", name,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003023 self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003024
3025 return PyString_FromString(repr);
3026 }
3027}
3028
3029static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003030 /* name, function, calling, documentation */
3031 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
3032 { NULL, NULL, 0, NULL }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003033};
3034
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003035static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003036static PySequenceMethods BufferAsSeq;
3037static PyMappingMethods BufferAsMapping;
3038
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003039 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003040BufferNew(buf_T *buf)
3041{
3042 /* We need to handle deletion of buffers underneath us.
3043 * If we add a "b_python*_ref" field to the buf_T structure,
3044 * then we can get at it in buf_freeall() in vim. We then
3045 * need to create only ONE Python object per buffer - if
3046 * we try to create a second, just INCREF the existing one
3047 * and return it. The (single) Python object referring to
3048 * the buffer is stored in "b_python*_ref".
3049 * Question: what to do on a buf_freeall(). We'll probably
3050 * have to either delete the Python object (DECREF it to
3051 * zero - a bad idea, as it leaves dangling refs!) or
3052 * set the buf_T * value to an invalid value (-1?), which
3053 * means we need checks in all access functions... Bah.
3054 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003055 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003056 * b_python_ref and b_python3_ref fields respectively.
3057 */
3058
3059 BufferObject *self;
3060
3061 if (BUF_PYTHON_REF(buf) != NULL)
3062 {
3063 self = BUF_PYTHON_REF(buf);
3064 Py_INCREF(self);
3065 }
3066 else
3067 {
3068 self = PyObject_NEW(BufferObject, &BufferType);
3069 if (self == NULL)
3070 return NULL;
3071 self->buf = buf;
3072 BUF_PYTHON_REF(buf) = self;
3073 }
3074
3075 return (PyObject *)(self);
3076}
3077
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003078 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003079BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003080{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003081 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3082 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003083
3084 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003085}
3086
Bram Moolenaar971db462013-05-12 18:44:48 +02003087 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003088BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003089{
3090 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003091 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003092 return -1; /* ??? */
3093
Bram Moolenaard6e39182013-05-21 18:30:34 +02003094 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003095}
3096
3097 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003098BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003099{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003100 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003101}
3102
3103 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003104BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003105{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003106 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003107}
3108
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003109 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003110BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003111{
3112 if (strcmp(name, "name") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003113 return Py_BuildValue("s", self->buf->b_ffname);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003114 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003115 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003116 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003117 return DictionaryNew(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003118 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003119 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3120 (PyObject *) self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003121 else if (strcmp(name,"__members__") == 0)
3122 return Py_BuildValue("[ssss]", "name", "number", "vars", "options");
3123 else
3124 return NULL;
3125}
3126
3127 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003128BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003129{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003130 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003131}
3132
3133 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003134BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003135{
3136 pos_T *posp;
3137 char *pmark;
3138 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003139 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003140
Bram Moolenaard6e39182013-05-21 18:30:34 +02003141 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003142 return NULL;
3143
3144 if (!PyArg_ParseTuple(args, "s", &pmark))
3145 return NULL;
3146 mark = *pmark;
3147
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003148 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02003149 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003150 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003151 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003152 if (VimTryEnd())
3153 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003154
3155 if (posp == NULL)
3156 {
3157 PyErr_SetVim(_("invalid mark name"));
3158 return NULL;
3159 }
3160
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003161 if (posp->lnum <= 0)
3162 {
3163 /* Or raise an error? */
3164 Py_INCREF(Py_None);
3165 return Py_None;
3166 }
3167
3168 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3169}
3170
3171 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003172BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003173{
3174 PyInt start;
3175 PyInt end;
3176
Bram Moolenaard6e39182013-05-21 18:30:34 +02003177 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003178 return NULL;
3179
3180 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3181 return NULL;
3182
Bram Moolenaard6e39182013-05-21 18:30:34 +02003183 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003184}
3185
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003186 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003187BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003188{
3189 static char repr[100];
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003190
Bram Moolenaard6e39182013-05-21 18:30:34 +02003191 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003192 {
3193 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3194 return PyString_FromString(repr);
3195 }
3196 else
3197 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003198 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003199 PyInt len;
3200
3201 if (name == NULL)
3202 name = "";
3203 len = strlen(name);
3204
3205 if (len > 35)
3206 name = name + (35 - len);
3207
3208 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3209
3210 return PyString_FromString(repr);
3211 }
3212}
3213
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003214static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003215 /* name, function, calling, documentation */
3216 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
3217 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
3218 {"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 +01003219#if PY_VERSION_HEX >= 0x03000000
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003220 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, "List buffer attributes" },
Bram Moolenaar7f85d292012-02-04 20:17:26 +01003221#endif
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003222 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003223};
3224
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003225/*
3226 * Buffer list object - Implementation
3227 */
3228
3229static PyTypeObject BufMapType;
3230
3231typedef struct
3232{
3233 PyObject_HEAD
3234} BufMapObject;
3235
3236 static PyInt
3237BufMapLength(PyObject *self UNUSED)
3238{
3239 buf_T *b = firstbuf;
3240 PyInt n = 0;
3241
3242 while (b)
3243 {
3244 ++n;
3245 b = b->b_next;
3246 }
3247
3248 return n;
3249}
3250
3251 static PyObject *
3252BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3253{
3254 buf_T *b;
3255 int bnr;
3256
3257#if PY_MAJOR_VERSION < 3
3258 if (PyInt_Check(keyObject))
3259 bnr = PyInt_AsLong(keyObject);
3260 else
3261#endif
3262 if (PyLong_Check(keyObject))
3263 bnr = PyLong_AsLong(keyObject);
3264 else
3265 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003266 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003267 return NULL;
3268 }
3269
3270 b = buflist_findnr(bnr);
3271
3272 if (b)
3273 return BufferNew(b);
3274 else
3275 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003276 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003277 return NULL;
3278 }
3279}
3280
3281 static void
3282BufMapIterDestruct(PyObject *buffer)
3283{
3284 /* Iteration was stopped before all buffers were processed */
3285 if (buffer)
3286 {
3287 Py_DECREF(buffer);
3288 }
3289}
3290
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003291 static int
3292BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3293{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003294 if (buffer)
3295 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003296 return 0;
3297}
3298
3299 static int
3300BufMapIterClear(PyObject **buffer)
3301{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003302 if (*buffer)
3303 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003304 return 0;
3305}
3306
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003307 static PyObject *
3308BufMapIterNext(PyObject **buffer)
3309{
3310 PyObject *next;
3311 PyObject *r;
3312
3313 if (!*buffer)
3314 return NULL;
3315
3316 r = *buffer;
3317
3318 if (CheckBuffer((BufferObject *)(r)))
3319 {
3320 *buffer = NULL;
3321 return NULL;
3322 }
3323
3324 if (!((BufferObject *)(r))->buf->b_next)
3325 next = NULL;
3326 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3327 return NULL;
3328 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02003329 /* Do not increment reference: we no longer hold it (decref), but whoever
3330 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003331 return r;
3332}
3333
3334 static PyObject *
3335BufMapIter(PyObject *self UNUSED)
3336{
3337 PyObject *buffer;
3338
3339 buffer = BufferNew(firstbuf);
3340 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003341 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3342 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003343}
3344
3345static PyMappingMethods BufMapAsMapping = {
3346 (lenfunc) BufMapLength,
3347 (binaryfunc) BufMapItem,
3348 (objobjargproc) 0,
3349};
3350
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003351/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003352 */
3353
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003354 static PyObject *
3355CurrentGetattr(PyObject *self UNUSED, char *name)
3356{
3357 if (strcmp(name, "buffer") == 0)
3358 return (PyObject *)BufferNew(curbuf);
3359 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003360 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003361 else if (strcmp(name, "tabpage") == 0)
3362 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003363 else if (strcmp(name, "line") == 0)
3364 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3365 else if (strcmp(name, "range") == 0)
3366 return RangeNew(curbuf, RangeStart, RangeEnd);
3367 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003368 return Py_BuildValue("[sssss]", "buffer", "window", "line", "range",
3369 "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003370 else
3371 {
3372 PyErr_SetString(PyExc_AttributeError, name);
3373 return NULL;
3374 }
3375}
3376
3377 static int
3378CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3379{
3380 if (strcmp(name, "line") == 0)
3381 {
3382 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3383 return -1;
3384
3385 return 0;
3386 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003387 else if (strcmp(name, "buffer") == 0)
3388 {
3389 int count;
3390
3391 if (value->ob_type != &BufferType)
3392 {
3393 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3394 return -1;
3395 }
3396
3397 if (CheckBuffer((BufferObject *)(value)))
3398 return -1;
3399 count = ((BufferObject *)(value))->buf->b_fnum;
3400
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003401 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003402 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3403 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003404 if (VimTryEnd())
3405 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003406 PyErr_SetVim(_("failed to switch to given buffer"));
3407 return -1;
3408 }
3409
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003410 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003411 }
3412 else if (strcmp(name, "window") == 0)
3413 {
3414 int count;
3415
3416 if (value->ob_type != &WindowType)
3417 {
3418 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3419 return -1;
3420 }
3421
3422 if (CheckWindow((WindowObject *)(value)))
3423 return -1;
3424 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3425
3426 if (!count)
3427 {
3428 PyErr_SetString(PyExc_ValueError,
3429 _("failed to find window in the current tab page"));
3430 return -1;
3431 }
3432
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003433 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003434 win_goto(((WindowObject *)(value))->win);
3435 if (((WindowObject *)(value))->win != curwin)
3436 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003437 if (VimTryEnd())
3438 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003439 PyErr_SetString(PyExc_RuntimeError,
3440 _("did not switch to the specified window"));
3441 return -1;
3442 }
3443
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003444 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003445 }
3446 else if (strcmp(name, "tabpage") == 0)
3447 {
3448 if (value->ob_type != &TabPageType)
3449 {
3450 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3451 return -1;
3452 }
3453
3454 if (CheckTabPage((TabPageObject *)(value)))
3455 return -1;
3456
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003457 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003458 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3459 if (((TabPageObject *)(value))->tab != curtab)
3460 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003461 if (VimTryEnd())
3462 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003463 PyErr_SetString(PyExc_RuntimeError,
3464 _("did not switch to the specified tab page"));
3465 return -1;
3466 }
3467
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003468 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003469 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003470 else
3471 {
3472 PyErr_SetString(PyExc_AttributeError, name);
3473 return -1;
3474 }
3475}
3476
Bram Moolenaardb913952012-06-29 12:54:53 +02003477 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003478init_range_cmd(exarg_T *eap)
3479{
3480 RangeStart = eap->line1;
3481 RangeEnd = eap->line2;
3482}
3483
3484 static void
3485init_range_eval(typval_T *rettv UNUSED)
3486{
3487 RangeStart = (PyInt) curwin->w_cursor.lnum;
3488 RangeEnd = RangeStart;
3489}
3490
3491 static void
3492run_cmd(const char *cmd, void *arg UNUSED, PyGILState_STATE *pygilstate UNUSED)
3493{
3494 PyRun_SimpleString((char *) cmd);
3495}
3496
3497static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
3498static int code_hdr_len = 30;
3499
3500 static void
3501run_do(const char *cmd, void *arg UNUSED, PyGILState_STATE *pygilstate)
3502{
3503 PyInt lnum;
3504 size_t len;
3505 char *code;
3506 int status;
3507 PyObject *pyfunc, *pymain;
3508
3509 if (u_save(RangeStart - 1, RangeEnd + 1) != OK)
3510 {
3511 EMSG(_("cannot save undo information"));
3512 return;
3513 }
3514
3515 len = code_hdr_len + STRLEN(cmd);
3516 code = PyMem_New(char, len + 1);
3517 memcpy(code, code_hdr, code_hdr_len);
3518 STRCPY(code + code_hdr_len, cmd);
3519 status = PyRun_SimpleString(code);
3520 PyMem_Free(code);
3521
3522 if (status)
3523 {
3524 EMSG(_("failed to run the code"));
3525 return;
3526 }
3527
3528 status = 0;
3529 pymain = PyImport_AddModule("__main__");
3530 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
3531 PyGILState_Release(*pygilstate);
3532
3533 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
3534 {
3535 PyObject *line, *linenr, *ret;
3536
3537 *pygilstate = PyGILState_Ensure();
3538 if (!(line = GetBufferLine(curbuf, lnum)))
3539 goto err;
3540 if (!(linenr = PyInt_FromLong((long) lnum)))
3541 {
3542 Py_DECREF(line);
3543 goto err;
3544 }
3545 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
3546 Py_DECREF(line);
3547 Py_DECREF(linenr);
3548 if (!ret)
3549 goto err;
3550
3551 if (ret != Py_None)
3552 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
3553 goto err;
3554
3555 Py_XDECREF(ret);
3556 PythonIO_Flush();
3557 PyGILState_Release(*pygilstate);
3558 }
3559 goto out;
3560err:
3561 *pygilstate = PyGILState_Ensure();
3562 PyErr_PrintEx(0);
3563 PythonIO_Flush();
3564 status = 1;
3565out:
3566 if (!status)
3567 *pygilstate = PyGILState_Ensure();
3568 Py_DECREF(pyfunc);
3569 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
3570 if (status)
3571 return;
3572 check_cursor();
3573 update_curbuf(NOT_VALID);
3574}
3575
3576 static void
3577run_eval(const char *cmd, typval_T *rettv, PyGILState_STATE *pygilstate UNUSED)
3578{
3579 PyObject *r;
3580
3581 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
3582 if (r == NULL)
3583 {
3584 if (PyErr_Occurred() && !msg_silent)
3585 PyErr_PrintEx(0);
3586 EMSG(_("E858: Eval did not return a valid python object"));
3587 }
3588 else
3589 {
3590 if (ConvertFromPyObject(r, rettv) == -1)
3591 EMSG(_("E859: Failed to convert returned python object to vim value"));
3592 Py_DECREF(r);
3593 }
3594 PyErr_Clear();
3595}
3596
3597 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02003598set_ref_in_py(const int copyID)
3599{
3600 pylinkedlist_T *cur;
3601 dict_T *dd;
3602 list_T *ll;
3603
3604 if (lastdict != NULL)
3605 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3606 {
3607 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3608 if (dd->dv_copyID != copyID)
3609 {
3610 dd->dv_copyID = copyID;
3611 set_ref_in_ht(&dd->dv_hashtab, copyID);
3612 }
3613 }
3614
3615 if (lastlist != NULL)
3616 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3617 {
3618 ll = ((ListObject *) (cur->pll_obj))->list;
3619 if (ll->lv_copyID != copyID)
3620 {
3621 ll->lv_copyID = copyID;
3622 set_ref_in_list(ll, copyID);
3623 }
3624 }
3625}
3626
3627 static int
3628set_string_copy(char_u *str, typval_T *tv)
3629{
3630 tv->vval.v_string = vim_strsave(str);
3631 if (tv->vval.v_string == NULL)
3632 {
3633 PyErr_NoMemory();
3634 return -1;
3635 }
3636 return 0;
3637}
3638
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003639 static int
3640pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3641{
3642 dict_T *d;
3643 char_u *key;
3644 dictitem_T *di;
3645 PyObject *keyObject;
3646 PyObject *valObject;
3647 Py_ssize_t iter = 0;
3648
3649 d = dict_alloc();
3650 if (d == NULL)
3651 {
3652 PyErr_NoMemory();
3653 return -1;
3654 }
3655
3656 tv->v_type = VAR_DICT;
3657 tv->vval.v_dict = d;
3658
3659 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
3660 {
3661 DICTKEY_DECL
3662
3663 if (keyObject == NULL)
3664 return -1;
3665 if (valObject == NULL)
3666 return -1;
3667
3668 DICTKEY_GET_NOTEMPTY(-1)
3669
3670 di = dictitem_alloc(key);
3671
3672 DICTKEY_UNREF
3673
3674 if (di == NULL)
3675 {
3676 PyErr_NoMemory();
3677 return -1;
3678 }
3679 di->di_tv.v_lock = 0;
3680
3681 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3682 {
3683 vim_free(di);
3684 return -1;
3685 }
3686 if (dict_add(d, di) == FAIL)
3687 {
3688 vim_free(di);
3689 PyErr_SetVim(_("failed to add key to dictionary"));
3690 return -1;
3691 }
3692 }
3693 return 0;
3694}
3695
3696 static int
3697pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3698{
3699 dict_T *d;
3700 char_u *key;
3701 dictitem_T *di;
3702 PyObject *list;
3703 PyObject *litem;
3704 PyObject *keyObject;
3705 PyObject *valObject;
3706 Py_ssize_t lsize;
3707
3708 d = dict_alloc();
3709 if (d == NULL)
3710 {
3711 PyErr_NoMemory();
3712 return -1;
3713 }
3714
3715 tv->v_type = VAR_DICT;
3716 tv->vval.v_dict = d;
3717
3718 list = PyMapping_Items(obj);
3719 if (list == NULL)
3720 return -1;
3721 lsize = PyList_Size(list);
3722 while (lsize--)
3723 {
3724 DICTKEY_DECL
3725
3726 litem = PyList_GetItem(list, lsize);
3727 if (litem == NULL)
3728 {
3729 Py_DECREF(list);
3730 return -1;
3731 }
3732
3733 keyObject = PyTuple_GetItem(litem, 0);
3734 if (keyObject == NULL)
3735 {
3736 Py_DECREF(list);
3737 Py_DECREF(litem);
3738 return -1;
3739 }
3740
3741 DICTKEY_GET_NOTEMPTY(-1)
3742
3743 valObject = PyTuple_GetItem(litem, 1);
3744 if (valObject == NULL)
3745 {
3746 Py_DECREF(list);
3747 Py_DECREF(litem);
3748 return -1;
3749 }
3750
3751 di = dictitem_alloc(key);
3752
3753 DICTKEY_UNREF
3754
3755 if (di == NULL)
3756 {
3757 Py_DECREF(list);
3758 Py_DECREF(litem);
3759 PyErr_NoMemory();
3760 return -1;
3761 }
3762 di->di_tv.v_lock = 0;
3763
3764 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3765 {
3766 vim_free(di);
3767 Py_DECREF(list);
3768 Py_DECREF(litem);
3769 return -1;
3770 }
3771 if (dict_add(d, di) == FAIL)
3772 {
3773 vim_free(di);
3774 Py_DECREF(list);
3775 Py_DECREF(litem);
3776 PyErr_SetVim(_("failed to add key to dictionary"));
3777 return -1;
3778 }
3779 Py_DECREF(litem);
3780 }
3781 Py_DECREF(list);
3782 return 0;
3783}
3784
3785 static int
3786pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3787{
3788 list_T *l;
3789
3790 l = list_alloc();
3791 if (l == NULL)
3792 {
3793 PyErr_NoMemory();
3794 return -1;
3795 }
3796
3797 tv->v_type = VAR_LIST;
3798 tv->vval.v_list = l;
3799
3800 if (list_py_concat(l, obj, lookupDict) == -1)
3801 return -1;
3802
3803 return 0;
3804}
3805
3806 static int
3807pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3808{
3809 PyObject *iterator = PyObject_GetIter(obj);
3810 PyObject *item;
3811 list_T *l;
3812 listitem_T *li;
3813
3814 l = list_alloc();
3815
3816 if (l == NULL)
3817 {
3818 PyErr_NoMemory();
3819 return -1;
3820 }
3821
3822 tv->vval.v_list = l;
3823 tv->v_type = VAR_LIST;
3824
3825
3826 if (iterator == NULL)
3827 return -1;
3828
3829 while ((item = PyIter_Next(obj)))
3830 {
3831 li = listitem_alloc();
3832 if (li == NULL)
3833 {
3834 PyErr_NoMemory();
3835 return -1;
3836 }
3837 li->li_tv.v_lock = 0;
3838
3839 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
3840 return -1;
3841
3842 list_append(l, li);
3843
3844 Py_DECREF(item);
3845 }
3846
3847 Py_DECREF(iterator);
3848 return 0;
3849}
3850
Bram Moolenaardb913952012-06-29 12:54:53 +02003851typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
3852
3853 static int
3854convert_dl(PyObject *obj, typval_T *tv,
3855 pytotvfunc py_to_tv, PyObject *lookupDict)
3856{
3857 PyObject *capsule;
3858 char hexBuf[sizeof(void *) * 2 + 3];
3859
3860 sprintf(hexBuf, "%p", obj);
3861
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003862# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003863 capsule = PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003864# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003865 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003866# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02003867 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02003868 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003869# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003870 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02003871# else
3872 capsule = PyCObject_FromVoidPtr(tv, NULL);
3873# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003874 PyDict_SetItemString(lookupDict, hexBuf, capsule);
3875 Py_DECREF(capsule);
3876 if (py_to_tv(obj, tv, lookupDict) == -1)
3877 {
3878 tv->v_type = VAR_UNKNOWN;
3879 return -1;
3880 }
3881 /* As we are not using copy_tv which increments reference count we must
3882 * do it ourself. */
3883 switch(tv->v_type)
3884 {
3885 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
3886 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
3887 }
3888 }
3889 else
3890 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003891 typval_T *v;
3892
3893# ifdef PY_USE_CAPSULE
3894 v = PyCapsule_GetPointer(capsule, NULL);
3895# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003896 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003897# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003898 copy_tv(v, tv);
3899 }
3900 return 0;
3901}
3902
3903 static int
3904ConvertFromPyObject(PyObject *obj, typval_T *tv)
3905{
3906 PyObject *lookup_dict;
3907 int r;
3908
3909 lookup_dict = PyDict_New();
3910 r = _ConvertFromPyObject(obj, tv, lookup_dict);
3911 Py_DECREF(lookup_dict);
3912 return r;
3913}
3914
3915 static int
3916_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3917{
3918 if (obj->ob_type == &DictionaryType)
3919 {
3920 tv->v_type = VAR_DICT;
3921 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
3922 ++tv->vval.v_dict->dv_refcount;
3923 }
3924 else if (obj->ob_type == &ListType)
3925 {
3926 tv->v_type = VAR_LIST;
3927 tv->vval.v_list = (((ListObject *)(obj))->list);
3928 ++tv->vval.v_list->lv_refcount;
3929 }
3930 else if (obj->ob_type == &FunctionType)
3931 {
3932 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
3933 return -1;
3934
3935 tv->v_type = VAR_FUNC;
3936 func_ref(tv->vval.v_string);
3937 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003938 else if (PyBytes_Check(obj))
3939 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003940 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02003941
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003942 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
3943 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003944 if (result == NULL)
3945 return -1;
3946
3947 if (set_string_copy(result, tv) == -1)
3948 return -1;
3949
3950 tv->v_type = VAR_STRING;
3951 }
3952 else if (PyUnicode_Check(obj))
3953 {
3954 PyObject *bytes;
3955 char_u *result;
3956
Bram Moolenaardb913952012-06-29 12:54:53 +02003957 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
3958 if (bytes == NULL)
3959 return -1;
3960
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003961 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
3962 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003963 if (result == NULL)
3964 return -1;
3965
3966 if (set_string_copy(result, tv) == -1)
3967 {
3968 Py_XDECREF(bytes);
3969 return -1;
3970 }
3971 Py_XDECREF(bytes);
3972
3973 tv->v_type = VAR_STRING;
3974 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02003975#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02003976 else if (PyInt_Check(obj))
3977 {
3978 tv->v_type = VAR_NUMBER;
3979 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
3980 }
3981#endif
3982 else if (PyLong_Check(obj))
3983 {
3984 tv->v_type = VAR_NUMBER;
3985 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
3986 }
3987 else if (PyDict_Check(obj))
3988 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
3989#ifdef FEAT_FLOAT
3990 else if (PyFloat_Check(obj))
3991 {
3992 tv->v_type = VAR_FLOAT;
3993 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
3994 }
3995#endif
3996 else if (PyIter_Check(obj))
3997 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
3998 else if (PySequence_Check(obj))
3999 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
4000 else if (PyMapping_Check(obj))
4001 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
4002 else
4003 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004004 PyErr_SetString(PyExc_TypeError,
4005 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004006 return -1;
4007 }
4008 return 0;
4009}
4010
4011 static PyObject *
4012ConvertToPyObject(typval_T *tv)
4013{
4014 if (tv == NULL)
4015 {
4016 PyErr_SetVim(_("NULL reference passed"));
4017 return NULL;
4018 }
4019 switch (tv->v_type)
4020 {
4021 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004022 return PyBytes_FromString(tv->vval.v_string == NULL
4023 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004024 case VAR_NUMBER:
4025 return PyLong_FromLong((long) tv->vval.v_number);
4026#ifdef FEAT_FLOAT
4027 case VAR_FLOAT:
4028 return PyFloat_FromDouble((double) tv->vval.v_float);
4029#endif
4030 case VAR_LIST:
4031 return ListNew(tv->vval.v_list);
4032 case VAR_DICT:
4033 return DictionaryNew(tv->vval.v_dict);
4034 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004035 return FunctionNew(tv->vval.v_string == NULL
4036 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004037 case VAR_UNKNOWN:
4038 Py_INCREF(Py_None);
4039 return Py_None;
4040 default:
4041 PyErr_SetVim(_("internal error: invalid value type"));
4042 return NULL;
4043 }
4044}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004045
4046typedef struct
4047{
4048 PyObject_HEAD
4049} CurrentObject;
4050static PyTypeObject CurrentType;
4051
4052 static void
4053init_structs(void)
4054{
4055 vim_memset(&OutputType, 0, sizeof(OutputType));
4056 OutputType.tp_name = "vim.message";
4057 OutputType.tp_basicsize = sizeof(OutputObject);
4058 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4059 OutputType.tp_doc = "vim message object";
4060 OutputType.tp_methods = OutputMethods;
4061#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004062 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4063 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004064 OutputType.tp_alloc = call_PyType_GenericAlloc;
4065 OutputType.tp_new = call_PyType_GenericNew;
4066 OutputType.tp_free = call_PyObject_Free;
4067#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004068 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4069 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004070#endif
4071
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004072 vim_memset(&IterType, 0, sizeof(IterType));
4073 IterType.tp_name = "vim.iter";
4074 IterType.tp_basicsize = sizeof(IterObject);
4075 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
4076 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004077 IterType.tp_iter = (getiterfunc)IterIter;
4078 IterType.tp_iternext = (iternextfunc)IterNext;
4079 IterType.tp_dealloc = (destructor)IterDestructor;
4080 IterType.tp_traverse = (traverseproc)IterTraverse;
4081 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004082
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004083 vim_memset(&BufferType, 0, sizeof(BufferType));
4084 BufferType.tp_name = "vim.buffer";
4085 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004086 BufferType.tp_dealloc = (destructor)BufferDestructor;
4087 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004088 BufferType.tp_as_sequence = &BufferAsSeq;
4089 BufferType.tp_as_mapping = &BufferAsMapping;
4090 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4091 BufferType.tp_doc = "vim buffer object";
4092 BufferType.tp_methods = BufferMethods;
4093#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004094 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004095 BufferType.tp_alloc = call_PyType_GenericAlloc;
4096 BufferType.tp_new = call_PyType_GenericNew;
4097 BufferType.tp_free = call_PyObject_Free;
4098#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004099 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004100#endif
4101
4102 vim_memset(&WindowType, 0, sizeof(WindowType));
4103 WindowType.tp_name = "vim.window";
4104 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004105 WindowType.tp_dealloc = (destructor)WindowDestructor;
4106 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004107 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
4108 WindowType.tp_doc = "vim Window object";
4109 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004110 WindowType.tp_traverse = (traverseproc)WindowTraverse;
4111 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004112#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004113 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
4114 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004115 WindowType.tp_alloc = call_PyType_GenericAlloc;
4116 WindowType.tp_new = call_PyType_GenericNew;
4117 WindowType.tp_free = call_PyObject_Free;
4118#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004119 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
4120 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004121#endif
4122
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004123 vim_memset(&TabPageType, 0, sizeof(TabPageType));
4124 TabPageType.tp_name = "vim.tabpage";
4125 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004126 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
4127 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004128 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
4129 TabPageType.tp_doc = "vim tab page object";
4130 TabPageType.tp_methods = TabPageMethods;
4131#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004132 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004133 TabPageType.tp_alloc = call_PyType_GenericAlloc;
4134 TabPageType.tp_new = call_PyType_GenericNew;
4135 TabPageType.tp_free = call_PyObject_Free;
4136#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004137 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004138#endif
4139
Bram Moolenaardfa38d42013-05-15 13:38:47 +02004140 vim_memset(&BufMapType, 0, sizeof(BufMapType));
4141 BufMapType.tp_name = "vim.bufferlist";
4142 BufMapType.tp_basicsize = sizeof(BufMapObject);
4143 BufMapType.tp_as_mapping = &BufMapAsMapping;
4144 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004145 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004146 BufferType.tp_doc = "vim buffer list";
4147
4148 vim_memset(&WinListType, 0, sizeof(WinListType));
4149 WinListType.tp_name = "vim.windowlist";
4150 WinListType.tp_basicsize = sizeof(WinListType);
4151 WinListType.tp_as_sequence = &WinListAsSeq;
4152 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
4153 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004154 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004155
4156 vim_memset(&TabListType, 0, sizeof(TabListType));
4157 TabListType.tp_name = "vim.tabpagelist";
4158 TabListType.tp_basicsize = sizeof(TabListType);
4159 TabListType.tp_as_sequence = &TabListAsSeq;
4160 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
4161 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004162
4163 vim_memset(&RangeType, 0, sizeof(RangeType));
4164 RangeType.tp_name = "vim.range";
4165 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004166 RangeType.tp_dealloc = (destructor)RangeDestructor;
4167 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004168 RangeType.tp_as_sequence = &RangeAsSeq;
4169 RangeType.tp_as_mapping = &RangeAsMapping;
4170 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
4171 RangeType.tp_doc = "vim Range object";
4172 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004173 RangeType.tp_traverse = (traverseproc)RangeTraverse;
4174 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004175#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004176 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004177 RangeType.tp_alloc = call_PyType_GenericAlloc;
4178 RangeType.tp_new = call_PyType_GenericNew;
4179 RangeType.tp_free = call_PyObject_Free;
4180#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004181 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004182#endif
4183
4184 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4185 CurrentType.tp_name = "vim.currentdata";
4186 CurrentType.tp_basicsize = sizeof(CurrentObject);
4187 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4188 CurrentType.tp_doc = "vim current object";
4189#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004190 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
4191 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004192#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004193 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
4194 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004195#endif
4196
4197 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4198 DictionaryType.tp_name = "vim.dictionary";
4199 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004200 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004201 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4202 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4203 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4204 DictionaryType.tp_methods = DictionaryMethods;
4205#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004206 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
4207 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004208#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004209 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
4210 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004211#endif
4212
4213 vim_memset(&ListType, 0, sizeof(ListType));
4214 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004215 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004216 ListType.tp_basicsize = sizeof(ListObject);
4217 ListType.tp_as_sequence = &ListAsSeq;
4218 ListType.tp_as_mapping = &ListAsMapping;
4219 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4220 ListType.tp_doc = "list pushing modifications to vim structure";
4221 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004222 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004223#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004224 ListType.tp_getattro = (getattrofunc)ListGetattro;
4225 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004226#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004227 ListType.tp_getattr = (getattrfunc)ListGetattr;
4228 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004229#endif
4230
4231 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004232 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004233 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004234 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
4235 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004236 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4237 FunctionType.tp_doc = "object that calls vim function";
4238 FunctionType.tp_methods = FunctionMethods;
4239#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004240 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004241#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004242 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004243#endif
4244
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004245 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4246 OptionsType.tp_name = "vim.options";
4247 OptionsType.tp_basicsize = sizeof(OptionsObject);
4248 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4249 OptionsType.tp_doc = "object for manipulating options";
4250 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004251 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
4252 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
4253 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004254
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004255#if PY_MAJOR_VERSION >= 3
4256 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4257 vimmodule.m_name = "vim";
4258 vimmodule.m_doc = "Vim Python interface\n";
4259 vimmodule.m_size = -1;
4260 vimmodule.m_methods = VimMethods;
4261#endif
4262}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004263
4264#define PYTYPE_READY(type) \
4265 if (PyType_Ready(&type)) \
4266 return -1;
4267
4268 static int
4269init_types()
4270{
4271 PYTYPE_READY(IterType);
4272 PYTYPE_READY(BufferType);
4273 PYTYPE_READY(RangeType);
4274 PYTYPE_READY(WindowType);
4275 PYTYPE_READY(TabPageType);
4276 PYTYPE_READY(BufMapType);
4277 PYTYPE_READY(WinListType);
4278 PYTYPE_READY(TabListType);
4279 PYTYPE_READY(CurrentType);
4280 PYTYPE_READY(DictionaryType);
4281 PYTYPE_READY(ListType);
4282 PYTYPE_READY(FunctionType);
4283 PYTYPE_READY(OptionsType);
4284 PYTYPE_READY(OutputType);
4285 return 0;
4286}
4287
4288static BufMapObject TheBufferMap =
4289{
4290 PyObject_HEAD_INIT(&BufMapType)
4291};
4292
4293static WinListObject TheWindowList =
4294{
4295 PyObject_HEAD_INIT(&WinListType)
4296 NULL
4297};
4298
4299static CurrentObject TheCurrent =
4300{
4301 PyObject_HEAD_INIT(&CurrentType)
4302};
4303
4304static TabListObject TheTabPageList =
4305{
4306 PyObject_HEAD_INIT(&TabListType)
4307};
4308
4309static struct numeric_constant {
4310 char *name;
4311 int value;
4312} numeric_constants[] = {
4313 {"VAR_LOCKED", VAR_LOCKED},
4314 {"VAR_FIXED", VAR_FIXED},
4315 {"VAR_SCOPE", VAR_SCOPE},
4316 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
4317};
4318
4319static struct object_constant {
4320 char *name;
4321 PyObject *value;
4322} object_constants[] = {
4323 {"buffers", (PyObject *)(void *)&TheBufferMap},
4324 {"windows", (PyObject *)(void *)&TheWindowList},
4325 {"tabpages", (PyObject *)(void *)&TheTabPageList},
4326 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02004327
4328 {"Buffer", (PyObject *)&BufferType},
4329 {"Range", (PyObject *)&RangeType},
4330 {"Window", (PyObject *)&WindowType},
4331 {"TabPage", (PyObject *)&TabPageType},
4332 {"Dictionary", (PyObject *)&DictionaryType},
4333 {"List", (PyObject *)&ListType},
4334 {"Function", (PyObject *)&FunctionType},
4335 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004336};
4337
4338typedef int (*object_adder)(PyObject *, const char *, PyObject *);
4339
4340#define ADD_OBJECT(m, name, obj) \
4341 if (add_object(m, name, obj)) \
4342 return -1;
4343
4344#define ADD_CHECKED_OBJECT(m, name, obj) \
4345 { \
4346 PyObject *value = obj; \
4347 if (!value) \
4348 return -1; \
4349 ADD_OBJECT(m, name, value); \
4350 }
4351
4352 static int
4353populate_module(PyObject *m, object_adder add_object)
4354{
4355 int i;
4356
4357 for (i = 0; i < (int)(sizeof(numeric_constants)
4358 / sizeof(struct numeric_constant));
4359 ++i)
4360 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
4361 PyInt_FromLong(numeric_constants[i].value));
4362
4363 for (i = 0; i < (int)(sizeof(object_constants)
4364 / sizeof(struct object_constant));
4365 ++i)
4366 {
4367 PyObject *value;
4368
4369 value = object_constants[i].value;
4370 Py_INCREF(value);
4371 ADD_OBJECT(m, object_constants[i].name, value);
4372 }
4373
4374 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
4375 return -1;
4376 ADD_OBJECT(m, "error", VimError);
4377
4378 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict));
4379 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
4380 ADD_CHECKED_OBJECT(m, "options",
4381 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
4382 return 0;
4383}