blob: eae389437818735513d913ad606613484160a48a [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 Moolenaare9ba5162013-05-29 22:02:22 +020033#define DICTKEY_DECL \
34 PyObject *dictkey_todecref;
35#define DICTKEY_GET(err) \
36 if (!(key = StringToChars(keyObject, &dictkey_todecref))) \
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +020037 return err; \
38 if (*key == NUL) \
39 { \
40 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
41 return err; \
42 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +020043#define DICTKEY_UNREF \
44 Py_XDECREF(dictkey_todecref);
45
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020046typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020047typedef void (*runner)(const char *, void *
48#ifdef PY_CAN_RECURSE
49 , PyGILState_STATE *
50#endif
51 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020052
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020053static int ConvertFromPyObject(PyObject *, typval_T *);
54static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020055static PyObject *WindowNew(win_T *, tabpage_T *);
56static PyObject *BufferNew (buf_T *);
57static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020058
59static PyInt RangeStart;
60static PyInt RangeEnd;
61
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020062static PyObject *globals;
63
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020064/*
65 * obtain a lock on the Vim data structures
66 */
67 static void
68Python_Lock_Vim(void)
69{
70}
71
72/*
73 * release a lock on the Vim data structures
74 */
75 static void
76Python_Release_Vim(void)
77{
78}
79
Bram Moolenaare9ba5162013-05-29 22:02:22 +020080/*
81 * The "todecref" argument holds a pointer to PyObject * that must be
82 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
83 * was needed to generate returned value is object.
84 *
85 * Use Py_XDECREF to decrement reference count.
86 */
87 static char_u *
88StringToChars(PyObject *object, PyObject **todecref)
89{
90 char_u *p;
91 PyObject *bytes = NULL;
92
93 if (PyBytes_Check(object))
94 {
95
96 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
97 return NULL;
98 if (p == NULL)
99 return NULL;
100
101 *todecref = NULL;
102 }
103 else if (PyUnicode_Check(object))
104 {
105 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
106 if (bytes == NULL)
107 return NULL;
108
109 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
110 return NULL;
111 if (p == NULL)
112 return NULL;
113
114 *todecref = bytes;
115 }
116 else
117 {
118 PyErr_SetString(PyExc_TypeError, _("object must be string"));
119 return NULL;
120 }
121
122 return (char_u *) p;
123}
124
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200125 static int
126add_string(PyObject *list, char *s)
127{
128 PyObject *string;
129
130 if (!(string = PyString_FromString(s)))
131 return -1;
132 if (PyList_Append(list, string))
133 {
134 Py_DECREF(string);
135 return -1;
136 }
137
138 Py_DECREF(string);
139 return 0;
140}
141
142 static PyObject *
143ObjectDir(PyObject *self, char **attributes)
144{
145 PyMethodDef *method;
146 char **attr;
147 PyObject *r;
148
149 if (!(r = PyList_New(0)))
150 return NULL;
151
152 if (self)
153 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
154 if (add_string(r, (char *) method->ml_name))
155 {
156 Py_DECREF(r);
157 return NULL;
158 }
159
160 for (attr = attributes ; *attr ; ++attr)
161 if (add_string(r, *attr))
162 {
163 Py_DECREF(r);
164 return NULL;
165 }
166
167#if PY_MAJOR_VERSION < 3
168 if (add_string(r, "__members__"))
169 {
170 Py_DECREF(r);
171 return NULL;
172 }
173#endif
174
175 return r;
176}
177
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200178/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200179 */
180
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200181/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200182typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200183
184static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200185
186typedef struct
187{
188 PyObject_HEAD
189 long softspace;
190 long error;
191} OutputObject;
192
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200193static char *OutputAttrs[] = {
194 "softspace",
195 NULL
196};
197
198 static PyObject *
199OutputDir(PyObject *self)
200{
201 return ObjectDir(self, OutputAttrs);
202}
203
Bram Moolenaar77045652012-09-21 13:46:06 +0200204 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200205OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200206{
207 if (val == NULL)
208 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200209 PyErr_SetString(PyExc_AttributeError,
210 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200211 return -1;
212 }
213
214 if (strcmp(name, "softspace") == 0)
215 {
216 if (!PyInt_Check(val))
217 {
218 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
219 return -1;
220 }
221
Bram Moolenaard6e39182013-05-21 18:30:34 +0200222 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200223 return 0;
224 }
225
226 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
227 return -1;
228}
229
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200230/* Buffer IO, we write one whole line at a time. */
231static garray_T io_ga = {0, 0, 1, 80, NULL};
232static writefn old_fn = NULL;
233
234 static void
235PythonIO_Flush(void)
236{
237 if (old_fn != NULL && io_ga.ga_len > 0)
238 {
239 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
240 old_fn((char_u *)io_ga.ga_data);
241 }
242 io_ga.ga_len = 0;
243}
244
245 static void
246writer(writefn fn, char_u *str, PyInt n)
247{
248 char_u *ptr;
249
250 /* Flush when switching output function. */
251 if (fn != old_fn)
252 PythonIO_Flush();
253 old_fn = fn;
254
255 /* Write each NL separated line. Text after the last NL is kept for
256 * writing later. */
257 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
258 {
259 PyInt len = ptr - str;
260
261 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
262 break;
263
264 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
265 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
266 fn((char_u *)io_ga.ga_data);
267 str = ptr + 1;
268 n -= len + 1;
269 io_ga.ga_len = 0;
270 }
271
272 /* Put the remaining text into io_ga for later printing. */
273 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
274 {
275 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
276 io_ga.ga_len += (int)n;
277 }
278}
279
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200280 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200281OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200282{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200283 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200284 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200285 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200286
Bram Moolenaar27564802011-09-07 19:30:21 +0200287 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200288 return NULL;
289
290 Py_BEGIN_ALLOW_THREADS
291 Python_Lock_Vim();
292 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
293 Python_Release_Vim();
294 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200295 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200296
297 Py_INCREF(Py_None);
298 return Py_None;
299}
300
301 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200302OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200303{
304 PyInt n;
305 PyInt i;
306 PyObject *list;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200307 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200308
309 if (!PyArg_ParseTuple(args, "O", &list))
310 return NULL;
311 Py_INCREF(list);
312
Bram Moolenaardb913952012-06-29 12:54:53 +0200313 if (!PyList_Check(list))
314 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
316 Py_DECREF(list);
317 return NULL;
318 }
319
320 n = PyList_Size(list);
321
322 for (i = 0; i < n; ++i)
323 {
324 PyObject *line = PyList_GetItem(list, i);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200325 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200326 PyInt len;
327
Bram Moolenaardb913952012-06-29 12:54:53 +0200328 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
329 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200330 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
331 Py_DECREF(list);
332 return NULL;
333 }
334
335 Py_BEGIN_ALLOW_THREADS
336 Python_Lock_Vim();
337 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
338 Python_Release_Vim();
339 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200340 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200341 }
342
343 Py_DECREF(list);
344 Py_INCREF(Py_None);
345 return Py_None;
346}
347
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100348 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200349OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100350{
351 /* do nothing */
352 Py_INCREF(Py_None);
353 return Py_None;
354}
355
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200356/***************/
357
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200358static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200359 /* name, function, calling, doc */
360 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
361 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
362 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200363 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200364 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200365};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200366
367static OutputObject Output =
368{
369 PyObject_HEAD_INIT(&OutputType)
370 0,
371 0
372};
373
374static OutputObject Error =
375{
376 PyObject_HEAD_INIT(&OutputType)
377 0,
378 1
379};
380
381 static int
382PythonIO_Init_io(void)
383{
384 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
385 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
386
387 if (PyErr_Occurred())
388 {
389 EMSG(_("E264: Python: Error initialising I/O objects"));
390 return -1;
391 }
392
393 return 0;
394}
395
396
397static PyObject *VimError;
398
399/* Check to see whether a Vim error has been reported, or a keyboard
400 * interrupt has been detected.
401 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200402
403 static void
404VimTryStart(void)
405{
406 ++trylevel;
407}
408
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200409 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200410VimTryEnd(void)
411{
412 --trylevel;
413 if (got_int)
414 {
415 PyErr_SetNone(PyExc_KeyboardInterrupt);
416 return 1;
417 }
418 else if (!did_throw)
419 return 0;
420 else if (PyErr_Occurred())
421 return 1;
422 else
423 {
424 PyErr_SetVim((char *) current_exception->value);
425 discard_current_exception();
426 return 1;
427 }
428}
429
430 static int
431VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200432{
433 if (got_int)
434 {
435 PyErr_SetNone(PyExc_KeyboardInterrupt);
436 return 1;
437 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200438 return 0;
439}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200440
441/* Vim module - Implementation
442 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200443
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200444 static PyObject *
445VimCommand(PyObject *self UNUSED, PyObject *args)
446{
447 char *cmd;
448 PyObject *result;
449
450 if (!PyArg_ParseTuple(args, "s", &cmd))
451 return NULL;
452
453 PyErr_Clear();
454
455 Py_BEGIN_ALLOW_THREADS
456 Python_Lock_Vim();
457
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200458 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200459 do_cmdline_cmd((char_u *)cmd);
460 update_screen(VALID);
461
462 Python_Release_Vim();
463 Py_END_ALLOW_THREADS
464
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200465 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200466 result = NULL;
467 else
468 result = Py_None;
469
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200470
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200471 Py_XINCREF(result);
472 return result;
473}
474
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200475/*
476 * Function to translate a typval_T into a PyObject; this will recursively
477 * translate lists/dictionaries into their Python equivalents.
478 *
479 * The depth parameter is to avoid infinite recursion, set it to 1 when
480 * you call VimToPython.
481 */
482 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200483VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200484{
485 PyObject *result;
486 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200487 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200488
489 /* Avoid infinite recursion */
490 if (depth > 100)
491 {
492 Py_INCREF(Py_None);
493 result = Py_None;
494 return result;
495 }
496
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200497 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200498 * then and we can use it again. */
499 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
500 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
501 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200502 sprintf(ptrBuf, "%p",
503 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
504 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200505
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200506 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200507 {
508 Py_INCREF(result);
509 return result;
510 }
511 }
512
513 if (our_tv->v_type == VAR_STRING)
514 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200515 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200516 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200517 }
518 else if (our_tv->v_type == VAR_NUMBER)
519 {
520 char buf[NUMBUFLEN];
521
522 /* For backwards compatibility numbers are stored as strings. */
523 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200524 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200525 }
526# ifdef FEAT_FLOAT
527 else if (our_tv->v_type == VAR_FLOAT)
528 {
529 char buf[NUMBUFLEN];
530
531 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200532 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200533 }
534# endif
535 else if (our_tv->v_type == VAR_LIST)
536 {
537 list_T *list = our_tv->vval.v_list;
538 listitem_T *curr;
539
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200540 if (list == NULL)
541 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200542
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200543 if (!(result = PyList_New(0)))
544 return NULL;
545
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200546 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200547 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200548 Py_DECREF(result);
549 return NULL;
550 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200551
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200552 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
553 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200554 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200555 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200556 Py_DECREF(result);
557 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200558 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200559 if (PyList_Append(result, newObj))
560 {
561 Py_DECREF(newObj);
562 Py_DECREF(result);
563 return NULL;
564 }
565 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200566 }
567 }
568 else if (our_tv->v_type == VAR_DICT)
569 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200570
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200571 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
572 long_u todo = ht->ht_used;
573 hashitem_T *hi;
574 dictitem_T *di;
575 if (our_tv->vval.v_dict == NULL)
576 return NULL;
577
578 if (!(result = PyDict_New()))
579 return NULL;
580
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200581 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200582 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200583 Py_DECREF(result);
584 return NULL;
585 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200586
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200587 for (hi = ht->ht_array; todo > 0; ++hi)
588 {
589 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200590 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200591 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200592
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200593 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200594 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200595 {
596 Py_DECREF(result);
597 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200598 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200599 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
600 {
601 Py_DECREF(result);
602 Py_DECREF(newObj);
603 return NULL;
604 }
605 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200606 }
607 }
608 }
609 else
610 {
611 Py_INCREF(Py_None);
612 result = Py_None;
613 }
614
615 return result;
616}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200617
618 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200619VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200620{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200621 char *expr;
622 typval_T *our_tv;
623 PyObject *result;
624 PyObject *lookup_dict;
625
626 if (!PyArg_ParseTuple(args, "s", &expr))
627 return NULL;
628
629 Py_BEGIN_ALLOW_THREADS
630 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200631 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200632 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200633 Python_Release_Vim();
634 Py_END_ALLOW_THREADS
635
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200636 if (VimTryEnd())
637 return NULL;
638
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200639 if (our_tv == NULL)
640 {
641 PyErr_SetVim(_("invalid expression"));
642 return NULL;
643 }
644
645 /* Convert the Vim type into a Python type. Create a dictionary that's
646 * used to check for recursive loops. */
647 lookup_dict = PyDict_New();
648 result = VimToPython(our_tv, 1, lookup_dict);
649 Py_DECREF(lookup_dict);
650
651
652 Py_BEGIN_ALLOW_THREADS
653 Python_Lock_Vim();
654 free_tv(our_tv);
655 Python_Release_Vim();
656 Py_END_ALLOW_THREADS
657
658 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200659}
660
Bram Moolenaardb913952012-06-29 12:54:53 +0200661static PyObject *ConvertToPyObject(typval_T *);
662
663 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200664VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200665{
Bram Moolenaardb913952012-06-29 12:54:53 +0200666 char *expr;
667 typval_T *our_tv;
668 PyObject *result;
669
670 if (!PyArg_ParseTuple(args, "s", &expr))
671 return NULL;
672
673 Py_BEGIN_ALLOW_THREADS
674 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200675 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200676 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200677 Python_Release_Vim();
678 Py_END_ALLOW_THREADS
679
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200680 if (VimTryEnd())
681 return NULL;
682
Bram Moolenaardb913952012-06-29 12:54:53 +0200683 if (our_tv == NULL)
684 {
685 PyErr_SetVim(_("invalid expression"));
686 return NULL;
687 }
688
689 result = ConvertToPyObject(our_tv);
690 Py_BEGIN_ALLOW_THREADS
691 Python_Lock_Vim();
692 free_tv(our_tv);
693 Python_Release_Vim();
694 Py_END_ALLOW_THREADS
695
696 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200697}
698
699 static PyObject *
700VimStrwidth(PyObject *self UNUSED, PyObject *args)
701{
702 char *expr;
703
704 if (!PyArg_ParseTuple(args, "s", &expr))
705 return NULL;
706
Bram Moolenaara54bf402012-12-05 16:30:07 +0100707 return PyLong_FromLong(
708#ifdef FEAT_MBYTE
709 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
710#else
711 STRLEN(expr)
712#endif
713 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200714}
715
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200716/*
717 * Vim module - Definitions
718 */
719
720static struct PyMethodDef VimMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200721 /* name, function, calling, documentation */
722 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
723 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
724 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
725 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
726 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200727};
728
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200729/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200730 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200731 */
732
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200733static PyTypeObject IterType;
734
735typedef PyObject *(*nextfun)(void **);
736typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200737typedef int (*traversefun)(void *, visitproc, void *);
738typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200739
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200740/* Main purpose of this object is removing the need for do python
741 * initialization (i.e. PyType_Ready and setting type attributes) for a big
742 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200743
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200744typedef struct
745{
746 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200747 void *cur;
748 nextfun next;
749 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200750 traversefun traverse;
751 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200752} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200753
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200754 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200755IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
756 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200757{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200758 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200759
Bram Moolenaar774267b2013-05-21 20:51:59 +0200760 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200761 self->cur = start;
762 self->next = next;
763 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200764 self->traverse = traverse;
765 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200766
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200767 return (PyObject *)(self);
768}
769
770 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200771IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200772{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200773 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200774 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200775 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200776}
777
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200778 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200779IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200780{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200781 if (self->traverse != NULL)
782 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200783 else
784 return 0;
785}
786
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200787/* Mac OSX defines clear() somewhere. */
788#ifdef clear
789# undef clear
790#endif
791
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200792 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200793IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200794{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200795 if (self->clear != NULL)
796 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200797 else
798 return 0;
799}
800
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200801 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200802IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200803{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200804 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805}
806
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200807 static PyObject *
808IterIter(PyObject *self)
809{
810 return self;
811}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200812
Bram Moolenaardb913952012-06-29 12:54:53 +0200813typedef struct pylinkedlist_S {
814 struct pylinkedlist_S *pll_next;
815 struct pylinkedlist_S *pll_prev;
816 PyObject *pll_obj;
817} pylinkedlist_T;
818
819static pylinkedlist_T *lastdict = NULL;
820static pylinkedlist_T *lastlist = NULL;
821
822 static void
823pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
824{
825 if (ref->pll_prev == NULL)
826 {
827 if (ref->pll_next == NULL)
828 {
829 *last = NULL;
830 return;
831 }
832 }
833 else
834 ref->pll_prev->pll_next = ref->pll_next;
835
836 if (ref->pll_next == NULL)
837 *last = ref->pll_prev;
838 else
839 ref->pll_next->pll_prev = ref->pll_prev;
840}
841
842 static void
843pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
844{
845 if (*last == NULL)
846 ref->pll_prev = NULL;
847 else
848 {
849 (*last)->pll_next = ref;
850 ref->pll_prev = *last;
851 }
852 ref->pll_next = NULL;
853 ref->pll_obj = self;
854 *last = ref;
855}
856
857static PyTypeObject DictionaryType;
858
859typedef struct
860{
861 PyObject_HEAD
862 dict_T *dict;
863 pylinkedlist_T ref;
864} DictionaryObject;
865
866 static PyObject *
867DictionaryNew(dict_T *dict)
868{
869 DictionaryObject *self;
870
871 self = PyObject_NEW(DictionaryObject, &DictionaryType);
872 if (self == NULL)
873 return NULL;
874 self->dict = dict;
875 ++dict->dv_refcount;
876
877 pyll_add((PyObject *)(self), &self->ref, &lastdict);
878
879 return (PyObject *)(self);
880}
881
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200882 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200883DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200884{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200885 pyll_remove(&self->ref, &lastdict);
886 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200887
888 DESTRUCTOR_FINISH(self);
889}
890
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200891static char *DictionaryAttrs[] = {
892 "locked", "scope",
893 NULL
894};
895
896 static PyObject *
897DictionaryDir(PyObject *self)
898{
899 return ObjectDir(self, DictionaryAttrs);
900}
901
Bram Moolenaardb913952012-06-29 12:54:53 +0200902 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200903DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200904{
905 if (val == NULL)
906 {
907 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
908 return -1;
909 }
910
911 if (strcmp(name, "locked") == 0)
912 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200913 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200914 {
915 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
916 return -1;
917 }
918 else
919 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200920 int istrue = PyObject_IsTrue(val);
921 if (istrue == -1)
922 return -1;
923 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200924 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200925 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200926 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200927 }
928 return 0;
929 }
930 else
931 {
932 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
933 return -1;
934 }
935}
936
937 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200938DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200939{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200940 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +0200941}
942
943 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200944DictionaryItem(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200945{
946 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200947 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200948 DICTKEY_DECL
949
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +0200950 DICTKEY_GET(NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200951
Bram Moolenaard6e39182013-05-21 18:30:34 +0200952 di = dict_find(self->dict, key, -1);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200953
Bram Moolenaar696c2112012-09-21 13:43:14 +0200954 DICTKEY_UNREF
955
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200956 if (di == NULL)
957 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200958 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200959 return NULL;
960 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200961
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200962 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200963}
964
965 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200966DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200967{
968 char_u *key;
969 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200970 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +0200971 dictitem_T *di;
972 DICTKEY_DECL
973
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200974 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +0200975 {
976 PyErr_SetVim(_("dict is locked"));
977 return -1;
978 }
979
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +0200980 DICTKEY_GET(-1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200981
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200982 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +0200983
984 if (valObject == NULL)
985 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200986 hashitem_T *hi;
987
Bram Moolenaardb913952012-06-29 12:54:53 +0200988 if (di == NULL)
989 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200990 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200991 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +0200992 return -1;
993 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200994 hi = hash_find(&dict->dv_hashtab, di->di_key);
995 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +0200996 dictitem_free(di);
997 return 0;
998 }
999
1000 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001001 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001002
1003 if (di == NULL)
1004 {
1005 di = dictitem_alloc(key);
1006 if (di == NULL)
1007 {
1008 PyErr_NoMemory();
1009 return -1;
1010 }
1011 di->di_tv.v_lock = 0;
1012
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001013 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001014 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001015 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +02001016 vim_free(di);
1017 PyErr_SetVim(_("failed to add key to dictionary"));
1018 return -1;
1019 }
1020 }
1021 else
1022 clear_tv(&di->di_tv);
1023
1024 DICTKEY_UNREF
1025
1026 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001027 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001028 return 0;
1029}
1030
1031 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001032DictionaryListKeys(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001033{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001034 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001035 long_u todo = dict->dv_hashtab.ht_used;
1036 Py_ssize_t i = 0;
1037 PyObject *r;
1038 hashitem_T *hi;
1039
1040 r = PyList_New(todo);
1041 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1042 {
1043 if (!HASHITEM_EMPTY(hi))
1044 {
1045 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
1046 --todo;
1047 ++i;
1048 }
1049 }
1050 return r;
1051}
1052
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001053static PyMappingMethods DictionaryAsMapping = {
1054 (lenfunc) DictionaryLength,
1055 (binaryfunc) DictionaryItem,
1056 (objobjargproc) DictionaryAssItem,
1057};
1058
Bram Moolenaardb913952012-06-29 12:54:53 +02001059static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001060 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001061 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1062 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001063};
1064
1065static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001066static PySequenceMethods ListAsSeq;
1067static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001068
1069typedef struct
1070{
1071 PyObject_HEAD
1072 list_T *list;
1073 pylinkedlist_T ref;
1074} ListObject;
1075
1076 static PyObject *
1077ListNew(list_T *list)
1078{
1079 ListObject *self;
1080
1081 self = PyObject_NEW(ListObject, &ListType);
1082 if (self == NULL)
1083 return NULL;
1084 self->list = list;
1085 ++list->lv_refcount;
1086
1087 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1088
1089 return (PyObject *)(self);
1090}
1091
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001092 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001093ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001094{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001095 pyll_remove(&self->ref, &lastlist);
1096 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001097
1098 DESTRUCTOR_FINISH(self);
1099}
1100
Bram Moolenaardb913952012-06-29 12:54:53 +02001101 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001102list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001103{
1104 Py_ssize_t i;
1105 Py_ssize_t lsize = PySequence_Size(obj);
1106 PyObject *litem;
1107 listitem_T *li;
1108
1109 for(i=0; i<lsize; i++)
1110 {
1111 li = listitem_alloc();
1112 if (li == NULL)
1113 {
1114 PyErr_NoMemory();
1115 return -1;
1116 }
1117 li->li_tv.v_lock = 0;
1118
1119 litem = PySequence_GetItem(obj, i);
1120 if (litem == NULL)
1121 return -1;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001122 if (_ConvertFromPyObject(litem, &li->li_tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001123 return -1;
1124
1125 list_append(l, li);
1126 }
1127 return 0;
1128}
1129
Bram Moolenaardb913952012-06-29 12:54:53 +02001130 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001131ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001132{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001133 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001134}
1135
1136 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001137ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001138{
1139 listitem_T *li;
1140
Bram Moolenaard6e39182013-05-21 18:30:34 +02001141 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001142 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001143 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001144 return NULL;
1145 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001146 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001147 if (li == NULL)
1148 {
1149 PyErr_SetVim(_("internal error: failed to get vim list item"));
1150 return NULL;
1151 }
1152 return ConvertToPyObject(&li->li_tv);
1153}
1154
1155#define PROC_RANGE \
1156 if (last < 0) {\
1157 if (last < -size) \
1158 last = 0; \
1159 else \
1160 last += size; \
1161 } \
1162 if (first < 0) \
1163 first = 0; \
1164 if (first > size) \
1165 first = size; \
1166 if (last > size) \
1167 last = size;
1168
1169 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001170ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001171{
1172 PyInt i;
1173 PyInt size = ListLength(self);
1174 PyInt n;
1175 PyObject *list;
1176 int reversed = 0;
1177
1178 PROC_RANGE
1179 if (first >= last)
1180 first = last;
1181
1182 n = last-first;
1183 list = PyList_New(n);
1184 if (list == NULL)
1185 return NULL;
1186
1187 for (i = 0; i < n; ++i)
1188 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001189 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001190 if (item == NULL)
1191 {
1192 Py_DECREF(list);
1193 return NULL;
1194 }
1195
1196 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1197 {
1198 Py_DECREF(item);
1199 Py_DECREF(list);
1200 return NULL;
1201 }
1202 }
1203
1204 return list;
1205}
1206
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001207typedef struct
1208{
1209 listwatch_T lw;
1210 list_T *list;
1211} listiterinfo_T;
1212
1213 static void
1214ListIterDestruct(listiterinfo_T *lii)
1215{
1216 list_rem_watch(lii->list, &lii->lw);
1217 PyMem_Free(lii);
1218}
1219
1220 static PyObject *
1221ListIterNext(listiterinfo_T **lii)
1222{
1223 PyObject *r;
1224
1225 if (!((*lii)->lw.lw_item))
1226 return NULL;
1227
1228 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1229 return NULL;
1230
1231 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1232
1233 return r;
1234}
1235
1236 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001237ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001238{
1239 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001240 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001241
1242 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1243 {
1244 PyErr_NoMemory();
1245 return NULL;
1246 }
1247
1248 list_add_watch(l, &lii->lw);
1249 lii->lw.lw_item = l->lv_first;
1250 lii->list = l;
1251
1252 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001253 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1254 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001255}
1256
Bram Moolenaardb913952012-06-29 12:54:53 +02001257 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001258ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001259{
1260 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001261 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001262 listitem_T *li;
1263 Py_ssize_t length = ListLength(self);
1264
1265 if (l->lv_lock)
1266 {
1267 PyErr_SetVim(_("list is locked"));
1268 return -1;
1269 }
1270 if (index>length || (index==length && obj==NULL))
1271 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001272 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001273 return -1;
1274 }
1275
1276 if (obj == NULL)
1277 {
1278 li = list_find(l, (long) index);
1279 list_remove(l, li, li);
1280 clear_tv(&li->li_tv);
1281 vim_free(li);
1282 return 0;
1283 }
1284
1285 if (ConvertFromPyObject(obj, &tv) == -1)
1286 return -1;
1287
1288 if (index == length)
1289 {
1290 if (list_append_tv(l, &tv) == FAIL)
1291 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001292 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001293 PyErr_SetVim(_("Failed to add item to list"));
1294 return -1;
1295 }
1296 }
1297 else
1298 {
1299 li = list_find(l, (long) index);
1300 clear_tv(&li->li_tv);
1301 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001302 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001303 }
1304 return 0;
1305}
1306
1307 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001308ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001309{
1310 PyInt size = ListLength(self);
1311 Py_ssize_t i;
1312 Py_ssize_t lsize;
1313 PyObject *litem;
1314 listitem_T *li;
1315 listitem_T *next;
1316 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001317 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001318
1319 if (l->lv_lock)
1320 {
1321 PyErr_SetVim(_("list is locked"));
1322 return -1;
1323 }
1324
1325 PROC_RANGE
1326
1327 if (first == size)
1328 li = NULL;
1329 else
1330 {
1331 li = list_find(l, (long) first);
1332 if (li == NULL)
1333 {
1334 PyErr_SetVim(_("internal error: no vim list item"));
1335 return -1;
1336 }
1337 if (last > first)
1338 {
1339 i = last - first;
1340 while (i-- && li != NULL)
1341 {
1342 next = li->li_next;
1343 listitem_remove(l, li);
1344 li = next;
1345 }
1346 }
1347 }
1348
1349 if (obj == NULL)
1350 return 0;
1351
1352 if (!PyList_Check(obj))
1353 {
1354 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1355 return -1;
1356 }
1357
1358 lsize = PyList_Size(obj);
1359
1360 for(i=0; i<lsize; i++)
1361 {
1362 litem = PyList_GetItem(obj, i);
1363 if (litem == NULL)
1364 return -1;
1365 if (ConvertFromPyObject(litem, &v) == -1)
1366 return -1;
1367 if (list_insert_tv(l, &v, li) == FAIL)
1368 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001369 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001370 PyErr_SetVim(_("internal error: failed to add item to list"));
1371 return -1;
1372 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001373 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001374 }
1375 return 0;
1376}
1377
1378 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001379ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001380{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001381 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001382 PyObject *lookup_dict;
1383
1384 if (l->lv_lock)
1385 {
1386 PyErr_SetVim(_("list is locked"));
1387 return NULL;
1388 }
1389
1390 if (!PySequence_Check(obj))
1391 {
1392 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1393 return NULL;
1394 }
1395
1396 lookup_dict = PyDict_New();
1397 if (list_py_concat(l, obj, lookup_dict) == -1)
1398 {
1399 Py_DECREF(lookup_dict);
1400 return NULL;
1401 }
1402 Py_DECREF(lookup_dict);
1403
1404 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001405 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001406}
1407
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001408static char *ListAttrs[] = {
1409 "locked",
1410 NULL
1411};
1412
1413 static PyObject *
1414ListDir(PyObject *self)
1415{
1416 return ObjectDir(self, ListAttrs);
1417}
1418
Bram Moolenaar66b79852012-09-21 14:00:35 +02001419 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001420ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001421{
1422 if (val == NULL)
1423 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001424 PyErr_SetString(PyExc_AttributeError,
1425 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001426 return -1;
1427 }
1428
1429 if (strcmp(name, "locked") == 0)
1430 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001431 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001432 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001433 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001434 return -1;
1435 }
1436 else
1437 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001438 int istrue = PyObject_IsTrue(val);
1439 if (istrue == -1)
1440 return -1;
1441 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001442 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001443 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001444 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001445 }
1446 return 0;
1447 }
1448 else
1449 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001450 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001451 return -1;
1452 }
1453}
1454
Bram Moolenaardb913952012-06-29 12:54:53 +02001455static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001456 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1457 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
1458 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001459};
1460
1461typedef struct
1462{
1463 PyObject_HEAD
1464 char_u *name;
1465} FunctionObject;
1466
1467static PyTypeObject FunctionType;
1468
1469 static PyObject *
1470FunctionNew(char_u *name)
1471{
1472 FunctionObject *self;
1473
1474 self = PyObject_NEW(FunctionObject, &FunctionType);
1475 if (self == NULL)
1476 return NULL;
1477 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1478 if (self->name == NULL)
1479 {
1480 PyErr_NoMemory();
1481 return NULL;
1482 }
1483 STRCPY(self->name, name);
1484 func_ref(name);
1485 return (PyObject *)(self);
1486}
1487
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001488 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001489FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001490{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001491 func_unref(self->name);
1492 PyMem_Free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001493
1494 DESTRUCTOR_FINISH(self);
1495}
1496
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001497static char *FunctionAttrs[] = {
1498 "softspace",
1499 NULL
1500};
1501
1502 static PyObject *
1503FunctionDir(PyObject *self)
1504{
1505 return ObjectDir(self, FunctionAttrs);
1506}
1507
Bram Moolenaardb913952012-06-29 12:54:53 +02001508 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001509FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02001510{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001511 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02001512 typval_T args;
1513 typval_T selfdicttv;
1514 typval_T rettv;
1515 dict_T *selfdict = NULL;
1516 PyObject *selfdictObject;
1517 PyObject *result;
1518 int error;
1519
1520 if (ConvertFromPyObject(argsObject, &args) == -1)
1521 return NULL;
1522
1523 if (kwargs != NULL)
1524 {
1525 selfdictObject = PyDict_GetItemString(kwargs, "self");
1526 if (selfdictObject != NULL)
1527 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001528 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001529 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001530 PyErr_SetString(PyExc_TypeError,
1531 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001532 clear_tv(&args);
1533 return NULL;
1534 }
1535 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001536 {
1537 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02001538 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001539 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001540 selfdict = selfdicttv.vval.v_dict;
1541 }
1542 }
1543
Bram Moolenaar71700b82013-05-15 17:49:05 +02001544 Py_BEGIN_ALLOW_THREADS
1545 Python_Lock_Vim();
1546
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001547 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02001548 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001549
1550 Python_Release_Vim();
1551 Py_END_ALLOW_THREADS
1552
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001553 if (VimTryEnd())
1554 result = NULL;
1555 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02001556 {
1557 result = NULL;
1558 PyErr_SetVim(_("failed to run function"));
1559 }
1560 else
1561 result = ConvertToPyObject(&rettv);
1562
Bram Moolenaardb913952012-06-29 12:54:53 +02001563 clear_tv(&args);
1564 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001565 if (selfdict != NULL)
1566 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001567
1568 return result;
1569}
1570
1571static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001572 {"__call__",(PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1573 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
1574 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001575};
1576
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001577/*
1578 * Options object
1579 */
1580
1581static PyTypeObject OptionsType;
1582
1583typedef int (*checkfun)(void *);
1584
1585typedef struct
1586{
1587 PyObject_HEAD
1588 int opt_type;
1589 void *from;
1590 checkfun Check;
1591 PyObject *fromObj;
1592} OptionsObject;
1593
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001594 static int
1595dummy_check(void *arg UNUSED)
1596{
1597 return 0;
1598}
1599
1600 static PyObject *
1601OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1602{
1603 OptionsObject *self;
1604
Bram Moolenaar774267b2013-05-21 20:51:59 +02001605 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001606 if (self == NULL)
1607 return NULL;
1608
1609 self->opt_type = opt_type;
1610 self->from = from;
1611 self->Check = Check;
1612 self->fromObj = fromObj;
1613 if (fromObj)
1614 Py_INCREF(fromObj);
1615
1616 return (PyObject *)(self);
1617}
1618
1619 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001620OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001621{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001622 PyObject_GC_UnTrack((void *)(self));
1623 Py_XDECREF(self->fromObj);
1624 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001625}
1626
1627 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001628OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001629{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001630 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001631 return 0;
1632}
1633
1634 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001635OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001636{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001637 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001638 return 0;
1639}
1640
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001641 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001642OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001643{
1644 char_u *key;
1645 int flags;
1646 long numval;
1647 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001648 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001649
Bram Moolenaard6e39182013-05-21 18:30:34 +02001650 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001651 return NULL;
1652
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +02001653 DICTKEY_GET(NULL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001654
1655 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001656 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001657
1658 DICTKEY_UNREF
1659
1660 if (flags == 0)
1661 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001662 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001663 return NULL;
1664 }
1665
1666 if (flags & SOPT_UNSET)
1667 {
1668 Py_INCREF(Py_None);
1669 return Py_None;
1670 }
1671 else if (flags & SOPT_BOOL)
1672 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001673 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001674 r = numval ? Py_True : Py_False;
1675 Py_INCREF(r);
1676 return r;
1677 }
1678 else if (flags & SOPT_NUM)
1679 return PyInt_FromLong(numval);
1680 else if (flags & SOPT_STRING)
1681 {
1682 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001683 {
1684 PyObject *r = PyBytes_FromString((char *) stringval);
1685 vim_free(stringval);
1686 return r;
1687 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001688 else
1689 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001690 PyErr_SetString(PyExc_RuntimeError,
1691 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001692 return NULL;
1693 }
1694 }
1695 else
1696 {
1697 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1698 return NULL;
1699 }
1700}
1701
1702 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001703set_option_value_err(key, numval, stringval, opt_flags)
1704 char_u *key;
1705 int numval;
1706 char_u *stringval;
1707 int opt_flags;
1708{
1709 char_u *errmsg;
1710
1711 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
1712 {
1713 if (VimTryEnd())
1714 return FAIL;
1715 PyErr_SetVim((char *)errmsg);
1716 return FAIL;
1717 }
1718 return OK;
1719}
1720
1721 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001722set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1723 char_u *key;
1724 int numval;
1725 char_u *stringval;
1726 int opt_flags;
1727 int opt_type;
1728 void *from;
1729{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001730 win_T *save_curwin = NULL;
1731 tabpage_T *save_curtab = NULL;
1732 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001733 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001734
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001735 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001736 switch (opt_type)
1737 {
1738 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001739 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1740 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001741 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001742 if (VimTryEnd())
1743 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001744 PyErr_SetVim("Problem while switching windows.");
1745 return -1;
1746 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001747 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001748 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001749 if (r == FAIL)
1750 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001751 break;
1752 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001753 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001754 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001755 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001756 if (r == FAIL)
1757 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001758 break;
1759 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001760 r = set_option_value_err(key, numval, stringval, opt_flags);
1761 if (r == FAIL)
1762 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001763 break;
1764 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001765 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001766}
1767
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001768 static void *
1769py_memsave(void *p, size_t len)
1770{
1771 void *r;
1772 if (!(r = PyMem_Malloc(len)))
1773 return NULL;
1774 mch_memmove(r, p, len);
1775 return r;
1776}
1777
1778#define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1))
1779
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001780 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001781OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001782{
1783 char_u *key;
1784 int flags;
1785 int opt_flags;
1786 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001787 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001788
Bram Moolenaard6e39182013-05-21 18:30:34 +02001789 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001790 return -1;
1791
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +02001792 DICTKEY_GET(-1)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001793
1794 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001795 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001796
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001797 if (flags == 0)
1798 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001799 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001800 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001801 return -1;
1802 }
1803
1804 if (valObject == NULL)
1805 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001806 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001807 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001808 PyErr_SetString(PyExc_ValueError,
1809 _("unable to unset global option"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001810 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001811 return -1;
1812 }
1813 else if (!(flags & SOPT_GLOBAL))
1814 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001815 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1816 "without global value"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001817 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001818 return -1;
1819 }
1820 else
1821 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001822 unset_global_local_option(key, self->from);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001823 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001824 return 0;
1825 }
1826 }
1827
Bram Moolenaard6e39182013-05-21 18:30:34 +02001828 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001829
1830 if (flags & SOPT_BOOL)
1831 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001832 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001833
Bram Moolenaarb983f752013-05-15 16:11:50 +02001834 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001835 r = -1;
1836 else
1837 r = set_option_value_for(key, istrue, NULL,
1838 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001839 }
1840 else if (flags & SOPT_NUM)
1841 {
1842 int val;
1843
1844#if PY_MAJOR_VERSION < 3
1845 if (PyInt_Check(valObject))
1846 val = PyInt_AsLong(valObject);
1847 else
1848#endif
1849 if (PyLong_Check(valObject))
1850 val = PyLong_AsLong(valObject);
1851 else
1852 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001853 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001854 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001855 return -1;
1856 }
1857
1858 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001859 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001860 }
1861 else
1862 {
1863 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001864 PyObject *todecref;
1865
1866 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001867 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001868 r = set_option_value_for(key, 0, val, opt_flags,
1869 self->opt_type, self->from);
1870 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001871 }
1872 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001873 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001874 }
1875
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001876 DICTKEY_UNREF
1877
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001878 return r;
1879}
1880
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001881static PyMappingMethods OptionsAsMapping = {
1882 (lenfunc) NULL,
1883 (binaryfunc) OptionsItem,
1884 (objobjargproc) OptionsAssItem,
1885};
1886
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001887/* Tabpage object
1888 */
1889
1890typedef struct
1891{
1892 PyObject_HEAD
1893 tabpage_T *tab;
1894} TabPageObject;
1895
1896static PyObject *WinListNew(TabPageObject *tabObject);
1897
1898static PyTypeObject TabPageType;
1899
1900 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001901CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001902{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001903 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001904 {
1905 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1906 return -1;
1907 }
1908
1909 return 0;
1910}
1911
1912 static PyObject *
1913TabPageNew(tabpage_T *tab)
1914{
1915 TabPageObject *self;
1916
1917 if (TAB_PYTHON_REF(tab))
1918 {
1919 self = TAB_PYTHON_REF(tab);
1920 Py_INCREF(self);
1921 }
1922 else
1923 {
1924 self = PyObject_NEW(TabPageObject, &TabPageType);
1925 if (self == NULL)
1926 return NULL;
1927 self->tab = tab;
1928 TAB_PYTHON_REF(tab) = self;
1929 }
1930
1931 return (PyObject *)(self);
1932}
1933
1934 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001935TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001936{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001937 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
1938 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001939
1940 DESTRUCTOR_FINISH(self);
1941}
1942
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001943static char *TabPageAttrs[] = {
1944 "windows", "number", "vars", "window", "valid",
1945 NULL
1946};
1947
1948 static PyObject *
1949TabPageDir(PyObject *self)
1950{
1951 return ObjectDir(self, TabPageAttrs);
1952}
1953
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001954 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001955TabPageAttrValid(TabPageObject *self, char *name)
1956{
1957 PyObject *r;
1958
1959 if (strcmp(name, "valid") != 0)
1960 return NULL;
1961
1962 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
1963 Py_INCREF(r);
1964 return r;
1965}
1966
1967 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001968TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001969{
1970 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001971 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001972 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001973 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001974 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001975 return DictionaryNew(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001976 else if (strcmp(name, "window") == 0)
1977 {
1978 /* For current tab window.c does not bother to set or update tp_curwin
1979 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02001980 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001981 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001982 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001983 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001984 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001985 else if (strcmp(name, "__members__") == 0)
1986 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001987 return NULL;
1988}
1989
1990 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001991TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001992{
1993 static char repr[100];
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001994
Bram Moolenaard6e39182013-05-21 18:30:34 +02001995 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001996 {
1997 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
1998 return PyString_FromString(repr);
1999 }
2000 else
2001 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002002 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002003
2004 if (t == 0)
2005 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
2006 (self));
2007 else
2008 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
2009
2010 return PyString_FromString(repr);
2011 }
2012}
2013
2014static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002015 /* name, function, calling, documentation */
2016 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2017 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002018};
2019
2020/*
2021 * Window list object
2022 */
2023
2024static PyTypeObject TabListType;
2025static PySequenceMethods TabListAsSeq;
2026
2027typedef struct
2028{
2029 PyObject_HEAD
2030} TabListObject;
2031
2032 static PyInt
2033TabListLength(PyObject *self UNUSED)
2034{
2035 tabpage_T *tp = first_tabpage;
2036 PyInt n = 0;
2037
2038 while (tp != NULL)
2039 {
2040 ++n;
2041 tp = tp->tp_next;
2042 }
2043
2044 return n;
2045}
2046
2047 static PyObject *
2048TabListItem(PyObject *self UNUSED, PyInt n)
2049{
2050 tabpage_T *tp;
2051
2052 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2053 if (n == 0)
2054 return TabPageNew(tp);
2055
2056 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2057 return NULL;
2058}
2059
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002060/* Window object
2061 */
2062
2063typedef struct
2064{
2065 PyObject_HEAD
2066 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002067 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002068} WindowObject;
2069
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002070static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002071
2072 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002073CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002074{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002075 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002076 {
2077 PyErr_SetVim(_("attempt to refer to deleted window"));
2078 return -1;
2079 }
2080
2081 return 0;
2082}
2083
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002084 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002085WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002086{
2087 /* We need to handle deletion of windows underneath us.
2088 * If we add a "w_python*_ref" field to the win_T structure,
2089 * then we can get at it in win_free() in vim. We then
2090 * need to create only ONE Python object per window - if
2091 * we try to create a second, just INCREF the existing one
2092 * and return it. The (single) Python object referring to
2093 * the window is stored in "w_python*_ref".
2094 * On a win_free() we set the Python object's win_T* field
2095 * to an invalid value. We trap all uses of a window
2096 * object, and reject them if the win_T* field is invalid.
2097 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002098 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002099 * w_python_ref and w_python3_ref fields respectively.
2100 */
2101
2102 WindowObject *self;
2103
2104 if (WIN_PYTHON_REF(win))
2105 {
2106 self = WIN_PYTHON_REF(win);
2107 Py_INCREF(self);
2108 }
2109 else
2110 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002111 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002112 if (self == NULL)
2113 return NULL;
2114 self->win = win;
2115 WIN_PYTHON_REF(win) = self;
2116 }
2117
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002118 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2119
Bram Moolenaar971db462013-05-12 18:44:48 +02002120 return (PyObject *)(self);
2121}
2122
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002123 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002124WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002125{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002126 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002127 if (self->win && self->win != INVALID_WINDOW_VALUE)
2128 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002129 Py_XDECREF(((PyObject *)(self->tabObject)));
2130 PyObject_GC_Del((void *)(self));
2131}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002132
Bram Moolenaar774267b2013-05-21 20:51:59 +02002133 static int
2134WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2135{
2136 Py_VISIT(((PyObject *)(self->tabObject)));
2137 return 0;
2138}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002139
Bram Moolenaar774267b2013-05-21 20:51:59 +02002140 static int
2141WindowClear(WindowObject *self)
2142{
2143 Py_CLEAR(self->tabObject);
2144 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002145}
2146
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002147 static win_T *
2148get_firstwin(TabPageObject *tabObject)
2149{
2150 if (tabObject)
2151 {
2152 if (CheckTabPage(tabObject))
2153 return NULL;
2154 /* For current tab window.c does not bother to set or update tp_firstwin
2155 */
2156 else if (tabObject->tab == curtab)
2157 return firstwin;
2158 else
2159 return tabObject->tab->tp_firstwin;
2160 }
2161 else
2162 return firstwin;
2163}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002164static char *WindowAttrs[] = {
2165 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2166 "tabpage", "valid",
2167 NULL
2168};
2169
2170 static PyObject *
2171WindowDir(PyObject *self)
2172{
2173 return ObjectDir(self, WindowAttrs);
2174}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002175
Bram Moolenaar971db462013-05-12 18:44:48 +02002176 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002177WindowAttrValid(WindowObject *self, char *name)
2178{
2179 PyObject *r;
2180
2181 if (strcmp(name, "valid") != 0)
2182 return NULL;
2183
2184 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2185 Py_INCREF(r);
2186 return r;
2187}
2188
2189 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002190WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002191{
2192 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002193 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002194 else if (strcmp(name, "cursor") == 0)
2195 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002196 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002197
2198 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2199 }
2200 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002201 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002202#ifdef FEAT_WINDOWS
2203 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002204 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002205#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002206#ifdef FEAT_VERTSPLIT
2207 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002208 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002209 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002210 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002211#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002212 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002213 return DictionaryNew(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002214 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002215 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2216 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002217 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002218 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002219 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002220 return NULL;
2221 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002222 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002223 }
2224 else if (strcmp(name, "tabpage") == 0)
2225 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002226 Py_INCREF(self->tabObject);
2227 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002228 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002229 else if (strcmp(name, "__members__") == 0)
2230 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002231 else
2232 return NULL;
2233}
2234
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002235 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002236WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002237{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002238 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002239 return -1;
2240
2241 if (strcmp(name, "buffer") == 0)
2242 {
2243 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2244 return -1;
2245 }
2246 else if (strcmp(name, "cursor") == 0)
2247 {
2248 long lnum;
2249 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002250
2251 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2252 return -1;
2253
Bram Moolenaard6e39182013-05-21 18:30:34 +02002254 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002255 {
2256 PyErr_SetVim(_("cursor position outside buffer"));
2257 return -1;
2258 }
2259
2260 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002261 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002262 return -1;
2263
Bram Moolenaard6e39182013-05-21 18:30:34 +02002264 self->win->w_cursor.lnum = lnum;
2265 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002266#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002267 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002268#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002269 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002270 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002271
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002272 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002273 return 0;
2274 }
2275 else if (strcmp(name, "height") == 0)
2276 {
2277 int height;
2278 win_T *savewin;
2279
2280 if (!PyArg_Parse(val, "i", &height))
2281 return -1;
2282
2283#ifdef FEAT_GUI
2284 need_mouse_correct = TRUE;
2285#endif
2286 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002287 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002288
2289 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002290 win_setheight(height);
2291 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002292 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002293 return -1;
2294
2295 return 0;
2296 }
2297#ifdef FEAT_VERTSPLIT
2298 else if (strcmp(name, "width") == 0)
2299 {
2300 int width;
2301 win_T *savewin;
2302
2303 if (!PyArg_Parse(val, "i", &width))
2304 return -1;
2305
2306#ifdef FEAT_GUI
2307 need_mouse_correct = TRUE;
2308#endif
2309 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002310 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002311
2312 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002313 win_setwidth(width);
2314 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002315 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002316 return -1;
2317
2318 return 0;
2319 }
2320#endif
2321 else
2322 {
2323 PyErr_SetString(PyExc_AttributeError, name);
2324 return -1;
2325 }
2326}
2327
2328 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002329WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002330{
2331 static char repr[100];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002332
Bram Moolenaard6e39182013-05-21 18:30:34 +02002333 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002334 {
2335 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2336 return PyString_FromString(repr);
2337 }
2338 else
2339 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002340 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002341
Bram Moolenaar6d216452013-05-12 19:00:41 +02002342 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002343 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2344 (self));
2345 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002346 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002347
2348 return PyString_FromString(repr);
2349 }
2350}
2351
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002352static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002353 /* name, function, calling, documentation */
2354 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2355 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002356};
2357
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002358/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002359 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002360 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002361
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002362static PyTypeObject WinListType;
2363static PySequenceMethods WinListAsSeq;
2364
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002365typedef struct
2366{
2367 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002368 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002369} WinListObject;
2370
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002371 static PyObject *
2372WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002373{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002374 WinListObject *self;
2375
2376 self = PyObject_NEW(WinListObject, &WinListType);
2377 self->tabObject = tabObject;
2378 Py_INCREF(tabObject);
2379
2380 return (PyObject *)(self);
2381}
2382
2383 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002384WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002385{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002386 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002387
2388 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02002389 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002390 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02002391 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002392
2393 DESTRUCTOR_FINISH(self);
2394}
2395
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002396 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002397WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002398{
2399 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002400 PyInt n = 0;
2401
Bram Moolenaard6e39182013-05-21 18:30:34 +02002402 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002403 return -1;
2404
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002405 while (w != NULL)
2406 {
2407 ++n;
2408 w = W_NEXT(w);
2409 }
2410
2411 return n;
2412}
2413
2414 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002415WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002416{
2417 win_T *w;
2418
Bram Moolenaard6e39182013-05-21 18:30:34 +02002419 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002420 return NULL;
2421
2422 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002423 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002424 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002425
2426 PyErr_SetString(PyExc_IndexError, _("no such window"));
2427 return NULL;
2428}
2429
2430/* Convert a Python string into a Vim line.
2431 *
2432 * The result is in allocated memory. All internal nulls are replaced by
2433 * newline characters. It is an error for the string to contain newline
2434 * characters.
2435 *
2436 * On errors, the Python exception data is set, and NULL is returned.
2437 */
2438 static char *
2439StringToLine(PyObject *obj)
2440{
2441 const char *str;
2442 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002443 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002444 PyInt len;
2445 PyInt i;
2446 char *p;
2447
2448 if (obj == NULL || !PyString_Check(obj))
2449 {
2450 PyErr_BadArgument();
2451 return NULL;
2452 }
2453
Bram Moolenaar19e60942011-06-19 00:27:51 +02002454 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2455 str = PyString_AsString(bytes);
2456 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002457
2458 /*
2459 * Error checking: String must not contain newlines, as we
2460 * are replacing a single line, and we must replace it with
2461 * a single line.
2462 * A trailing newline is removed, so that append(f.readlines()) works.
2463 */
2464 p = memchr(str, '\n', len);
2465 if (p != NULL)
2466 {
2467 if (p == str + len - 1)
2468 --len;
2469 else
2470 {
2471 PyErr_SetVim(_("string cannot contain newlines"));
2472 return NULL;
2473 }
2474 }
2475
2476 /* Create a copy of the string, with internal nulls replaced by
2477 * newline characters, as is the vim convention.
2478 */
2479 save = (char *)alloc((unsigned)(len+1));
2480 if (save == NULL)
2481 {
2482 PyErr_NoMemory();
2483 return NULL;
2484 }
2485
2486 for (i = 0; i < len; ++i)
2487 {
2488 if (str[i] == '\0')
2489 save[i] = '\n';
2490 else
2491 save[i] = str[i];
2492 }
2493
2494 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002495 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002496
2497 return save;
2498}
2499
2500/* Get a line from the specified buffer. The line number is
2501 * in Vim format (1-based). The line is returned as a Python
2502 * string object.
2503 */
2504 static PyObject *
2505GetBufferLine(buf_T *buf, PyInt n)
2506{
2507 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2508}
2509
2510
2511/* Get a list of lines from the specified buffer. The line numbers
2512 * are in Vim format (1-based). The range is from lo up to, but not
2513 * including, hi. The list is returned as a Python list of string objects.
2514 */
2515 static PyObject *
2516GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2517{
2518 PyInt i;
2519 PyInt n = hi - lo;
2520 PyObject *list = PyList_New(n);
2521
2522 if (list == NULL)
2523 return NULL;
2524
2525 for (i = 0; i < n; ++i)
2526 {
2527 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2528
2529 /* Error check - was the Python string creation OK? */
2530 if (str == NULL)
2531 {
2532 Py_DECREF(list);
2533 return NULL;
2534 }
2535
2536 /* Set the list item */
2537 if (PyList_SetItem(list, i, str))
2538 {
2539 Py_DECREF(str);
2540 Py_DECREF(list);
2541 return NULL;
2542 }
2543 }
2544
2545 /* The ownership of the Python list is passed to the caller (ie,
2546 * the caller should Py_DECREF() the object when it is finished
2547 * with it).
2548 */
2549
2550 return list;
2551}
2552
2553/*
2554 * Check if deleting lines made the cursor position invalid.
2555 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2556 * deleted).
2557 */
2558 static void
2559py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2560{
2561 if (curwin->w_cursor.lnum >= lo)
2562 {
2563 /* Adjust the cursor position if it's in/after the changed
2564 * lines. */
2565 if (curwin->w_cursor.lnum >= hi)
2566 {
2567 curwin->w_cursor.lnum += extra;
2568 check_cursor_col();
2569 }
2570 else if (extra < 0)
2571 {
2572 curwin->w_cursor.lnum = lo;
2573 check_cursor();
2574 }
2575 else
2576 check_cursor_col();
2577 changed_cline_bef_curs();
2578 }
2579 invalidate_botline();
2580}
2581
Bram Moolenaar19e60942011-06-19 00:27:51 +02002582/*
2583 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002584 * in Vim format (1-based). The replacement line is given as
2585 * a Python string object. The object is checked for validity
2586 * and correct format. Errors are returned as a value of FAIL.
2587 * The return value is OK on success.
2588 * If OK is returned and len_change is not NULL, *len_change
2589 * is set to the change in the buffer length.
2590 */
2591 static int
2592SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2593{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002594 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002595 * There are three cases:
2596 * 1. NULL, or None - this is a deletion.
2597 * 2. A string - this is a replacement.
2598 * 3. Anything else - this is an error.
2599 */
2600 if (line == Py_None || line == NULL)
2601 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002602 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002603
2604 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002605 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002606
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002607 VimTryStart();
2608
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002609 if (u_savedel((linenr_T)n, 1L) == FAIL)
2610 PyErr_SetVim(_("cannot save undo information"));
2611 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2612 PyErr_SetVim(_("cannot delete line"));
2613 else
2614 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002615 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002616 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2617 deleted_lines_mark((linenr_T)n, 1L);
2618 }
2619
Bram Moolenaar105bc352013-05-17 16:03:57 +02002620 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002621
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002622 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002623 return FAIL;
2624
2625 if (len_change)
2626 *len_change = -1;
2627
2628 return OK;
2629 }
2630 else if (PyString_Check(line))
2631 {
2632 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002633 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002634
2635 if (save == NULL)
2636 return FAIL;
2637
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002638 VimTryStart();
2639
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002640 /* We do not need to free "save" if ml_replace() consumes it. */
2641 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002642 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002643
2644 if (u_savesub((linenr_T)n) == FAIL)
2645 {
2646 PyErr_SetVim(_("cannot save undo information"));
2647 vim_free(save);
2648 }
2649 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2650 {
2651 PyErr_SetVim(_("cannot replace line"));
2652 vim_free(save);
2653 }
2654 else
2655 changed_bytes((linenr_T)n, 0);
2656
Bram Moolenaar105bc352013-05-17 16:03:57 +02002657 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002658
2659 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002660 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002661 check_cursor_col();
2662
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002663 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002664 return FAIL;
2665
2666 if (len_change)
2667 *len_change = 0;
2668
2669 return OK;
2670 }
2671 else
2672 {
2673 PyErr_BadArgument();
2674 return FAIL;
2675 }
2676}
2677
Bram Moolenaar19e60942011-06-19 00:27:51 +02002678/* Replace a range of lines in the specified buffer. The line numbers are in
2679 * Vim format (1-based). The range is from lo up to, but not including, hi.
2680 * The replacement lines are given as a Python list of string objects. The
2681 * list is checked for validity and correct format. Errors are returned as a
2682 * value of FAIL. The return value is OK on success.
2683 * If OK is returned and len_change is not NULL, *len_change
2684 * is set to the change in the buffer length.
2685 */
2686 static int
2687SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2688{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002689 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002690 * There are three cases:
2691 * 1. NULL, or None - this is a deletion.
2692 * 2. A list - this is a replacement.
2693 * 3. Anything else - this is an error.
2694 */
2695 if (list == Py_None || list == NULL)
2696 {
2697 PyInt i;
2698 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002699 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002700
2701 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002702 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002703 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002704
2705 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2706 PyErr_SetVim(_("cannot save undo information"));
2707 else
2708 {
2709 for (i = 0; i < n; ++i)
2710 {
2711 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2712 {
2713 PyErr_SetVim(_("cannot delete line"));
2714 break;
2715 }
2716 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002717 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002718 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2719 deleted_lines_mark((linenr_T)lo, (long)i);
2720 }
2721
Bram Moolenaar105bc352013-05-17 16:03:57 +02002722 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002723
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002724 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002725 return FAIL;
2726
2727 if (len_change)
2728 *len_change = -n;
2729
2730 return OK;
2731 }
2732 else if (PyList_Check(list))
2733 {
2734 PyInt i;
2735 PyInt new_len = PyList_Size(list);
2736 PyInt old_len = hi - lo;
2737 PyInt extra = 0; /* lines added to text, can be negative */
2738 char **array;
2739 buf_T *savebuf;
2740
2741 if (new_len == 0) /* avoid allocating zero bytes */
2742 array = NULL;
2743 else
2744 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002745 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002746 if (array == NULL)
2747 {
2748 PyErr_NoMemory();
2749 return FAIL;
2750 }
2751 }
2752
2753 for (i = 0; i < new_len; ++i)
2754 {
2755 PyObject *line = PyList_GetItem(list, i);
2756
2757 array[i] = StringToLine(line);
2758 if (array[i] == NULL)
2759 {
2760 while (i)
2761 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002762 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002763 return FAIL;
2764 }
2765 }
2766
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002767 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02002768 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002769
2770 // START of region without "return". Must call restore_buffer()!
2771 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002772
2773 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2774 PyErr_SetVim(_("cannot save undo information"));
2775
2776 /* If the size of the range is reducing (ie, new_len < old_len) we
2777 * need to delete some old_len. We do this at the start, by
2778 * repeatedly deleting line "lo".
2779 */
2780 if (!PyErr_Occurred())
2781 {
2782 for (i = 0; i < old_len - new_len; ++i)
2783 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2784 {
2785 PyErr_SetVim(_("cannot delete line"));
2786 break;
2787 }
2788 extra -= i;
2789 }
2790
2791 /* For as long as possible, replace the existing old_len with the
2792 * new old_len. This is a more efficient operation, as it requires
2793 * less memory allocation and freeing.
2794 */
2795 if (!PyErr_Occurred())
2796 {
2797 for (i = 0; i < old_len && i < new_len; ++i)
2798 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2799 == FAIL)
2800 {
2801 PyErr_SetVim(_("cannot replace line"));
2802 break;
2803 }
2804 }
2805 else
2806 i = 0;
2807
2808 /* Now we may need to insert the remaining new old_len. If we do, we
2809 * must free the strings as we finish with them (we can't pass the
2810 * responsibility to vim in this case).
2811 */
2812 if (!PyErr_Occurred())
2813 {
2814 while (i < new_len)
2815 {
2816 if (ml_append((linenr_T)(lo + i - 1),
2817 (char_u *)array[i], 0, FALSE) == FAIL)
2818 {
2819 PyErr_SetVim(_("cannot insert line"));
2820 break;
2821 }
2822 vim_free(array[i]);
2823 ++i;
2824 ++extra;
2825 }
2826 }
2827
2828 /* Free any left-over old_len, as a result of an error */
2829 while (i < new_len)
2830 {
2831 vim_free(array[i]);
2832 ++i;
2833 }
2834
2835 /* Free the array of old_len. All of its contents have now
2836 * been dealt with (either freed, or the responsibility passed
2837 * to vim.
2838 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002839 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002840
2841 /* Adjust marks. Invalidate any which lie in the
2842 * changed range, and move any in the remainder of the buffer.
2843 */
2844 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2845 (long)MAXLNUM, (long)extra);
2846 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2847
Bram Moolenaar105bc352013-05-17 16:03:57 +02002848 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002849 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2850
Bram Moolenaar105bc352013-05-17 16:03:57 +02002851 // END of region without "return".
2852 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002853
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002854 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002855 return FAIL;
2856
2857 if (len_change)
2858 *len_change = new_len - old_len;
2859
2860 return OK;
2861 }
2862 else
2863 {
2864 PyErr_BadArgument();
2865 return FAIL;
2866 }
2867}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002868
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002869/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002870 * The line number is in Vim format (1-based). The lines to be inserted are
2871 * given as a Python list of string objects or as a single string. The lines
2872 * to be added are checked for validity and correct format. Errors are
2873 * returned as a value of FAIL. The return value is OK on success.
2874 * If OK is returned and len_change is not NULL, *len_change
2875 * is set to the change in the buffer length.
2876 */
2877 static int
2878InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2879{
2880 /* First of all, we check the type of the supplied Python object.
2881 * It must be a string or a list, or the call is in error.
2882 */
2883 if (PyString_Check(lines))
2884 {
2885 char *str = StringToLine(lines);
2886 buf_T *savebuf;
2887
2888 if (str == NULL)
2889 return FAIL;
2890
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002891 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002892 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002893 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002894
2895 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2896 PyErr_SetVim(_("cannot save undo information"));
2897 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2898 PyErr_SetVim(_("cannot insert line"));
2899 else
2900 appended_lines_mark((linenr_T)n, 1L);
2901
2902 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002903 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002904 update_screen(VALID);
2905
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002906 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002907 return FAIL;
2908
2909 if (len_change)
2910 *len_change = 1;
2911
2912 return OK;
2913 }
2914 else if (PyList_Check(lines))
2915 {
2916 PyInt i;
2917 PyInt size = PyList_Size(lines);
2918 char **array;
2919 buf_T *savebuf;
2920
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002921 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002922 if (array == NULL)
2923 {
2924 PyErr_NoMemory();
2925 return FAIL;
2926 }
2927
2928 for (i = 0; i < size; ++i)
2929 {
2930 PyObject *line = PyList_GetItem(lines, i);
2931 array[i] = StringToLine(line);
2932
2933 if (array[i] == NULL)
2934 {
2935 while (i)
2936 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002937 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002938 return FAIL;
2939 }
2940 }
2941
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002942 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002943 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002944 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002945
2946 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2947 PyErr_SetVim(_("cannot save undo information"));
2948 else
2949 {
2950 for (i = 0; i < size; ++i)
2951 {
2952 if (ml_append((linenr_T)(n + i),
2953 (char_u *)array[i], 0, FALSE) == FAIL)
2954 {
2955 PyErr_SetVim(_("cannot insert line"));
2956
2957 /* Free the rest of the lines */
2958 while (i < size)
2959 vim_free(array[i++]);
2960
2961 break;
2962 }
2963 vim_free(array[i]);
2964 }
2965 if (i > 0)
2966 appended_lines_mark((linenr_T)n, (long)i);
2967 }
2968
2969 /* Free the array of lines. All of its contents have now
2970 * been freed.
2971 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002972 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002973
Bram Moolenaar105bc352013-05-17 16:03:57 +02002974 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002975 update_screen(VALID);
2976
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002977 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002978 return FAIL;
2979
2980 if (len_change)
2981 *len_change = size;
2982
2983 return OK;
2984 }
2985 else
2986 {
2987 PyErr_BadArgument();
2988 return FAIL;
2989 }
2990}
2991
2992/*
2993 * Common routines for buffers and line ranges
2994 * -------------------------------------------
2995 */
2996
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002997typedef struct
2998{
2999 PyObject_HEAD
3000 buf_T *buf;
3001} BufferObject;
3002
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003003 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003004CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003005{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003006 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003007 {
3008 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3009 return -1;
3010 }
3011
3012 return 0;
3013}
3014
3015 static PyObject *
3016RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3017{
3018 if (CheckBuffer(self))
3019 return NULL;
3020
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003021 if (end == -1)
3022 end = self->buf->b_ml.ml_line_count;
3023
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003024 if (n < 0)
3025 n += end - start + 1;
3026
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003027 if (n < 0 || n > end - start)
3028 {
3029 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3030 return NULL;
3031 }
3032
3033 return GetBufferLine(self->buf, n+start);
3034}
3035
3036 static PyObject *
3037RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3038{
3039 PyInt size;
3040
3041 if (CheckBuffer(self))
3042 return NULL;
3043
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003044 if (end == -1)
3045 end = self->buf->b_ml.ml_line_count;
3046
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003047 size = end - start + 1;
3048
3049 if (lo < 0)
3050 lo = 0;
3051 else if (lo > size)
3052 lo = size;
3053 if (hi < 0)
3054 hi = 0;
3055 if (hi < lo)
3056 hi = lo;
3057 else if (hi > size)
3058 hi = size;
3059
3060 return GetBufferLineList(self->buf, lo+start, hi+start);
3061}
3062
3063 static PyInt
3064RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3065{
3066 PyInt len_change;
3067
3068 if (CheckBuffer(self))
3069 return -1;
3070
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003071 if (end == -1)
3072 end = self->buf->b_ml.ml_line_count;
3073
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003074 if (n < 0)
3075 n += end - start + 1;
3076
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003077 if (n < 0 || n > end - start)
3078 {
3079 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3080 return -1;
3081 }
3082
3083 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3084 return -1;
3085
3086 if (new_end)
3087 *new_end = end + len_change;
3088
3089 return 0;
3090}
3091
Bram Moolenaar19e60942011-06-19 00:27:51 +02003092 static PyInt
3093RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3094{
3095 PyInt size;
3096 PyInt len_change;
3097
3098 /* Self must be a valid buffer */
3099 if (CheckBuffer(self))
3100 return -1;
3101
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003102 if (end == -1)
3103 end = self->buf->b_ml.ml_line_count;
3104
Bram Moolenaar19e60942011-06-19 00:27:51 +02003105 /* Sort out the slice range */
3106 size = end - start + 1;
3107
3108 if (lo < 0)
3109 lo = 0;
3110 else if (lo > size)
3111 lo = size;
3112 if (hi < 0)
3113 hi = 0;
3114 if (hi < lo)
3115 hi = lo;
3116 else if (hi > size)
3117 hi = size;
3118
3119 if (SetBufferLineList(self->buf, lo + start, hi + start,
3120 val, &len_change) == FAIL)
3121 return -1;
3122
3123 if (new_end)
3124 *new_end = end + len_change;
3125
3126 return 0;
3127}
3128
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003129
3130 static PyObject *
3131RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3132{
3133 PyObject *lines;
3134 PyInt len_change;
3135 PyInt max;
3136 PyInt n;
3137
3138 if (CheckBuffer(self))
3139 return NULL;
3140
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003141 if (end == -1)
3142 end = self->buf->b_ml.ml_line_count;
3143
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003144 max = n = end - start + 1;
3145
3146 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3147 return NULL;
3148
3149 if (n < 0 || n > max)
3150 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003151 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003152 return NULL;
3153 }
3154
3155 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3156 return NULL;
3157
3158 if (new_end)
3159 *new_end = end + len_change;
3160
3161 Py_INCREF(Py_None);
3162 return Py_None;
3163}
3164
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003165/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003166 */
3167
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003168static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003169static PySequenceMethods RangeAsSeq;
3170static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003171
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003172typedef struct
3173{
3174 PyObject_HEAD
3175 BufferObject *buf;
3176 PyInt start;
3177 PyInt end;
3178} RangeObject;
3179
3180 static PyObject *
3181RangeNew(buf_T *buf, PyInt start, PyInt end)
3182{
3183 BufferObject *bufr;
3184 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003185 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003186 if (self == NULL)
3187 return NULL;
3188
3189 bufr = (BufferObject *)BufferNew(buf);
3190 if (bufr == NULL)
3191 {
3192 Py_DECREF(self);
3193 return NULL;
3194 }
3195 Py_INCREF(bufr);
3196
3197 self->buf = bufr;
3198 self->start = start;
3199 self->end = end;
3200
3201 return (PyObject *)(self);
3202}
3203
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003204 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003205RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003206{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003207 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003208 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003209 PyObject_GC_Del((void *)(self));
3210}
3211
3212 static int
3213RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3214{
3215 Py_VISIT(((PyObject *)(self->buf)));
3216 return 0;
3217}
3218
3219 static int
3220RangeClear(RangeObject *self)
3221{
3222 Py_CLEAR(self->buf);
3223 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003224}
3225
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003226 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003227RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003228{
3229 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003230 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003231 return -1; /* ??? */
3232
Bram Moolenaard6e39182013-05-21 18:30:34 +02003233 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003234}
3235
3236 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003237RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003238{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003239 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003240}
3241
3242 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003243RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003244{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003245 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003246}
3247
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003248static char *RangeAttrs[] = {
3249 "start", "end",
3250 NULL
3251};
3252
3253 static PyObject *
3254RangeDir(PyObject *self)
3255{
3256 return ObjectDir(self, RangeAttrs);
3257}
3258
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003259 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003260RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003261{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003262 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003263}
3264
3265 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003266RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003267{
3268 static char repr[100];
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003269
Bram Moolenaard6e39182013-05-21 18:30:34 +02003270 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003271 {
3272 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
3273 (self));
3274 return PyString_FromString(repr);
3275 }
3276 else
3277 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003278 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003279 int len;
3280
3281 if (name == NULL)
3282 name = "";
3283 len = (int)strlen(name);
3284
3285 if (len > 45)
3286 name = name + (45 - len);
3287
3288 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
3289 len > 45 ? "..." : "", name,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003290 self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003291
3292 return PyString_FromString(repr);
3293 }
3294}
3295
3296static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003297 /* name, function, calling, documentation */
3298 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003299 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3300 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003301};
3302
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003303static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003304static PySequenceMethods BufferAsSeq;
3305static PyMappingMethods BufferAsMapping;
3306
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003307 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003308BufferNew(buf_T *buf)
3309{
3310 /* We need to handle deletion of buffers underneath us.
3311 * If we add a "b_python*_ref" field to the buf_T structure,
3312 * then we can get at it in buf_freeall() in vim. We then
3313 * need to create only ONE Python object per buffer - if
3314 * we try to create a second, just INCREF the existing one
3315 * and return it. The (single) Python object referring to
3316 * the buffer is stored in "b_python*_ref".
3317 * Question: what to do on a buf_freeall(). We'll probably
3318 * have to either delete the Python object (DECREF it to
3319 * zero - a bad idea, as it leaves dangling refs!) or
3320 * set the buf_T * value to an invalid value (-1?), which
3321 * means we need checks in all access functions... Bah.
3322 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003323 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003324 * b_python_ref and b_python3_ref fields respectively.
3325 */
3326
3327 BufferObject *self;
3328
3329 if (BUF_PYTHON_REF(buf) != NULL)
3330 {
3331 self = BUF_PYTHON_REF(buf);
3332 Py_INCREF(self);
3333 }
3334 else
3335 {
3336 self = PyObject_NEW(BufferObject, &BufferType);
3337 if (self == NULL)
3338 return NULL;
3339 self->buf = buf;
3340 BUF_PYTHON_REF(buf) = self;
3341 }
3342
3343 return (PyObject *)(self);
3344}
3345
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003346 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003347BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003348{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003349 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3350 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003351
3352 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003353}
3354
Bram Moolenaar971db462013-05-12 18:44:48 +02003355 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003356BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003357{
3358 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003359 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003360 return -1; /* ??? */
3361
Bram Moolenaard6e39182013-05-21 18:30:34 +02003362 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003363}
3364
3365 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003367{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003368 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003369}
3370
3371 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003372BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003373{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003374 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003375}
3376
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003377static char *BufferAttrs[] = {
3378 "name", "number", "vars", "options", "valid",
3379 NULL
3380};
3381
3382 static PyObject *
3383BufferDir(PyObject *self)
3384{
3385 return ObjectDir(self, BufferAttrs);
3386}
3387
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003388 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003389BufferAttrValid(BufferObject *self, char *name)
3390{
3391 PyObject *r;
3392
3393 if (strcmp(name, "valid") != 0)
3394 return NULL;
3395
3396 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
3397 Py_INCREF(r);
3398 return r;
3399}
3400
3401 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003402BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003403{
3404 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02003405 return PyString_FromString((self->buf->b_ffname == NULL
3406 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003407 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003408 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003409 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003410 return DictionaryNew(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003411 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003412 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3413 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003414 else if (strcmp(name, "__members__") == 0)
3415 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003416 else
3417 return NULL;
3418}
3419
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003420 static int
3421BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3422{
3423 if (CheckBuffer(self))
3424 return -1;
3425
3426 if (strcmp(name, "name") == 0)
3427 {
3428 char_u *val;
3429 aco_save_T aco;
3430 int r;
3431 PyObject *todecref;
3432
3433 if (!(val = StringToChars(valObject, &todecref)))
3434 return -1;
3435
3436 VimTryStart();
3437 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3438 aucmd_prepbuf(&aco, self->buf);
3439 r = rename_buffer(val);
3440 aucmd_restbuf(&aco);
3441 Py_XDECREF(todecref);
3442 if (VimTryEnd())
3443 return -1;
3444
3445 if (r == FAIL)
3446 {
3447 PyErr_SetVim(_("failed to rename buffer"));
3448 return -1;
3449 }
3450 return 0;
3451 }
3452 else
3453 {
3454 PyErr_SetString(PyExc_AttributeError, name);
3455 return -1;
3456 }
3457}
3458
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003459 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003460BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003461{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003462 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003463}
3464
3465 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003466BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003467{
3468 pos_T *posp;
3469 char *pmark;
3470 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003471 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003472
Bram Moolenaard6e39182013-05-21 18:30:34 +02003473 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003474 return NULL;
3475
3476 if (!PyArg_ParseTuple(args, "s", &pmark))
3477 return NULL;
3478 mark = *pmark;
3479
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003480 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02003481 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003482 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003483 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003484 if (VimTryEnd())
3485 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003486
3487 if (posp == NULL)
3488 {
3489 PyErr_SetVim(_("invalid mark name"));
3490 return NULL;
3491 }
3492
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003493 if (posp->lnum <= 0)
3494 {
3495 /* Or raise an error? */
3496 Py_INCREF(Py_None);
3497 return Py_None;
3498 }
3499
3500 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3501}
3502
3503 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003504BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003505{
3506 PyInt start;
3507 PyInt end;
3508
Bram Moolenaard6e39182013-05-21 18:30:34 +02003509 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003510 return NULL;
3511
3512 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3513 return NULL;
3514
Bram Moolenaard6e39182013-05-21 18:30:34 +02003515 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003516}
3517
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003518 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003519BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003520{
3521 static char repr[100];
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003522
Bram Moolenaard6e39182013-05-21 18:30:34 +02003523 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003524 {
3525 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3526 return PyString_FromString(repr);
3527 }
3528 else
3529 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003530 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003531 PyInt len;
3532
3533 if (name == NULL)
3534 name = "";
3535 len = strlen(name);
3536
3537 if (len > 35)
3538 name = name + (35 - len);
3539
3540 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3541
3542 return PyString_FromString(repr);
3543 }
3544}
3545
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003546static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003547 /* name, function, calling, documentation */
3548 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
3549 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
3550 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003551 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
3552 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003553};
3554
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003555/*
3556 * Buffer list object - Implementation
3557 */
3558
3559static PyTypeObject BufMapType;
3560
3561typedef struct
3562{
3563 PyObject_HEAD
3564} BufMapObject;
3565
3566 static PyInt
3567BufMapLength(PyObject *self UNUSED)
3568{
3569 buf_T *b = firstbuf;
3570 PyInt n = 0;
3571
3572 while (b)
3573 {
3574 ++n;
3575 b = b->b_next;
3576 }
3577
3578 return n;
3579}
3580
3581 static PyObject *
3582BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3583{
3584 buf_T *b;
3585 int bnr;
3586
3587#if PY_MAJOR_VERSION < 3
3588 if (PyInt_Check(keyObject))
3589 bnr = PyInt_AsLong(keyObject);
3590 else
3591#endif
3592 if (PyLong_Check(keyObject))
3593 bnr = PyLong_AsLong(keyObject);
3594 else
3595 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003596 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003597 return NULL;
3598 }
3599
3600 b = buflist_findnr(bnr);
3601
3602 if (b)
3603 return BufferNew(b);
3604 else
3605 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003606 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003607 return NULL;
3608 }
3609}
3610
3611 static void
3612BufMapIterDestruct(PyObject *buffer)
3613{
3614 /* Iteration was stopped before all buffers were processed */
3615 if (buffer)
3616 {
3617 Py_DECREF(buffer);
3618 }
3619}
3620
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003621 static int
3622BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3623{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003624 if (buffer)
3625 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003626 return 0;
3627}
3628
3629 static int
3630BufMapIterClear(PyObject **buffer)
3631{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003632 if (*buffer)
3633 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003634 return 0;
3635}
3636
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003637 static PyObject *
3638BufMapIterNext(PyObject **buffer)
3639{
3640 PyObject *next;
3641 PyObject *r;
3642
3643 if (!*buffer)
3644 return NULL;
3645
3646 r = *buffer;
3647
3648 if (CheckBuffer((BufferObject *)(r)))
3649 {
3650 *buffer = NULL;
3651 return NULL;
3652 }
3653
3654 if (!((BufferObject *)(r))->buf->b_next)
3655 next = NULL;
3656 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3657 return NULL;
3658 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02003659 /* Do not increment reference: we no longer hold it (decref), but whoever
3660 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003661 return r;
3662}
3663
3664 static PyObject *
3665BufMapIter(PyObject *self UNUSED)
3666{
3667 PyObject *buffer;
3668
3669 buffer = BufferNew(firstbuf);
3670 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003671 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3672 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003673}
3674
3675static PyMappingMethods BufMapAsMapping = {
3676 (lenfunc) BufMapLength,
3677 (binaryfunc) BufMapItem,
3678 (objobjargproc) 0,
3679};
3680
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003681/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003682 */
3683
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003684static char *CurrentAttrs[] = {
3685 "buffer", "window", "line", "range", "tabpage",
3686 NULL
3687};
3688
3689 static PyObject *
3690CurrentDir(PyObject *self)
3691{
3692 return ObjectDir(self, CurrentAttrs);
3693}
3694
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003695 static PyObject *
3696CurrentGetattr(PyObject *self UNUSED, char *name)
3697{
3698 if (strcmp(name, "buffer") == 0)
3699 return (PyObject *)BufferNew(curbuf);
3700 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003701 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003702 else if (strcmp(name, "tabpage") == 0)
3703 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003704 else if (strcmp(name, "line") == 0)
3705 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3706 else if (strcmp(name, "range") == 0)
3707 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003708 else if (strcmp(name, "__members__") == 0)
3709 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003710 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003711#if PY_MAJOR_VERSION < 3
3712 return Py_FindMethod(WindowMethods, self, name);
3713#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003714 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003715#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003716}
3717
3718 static int
3719CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3720{
3721 if (strcmp(name, "line") == 0)
3722 {
3723 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3724 return -1;
3725
3726 return 0;
3727 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003728 else if (strcmp(name, "buffer") == 0)
3729 {
3730 int count;
3731
3732 if (value->ob_type != &BufferType)
3733 {
3734 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3735 return -1;
3736 }
3737
3738 if (CheckBuffer((BufferObject *)(value)))
3739 return -1;
3740 count = ((BufferObject *)(value))->buf->b_fnum;
3741
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003742 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003743 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3744 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003745 if (VimTryEnd())
3746 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003747 PyErr_SetVim(_("failed to switch to given buffer"));
3748 return -1;
3749 }
3750
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003751 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003752 }
3753 else if (strcmp(name, "window") == 0)
3754 {
3755 int count;
3756
3757 if (value->ob_type != &WindowType)
3758 {
3759 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3760 return -1;
3761 }
3762
3763 if (CheckWindow((WindowObject *)(value)))
3764 return -1;
3765 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3766
3767 if (!count)
3768 {
3769 PyErr_SetString(PyExc_ValueError,
3770 _("failed to find window in the current tab page"));
3771 return -1;
3772 }
3773
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003774 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003775 win_goto(((WindowObject *)(value))->win);
3776 if (((WindowObject *)(value))->win != curwin)
3777 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003778 if (VimTryEnd())
3779 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003780 PyErr_SetString(PyExc_RuntimeError,
3781 _("did not switch to the specified window"));
3782 return -1;
3783 }
3784
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003785 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003786 }
3787 else if (strcmp(name, "tabpage") == 0)
3788 {
3789 if (value->ob_type != &TabPageType)
3790 {
3791 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3792 return -1;
3793 }
3794
3795 if (CheckTabPage((TabPageObject *)(value)))
3796 return -1;
3797
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003798 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003799 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3800 if (((TabPageObject *)(value))->tab != curtab)
3801 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003802 if (VimTryEnd())
3803 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003804 PyErr_SetString(PyExc_RuntimeError,
3805 _("did not switch to the specified tab page"));
3806 return -1;
3807 }
3808
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003809 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003810 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003811 else
3812 {
3813 PyErr_SetString(PyExc_AttributeError, name);
3814 return -1;
3815 }
3816}
3817
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003818static struct PyMethodDef CurrentMethods[] = {
3819 /* name, function, calling, documentation */
3820 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
3821 { NULL, NULL, 0, NULL}
3822};
3823
Bram Moolenaardb913952012-06-29 12:54:53 +02003824 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003825init_range_cmd(exarg_T *eap)
3826{
3827 RangeStart = eap->line1;
3828 RangeEnd = eap->line2;
3829}
3830
3831 static void
3832init_range_eval(typval_T *rettv UNUSED)
3833{
3834 RangeStart = (PyInt) curwin->w_cursor.lnum;
3835 RangeEnd = RangeStart;
3836}
3837
3838 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003839run_cmd(const char *cmd, void *arg UNUSED
3840#ifdef PY_CAN_RECURSE
3841 , PyGILState_STATE *pygilstate UNUSED
3842#endif
3843 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003844{
3845 PyRun_SimpleString((char *) cmd);
3846}
3847
3848static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
3849static int code_hdr_len = 30;
3850
3851 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003852run_do(const char *cmd, void *arg UNUSED
3853#ifdef PY_CAN_RECURSE
3854 , PyGILState_STATE *pygilstate
3855#endif
3856 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003857{
3858 PyInt lnum;
3859 size_t len;
3860 char *code;
3861 int status;
3862 PyObject *pyfunc, *pymain;
3863
Bram Moolenaar4ac66762013-05-28 22:31:46 +02003864 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003865 {
3866 EMSG(_("cannot save undo information"));
3867 return;
3868 }
3869
3870 len = code_hdr_len + STRLEN(cmd);
3871 code = PyMem_New(char, len + 1);
3872 memcpy(code, code_hdr, code_hdr_len);
3873 STRCPY(code + code_hdr_len, cmd);
3874 status = PyRun_SimpleString(code);
3875 PyMem_Free(code);
3876
3877 if (status)
3878 {
3879 EMSG(_("failed to run the code"));
3880 return;
3881 }
3882
3883 status = 0;
3884 pymain = PyImport_AddModule("__main__");
3885 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003886#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003887 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003888#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003889
3890 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
3891 {
3892 PyObject *line, *linenr, *ret;
3893
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003894#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003895 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003896#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003897 if (!(line = GetBufferLine(curbuf, lnum)))
3898 goto err;
3899 if (!(linenr = PyInt_FromLong((long) lnum)))
3900 {
3901 Py_DECREF(line);
3902 goto err;
3903 }
3904 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
3905 Py_DECREF(line);
3906 Py_DECREF(linenr);
3907 if (!ret)
3908 goto err;
3909
3910 if (ret != Py_None)
3911 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
3912 goto err;
3913
3914 Py_XDECREF(ret);
3915 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003916#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003917 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003918#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003919 }
3920 goto out;
3921err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003922#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003923 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003924#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003925 PyErr_PrintEx(0);
3926 PythonIO_Flush();
3927 status = 1;
3928out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003929#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003930 if (!status)
3931 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003932#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003933 Py_DECREF(pyfunc);
3934 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
3935 if (status)
3936 return;
3937 check_cursor();
3938 update_curbuf(NOT_VALID);
3939}
3940
3941 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003942run_eval(const char *cmd, typval_T *rettv
3943#ifdef PY_CAN_RECURSE
3944 , PyGILState_STATE *pygilstate UNUSED
3945#endif
3946 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003947{
3948 PyObject *r;
3949
3950 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
3951 if (r == NULL)
3952 {
3953 if (PyErr_Occurred() && !msg_silent)
3954 PyErr_PrintEx(0);
3955 EMSG(_("E858: Eval did not return a valid python object"));
3956 }
3957 else
3958 {
3959 if (ConvertFromPyObject(r, rettv) == -1)
3960 EMSG(_("E859: Failed to convert returned python object to vim value"));
3961 Py_DECREF(r);
3962 }
3963 PyErr_Clear();
3964}
3965
3966 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02003967set_ref_in_py(const int copyID)
3968{
3969 pylinkedlist_T *cur;
3970 dict_T *dd;
3971 list_T *ll;
3972
3973 if (lastdict != NULL)
3974 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3975 {
3976 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3977 if (dd->dv_copyID != copyID)
3978 {
3979 dd->dv_copyID = copyID;
3980 set_ref_in_ht(&dd->dv_hashtab, copyID);
3981 }
3982 }
3983
3984 if (lastlist != NULL)
3985 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3986 {
3987 ll = ((ListObject *) (cur->pll_obj))->list;
3988 if (ll->lv_copyID != copyID)
3989 {
3990 ll->lv_copyID = copyID;
3991 set_ref_in_list(ll, copyID);
3992 }
3993 }
3994}
3995
3996 static int
3997set_string_copy(char_u *str, typval_T *tv)
3998{
3999 tv->vval.v_string = vim_strsave(str);
4000 if (tv->vval.v_string == NULL)
4001 {
4002 PyErr_NoMemory();
4003 return -1;
4004 }
4005 return 0;
4006}
4007
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004008 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004009pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004010{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004011 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004012 char_u *key;
4013 dictitem_T *di;
4014 PyObject *keyObject;
4015 PyObject *valObject;
4016 Py_ssize_t iter = 0;
4017
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004018 dict = dict_alloc();
4019 if (dict == NULL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004020 {
4021 PyErr_NoMemory();
4022 return -1;
4023 }
4024
4025 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004026 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004027
4028 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4029 {
4030 DICTKEY_DECL
4031
4032 if (keyObject == NULL)
4033 return -1;
4034 if (valObject == NULL)
4035 return -1;
4036
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +02004037 DICTKEY_GET(-1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004038
4039 di = dictitem_alloc(key);
4040
4041 DICTKEY_UNREF
4042
4043 if (di == NULL)
4044 {
4045 PyErr_NoMemory();
4046 return -1;
4047 }
4048 di->di_tv.v_lock = 0;
4049
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004050 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004051 {
4052 vim_free(di);
4053 return -1;
4054 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004055
4056 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004057 {
4058 vim_free(di);
4059 PyErr_SetVim(_("failed to add key to dictionary"));
4060 return -1;
4061 }
4062 }
4063 return 0;
4064}
4065
4066 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004067pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004068{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004069 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004070 char_u *key;
4071 dictitem_T *di;
4072 PyObject *list;
4073 PyObject *litem;
4074 PyObject *keyObject;
4075 PyObject *valObject;
4076 Py_ssize_t lsize;
4077
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004078 dict = dict_alloc();
4079 if (dict == NULL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004080 {
4081 PyErr_NoMemory();
4082 return -1;
4083 }
4084
4085 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004086 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004087
4088 list = PyMapping_Items(obj);
4089 if (list == NULL)
4090 return -1;
4091 lsize = PyList_Size(list);
4092 while (lsize--)
4093 {
4094 DICTKEY_DECL
4095
4096 litem = PyList_GetItem(list, lsize);
4097 if (litem == NULL)
4098 {
4099 Py_DECREF(list);
4100 return -1;
4101 }
4102
4103 keyObject = PyTuple_GetItem(litem, 0);
4104 if (keyObject == NULL)
4105 {
4106 Py_DECREF(list);
4107 Py_DECREF(litem);
4108 return -1;
4109 }
4110
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +02004111 DICTKEY_GET(-1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004112
4113 valObject = PyTuple_GetItem(litem, 1);
4114 if (valObject == NULL)
4115 {
4116 Py_DECREF(list);
4117 Py_DECREF(litem);
4118 return -1;
4119 }
4120
4121 di = dictitem_alloc(key);
4122
4123 DICTKEY_UNREF
4124
4125 if (di == NULL)
4126 {
4127 Py_DECREF(list);
4128 Py_DECREF(litem);
4129 PyErr_NoMemory();
4130 return -1;
4131 }
4132 di->di_tv.v_lock = 0;
4133
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004134 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004135 {
4136 vim_free(di);
4137 Py_DECREF(list);
4138 Py_DECREF(litem);
4139 return -1;
4140 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004141 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004142 {
4143 vim_free(di);
4144 Py_DECREF(list);
4145 Py_DECREF(litem);
4146 PyErr_SetVim(_("failed to add key to dictionary"));
4147 return -1;
4148 }
4149 Py_DECREF(litem);
4150 }
4151 Py_DECREF(list);
4152 return 0;
4153}
4154
4155 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004156pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004157{
4158 list_T *l;
4159
4160 l = list_alloc();
4161 if (l == NULL)
4162 {
4163 PyErr_NoMemory();
4164 return -1;
4165 }
4166
4167 tv->v_type = VAR_LIST;
4168 tv->vval.v_list = l;
4169
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004170 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004171 return -1;
4172
4173 return 0;
4174}
4175
4176 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004177pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004178{
4179 PyObject *iterator = PyObject_GetIter(obj);
4180 PyObject *item;
4181 list_T *l;
4182 listitem_T *li;
4183
4184 l = list_alloc();
4185
4186 if (l == NULL)
4187 {
4188 PyErr_NoMemory();
4189 return -1;
4190 }
4191
4192 tv->vval.v_list = l;
4193 tv->v_type = VAR_LIST;
4194
4195
4196 if (iterator == NULL)
4197 return -1;
4198
4199 while ((item = PyIter_Next(obj)))
4200 {
4201 li = listitem_alloc();
4202 if (li == NULL)
4203 {
4204 PyErr_NoMemory();
4205 return -1;
4206 }
4207 li->li_tv.v_lock = 0;
4208
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004209 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004210 return -1;
4211
4212 list_append(l, li);
4213
4214 Py_DECREF(item);
4215 }
4216
4217 Py_DECREF(iterator);
4218 return 0;
4219}
4220
Bram Moolenaardb913952012-06-29 12:54:53 +02004221typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4222
4223 static int
4224convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004225 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004226{
4227 PyObject *capsule;
4228 char hexBuf[sizeof(void *) * 2 + 3];
4229
4230 sprintf(hexBuf, "%p", obj);
4231
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004232# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004233 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004234# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004235 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004236# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004237 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004238 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004239# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004240 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004241# else
4242 capsule = PyCObject_FromVoidPtr(tv, NULL);
4243# endif
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004244 PyDict_SetItemString(lookup_dict, hexBuf, capsule);
Bram Moolenaardb913952012-06-29 12:54:53 +02004245 Py_DECREF(capsule);
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004246 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004247 {
4248 tv->v_type = VAR_UNKNOWN;
4249 return -1;
4250 }
4251 /* As we are not using copy_tv which increments reference count we must
4252 * do it ourself. */
4253 switch(tv->v_type)
4254 {
4255 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4256 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4257 }
4258 }
4259 else
4260 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004261 typval_T *v;
4262
4263# ifdef PY_USE_CAPSULE
4264 v = PyCapsule_GetPointer(capsule, NULL);
4265# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004266 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004267# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004268 copy_tv(v, tv);
4269 }
4270 return 0;
4271}
4272
4273 static int
4274ConvertFromPyObject(PyObject *obj, typval_T *tv)
4275{
4276 PyObject *lookup_dict;
4277 int r;
4278
4279 lookup_dict = PyDict_New();
4280 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4281 Py_DECREF(lookup_dict);
4282 return r;
4283}
4284
4285 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004286_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004287{
4288 if (obj->ob_type == &DictionaryType)
4289 {
4290 tv->v_type = VAR_DICT;
4291 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4292 ++tv->vval.v_dict->dv_refcount;
4293 }
4294 else if (obj->ob_type == &ListType)
4295 {
4296 tv->v_type = VAR_LIST;
4297 tv->vval.v_list = (((ListObject *)(obj))->list);
4298 ++tv->vval.v_list->lv_refcount;
4299 }
4300 else if (obj->ob_type == &FunctionType)
4301 {
4302 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4303 return -1;
4304
4305 tv->v_type = VAR_FUNC;
4306 func_ref(tv->vval.v_string);
4307 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004308 else if (PyBytes_Check(obj))
4309 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004310 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004311
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004312 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4313 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004314 if (result == NULL)
4315 return -1;
4316
4317 if (set_string_copy(result, tv) == -1)
4318 return -1;
4319
4320 tv->v_type = VAR_STRING;
4321 }
4322 else if (PyUnicode_Check(obj))
4323 {
4324 PyObject *bytes;
4325 char_u *result;
4326
Bram Moolenaardb913952012-06-29 12:54:53 +02004327 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4328 if (bytes == NULL)
4329 return -1;
4330
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004331 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4332 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004333 if (result == NULL)
4334 return -1;
4335
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004336 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004337 {
4338 Py_XDECREF(bytes);
4339 return -1;
4340 }
4341 Py_XDECREF(bytes);
4342
4343 tv->v_type = VAR_STRING;
4344 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004345#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004346 else if (PyInt_Check(obj))
4347 {
4348 tv->v_type = VAR_NUMBER;
4349 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4350 }
4351#endif
4352 else if (PyLong_Check(obj))
4353 {
4354 tv->v_type = VAR_NUMBER;
4355 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4356 }
4357 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004358 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004359#ifdef FEAT_FLOAT
4360 else if (PyFloat_Check(obj))
4361 {
4362 tv->v_type = VAR_FLOAT;
4363 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4364 }
4365#endif
4366 else if (PyIter_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004367 return convert_dl(obj, tv, pyiter_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004368 else if (PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004369 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004370 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004371 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004372 else
4373 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004374 PyErr_SetString(PyExc_TypeError,
4375 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004376 return -1;
4377 }
4378 return 0;
4379}
4380
4381 static PyObject *
4382ConvertToPyObject(typval_T *tv)
4383{
4384 if (tv == NULL)
4385 {
4386 PyErr_SetVim(_("NULL reference passed"));
4387 return NULL;
4388 }
4389 switch (tv->v_type)
4390 {
4391 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004392 return PyBytes_FromString(tv->vval.v_string == NULL
4393 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004394 case VAR_NUMBER:
4395 return PyLong_FromLong((long) tv->vval.v_number);
4396#ifdef FEAT_FLOAT
4397 case VAR_FLOAT:
4398 return PyFloat_FromDouble((double) tv->vval.v_float);
4399#endif
4400 case VAR_LIST:
4401 return ListNew(tv->vval.v_list);
4402 case VAR_DICT:
4403 return DictionaryNew(tv->vval.v_dict);
4404 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004405 return FunctionNew(tv->vval.v_string == NULL
4406 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004407 case VAR_UNKNOWN:
4408 Py_INCREF(Py_None);
4409 return Py_None;
4410 default:
4411 PyErr_SetVim(_("internal error: invalid value type"));
4412 return NULL;
4413 }
4414}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004415
4416typedef struct
4417{
4418 PyObject_HEAD
4419} CurrentObject;
4420static PyTypeObject CurrentType;
4421
4422 static void
4423init_structs(void)
4424{
4425 vim_memset(&OutputType, 0, sizeof(OutputType));
4426 OutputType.tp_name = "vim.message";
4427 OutputType.tp_basicsize = sizeof(OutputObject);
4428 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4429 OutputType.tp_doc = "vim message object";
4430 OutputType.tp_methods = OutputMethods;
4431#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004432 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4433 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004434 OutputType.tp_alloc = call_PyType_GenericAlloc;
4435 OutputType.tp_new = call_PyType_GenericNew;
4436 OutputType.tp_free = call_PyObject_Free;
4437#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004438 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4439 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004440#endif
4441
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004442 vim_memset(&IterType, 0, sizeof(IterType));
4443 IterType.tp_name = "vim.iter";
4444 IterType.tp_basicsize = sizeof(IterObject);
4445 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
4446 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004447 IterType.tp_iter = (getiterfunc)IterIter;
4448 IterType.tp_iternext = (iternextfunc)IterNext;
4449 IterType.tp_dealloc = (destructor)IterDestructor;
4450 IterType.tp_traverse = (traverseproc)IterTraverse;
4451 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004452
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004453 vim_memset(&BufferType, 0, sizeof(BufferType));
4454 BufferType.tp_name = "vim.buffer";
4455 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004456 BufferType.tp_dealloc = (destructor)BufferDestructor;
4457 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004458 BufferType.tp_as_sequence = &BufferAsSeq;
4459 BufferType.tp_as_mapping = &BufferAsMapping;
4460 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4461 BufferType.tp_doc = "vim buffer object";
4462 BufferType.tp_methods = BufferMethods;
4463#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004464 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004465 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004466 BufferType.tp_alloc = call_PyType_GenericAlloc;
4467 BufferType.tp_new = call_PyType_GenericNew;
4468 BufferType.tp_free = call_PyObject_Free;
4469#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004470 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004471 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004472#endif
4473
4474 vim_memset(&WindowType, 0, sizeof(WindowType));
4475 WindowType.tp_name = "vim.window";
4476 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004477 WindowType.tp_dealloc = (destructor)WindowDestructor;
4478 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004479 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
4480 WindowType.tp_doc = "vim Window object";
4481 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004482 WindowType.tp_traverse = (traverseproc)WindowTraverse;
4483 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004484#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004485 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
4486 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004487 WindowType.tp_alloc = call_PyType_GenericAlloc;
4488 WindowType.tp_new = call_PyType_GenericNew;
4489 WindowType.tp_free = call_PyObject_Free;
4490#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004491 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
4492 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004493#endif
4494
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004495 vim_memset(&TabPageType, 0, sizeof(TabPageType));
4496 TabPageType.tp_name = "vim.tabpage";
4497 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004498 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
4499 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004500 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
4501 TabPageType.tp_doc = "vim tab page object";
4502 TabPageType.tp_methods = TabPageMethods;
4503#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004504 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004505 TabPageType.tp_alloc = call_PyType_GenericAlloc;
4506 TabPageType.tp_new = call_PyType_GenericNew;
4507 TabPageType.tp_free = call_PyObject_Free;
4508#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004509 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004510#endif
4511
Bram Moolenaardfa38d42013-05-15 13:38:47 +02004512 vim_memset(&BufMapType, 0, sizeof(BufMapType));
4513 BufMapType.tp_name = "vim.bufferlist";
4514 BufMapType.tp_basicsize = sizeof(BufMapObject);
4515 BufMapType.tp_as_mapping = &BufMapAsMapping;
4516 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004517 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004518 BufferType.tp_doc = "vim buffer list";
4519
4520 vim_memset(&WinListType, 0, sizeof(WinListType));
4521 WinListType.tp_name = "vim.windowlist";
4522 WinListType.tp_basicsize = sizeof(WinListType);
4523 WinListType.tp_as_sequence = &WinListAsSeq;
4524 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
4525 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004526 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004527
4528 vim_memset(&TabListType, 0, sizeof(TabListType));
4529 TabListType.tp_name = "vim.tabpagelist";
4530 TabListType.tp_basicsize = sizeof(TabListType);
4531 TabListType.tp_as_sequence = &TabListAsSeq;
4532 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
4533 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004534
4535 vim_memset(&RangeType, 0, sizeof(RangeType));
4536 RangeType.tp_name = "vim.range";
4537 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004538 RangeType.tp_dealloc = (destructor)RangeDestructor;
4539 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004540 RangeType.tp_as_sequence = &RangeAsSeq;
4541 RangeType.tp_as_mapping = &RangeAsMapping;
4542 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
4543 RangeType.tp_doc = "vim Range object";
4544 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004545 RangeType.tp_traverse = (traverseproc)RangeTraverse;
4546 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004547#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004548 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004549 RangeType.tp_alloc = call_PyType_GenericAlloc;
4550 RangeType.tp_new = call_PyType_GenericNew;
4551 RangeType.tp_free = call_PyObject_Free;
4552#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004553 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004554#endif
4555
4556 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4557 CurrentType.tp_name = "vim.currentdata";
4558 CurrentType.tp_basicsize = sizeof(CurrentObject);
4559 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4560 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004561 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004562#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004563 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
4564 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004565#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004566 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
4567 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004568#endif
4569
4570 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4571 DictionaryType.tp_name = "vim.dictionary";
4572 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004573 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004574 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4575 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4576 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4577 DictionaryType.tp_methods = DictionaryMethods;
4578#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004579 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
4580 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004581#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004582 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
4583 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004584#endif
4585
4586 vim_memset(&ListType, 0, sizeof(ListType));
4587 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004588 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004589 ListType.tp_basicsize = sizeof(ListObject);
4590 ListType.tp_as_sequence = &ListAsSeq;
4591 ListType.tp_as_mapping = &ListAsMapping;
4592 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4593 ListType.tp_doc = "list pushing modifications to vim structure";
4594 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004595 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004596#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004597 ListType.tp_getattro = (getattrofunc)ListGetattro;
4598 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004599#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004600 ListType.tp_getattr = (getattrfunc)ListGetattr;
4601 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004602#endif
4603
4604 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004605 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004606 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004607 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
4608 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004609 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4610 FunctionType.tp_doc = "object that calls vim function";
4611 FunctionType.tp_methods = FunctionMethods;
4612#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004613 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004614#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004615 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004616#endif
4617
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004618 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4619 OptionsType.tp_name = "vim.options";
4620 OptionsType.tp_basicsize = sizeof(OptionsObject);
4621 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4622 OptionsType.tp_doc = "object for manipulating options";
4623 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004624 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
4625 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
4626 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004627
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004628#if PY_MAJOR_VERSION >= 3
4629 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4630 vimmodule.m_name = "vim";
4631 vimmodule.m_doc = "Vim Python interface\n";
4632 vimmodule.m_size = -1;
4633 vimmodule.m_methods = VimMethods;
4634#endif
4635}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004636
4637#define PYTYPE_READY(type) \
4638 if (PyType_Ready(&type)) \
4639 return -1;
4640
4641 static int
4642init_types()
4643{
4644 PYTYPE_READY(IterType);
4645 PYTYPE_READY(BufferType);
4646 PYTYPE_READY(RangeType);
4647 PYTYPE_READY(WindowType);
4648 PYTYPE_READY(TabPageType);
4649 PYTYPE_READY(BufMapType);
4650 PYTYPE_READY(WinListType);
4651 PYTYPE_READY(TabListType);
4652 PYTYPE_READY(CurrentType);
4653 PYTYPE_READY(DictionaryType);
4654 PYTYPE_READY(ListType);
4655 PYTYPE_READY(FunctionType);
4656 PYTYPE_READY(OptionsType);
4657 PYTYPE_READY(OutputType);
4658 return 0;
4659}
4660
4661static BufMapObject TheBufferMap =
4662{
4663 PyObject_HEAD_INIT(&BufMapType)
4664};
4665
4666static WinListObject TheWindowList =
4667{
4668 PyObject_HEAD_INIT(&WinListType)
4669 NULL
4670};
4671
4672static CurrentObject TheCurrent =
4673{
4674 PyObject_HEAD_INIT(&CurrentType)
4675};
4676
4677static TabListObject TheTabPageList =
4678{
4679 PyObject_HEAD_INIT(&TabListType)
4680};
4681
4682static struct numeric_constant {
4683 char *name;
4684 int value;
4685} numeric_constants[] = {
4686 {"VAR_LOCKED", VAR_LOCKED},
4687 {"VAR_FIXED", VAR_FIXED},
4688 {"VAR_SCOPE", VAR_SCOPE},
4689 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
4690};
4691
4692static struct object_constant {
4693 char *name;
4694 PyObject *value;
4695} object_constants[] = {
4696 {"buffers", (PyObject *)(void *)&TheBufferMap},
4697 {"windows", (PyObject *)(void *)&TheWindowList},
4698 {"tabpages", (PyObject *)(void *)&TheTabPageList},
4699 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02004700
4701 {"Buffer", (PyObject *)&BufferType},
4702 {"Range", (PyObject *)&RangeType},
4703 {"Window", (PyObject *)&WindowType},
4704 {"TabPage", (PyObject *)&TabPageType},
4705 {"Dictionary", (PyObject *)&DictionaryType},
4706 {"List", (PyObject *)&ListType},
4707 {"Function", (PyObject *)&FunctionType},
4708 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004709};
4710
4711typedef int (*object_adder)(PyObject *, const char *, PyObject *);
4712
4713#define ADD_OBJECT(m, name, obj) \
4714 if (add_object(m, name, obj)) \
4715 return -1;
4716
4717#define ADD_CHECKED_OBJECT(m, name, obj) \
4718 { \
4719 PyObject *value = obj; \
4720 if (!value) \
4721 return -1; \
4722 ADD_OBJECT(m, name, value); \
4723 }
4724
4725 static int
4726populate_module(PyObject *m, object_adder add_object)
4727{
4728 int i;
4729
4730 for (i = 0; i < (int)(sizeof(numeric_constants)
4731 / sizeof(struct numeric_constant));
4732 ++i)
4733 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
4734 PyInt_FromLong(numeric_constants[i].value));
4735
4736 for (i = 0; i < (int)(sizeof(object_constants)
4737 / sizeof(struct object_constant));
4738 ++i)
4739 {
4740 PyObject *value;
4741
4742 value = object_constants[i].value;
4743 Py_INCREF(value);
4744 ADD_OBJECT(m, object_constants[i].name, value);
4745 }
4746
4747 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
4748 return -1;
4749 ADD_OBJECT(m, "error", VimError);
4750
4751 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict));
4752 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
4753 ADD_CHECKED_OBJECT(m, "options",
4754 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
4755 return 0;
4756}