blob: 4c80d2e30fa152d28e3966e9ba19398a73fb1d24 [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
Bram Moolenaar35eacd72013-05-30 22:06:33 +020029#define RAISE_NO_EMPTY_KEYS PyErr_SetString(PyExc_ValueError, \
30 _("empty keys are not allowed"))
31
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020032#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
33#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020034#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020035
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020036typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020037typedef void (*runner)(const char *, void *
38#ifdef PY_CAN_RECURSE
39 , PyGILState_STATE *
40#endif
41 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020042
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020043static int ConvertFromPyObject(PyObject *, typval_T *);
44static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020045static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020046static PyObject *WindowNew(win_T *, tabpage_T *);
47static PyObject *BufferNew (buf_T *);
48static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020049
50static PyInt RangeStart;
51static PyInt RangeEnd;
52
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020053static PyObject *globals;
54
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020055/*
56 * obtain a lock on the Vim data structures
57 */
58 static void
59Python_Lock_Vim(void)
60{
61}
62
63/*
64 * release a lock on the Vim data structures
65 */
66 static void
67Python_Release_Vim(void)
68{
69}
70
Bram Moolenaare9ba5162013-05-29 22:02:22 +020071/*
72 * The "todecref" argument holds a pointer to PyObject * that must be
73 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
74 * was needed to generate returned value is object.
75 *
76 * Use Py_XDECREF to decrement reference count.
77 */
78 static char_u *
79StringToChars(PyObject *object, PyObject **todecref)
80{
81 char_u *p;
82 PyObject *bytes = NULL;
83
84 if (PyBytes_Check(object))
85 {
86
87 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
88 return NULL;
89 if (p == NULL)
90 return NULL;
91
92 *todecref = NULL;
93 }
94 else if (PyUnicode_Check(object))
95 {
96 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
97 if (bytes == NULL)
98 return NULL;
99
100 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
101 return NULL;
102 if (p == NULL)
103 return NULL;
104
105 *todecref = bytes;
106 }
107 else
108 {
109 PyErr_SetString(PyExc_TypeError, _("object must be string"));
110 return NULL;
111 }
112
113 return (char_u *) p;
114}
115
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200116 static int
117add_string(PyObject *list, char *s)
118{
119 PyObject *string;
120
121 if (!(string = PyString_FromString(s)))
122 return -1;
123 if (PyList_Append(list, string))
124 {
125 Py_DECREF(string);
126 return -1;
127 }
128
129 Py_DECREF(string);
130 return 0;
131}
132
133 static PyObject *
134ObjectDir(PyObject *self, char **attributes)
135{
136 PyMethodDef *method;
137 char **attr;
138 PyObject *r;
139
140 if (!(r = PyList_New(0)))
141 return NULL;
142
143 if (self)
144 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
145 if (add_string(r, (char *) method->ml_name))
146 {
147 Py_DECREF(r);
148 return NULL;
149 }
150
151 for (attr = attributes ; *attr ; ++attr)
152 if (add_string(r, *attr))
153 {
154 Py_DECREF(r);
155 return NULL;
156 }
157
158#if PY_MAJOR_VERSION < 3
159 if (add_string(r, "__members__"))
160 {
161 Py_DECREF(r);
162 return NULL;
163 }
164#endif
165
166 return r;
167}
168
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200169/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200170 */
171
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200172/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200173typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200174
175static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200176
177typedef struct
178{
179 PyObject_HEAD
180 long softspace;
181 long error;
182} OutputObject;
183
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200184static char *OutputAttrs[] = {
185 "softspace",
186 NULL
187};
188
189 static PyObject *
190OutputDir(PyObject *self)
191{
192 return ObjectDir(self, OutputAttrs);
193}
194
Bram Moolenaar77045652012-09-21 13:46:06 +0200195 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200196OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200197{
198 if (val == NULL)
199 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200200 PyErr_SetString(PyExc_AttributeError,
201 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200202 return -1;
203 }
204
205 if (strcmp(name, "softspace") == 0)
206 {
207 if (!PyInt_Check(val))
208 {
209 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
210 return -1;
211 }
212
Bram Moolenaard6e39182013-05-21 18:30:34 +0200213 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200214 return 0;
215 }
216
217 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
218 return -1;
219}
220
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200221/* Buffer IO, we write one whole line at a time. */
222static garray_T io_ga = {0, 0, 1, 80, NULL};
223static writefn old_fn = NULL;
224
225 static void
226PythonIO_Flush(void)
227{
228 if (old_fn != NULL && io_ga.ga_len > 0)
229 {
230 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
231 old_fn((char_u *)io_ga.ga_data);
232 }
233 io_ga.ga_len = 0;
234}
235
236 static void
237writer(writefn fn, char_u *str, PyInt n)
238{
239 char_u *ptr;
240
241 /* Flush when switching output function. */
242 if (fn != old_fn)
243 PythonIO_Flush();
244 old_fn = fn;
245
246 /* Write each NL separated line. Text after the last NL is kept for
247 * writing later. */
248 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
249 {
250 PyInt len = ptr - str;
251
252 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
253 break;
254
255 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
256 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
257 fn((char_u *)io_ga.ga_data);
258 str = ptr + 1;
259 n -= len + 1;
260 io_ga.ga_len = 0;
261 }
262
263 /* Put the remaining text into io_ga for later printing. */
264 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
265 {
266 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
267 io_ga.ga_len += (int)n;
268 }
269}
270
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200271 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200272OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200273{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200274 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200275 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200276 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200277
Bram Moolenaar27564802011-09-07 19:30:21 +0200278 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200279 return NULL;
280
281 Py_BEGIN_ALLOW_THREADS
282 Python_Lock_Vim();
283 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
284 Python_Release_Vim();
285 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200286 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200287
288 Py_INCREF(Py_None);
289 return Py_None;
290}
291
292 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200293OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200294{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200295 PyObject *seq;
296 PyObject *iterator;
297 PyObject *item;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200298 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200299
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200300 if (!PyArg_ParseTuple(args, "O", &seq))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200301 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200302
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200303 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200304 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200305
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200306 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200307 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200308 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200309 PyInt len;
310
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200311 if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
Bram Moolenaardb913952012-06-29 12:54:53 +0200312 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200313 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200314 Py_DECREF(iterator);
315 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200316 return NULL;
317 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200318 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200319
320 Py_BEGIN_ALLOW_THREADS
321 Python_Lock_Vim();
322 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
323 Python_Release_Vim();
324 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200325 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200326 }
327
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200328 Py_DECREF(iterator);
329
330 /* Iterator may have finished due to an exception */
331 if (PyErr_Occurred())
332 return NULL;
333
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200334 Py_INCREF(Py_None);
335 return Py_None;
336}
337
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100338 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200339OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100340{
341 /* do nothing */
342 Py_INCREF(Py_None);
343 return Py_None;
344}
345
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200346/***************/
347
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200348static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200349 /* name, function, calling, doc */
350 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
351 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
352 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200353 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200354 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200355};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200356
357static OutputObject Output =
358{
359 PyObject_HEAD_INIT(&OutputType)
360 0,
361 0
362};
363
364static OutputObject Error =
365{
366 PyObject_HEAD_INIT(&OutputType)
367 0,
368 1
369};
370
371 static int
372PythonIO_Init_io(void)
373{
374 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
375 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
376
377 if (PyErr_Occurred())
378 {
379 EMSG(_("E264: Python: Error initialising I/O objects"));
380 return -1;
381 }
382
383 return 0;
384}
385
386
387static PyObject *VimError;
388
389/* Check to see whether a Vim error has been reported, or a keyboard
390 * interrupt has been detected.
391 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200392
393 static void
394VimTryStart(void)
395{
396 ++trylevel;
397}
398
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200399 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200400VimTryEnd(void)
401{
402 --trylevel;
403 if (got_int)
404 {
405 PyErr_SetNone(PyExc_KeyboardInterrupt);
406 return 1;
407 }
408 else if (!did_throw)
409 return 0;
410 else if (PyErr_Occurred())
411 return 1;
412 else
413 {
414 PyErr_SetVim((char *) current_exception->value);
415 discard_current_exception();
416 return 1;
417 }
418}
419
420 static int
421VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200422{
423 if (got_int)
424 {
425 PyErr_SetNone(PyExc_KeyboardInterrupt);
426 return 1;
427 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200428 return 0;
429}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200430
431/* Vim module - Implementation
432 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200433
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200434 static PyObject *
435VimCommand(PyObject *self UNUSED, PyObject *args)
436{
437 char *cmd;
438 PyObject *result;
439
440 if (!PyArg_ParseTuple(args, "s", &cmd))
441 return NULL;
442
443 PyErr_Clear();
444
445 Py_BEGIN_ALLOW_THREADS
446 Python_Lock_Vim();
447
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200448 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200449 do_cmdline_cmd((char_u *)cmd);
450 update_screen(VALID);
451
452 Python_Release_Vim();
453 Py_END_ALLOW_THREADS
454
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200455 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200456 result = NULL;
457 else
458 result = Py_None;
459
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200460
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200461 Py_XINCREF(result);
462 return result;
463}
464
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200465/*
466 * Function to translate a typval_T into a PyObject; this will recursively
467 * translate lists/dictionaries into their Python equivalents.
468 *
469 * The depth parameter is to avoid infinite recursion, set it to 1 when
470 * you call VimToPython.
471 */
472 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200473VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200474{
475 PyObject *result;
476 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200477 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200478
479 /* Avoid infinite recursion */
480 if (depth > 100)
481 {
482 Py_INCREF(Py_None);
483 result = Py_None;
484 return result;
485 }
486
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200487 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200488 * then and we can use it again. */
489 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
490 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
491 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200492 sprintf(ptrBuf, "%p",
493 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
494 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200495
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200496 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200497 {
498 Py_INCREF(result);
499 return result;
500 }
501 }
502
503 if (our_tv->v_type == VAR_STRING)
504 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200505 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200506 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200507 }
508 else if (our_tv->v_type == VAR_NUMBER)
509 {
510 char buf[NUMBUFLEN];
511
512 /* For backwards compatibility numbers are stored as strings. */
513 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200514 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200515 }
516# ifdef FEAT_FLOAT
517 else if (our_tv->v_type == VAR_FLOAT)
518 {
519 char buf[NUMBUFLEN];
520
521 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200522 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200523 }
524# endif
525 else if (our_tv->v_type == VAR_LIST)
526 {
527 list_T *list = our_tv->vval.v_list;
528 listitem_T *curr;
529
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200530 if (list == NULL)
531 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200532
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200533 if (!(result = PyList_New(0)))
534 return NULL;
535
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200536 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200537 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200538 Py_DECREF(result);
539 return NULL;
540 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200541
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200542 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
543 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200544 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200545 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200546 Py_DECREF(result);
547 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200548 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200549 if (PyList_Append(result, newObj))
550 {
551 Py_DECREF(newObj);
552 Py_DECREF(result);
553 return NULL;
554 }
555 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200556 }
557 }
558 else if (our_tv->v_type == VAR_DICT)
559 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200560
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200561 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
562 long_u todo = ht->ht_used;
563 hashitem_T *hi;
564 dictitem_T *di;
565 if (our_tv->vval.v_dict == NULL)
566 return NULL;
567
568 if (!(result = PyDict_New()))
569 return NULL;
570
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200571 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200572 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200573 Py_DECREF(result);
574 return NULL;
575 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200576
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200577 for (hi = ht->ht_array; todo > 0; ++hi)
578 {
579 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200580 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200581 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200582
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200583 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200584 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200585 {
586 Py_DECREF(result);
587 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200588 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200589 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
590 {
591 Py_DECREF(result);
592 Py_DECREF(newObj);
593 return NULL;
594 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200595 }
596 }
597 }
598 else
599 {
600 Py_INCREF(Py_None);
601 result = Py_None;
602 }
603
604 return result;
605}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200606
607 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200608VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200609{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200610 char *expr;
611 typval_T *our_tv;
612 PyObject *result;
613 PyObject *lookup_dict;
614
615 if (!PyArg_ParseTuple(args, "s", &expr))
616 return NULL;
617
618 Py_BEGIN_ALLOW_THREADS
619 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200620 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200621 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200622 Python_Release_Vim();
623 Py_END_ALLOW_THREADS
624
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200625 if (VimTryEnd())
626 return NULL;
627
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628 if (our_tv == NULL)
629 {
630 PyErr_SetVim(_("invalid expression"));
631 return NULL;
632 }
633
634 /* Convert the Vim type into a Python type. Create a dictionary that's
635 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200636 if (!(lookup_dict = PyDict_New()))
637 result = NULL;
638 else
639 {
640 result = VimToPython(our_tv, 1, lookup_dict);
641 Py_DECREF(lookup_dict);
642 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200643
644
645 Py_BEGIN_ALLOW_THREADS
646 Python_Lock_Vim();
647 free_tv(our_tv);
648 Python_Release_Vim();
649 Py_END_ALLOW_THREADS
650
651 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200652}
653
Bram Moolenaardb913952012-06-29 12:54:53 +0200654static PyObject *ConvertToPyObject(typval_T *);
655
656 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200657VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200658{
Bram Moolenaardb913952012-06-29 12:54:53 +0200659 char *expr;
660 typval_T *our_tv;
661 PyObject *result;
662
663 if (!PyArg_ParseTuple(args, "s", &expr))
664 return NULL;
665
666 Py_BEGIN_ALLOW_THREADS
667 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200668 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200669 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200670 Python_Release_Vim();
671 Py_END_ALLOW_THREADS
672
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200673 if (VimTryEnd())
674 return NULL;
675
Bram Moolenaardb913952012-06-29 12:54:53 +0200676 if (our_tv == NULL)
677 {
678 PyErr_SetVim(_("invalid expression"));
679 return NULL;
680 }
681
682 result = ConvertToPyObject(our_tv);
683 Py_BEGIN_ALLOW_THREADS
684 Python_Lock_Vim();
685 free_tv(our_tv);
686 Python_Release_Vim();
687 Py_END_ALLOW_THREADS
688
689 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200690}
691
692 static PyObject *
693VimStrwidth(PyObject *self UNUSED, PyObject *args)
694{
695 char *expr;
696
697 if (!PyArg_ParseTuple(args, "s", &expr))
698 return NULL;
699
Bram Moolenaara54bf402012-12-05 16:30:07 +0100700 return PyLong_FromLong(
701#ifdef FEAT_MBYTE
702 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
703#else
704 STRLEN(expr)
705#endif
706 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200707}
708
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200709/*
710 * Vim module - Definitions
711 */
712
713static struct PyMethodDef VimMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200714 /* name, function, calling, documentation */
715 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
716 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
717 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
718 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
719 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200720};
721
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200722/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200723 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200724 */
725
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200726static PyTypeObject IterType;
727
728typedef PyObject *(*nextfun)(void **);
729typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200730typedef int (*traversefun)(void *, visitproc, void *);
731typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200732
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200733/* Main purpose of this object is removing the need for do python
734 * initialization (i.e. PyType_Ready and setting type attributes) for a big
735 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200736
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200737typedef struct
738{
739 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200740 void *cur;
741 nextfun next;
742 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200743 traversefun traverse;
744 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200745} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200746
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200747 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200748IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
749 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200750{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200751 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200752
Bram Moolenaar774267b2013-05-21 20:51:59 +0200753 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200754 self->cur = start;
755 self->next = next;
756 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200757 self->traverse = traverse;
758 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200759
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200760 return (PyObject *)(self);
761}
762
763 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200764IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200765{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200766 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200767 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200768 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769}
770
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200771 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200772IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200773{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200774 if (self->traverse != NULL)
775 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200776 else
777 return 0;
778}
779
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200780/* Mac OSX defines clear() somewhere. */
781#ifdef clear
782# undef clear
783#endif
784
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200785 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200786IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200787{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200788 if (self->clear != NULL)
789 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200790 else
791 return 0;
792}
793
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200795IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200796{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200797 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798}
799
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200800 static PyObject *
801IterIter(PyObject *self)
802{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +0200803 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200804 return self;
805}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200806
Bram Moolenaardb913952012-06-29 12:54:53 +0200807typedef struct pylinkedlist_S {
808 struct pylinkedlist_S *pll_next;
809 struct pylinkedlist_S *pll_prev;
810 PyObject *pll_obj;
811} pylinkedlist_T;
812
813static pylinkedlist_T *lastdict = NULL;
814static pylinkedlist_T *lastlist = NULL;
815
816 static void
817pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
818{
819 if (ref->pll_prev == NULL)
820 {
821 if (ref->pll_next == NULL)
822 {
823 *last = NULL;
824 return;
825 }
826 }
827 else
828 ref->pll_prev->pll_next = ref->pll_next;
829
830 if (ref->pll_next == NULL)
831 *last = ref->pll_prev;
832 else
833 ref->pll_next->pll_prev = ref->pll_prev;
834}
835
836 static void
837pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
838{
839 if (*last == NULL)
840 ref->pll_prev = NULL;
841 else
842 {
843 (*last)->pll_next = ref;
844 ref->pll_prev = *last;
845 }
846 ref->pll_next = NULL;
847 ref->pll_obj = self;
848 *last = ref;
849}
850
851static PyTypeObject DictionaryType;
852
853typedef struct
854{
855 PyObject_HEAD
856 dict_T *dict;
857 pylinkedlist_T ref;
858} DictionaryObject;
859
Bram Moolenaara9922d62013-05-30 13:01:18 +0200860static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
861
862#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
863
Bram Moolenaardb913952012-06-29 12:54:53 +0200864 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +0200865DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +0200866{
867 DictionaryObject *self;
868
Bram Moolenaara9922d62013-05-30 13:01:18 +0200869 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200870 if (self == NULL)
871 return NULL;
872 self->dict = dict;
873 ++dict->dv_refcount;
874
875 pyll_add((PyObject *)(self), &self->ref, &lastdict);
876
877 return (PyObject *)(self);
878}
879
Bram Moolenaara9922d62013-05-30 13:01:18 +0200880 static dict_T *
881py_dict_alloc()
882{
883 dict_T *r;
884
885 if (!(r = dict_alloc()))
886 {
887 PyErr_NoMemory();
888 return NULL;
889 }
890 ++r->dv_refcount;
891
892 return r;
893}
894
895 static PyObject *
896DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
897{
898 DictionaryObject *self;
899 dict_T *dict;
900
901 if (!(dict = py_dict_alloc()))
902 return NULL;
903
904 self = (DictionaryObject *) DictionaryNew(subtype, dict);
905
906 --dict->dv_refcount;
907
908 if (kwargs || PyTuple_Size(args))
909 {
910 PyObject *tmp;
911 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
912 {
913 Py_DECREF(self);
914 return NULL;
915 }
916
917 Py_DECREF(tmp);
918 }
919
920 return (PyObject *)(self);
921}
922
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200923 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200924DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200925{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200926 pyll_remove(&self->ref, &lastdict);
927 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200928
929 DESTRUCTOR_FINISH(self);
930}
931
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200932static char *DictionaryAttrs[] = {
933 "locked", "scope",
934 NULL
935};
936
937 static PyObject *
938DictionaryDir(PyObject *self)
939{
940 return ObjectDir(self, DictionaryAttrs);
941}
942
Bram Moolenaardb913952012-06-29 12:54:53 +0200943 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200944DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200945{
946 if (val == NULL)
947 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200948 PyErr_SetString(PyExc_AttributeError,
949 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200950 return -1;
951 }
952
953 if (strcmp(name, "locked") == 0)
954 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200955 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200956 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200957 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200958 return -1;
959 }
960 else
961 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200962 int istrue = PyObject_IsTrue(val);
963 if (istrue == -1)
964 return -1;
965 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200966 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200967 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200968 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200969 }
970 return 0;
971 }
972 else
973 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200974 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200975 return -1;
976 }
977}
978
979 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200980DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200981{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200982 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +0200983}
984
Bram Moolenaara9922d62013-05-30 13:01:18 +0200985#define DICT_FLAG_HAS_DEFAULT 0x01
986#define DICT_FLAG_POP 0x02
987#define DICT_FLAG_NONE_DEFAULT 0x04
988#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
989#define DICT_FLAG_RETURN_PAIR 0x10
990
Bram Moolenaardb913952012-06-29 12:54:53 +0200991 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +0200992_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +0200993{
Bram Moolenaara9922d62013-05-30 13:01:18 +0200994 PyObject *keyObject;
995 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
996 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +0200997 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200998 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +0200999 dict_T *dict = self->dict;
1000 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001001 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001002
Bram Moolenaara9922d62013-05-30 13:01:18 +02001003 if (flags & DICT_FLAG_HAS_DEFAULT)
1004 {
1005 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1006 return NULL;
1007 }
1008 else
1009 keyObject = args;
1010
1011 if (flags & DICT_FLAG_RETURN_BOOL)
1012 defObject = Py_False;
1013
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001014 if (!(key = StringToChars(keyObject, &todecref)))
1015 return NULL;
1016
1017 if (*key == NUL)
1018 {
1019 RAISE_NO_EMPTY_KEYS;
1020 return NULL;
1021 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001022
Bram Moolenaara9922d62013-05-30 13:01:18 +02001023 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001024
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001025 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001026
Bram Moolenaara9922d62013-05-30 13:01:18 +02001027 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001028 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001029 if (defObject)
1030 {
1031 Py_INCREF(defObject);
1032 return defObject;
1033 }
1034 else
1035 {
1036 PyErr_SetObject(PyExc_KeyError, keyObject);
1037 return NULL;
1038 }
1039 }
1040 else if (flags & DICT_FLAG_RETURN_BOOL)
1041 {
1042 Py_INCREF(Py_True);
1043 return Py_True;
1044 }
1045
1046 di = dict_lookup(hi);
1047
1048 if (!(r = ConvertToPyObject(&di->di_tv)))
1049 return NULL;
1050
1051 if (flags & DICT_FLAG_POP)
1052 {
1053 if (dict->dv_lock)
1054 {
1055 PyErr_SetVim(_("dict is locked"));
1056 Py_DECREF(r);
1057 return NULL;
1058 }
1059
1060 hash_remove(&dict->dv_hashtab, hi);
1061 dictitem_free(di);
1062 }
1063
1064 if (flags & DICT_FLAG_RETURN_PAIR)
1065 {
1066 PyObject *tmp = r;
1067
1068 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, tmp)))
1069 {
1070 Py_DECREF(tmp);
1071 return NULL;
1072 }
1073 }
1074
1075 return r;
1076}
1077
1078 static PyObject *
1079DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1080{
1081 return _DictionaryItem(self, keyObject, 0);
1082}
1083
1084 static int
1085DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1086{
1087 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1088 int r;
1089
1090 r = (rObj == Py_True);
1091
1092 Py_DECREF(Py_True);
1093
1094 return r;
1095}
1096
1097typedef struct
1098{
1099 hashitem_T *ht_array;
1100 long_u ht_used;
1101 hashtab_T *ht;
1102 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001103 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001104} dictiterinfo_T;
1105
1106 static PyObject *
1107DictionaryIterNext(dictiterinfo_T **dii)
1108{
1109 PyObject *r;
1110
1111 if (!(*dii)->todo)
1112 return NULL;
1113
1114 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1115 (*dii)->ht->ht_used != (*dii)->ht_used)
1116 {
1117 PyErr_SetString(PyExc_RuntimeError,
1118 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001119 return NULL;
1120 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001121
Bram Moolenaara9922d62013-05-30 13:01:18 +02001122 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1123 ++((*dii)->hi);
1124
1125 --((*dii)->todo);
1126
1127 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1128 return NULL;
1129
1130 return r;
1131}
1132
1133 static PyObject *
1134DictionaryIter(DictionaryObject *self)
1135{
1136 dictiterinfo_T *dii;
1137 hashtab_T *ht;
1138
1139 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1140 {
1141 PyErr_NoMemory();
1142 return NULL;
1143 }
1144
1145 ht = &self->dict->dv_hashtab;
1146 dii->ht_array = ht->ht_array;
1147 dii->ht_used = ht->ht_used;
1148 dii->ht = ht;
1149 dii->hi = dii->ht_array;
1150 dii->todo = dii->ht_used;
1151
1152 return IterNew(dii,
1153 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1154 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001155}
1156
1157 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001158DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001159{
1160 char_u *key;
1161 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001162 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001163 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001164 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001165
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001166 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001167 {
1168 PyErr_SetVim(_("dict is locked"));
1169 return -1;
1170 }
1171
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001172 if (!(key = StringToChars(keyObject, &todecref)))
1173 return -1;
1174 if (*key == NUL)
1175 {
1176 RAISE_NO_EMPTY_KEYS;
1177 return -1;
1178 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001179
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001180 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001181
1182 if (valObject == NULL)
1183 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001184 hashitem_T *hi;
1185
Bram Moolenaardb913952012-06-29 12:54:53 +02001186 if (di == NULL)
1187 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001188 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001189 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001190 return -1;
1191 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001192 hi = hash_find(&dict->dv_hashtab, di->di_key);
1193 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001194 dictitem_free(di);
1195 return 0;
1196 }
1197
1198 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001199 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001200
1201 if (di == NULL)
1202 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001203 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001204 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001205 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001206 PyErr_NoMemory();
1207 return -1;
1208 }
1209 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001210 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001211
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001212 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001213 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001214 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001215 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001216 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001217 PyErr_SetVim(_("failed to add key to dictionary"));
1218 return -1;
1219 }
1220 }
1221 else
1222 clear_tv(&di->di_tv);
1223
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001224 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001225
1226 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001227 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001228 return 0;
1229}
1230
Bram Moolenaara9922d62013-05-30 13:01:18 +02001231typedef PyObject *(*hi_to_py)(hashitem_T *);
1232
Bram Moolenaardb913952012-06-29 12:54:53 +02001233 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001234DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001235{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001236 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001237 long_u todo = dict->dv_hashtab.ht_used;
1238 Py_ssize_t i = 0;
1239 PyObject *r;
1240 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001241 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001242
1243 r = PyList_New(todo);
1244 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1245 {
1246 if (!HASHITEM_EMPTY(hi))
1247 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001248 if (!(newObj = hiconvert(hi)))
1249 {
1250 Py_DECREF(r);
1251 return NULL;
1252 }
1253 if (PyList_SetItem(r, i, newObj))
1254 {
1255 Py_DECREF(r);
1256 Py_DECREF(newObj);
1257 return NULL;
1258 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001259 --todo;
1260 ++i;
1261 }
1262 }
1263 return r;
1264}
1265
Bram Moolenaara9922d62013-05-30 13:01:18 +02001266 static PyObject *
1267dict_key(hashitem_T *hi)
1268{
1269 return PyBytes_FromString((char *)(hi->hi_key));
1270}
1271
1272 static PyObject *
1273DictionaryListKeys(DictionaryObject *self)
1274{
1275 return DictionaryListObjects(self, dict_key);
1276}
1277
1278 static PyObject *
1279dict_val(hashitem_T *hi)
1280{
1281 dictitem_T *di;
1282
1283 di = dict_lookup(hi);
1284 return ConvertToPyObject(&di->di_tv);
1285}
1286
1287 static PyObject *
1288DictionaryListValues(DictionaryObject *self)
1289{
1290 return DictionaryListObjects(self, dict_val);
1291}
1292
1293 static PyObject *
1294dict_item(hashitem_T *hi)
1295{
1296 PyObject *keyObject;
1297 PyObject *valObject;
1298 PyObject *r;
1299
1300 if (!(keyObject = dict_key(hi)))
1301 return NULL;
1302
1303 if (!(valObject = dict_val(hi)))
1304 {
1305 Py_DECREF(keyObject);
1306 return NULL;
1307 }
1308
1309 r = Py_BuildValue("(OO)", keyObject, valObject);
1310
1311 Py_DECREF(keyObject);
1312 Py_DECREF(valObject);
1313
1314 return r;
1315}
1316
1317 static PyObject *
1318DictionaryListItems(DictionaryObject *self)
1319{
1320 return DictionaryListObjects(self, dict_item);
1321}
1322
1323 static PyObject *
1324DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1325{
1326 dict_T *dict = self->dict;
1327
1328 if (dict->dv_lock)
1329 {
1330 PyErr_SetVim(_("dict is locked"));
1331 return NULL;
1332 }
1333
1334 if (kwargs)
1335 {
1336 typval_T tv;
1337
1338 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1339 return NULL;
1340
1341 VimTryStart();
1342 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1343 clear_tv(&tv);
1344 if (VimTryEnd())
1345 return NULL;
1346 }
1347 else
1348 {
1349 PyObject *object;
1350
1351 if (!PyArg_Parse(args, "(O)", &object))
1352 return NULL;
1353
1354 if (PyObject_HasAttrString(object, "keys"))
1355 return DictionaryUpdate(self, NULL, object);
1356 else
1357 {
1358 PyObject *iterator;
1359 PyObject *item;
1360
1361 if (!(iterator = PyObject_GetIter(object)))
1362 return NULL;
1363
1364 while ((item = PyIter_Next(iterator)))
1365 {
1366 PyObject *fast;
1367 PyObject *keyObject;
1368 PyObject *valObject;
1369 PyObject *todecref;
1370 char_u *key;
1371 dictitem_T *di;
1372
1373 if (!(fast = PySequence_Fast(item, "")))
1374 {
1375 Py_DECREF(iterator);
1376 Py_DECREF(item);
1377 return NULL;
1378 }
1379
1380 Py_DECREF(item);
1381
1382 if (PySequence_Fast_GET_SIZE(fast) != 2)
1383 {
1384 Py_DECREF(iterator);
1385 Py_DECREF(fast);
1386 PyErr_SetString(PyExc_ValueError,
1387 _("expected sequence element of size 2"));
1388 return NULL;
1389 }
1390
1391 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1392
1393 if (!(key = StringToChars(keyObject, &todecref)))
1394 {
1395 Py_DECREF(iterator);
1396 Py_DECREF(fast);
1397 return NULL;
1398 }
1399
1400 di = dictitem_alloc(key);
1401
1402 Py_XDECREF(todecref);
1403
1404 if (di == NULL)
1405 {
1406 Py_DECREF(fast);
1407 Py_DECREF(iterator);
1408 PyErr_NoMemory();
1409 return NULL;
1410 }
1411 di->di_tv.v_lock = 0;
1412 di->di_tv.v_type = VAR_UNKNOWN;
1413
1414 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1415
1416 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1417 {
1418 Py_DECREF(iterator);
1419 Py_DECREF(fast);
1420 dictitem_free(di);
1421 return NULL;
1422 }
1423
1424 Py_DECREF(fast);
1425
1426 if (dict_add(dict, di) == FAIL)
1427 {
1428 Py_DECREF(iterator);
1429 dictitem_free(di);
1430 PyErr_SetVim(_("failed to add key to dictionary"));
1431 return NULL;
1432 }
1433 }
1434
1435 Py_DECREF(iterator);
1436
1437 /* Iterator may have finished due to an exception */
1438 if (PyErr_Occurred())
1439 return NULL;
1440 }
1441 }
1442 Py_INCREF(Py_None);
1443 return Py_None;
1444}
1445
1446 static PyObject *
1447DictionaryGet(DictionaryObject *self, PyObject *args)
1448{
1449 return _DictionaryItem(self, args,
1450 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1451}
1452
1453 static PyObject *
1454DictionaryPop(DictionaryObject *self, PyObject *args)
1455{
1456 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1457}
1458
1459 static PyObject *
1460DictionaryPopItem(DictionaryObject *self, PyObject *args)
1461{
1462 PyObject *keyObject;
1463
1464 if (!PyArg_ParseTuple(args, "O", &keyObject))
1465 return NULL;
1466
1467 return _DictionaryItem(self, keyObject,
1468 DICT_FLAG_POP|DICT_FLAG_RETURN_PAIR);
1469}
1470
1471 static PyObject *
1472DictionaryHasKey(DictionaryObject *self, PyObject *args)
1473{
1474 PyObject *keyObject;
1475
1476 if (!PyArg_ParseTuple(args, "O", &keyObject))
1477 return NULL;
1478
1479 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1480}
1481
1482static PySequenceMethods DictionaryAsSeq = {
1483 0, /* sq_length */
1484 0, /* sq_concat */
1485 0, /* sq_repeat */
1486 0, /* sq_item */
1487 0, /* sq_slice */
1488 0, /* sq_ass_item */
1489 0, /* sq_ass_slice */
1490 (objobjproc) DictionaryContains, /* sq_contains */
1491 0, /* sq_inplace_concat */
1492 0, /* sq_inplace_repeat */
1493};
1494
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001495static PyMappingMethods DictionaryAsMapping = {
1496 (lenfunc) DictionaryLength,
1497 (binaryfunc) DictionaryItem,
1498 (objobjargproc) DictionaryAssItem,
1499};
1500
Bram Moolenaardb913952012-06-29 12:54:53 +02001501static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001502 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001503 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1504 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1505 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1506 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1507 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
1508 {"popitem", (PyCFunction)DictionaryPopItem, METH_VARARGS, ""},
1509 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001510 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1511 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001512};
1513
1514static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001515static PySequenceMethods ListAsSeq;
1516static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001517
1518typedef struct
1519{
1520 PyObject_HEAD
1521 list_T *list;
1522 pylinkedlist_T ref;
1523} ListObject;
1524
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001525#define NEW_LIST(list) ListNew(&ListType, list)
1526
Bram Moolenaardb913952012-06-29 12:54:53 +02001527 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001528ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001529{
1530 ListObject *self;
1531
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001532 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001533 if (self == NULL)
1534 return NULL;
1535 self->list = list;
1536 ++list->lv_refcount;
1537
1538 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1539
1540 return (PyObject *)(self);
1541}
1542
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001543 static list_T *
1544py_list_alloc()
1545{
1546 list_T *r;
1547
1548 if (!(r = list_alloc()))
1549 {
1550 PyErr_NoMemory();
1551 return NULL;
1552 }
1553 ++r->lv_refcount;
1554
1555 return r;
1556}
1557
1558 static int
1559list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1560{
1561 PyObject *iterator;
1562 PyObject *item;
1563 listitem_T *li;
1564
1565 if (!(iterator = PyObject_GetIter(obj)))
1566 return -1;
1567
1568 while ((item = PyIter_Next(iterator)))
1569 {
1570 if (!(li = listitem_alloc()))
1571 {
1572 PyErr_NoMemory();
1573 Py_DECREF(item);
1574 Py_DECREF(iterator);
1575 return -1;
1576 }
1577 li->li_tv.v_lock = 0;
1578 li->li_tv.v_type = VAR_UNKNOWN;
1579
1580 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1581 {
1582 Py_DECREF(item);
1583 Py_DECREF(iterator);
1584 listitem_free(li);
1585 return -1;
1586 }
1587
1588 Py_DECREF(item);
1589
1590 list_append(l, li);
1591 }
1592
1593 Py_DECREF(iterator);
1594
1595 /* Iterator may have finished due to an exception */
1596 if (PyErr_Occurred())
1597 return -1;
1598
1599 return 0;
1600}
1601
1602 static PyObject *
1603ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1604{
1605 list_T *list;
1606 PyObject *obj = NULL;
1607
1608 if (kwargs)
1609 {
1610 PyErr_SetString(PyExc_TypeError,
1611 _("list constructor does not accept keyword arguments"));
1612 return NULL;
1613 }
1614
1615 if (!PyArg_ParseTuple(args, "|O", &obj))
1616 return NULL;
1617
1618 if (!(list = py_list_alloc()))
1619 return NULL;
1620
1621 if (obj)
1622 {
1623 PyObject *lookup_dict;
1624
1625 if (!(lookup_dict = PyDict_New()))
1626 {
1627 list_unref(list);
1628 return NULL;
1629 }
1630
1631 if (list_py_concat(list, obj, lookup_dict) == -1)
1632 {
1633 Py_DECREF(lookup_dict);
1634 list_unref(list);
1635 return NULL;
1636 }
1637
1638 Py_DECREF(lookup_dict);
1639 }
1640
1641 return ListNew(subtype, list);
1642}
1643
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001644 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001645ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001646{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001647 pyll_remove(&self->ref, &lastlist);
1648 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001649
1650 DESTRUCTOR_FINISH(self);
1651}
1652
Bram Moolenaardb913952012-06-29 12:54:53 +02001653 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001654ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001655{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001656 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001657}
1658
1659 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001660ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001661{
1662 listitem_T *li;
1663
Bram Moolenaard6e39182013-05-21 18:30:34 +02001664 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001665 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001666 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001667 return NULL;
1668 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001669 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001670 if (li == NULL)
1671 {
1672 PyErr_SetVim(_("internal error: failed to get vim list item"));
1673 return NULL;
1674 }
1675 return ConvertToPyObject(&li->li_tv);
1676}
1677
1678#define PROC_RANGE \
1679 if (last < 0) {\
1680 if (last < -size) \
1681 last = 0; \
1682 else \
1683 last += size; \
1684 } \
1685 if (first < 0) \
1686 first = 0; \
1687 if (first > size) \
1688 first = size; \
1689 if (last > size) \
1690 last = size;
1691
1692 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001693ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001694{
1695 PyInt i;
1696 PyInt size = ListLength(self);
1697 PyInt n;
1698 PyObject *list;
1699 int reversed = 0;
1700
1701 PROC_RANGE
1702 if (first >= last)
1703 first = last;
1704
1705 n = last-first;
1706 list = PyList_New(n);
1707 if (list == NULL)
1708 return NULL;
1709
1710 for (i = 0; i < n; ++i)
1711 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001712 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001713 if (item == NULL)
1714 {
1715 Py_DECREF(list);
1716 return NULL;
1717 }
1718
1719 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1720 {
1721 Py_DECREF(item);
1722 Py_DECREF(list);
1723 return NULL;
1724 }
1725 }
1726
1727 return list;
1728}
1729
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001730typedef struct
1731{
1732 listwatch_T lw;
1733 list_T *list;
1734} listiterinfo_T;
1735
1736 static void
1737ListIterDestruct(listiterinfo_T *lii)
1738{
1739 list_rem_watch(lii->list, &lii->lw);
1740 PyMem_Free(lii);
1741}
1742
1743 static PyObject *
1744ListIterNext(listiterinfo_T **lii)
1745{
1746 PyObject *r;
1747
1748 if (!((*lii)->lw.lw_item))
1749 return NULL;
1750
1751 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1752 return NULL;
1753
1754 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1755
1756 return r;
1757}
1758
1759 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001760ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001761{
1762 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001763 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001764
1765 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1766 {
1767 PyErr_NoMemory();
1768 return NULL;
1769 }
1770
1771 list_add_watch(l, &lii->lw);
1772 lii->lw.lw_item = l->lv_first;
1773 lii->list = l;
1774
1775 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001776 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1777 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001778}
1779
Bram Moolenaardb913952012-06-29 12:54:53 +02001780 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001781ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001782{
1783 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001784 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001785 listitem_T *li;
1786 Py_ssize_t length = ListLength(self);
1787
1788 if (l->lv_lock)
1789 {
1790 PyErr_SetVim(_("list is locked"));
1791 return -1;
1792 }
1793 if (index>length || (index==length && obj==NULL))
1794 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001795 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001796 return -1;
1797 }
1798
1799 if (obj == NULL)
1800 {
1801 li = list_find(l, (long) index);
1802 list_remove(l, li, li);
1803 clear_tv(&li->li_tv);
1804 vim_free(li);
1805 return 0;
1806 }
1807
1808 if (ConvertFromPyObject(obj, &tv) == -1)
1809 return -1;
1810
1811 if (index == length)
1812 {
1813 if (list_append_tv(l, &tv) == FAIL)
1814 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001815 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001816 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001817 return -1;
1818 }
1819 }
1820 else
1821 {
1822 li = list_find(l, (long) index);
1823 clear_tv(&li->li_tv);
1824 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001825 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001826 }
1827 return 0;
1828}
1829
1830 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001831ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001832{
1833 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001834 PyObject *iterator;
1835 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02001836 listitem_T *li;
1837 listitem_T *next;
1838 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001839 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001840 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02001841
1842 if (l->lv_lock)
1843 {
1844 PyErr_SetVim(_("list is locked"));
1845 return -1;
1846 }
1847
1848 PROC_RANGE
1849
1850 if (first == size)
1851 li = NULL;
1852 else
1853 {
1854 li = list_find(l, (long) first);
1855 if (li == NULL)
1856 {
1857 PyErr_SetVim(_("internal error: no vim list item"));
1858 return -1;
1859 }
1860 if (last > first)
1861 {
1862 i = last - first;
1863 while (i-- && li != NULL)
1864 {
1865 next = li->li_next;
1866 listitem_remove(l, li);
1867 li = next;
1868 }
1869 }
1870 }
1871
1872 if (obj == NULL)
1873 return 0;
1874
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001875 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001876 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001877
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001878 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001879 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001880 if (ConvertFromPyObject(item, &v) == -1)
1881 {
1882 Py_DECREF(iterator);
1883 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001884 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001885 }
1886 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001887 if (list_insert_tv(l, &v, li) == FAIL)
1888 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001889 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001890 PyErr_SetVim(_("internal error: failed to add item to list"));
1891 return -1;
1892 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001893 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001894 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001895 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02001896 return 0;
1897}
1898
1899 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001900ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001901{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001902 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001903 PyObject *lookup_dict;
1904
1905 if (l->lv_lock)
1906 {
1907 PyErr_SetVim(_("list is locked"));
1908 return NULL;
1909 }
1910
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001911 if (!(lookup_dict = PyDict_New()))
1912 return NULL;
1913
Bram Moolenaardb913952012-06-29 12:54:53 +02001914 if (list_py_concat(l, obj, lookup_dict) == -1)
1915 {
1916 Py_DECREF(lookup_dict);
1917 return NULL;
1918 }
1919 Py_DECREF(lookup_dict);
1920
1921 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001922 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001923}
1924
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001925static char *ListAttrs[] = {
1926 "locked",
1927 NULL
1928};
1929
1930 static PyObject *
1931ListDir(PyObject *self)
1932{
1933 return ObjectDir(self, ListAttrs);
1934}
1935
Bram Moolenaar66b79852012-09-21 14:00:35 +02001936 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001937ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001938{
1939 if (val == NULL)
1940 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001941 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001942 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001943 return -1;
1944 }
1945
1946 if (strcmp(name, "locked") == 0)
1947 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001948 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001949 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001950 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001951 return -1;
1952 }
1953 else
1954 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001955 int istrue = PyObject_IsTrue(val);
1956 if (istrue == -1)
1957 return -1;
1958 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001959 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001960 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001961 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001962 }
1963 return 0;
1964 }
1965 else
1966 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001967 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001968 return -1;
1969 }
1970}
1971
Bram Moolenaardb913952012-06-29 12:54:53 +02001972static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001973 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1974 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
1975 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001976};
1977
1978typedef struct
1979{
1980 PyObject_HEAD
1981 char_u *name;
1982} FunctionObject;
1983
1984static PyTypeObject FunctionType;
1985
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02001986#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
1987
Bram Moolenaardb913952012-06-29 12:54:53 +02001988 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02001989FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02001990{
1991 FunctionObject *self;
1992
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02001993 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
1994
Bram Moolenaardb913952012-06-29 12:54:53 +02001995 if (self == NULL)
1996 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02001997
1998 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02001999 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002000 if (!translated_function_exists(name))
2001 {
2002 PyErr_SetString(PyExc_ValueError,
2003 _("unnamed function does not exist"));
2004 return NULL;
2005 }
2006 self->name = vim_strsave(name);
2007 func_ref(self->name);
2008 }
2009 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002010 if ((self->name = get_expanded_name(name,
2011 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2012 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002013 {
Bram Moolenaar018acca2013-05-30 13:37:28 +02002014 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2015 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002016 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002017
2018 return (PyObject *)(self);
2019}
2020
2021 static PyObject *
2022FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2023{
2024 PyObject *self;
2025 char_u *name;
2026
2027 if (kwargs)
2028 {
2029 PyErr_SetString(PyExc_TypeError,
2030 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002031 return NULL;
2032 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002033
2034 if (!PyArg_ParseTuple(args, "s", &name))
2035 return NULL;
2036
2037 self = FunctionNew(subtype, name);
2038
2039 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002040}
2041
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002042 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002043FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002044{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002045 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002046 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002047
2048 DESTRUCTOR_FINISH(self);
2049}
2050
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002051static char *FunctionAttrs[] = {
2052 "softspace",
2053 NULL
2054};
2055
2056 static PyObject *
2057FunctionDir(PyObject *self)
2058{
2059 return ObjectDir(self, FunctionAttrs);
2060}
2061
Bram Moolenaardb913952012-06-29 12:54:53 +02002062 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002063FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002064{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002065 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002066 typval_T args;
2067 typval_T selfdicttv;
2068 typval_T rettv;
2069 dict_T *selfdict = NULL;
2070 PyObject *selfdictObject;
2071 PyObject *result;
2072 int error;
2073
2074 if (ConvertFromPyObject(argsObject, &args) == -1)
2075 return NULL;
2076
2077 if (kwargs != NULL)
2078 {
2079 selfdictObject = PyDict_GetItemString(kwargs, "self");
2080 if (selfdictObject != NULL)
2081 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002082 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002083 {
2084 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002085 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002086 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002087 selfdict = selfdicttv.vval.v_dict;
2088 }
2089 }
2090
Bram Moolenaar71700b82013-05-15 17:49:05 +02002091 Py_BEGIN_ALLOW_THREADS
2092 Python_Lock_Vim();
2093
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002094 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002095 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002096
2097 Python_Release_Vim();
2098 Py_END_ALLOW_THREADS
2099
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002100 if (VimTryEnd())
2101 result = NULL;
2102 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002103 {
2104 result = NULL;
2105 PyErr_SetVim(_("failed to run function"));
2106 }
2107 else
2108 result = ConvertToPyObject(&rettv);
2109
Bram Moolenaardb913952012-06-29 12:54:53 +02002110 clear_tv(&args);
2111 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002112 if (selfdict != NULL)
2113 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002114
2115 return result;
2116}
2117
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002118 static PyObject *
2119FunctionRepr(FunctionObject *self)
2120{
2121 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2122}
2123
Bram Moolenaardb913952012-06-29 12:54:53 +02002124static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002125 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2126 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002127};
2128
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002129/*
2130 * Options object
2131 */
2132
2133static PyTypeObject OptionsType;
2134
2135typedef int (*checkfun)(void *);
2136
2137typedef struct
2138{
2139 PyObject_HEAD
2140 int opt_type;
2141 void *from;
2142 checkfun Check;
2143 PyObject *fromObj;
2144} OptionsObject;
2145
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002146 static int
2147dummy_check(void *arg UNUSED)
2148{
2149 return 0;
2150}
2151
2152 static PyObject *
2153OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2154{
2155 OptionsObject *self;
2156
Bram Moolenaar774267b2013-05-21 20:51:59 +02002157 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002158 if (self == NULL)
2159 return NULL;
2160
2161 self->opt_type = opt_type;
2162 self->from = from;
2163 self->Check = Check;
2164 self->fromObj = fromObj;
2165 if (fromObj)
2166 Py_INCREF(fromObj);
2167
2168 return (PyObject *)(self);
2169}
2170
2171 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002172OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002173{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002174 PyObject_GC_UnTrack((void *)(self));
2175 Py_XDECREF(self->fromObj);
2176 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002177}
2178
2179 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002180OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002181{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002182 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002183 return 0;
2184}
2185
2186 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002187OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002188{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002189 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002190 return 0;
2191}
2192
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002193 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002194OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002195{
2196 char_u *key;
2197 int flags;
2198 long numval;
2199 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002200 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002201
Bram Moolenaard6e39182013-05-21 18:30:34 +02002202 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002203 return NULL;
2204
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002205 if (!(key = StringToChars(keyObject, &todecref)))
2206 return NULL;
2207 if (*key == NUL)
2208 {
2209 RAISE_NO_EMPTY_KEYS;
2210 return NULL;
2211 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002212
2213 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002214 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002215
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002216 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002217
2218 if (flags == 0)
2219 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002220 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002221 return NULL;
2222 }
2223
2224 if (flags & SOPT_UNSET)
2225 {
2226 Py_INCREF(Py_None);
2227 return Py_None;
2228 }
2229 else if (flags & SOPT_BOOL)
2230 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002231 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002232 r = numval ? Py_True : Py_False;
2233 Py_INCREF(r);
2234 return r;
2235 }
2236 else if (flags & SOPT_NUM)
2237 return PyInt_FromLong(numval);
2238 else if (flags & SOPT_STRING)
2239 {
2240 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002241 {
2242 PyObject *r = PyBytes_FromString((char *) stringval);
2243 vim_free(stringval);
2244 return r;
2245 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002246 else
2247 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002248 PyErr_SetString(PyExc_RuntimeError,
2249 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002250 return NULL;
2251 }
2252 }
2253 else
2254 {
2255 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2256 return NULL;
2257 }
2258}
2259
2260 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002261set_option_value_err(key, numval, stringval, opt_flags)
2262 char_u *key;
2263 int numval;
2264 char_u *stringval;
2265 int opt_flags;
2266{
2267 char_u *errmsg;
2268
2269 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2270 {
2271 if (VimTryEnd())
2272 return FAIL;
2273 PyErr_SetVim((char *)errmsg);
2274 return FAIL;
2275 }
2276 return OK;
2277}
2278
2279 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002280set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2281 char_u *key;
2282 int numval;
2283 char_u *stringval;
2284 int opt_flags;
2285 int opt_type;
2286 void *from;
2287{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002288 win_T *save_curwin = NULL;
2289 tabpage_T *save_curtab = NULL;
2290 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002291 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002292
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002293 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002294 switch (opt_type)
2295 {
2296 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002297 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2298 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002299 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002300 if (VimTryEnd())
2301 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002302 PyErr_SetVim("Problem while switching windows.");
2303 return -1;
2304 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002305 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002306 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002307 if (r == FAIL)
2308 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002309 break;
2310 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002311 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002312 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002313 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002314 if (r == FAIL)
2315 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002316 break;
2317 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002318 r = set_option_value_err(key, numval, stringval, opt_flags);
2319 if (r == FAIL)
2320 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002321 break;
2322 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002323 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002324}
2325
2326 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002327OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002328{
2329 char_u *key;
2330 int flags;
2331 int opt_flags;
2332 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002333 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002334
Bram Moolenaard6e39182013-05-21 18:30:34 +02002335 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002336 return -1;
2337
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002338 if (!(key = StringToChars(keyObject, &todecref)))
2339 return -1;
2340 if (*key == NUL)
2341 {
2342 RAISE_NO_EMPTY_KEYS;
2343 return -1;
2344 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002345
2346 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002347 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002348
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002349 if (flags == 0)
2350 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002351 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002352 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002353 return -1;
2354 }
2355
2356 if (valObject == NULL)
2357 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002358 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002359 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002360 PyErr_SetString(PyExc_ValueError,
2361 _("unable to unset global option"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002362 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002363 return -1;
2364 }
2365 else if (!(flags & SOPT_GLOBAL))
2366 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002367 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2368 "without global value"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002369 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002370 return -1;
2371 }
2372 else
2373 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002374 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002375 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002376 return 0;
2377 }
2378 }
2379
Bram Moolenaard6e39182013-05-21 18:30:34 +02002380 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002381
2382 if (flags & SOPT_BOOL)
2383 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002384 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002385
Bram Moolenaarb983f752013-05-15 16:11:50 +02002386 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002387 r = -1;
2388 else
2389 r = set_option_value_for(key, istrue, NULL,
2390 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002391 }
2392 else if (flags & SOPT_NUM)
2393 {
2394 int val;
2395
2396#if PY_MAJOR_VERSION < 3
2397 if (PyInt_Check(valObject))
2398 val = PyInt_AsLong(valObject);
2399 else
2400#endif
2401 if (PyLong_Check(valObject))
2402 val = PyLong_AsLong(valObject);
2403 else
2404 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002405 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002406 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002407 return -1;
2408 }
2409
2410 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002411 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002412 }
2413 else
2414 {
2415 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002416 PyObject *todecref;
2417
2418 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002419 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002420 r = set_option_value_for(key, 0, val, opt_flags,
2421 self->opt_type, self->from);
2422 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002423 }
2424 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002425 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002426 }
2427
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002428 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002429
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002430 return r;
2431}
2432
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002433static PyMappingMethods OptionsAsMapping = {
2434 (lenfunc) NULL,
2435 (binaryfunc) OptionsItem,
2436 (objobjargproc) OptionsAssItem,
2437};
2438
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002439/* Tabpage object
2440 */
2441
2442typedef struct
2443{
2444 PyObject_HEAD
2445 tabpage_T *tab;
2446} TabPageObject;
2447
2448static PyObject *WinListNew(TabPageObject *tabObject);
2449
2450static PyTypeObject TabPageType;
2451
2452 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002453CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002454{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002455 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002456 {
2457 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2458 return -1;
2459 }
2460
2461 return 0;
2462}
2463
2464 static PyObject *
2465TabPageNew(tabpage_T *tab)
2466{
2467 TabPageObject *self;
2468
2469 if (TAB_PYTHON_REF(tab))
2470 {
2471 self = TAB_PYTHON_REF(tab);
2472 Py_INCREF(self);
2473 }
2474 else
2475 {
2476 self = PyObject_NEW(TabPageObject, &TabPageType);
2477 if (self == NULL)
2478 return NULL;
2479 self->tab = tab;
2480 TAB_PYTHON_REF(tab) = self;
2481 }
2482
2483 return (PyObject *)(self);
2484}
2485
2486 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002487TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002488{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002489 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2490 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002491
2492 DESTRUCTOR_FINISH(self);
2493}
2494
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002495static char *TabPageAttrs[] = {
2496 "windows", "number", "vars", "window", "valid",
2497 NULL
2498};
2499
2500 static PyObject *
2501TabPageDir(PyObject *self)
2502{
2503 return ObjectDir(self, TabPageAttrs);
2504}
2505
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002506 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002507TabPageAttrValid(TabPageObject *self, char *name)
2508{
2509 PyObject *r;
2510
2511 if (strcmp(name, "valid") != 0)
2512 return NULL;
2513
2514 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2515 Py_INCREF(r);
2516 return r;
2517}
2518
2519 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002520TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002521{
2522 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002523 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002524 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002525 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002526 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002527 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002528 else if (strcmp(name, "window") == 0)
2529 {
2530 /* For current tab window.c does not bother to set or update tp_curwin
2531 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002532 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002533 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002534 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002535 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002536 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002537 else if (strcmp(name, "__members__") == 0)
2538 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002539 return NULL;
2540}
2541
2542 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002543TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002544{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002545 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002546 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002547 else
2548 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002549 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002550
2551 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002552 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2553 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002554 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002555 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002556 }
2557}
2558
2559static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002560 /* name, function, calling, documentation */
2561 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2562 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002563};
2564
2565/*
2566 * Window list object
2567 */
2568
2569static PyTypeObject TabListType;
2570static PySequenceMethods TabListAsSeq;
2571
2572typedef struct
2573{
2574 PyObject_HEAD
2575} TabListObject;
2576
2577 static PyInt
2578TabListLength(PyObject *self UNUSED)
2579{
2580 tabpage_T *tp = first_tabpage;
2581 PyInt n = 0;
2582
2583 while (tp != NULL)
2584 {
2585 ++n;
2586 tp = tp->tp_next;
2587 }
2588
2589 return n;
2590}
2591
2592 static PyObject *
2593TabListItem(PyObject *self UNUSED, PyInt n)
2594{
2595 tabpage_T *tp;
2596
2597 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2598 if (n == 0)
2599 return TabPageNew(tp);
2600
2601 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2602 return NULL;
2603}
2604
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002605/* Window object
2606 */
2607
2608typedef struct
2609{
2610 PyObject_HEAD
2611 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002612 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002613} WindowObject;
2614
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002615static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002616
2617 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002618CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002619{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002620 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002621 {
2622 PyErr_SetVim(_("attempt to refer to deleted window"));
2623 return -1;
2624 }
2625
2626 return 0;
2627}
2628
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002629 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002630WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002631{
2632 /* We need to handle deletion of windows underneath us.
2633 * If we add a "w_python*_ref" field to the win_T structure,
2634 * then we can get at it in win_free() in vim. We then
2635 * need to create only ONE Python object per window - if
2636 * we try to create a second, just INCREF the existing one
2637 * and return it. The (single) Python object referring to
2638 * the window is stored in "w_python*_ref".
2639 * On a win_free() we set the Python object's win_T* field
2640 * to an invalid value. We trap all uses of a window
2641 * object, and reject them if the win_T* field is invalid.
2642 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002643 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002644 * w_python_ref and w_python3_ref fields respectively.
2645 */
2646
2647 WindowObject *self;
2648
2649 if (WIN_PYTHON_REF(win))
2650 {
2651 self = WIN_PYTHON_REF(win);
2652 Py_INCREF(self);
2653 }
2654 else
2655 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002656 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002657 if (self == NULL)
2658 return NULL;
2659 self->win = win;
2660 WIN_PYTHON_REF(win) = self;
2661 }
2662
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002663 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2664
Bram Moolenaar971db462013-05-12 18:44:48 +02002665 return (PyObject *)(self);
2666}
2667
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002668 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002669WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002670{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002671 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002672 if (self->win && self->win != INVALID_WINDOW_VALUE)
2673 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002674 Py_XDECREF(((PyObject *)(self->tabObject)));
2675 PyObject_GC_Del((void *)(self));
2676}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002677
Bram Moolenaar774267b2013-05-21 20:51:59 +02002678 static int
2679WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2680{
2681 Py_VISIT(((PyObject *)(self->tabObject)));
2682 return 0;
2683}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002684
Bram Moolenaar774267b2013-05-21 20:51:59 +02002685 static int
2686WindowClear(WindowObject *self)
2687{
2688 Py_CLEAR(self->tabObject);
2689 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002690}
2691
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002692 static win_T *
2693get_firstwin(TabPageObject *tabObject)
2694{
2695 if (tabObject)
2696 {
2697 if (CheckTabPage(tabObject))
2698 return NULL;
2699 /* For current tab window.c does not bother to set or update tp_firstwin
2700 */
2701 else if (tabObject->tab == curtab)
2702 return firstwin;
2703 else
2704 return tabObject->tab->tp_firstwin;
2705 }
2706 else
2707 return firstwin;
2708}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002709static char *WindowAttrs[] = {
2710 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2711 "tabpage", "valid",
2712 NULL
2713};
2714
2715 static PyObject *
2716WindowDir(PyObject *self)
2717{
2718 return ObjectDir(self, WindowAttrs);
2719}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002720
Bram Moolenaar971db462013-05-12 18:44:48 +02002721 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002722WindowAttrValid(WindowObject *self, char *name)
2723{
2724 PyObject *r;
2725
2726 if (strcmp(name, "valid") != 0)
2727 return NULL;
2728
2729 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2730 Py_INCREF(r);
2731 return r;
2732}
2733
2734 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002735WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002736{
2737 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002738 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002739 else if (strcmp(name, "cursor") == 0)
2740 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002741 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002742
2743 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2744 }
2745 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002746 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002747#ifdef FEAT_WINDOWS
2748 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002749 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002750#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002751#ifdef FEAT_VERTSPLIT
2752 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002753 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002754 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002755 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002756#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002757 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002758 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002759 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002760 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2761 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002762 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002763 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002764 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002765 return NULL;
2766 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002767 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002768 }
2769 else if (strcmp(name, "tabpage") == 0)
2770 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002771 Py_INCREF(self->tabObject);
2772 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002773 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002774 else if (strcmp(name, "__members__") == 0)
2775 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002776 else
2777 return NULL;
2778}
2779
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002780 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002781WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002782{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002783 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002784 return -1;
2785
2786 if (strcmp(name, "buffer") == 0)
2787 {
2788 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2789 return -1;
2790 }
2791 else if (strcmp(name, "cursor") == 0)
2792 {
2793 long lnum;
2794 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002795
2796 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2797 return -1;
2798
Bram Moolenaard6e39182013-05-21 18:30:34 +02002799 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002800 {
2801 PyErr_SetVim(_("cursor position outside buffer"));
2802 return -1;
2803 }
2804
2805 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002806 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002807 return -1;
2808
Bram Moolenaard6e39182013-05-21 18:30:34 +02002809 self->win->w_cursor.lnum = lnum;
2810 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002811#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002812 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002813#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002814 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002815 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002816
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002817 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002818 return 0;
2819 }
2820 else if (strcmp(name, "height") == 0)
2821 {
2822 int height;
2823 win_T *savewin;
2824
2825 if (!PyArg_Parse(val, "i", &height))
2826 return -1;
2827
2828#ifdef FEAT_GUI
2829 need_mouse_correct = TRUE;
2830#endif
2831 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002832 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002833
2834 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002835 win_setheight(height);
2836 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002837 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002838 return -1;
2839
2840 return 0;
2841 }
2842#ifdef FEAT_VERTSPLIT
2843 else if (strcmp(name, "width") == 0)
2844 {
2845 int width;
2846 win_T *savewin;
2847
2848 if (!PyArg_Parse(val, "i", &width))
2849 return -1;
2850
2851#ifdef FEAT_GUI
2852 need_mouse_correct = TRUE;
2853#endif
2854 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002855 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002856
2857 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002858 win_setwidth(width);
2859 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002860 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002861 return -1;
2862
2863 return 0;
2864 }
2865#endif
2866 else
2867 {
2868 PyErr_SetString(PyExc_AttributeError, name);
2869 return -1;
2870 }
2871}
2872
2873 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002874WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002875{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002876 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002877 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002878 else
2879 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002880 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002881
Bram Moolenaar6d216452013-05-12 19:00:41 +02002882 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002883 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002884 (self));
2885 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002886 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002887 }
2888}
2889
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002890static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002891 /* name, function, calling, documentation */
2892 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2893 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002894};
2895
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002896/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002897 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002898 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002899
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002900static PyTypeObject WinListType;
2901static PySequenceMethods WinListAsSeq;
2902
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002903typedef struct
2904{
2905 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002906 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002907} WinListObject;
2908
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002909 static PyObject *
2910WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002911{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002912 WinListObject *self;
2913
2914 self = PyObject_NEW(WinListObject, &WinListType);
2915 self->tabObject = tabObject;
2916 Py_INCREF(tabObject);
2917
2918 return (PyObject *)(self);
2919}
2920
2921 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002922WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002923{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002924 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002925
2926 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02002927 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002928 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02002929 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002930
2931 DESTRUCTOR_FINISH(self);
2932}
2933
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002934 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002935WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002936{
2937 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002938 PyInt n = 0;
2939
Bram Moolenaard6e39182013-05-21 18:30:34 +02002940 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002941 return -1;
2942
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002943 while (w != NULL)
2944 {
2945 ++n;
2946 w = W_NEXT(w);
2947 }
2948
2949 return n;
2950}
2951
2952 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002953WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002954{
2955 win_T *w;
2956
Bram Moolenaard6e39182013-05-21 18:30:34 +02002957 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002958 return NULL;
2959
2960 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002961 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002962 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002963
2964 PyErr_SetString(PyExc_IndexError, _("no such window"));
2965 return NULL;
2966}
2967
2968/* Convert a Python string into a Vim line.
2969 *
2970 * The result is in allocated memory. All internal nulls are replaced by
2971 * newline characters. It is an error for the string to contain newline
2972 * characters.
2973 *
2974 * On errors, the Python exception data is set, and NULL is returned.
2975 */
2976 static char *
2977StringToLine(PyObject *obj)
2978{
2979 const char *str;
2980 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002981 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002982 PyInt len;
2983 PyInt i;
2984 char *p;
2985
2986 if (obj == NULL || !PyString_Check(obj))
2987 {
2988 PyErr_BadArgument();
2989 return NULL;
2990 }
2991
Bram Moolenaar19e60942011-06-19 00:27:51 +02002992 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2993 str = PyString_AsString(bytes);
2994 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002995
2996 /*
2997 * Error checking: String must not contain newlines, as we
2998 * are replacing a single line, and we must replace it with
2999 * a single line.
3000 * A trailing newline is removed, so that append(f.readlines()) works.
3001 */
3002 p = memchr(str, '\n', len);
3003 if (p != NULL)
3004 {
3005 if (p == str + len - 1)
3006 --len;
3007 else
3008 {
3009 PyErr_SetVim(_("string cannot contain newlines"));
3010 return NULL;
3011 }
3012 }
3013
3014 /* Create a copy of the string, with internal nulls replaced by
3015 * newline characters, as is the vim convention.
3016 */
3017 save = (char *)alloc((unsigned)(len+1));
3018 if (save == NULL)
3019 {
3020 PyErr_NoMemory();
3021 return NULL;
3022 }
3023
3024 for (i = 0; i < len; ++i)
3025 {
3026 if (str[i] == '\0')
3027 save[i] = '\n';
3028 else
3029 save[i] = str[i];
3030 }
3031
3032 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003033 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003034
3035 return save;
3036}
3037
3038/* Get a line from the specified buffer. The line number is
3039 * in Vim format (1-based). The line is returned as a Python
3040 * string object.
3041 */
3042 static PyObject *
3043GetBufferLine(buf_T *buf, PyInt n)
3044{
3045 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3046}
3047
3048
3049/* Get a list of lines from the specified buffer. The line numbers
3050 * are in Vim format (1-based). The range is from lo up to, but not
3051 * including, hi. The list is returned as a Python list of string objects.
3052 */
3053 static PyObject *
3054GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3055{
3056 PyInt i;
3057 PyInt n = hi - lo;
3058 PyObject *list = PyList_New(n);
3059
3060 if (list == NULL)
3061 return NULL;
3062
3063 for (i = 0; i < n; ++i)
3064 {
3065 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3066
3067 /* Error check - was the Python string creation OK? */
3068 if (str == NULL)
3069 {
3070 Py_DECREF(list);
3071 return NULL;
3072 }
3073
3074 /* Set the list item */
3075 if (PyList_SetItem(list, i, str))
3076 {
3077 Py_DECREF(str);
3078 Py_DECREF(list);
3079 return NULL;
3080 }
3081 }
3082
3083 /* The ownership of the Python list is passed to the caller (ie,
3084 * the caller should Py_DECREF() the object when it is finished
3085 * with it).
3086 */
3087
3088 return list;
3089}
3090
3091/*
3092 * Check if deleting lines made the cursor position invalid.
3093 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3094 * deleted).
3095 */
3096 static void
3097py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3098{
3099 if (curwin->w_cursor.lnum >= lo)
3100 {
3101 /* Adjust the cursor position if it's in/after the changed
3102 * lines. */
3103 if (curwin->w_cursor.lnum >= hi)
3104 {
3105 curwin->w_cursor.lnum += extra;
3106 check_cursor_col();
3107 }
3108 else if (extra < 0)
3109 {
3110 curwin->w_cursor.lnum = lo;
3111 check_cursor();
3112 }
3113 else
3114 check_cursor_col();
3115 changed_cline_bef_curs();
3116 }
3117 invalidate_botline();
3118}
3119
Bram Moolenaar19e60942011-06-19 00:27:51 +02003120/*
3121 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003122 * in Vim format (1-based). The replacement line is given as
3123 * a Python string object. The object is checked for validity
3124 * and correct format. Errors are returned as a value of FAIL.
3125 * The return value is OK on success.
3126 * If OK is returned and len_change is not NULL, *len_change
3127 * is set to the change in the buffer length.
3128 */
3129 static int
3130SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3131{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003132 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003133 * There are three cases:
3134 * 1. NULL, or None - this is a deletion.
3135 * 2. A string - this is a replacement.
3136 * 3. Anything else - this is an error.
3137 */
3138 if (line == Py_None || line == NULL)
3139 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003140 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003141
3142 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003143 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003144
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003145 VimTryStart();
3146
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003147 if (u_savedel((linenr_T)n, 1L) == FAIL)
3148 PyErr_SetVim(_("cannot save undo information"));
3149 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3150 PyErr_SetVim(_("cannot delete line"));
3151 else
3152 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003153 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003154 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3155 deleted_lines_mark((linenr_T)n, 1L);
3156 }
3157
Bram Moolenaar105bc352013-05-17 16:03:57 +02003158 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003159
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003160 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003161 return FAIL;
3162
3163 if (len_change)
3164 *len_change = -1;
3165
3166 return OK;
3167 }
3168 else if (PyString_Check(line))
3169 {
3170 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003171 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003172
3173 if (save == NULL)
3174 return FAIL;
3175
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003176 VimTryStart();
3177
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003178 /* We do not need to free "save" if ml_replace() consumes it. */
3179 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003180 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003181
3182 if (u_savesub((linenr_T)n) == FAIL)
3183 {
3184 PyErr_SetVim(_("cannot save undo information"));
3185 vim_free(save);
3186 }
3187 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3188 {
3189 PyErr_SetVim(_("cannot replace line"));
3190 vim_free(save);
3191 }
3192 else
3193 changed_bytes((linenr_T)n, 0);
3194
Bram Moolenaar105bc352013-05-17 16:03:57 +02003195 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003196
3197 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003198 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003199 check_cursor_col();
3200
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003201 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003202 return FAIL;
3203
3204 if (len_change)
3205 *len_change = 0;
3206
3207 return OK;
3208 }
3209 else
3210 {
3211 PyErr_BadArgument();
3212 return FAIL;
3213 }
3214}
3215
Bram Moolenaar19e60942011-06-19 00:27:51 +02003216/* Replace a range of lines in the specified buffer. The line numbers are in
3217 * Vim format (1-based). The range is from lo up to, but not including, hi.
3218 * The replacement lines are given as a Python list of string objects. The
3219 * list is checked for validity and correct format. Errors are returned as a
3220 * value of FAIL. The return value is OK on success.
3221 * If OK is returned and len_change is not NULL, *len_change
3222 * is set to the change in the buffer length.
3223 */
3224 static int
3225SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3226{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003227 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003228 * There are three cases:
3229 * 1. NULL, or None - this is a deletion.
3230 * 2. A list - this is a replacement.
3231 * 3. Anything else - this is an error.
3232 */
3233 if (list == Py_None || list == NULL)
3234 {
3235 PyInt i;
3236 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003237 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003238
3239 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003240 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003241 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003242
3243 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3244 PyErr_SetVim(_("cannot save undo information"));
3245 else
3246 {
3247 for (i = 0; i < n; ++i)
3248 {
3249 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3250 {
3251 PyErr_SetVim(_("cannot delete line"));
3252 break;
3253 }
3254 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003255 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003256 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3257 deleted_lines_mark((linenr_T)lo, (long)i);
3258 }
3259
Bram Moolenaar105bc352013-05-17 16:03:57 +02003260 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003261
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003262 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003263 return FAIL;
3264
3265 if (len_change)
3266 *len_change = -n;
3267
3268 return OK;
3269 }
3270 else if (PyList_Check(list))
3271 {
3272 PyInt i;
3273 PyInt new_len = PyList_Size(list);
3274 PyInt old_len = hi - lo;
3275 PyInt extra = 0; /* lines added to text, can be negative */
3276 char **array;
3277 buf_T *savebuf;
3278
3279 if (new_len == 0) /* avoid allocating zero bytes */
3280 array = NULL;
3281 else
3282 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003283 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003284 if (array == NULL)
3285 {
3286 PyErr_NoMemory();
3287 return FAIL;
3288 }
3289 }
3290
3291 for (i = 0; i < new_len; ++i)
3292 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003293 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003294
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003295 if (!(line = PyList_GetItem(list, i)) ||
3296 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003297 {
3298 while (i)
3299 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003300 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003301 return FAIL;
3302 }
3303 }
3304
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003305 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003306 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003307
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003308 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003309 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003310
3311 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3312 PyErr_SetVim(_("cannot save undo information"));
3313
3314 /* If the size of the range is reducing (ie, new_len < old_len) we
3315 * need to delete some old_len. We do this at the start, by
3316 * repeatedly deleting line "lo".
3317 */
3318 if (!PyErr_Occurred())
3319 {
3320 for (i = 0; i < old_len - new_len; ++i)
3321 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3322 {
3323 PyErr_SetVim(_("cannot delete line"));
3324 break;
3325 }
3326 extra -= i;
3327 }
3328
3329 /* For as long as possible, replace the existing old_len with the
3330 * new old_len. This is a more efficient operation, as it requires
3331 * less memory allocation and freeing.
3332 */
3333 if (!PyErr_Occurred())
3334 {
3335 for (i = 0; i < old_len && i < new_len; ++i)
3336 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3337 == FAIL)
3338 {
3339 PyErr_SetVim(_("cannot replace line"));
3340 break;
3341 }
3342 }
3343 else
3344 i = 0;
3345
3346 /* Now we may need to insert the remaining new old_len. If we do, we
3347 * must free the strings as we finish with them (we can't pass the
3348 * responsibility to vim in this case).
3349 */
3350 if (!PyErr_Occurred())
3351 {
3352 while (i < new_len)
3353 {
3354 if (ml_append((linenr_T)(lo + i - 1),
3355 (char_u *)array[i], 0, FALSE) == FAIL)
3356 {
3357 PyErr_SetVim(_("cannot insert line"));
3358 break;
3359 }
3360 vim_free(array[i]);
3361 ++i;
3362 ++extra;
3363 }
3364 }
3365
3366 /* Free any left-over old_len, as a result of an error */
3367 while (i < new_len)
3368 {
3369 vim_free(array[i]);
3370 ++i;
3371 }
3372
3373 /* Free the array of old_len. All of its contents have now
3374 * been dealt with (either freed, or the responsibility passed
3375 * to vim.
3376 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003377 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003378
3379 /* Adjust marks. Invalidate any which lie in the
3380 * changed range, and move any in the remainder of the buffer.
3381 */
3382 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3383 (long)MAXLNUM, (long)extra);
3384 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3385
Bram Moolenaar105bc352013-05-17 16:03:57 +02003386 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003387 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3388
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003389 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003390 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003391
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003392 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003393 return FAIL;
3394
3395 if (len_change)
3396 *len_change = new_len - old_len;
3397
3398 return OK;
3399 }
3400 else
3401 {
3402 PyErr_BadArgument();
3403 return FAIL;
3404 }
3405}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003406
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003407/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003408 * The line number is in Vim format (1-based). The lines to be inserted are
3409 * given as a Python list of string objects or as a single string. The lines
3410 * to be added are checked for validity and correct format. Errors are
3411 * returned as a value of FAIL. The return value is OK on success.
3412 * If OK is returned and len_change is not NULL, *len_change
3413 * is set to the change in the buffer length.
3414 */
3415 static int
3416InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3417{
3418 /* First of all, we check the type of the supplied Python object.
3419 * It must be a string or a list, or the call is in error.
3420 */
3421 if (PyString_Check(lines))
3422 {
3423 char *str = StringToLine(lines);
3424 buf_T *savebuf;
3425
3426 if (str == NULL)
3427 return FAIL;
3428
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003429 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003430 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003431 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003432
3433 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3434 PyErr_SetVim(_("cannot save undo information"));
3435 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3436 PyErr_SetVim(_("cannot insert line"));
3437 else
3438 appended_lines_mark((linenr_T)n, 1L);
3439
3440 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003441 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003442 update_screen(VALID);
3443
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003444 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003445 return FAIL;
3446
3447 if (len_change)
3448 *len_change = 1;
3449
3450 return OK;
3451 }
3452 else if (PyList_Check(lines))
3453 {
3454 PyInt i;
3455 PyInt size = PyList_Size(lines);
3456 char **array;
3457 buf_T *savebuf;
3458
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003459 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003460 if (array == NULL)
3461 {
3462 PyErr_NoMemory();
3463 return FAIL;
3464 }
3465
3466 for (i = 0; i < size; ++i)
3467 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003468 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003469
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003470 if (!(line = PyList_GetItem(lines, i)) ||
3471 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003472 {
3473 while (i)
3474 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003475 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003476 return FAIL;
3477 }
3478 }
3479
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003480 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003481 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003482 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003483
3484 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3485 PyErr_SetVim(_("cannot save undo information"));
3486 else
3487 {
3488 for (i = 0; i < size; ++i)
3489 {
3490 if (ml_append((linenr_T)(n + i),
3491 (char_u *)array[i], 0, FALSE) == FAIL)
3492 {
3493 PyErr_SetVim(_("cannot insert line"));
3494
3495 /* Free the rest of the lines */
3496 while (i < size)
3497 vim_free(array[i++]);
3498
3499 break;
3500 }
3501 vim_free(array[i]);
3502 }
3503 if (i > 0)
3504 appended_lines_mark((linenr_T)n, (long)i);
3505 }
3506
3507 /* Free the array of lines. All of its contents have now
3508 * been freed.
3509 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003510 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003511
Bram Moolenaar105bc352013-05-17 16:03:57 +02003512 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003513 update_screen(VALID);
3514
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003515 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003516 return FAIL;
3517
3518 if (len_change)
3519 *len_change = size;
3520
3521 return OK;
3522 }
3523 else
3524 {
3525 PyErr_BadArgument();
3526 return FAIL;
3527 }
3528}
3529
3530/*
3531 * Common routines for buffers and line ranges
3532 * -------------------------------------------
3533 */
3534
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003535typedef struct
3536{
3537 PyObject_HEAD
3538 buf_T *buf;
3539} BufferObject;
3540
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003541 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003542CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003543{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003544 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003545 {
3546 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3547 return -1;
3548 }
3549
3550 return 0;
3551}
3552
3553 static PyObject *
3554RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3555{
3556 if (CheckBuffer(self))
3557 return NULL;
3558
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003559 if (end == -1)
3560 end = self->buf->b_ml.ml_line_count;
3561
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003562 if (n < 0)
3563 n += end - start + 1;
3564
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003565 if (n < 0 || n > end - start)
3566 {
3567 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3568 return NULL;
3569 }
3570
3571 return GetBufferLine(self->buf, n+start);
3572}
3573
3574 static PyObject *
3575RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3576{
3577 PyInt size;
3578
3579 if (CheckBuffer(self))
3580 return NULL;
3581
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003582 if (end == -1)
3583 end = self->buf->b_ml.ml_line_count;
3584
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003585 size = end - start + 1;
3586
3587 if (lo < 0)
3588 lo = 0;
3589 else if (lo > size)
3590 lo = size;
3591 if (hi < 0)
3592 hi = 0;
3593 if (hi < lo)
3594 hi = lo;
3595 else if (hi > size)
3596 hi = size;
3597
3598 return GetBufferLineList(self->buf, lo+start, hi+start);
3599}
3600
3601 static PyInt
3602RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3603{
3604 PyInt len_change;
3605
3606 if (CheckBuffer(self))
3607 return -1;
3608
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003609 if (end == -1)
3610 end = self->buf->b_ml.ml_line_count;
3611
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003612 if (n < 0)
3613 n += end - start + 1;
3614
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003615 if (n < 0 || n > end - start)
3616 {
3617 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3618 return -1;
3619 }
3620
3621 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3622 return -1;
3623
3624 if (new_end)
3625 *new_end = end + len_change;
3626
3627 return 0;
3628}
3629
Bram Moolenaar19e60942011-06-19 00:27:51 +02003630 static PyInt
3631RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3632{
3633 PyInt size;
3634 PyInt len_change;
3635
3636 /* Self must be a valid buffer */
3637 if (CheckBuffer(self))
3638 return -1;
3639
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003640 if (end == -1)
3641 end = self->buf->b_ml.ml_line_count;
3642
Bram Moolenaar19e60942011-06-19 00:27:51 +02003643 /* Sort out the slice range */
3644 size = end - start + 1;
3645
3646 if (lo < 0)
3647 lo = 0;
3648 else if (lo > size)
3649 lo = size;
3650 if (hi < 0)
3651 hi = 0;
3652 if (hi < lo)
3653 hi = lo;
3654 else if (hi > size)
3655 hi = size;
3656
3657 if (SetBufferLineList(self->buf, lo + start, hi + start,
3658 val, &len_change) == FAIL)
3659 return -1;
3660
3661 if (new_end)
3662 *new_end = end + len_change;
3663
3664 return 0;
3665}
3666
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003667
3668 static PyObject *
3669RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3670{
3671 PyObject *lines;
3672 PyInt len_change;
3673 PyInt max;
3674 PyInt n;
3675
3676 if (CheckBuffer(self))
3677 return NULL;
3678
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003679 if (end == -1)
3680 end = self->buf->b_ml.ml_line_count;
3681
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003682 max = n = end - start + 1;
3683
3684 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3685 return NULL;
3686
3687 if (n < 0 || n > max)
3688 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003689 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003690 return NULL;
3691 }
3692
3693 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3694 return NULL;
3695
3696 if (new_end)
3697 *new_end = end + len_change;
3698
3699 Py_INCREF(Py_None);
3700 return Py_None;
3701}
3702
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003703/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003704 */
3705
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003706static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003707static PySequenceMethods RangeAsSeq;
3708static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003709
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003710typedef struct
3711{
3712 PyObject_HEAD
3713 BufferObject *buf;
3714 PyInt start;
3715 PyInt end;
3716} RangeObject;
3717
3718 static PyObject *
3719RangeNew(buf_T *buf, PyInt start, PyInt end)
3720{
3721 BufferObject *bufr;
3722 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003723 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003724 if (self == NULL)
3725 return NULL;
3726
3727 bufr = (BufferObject *)BufferNew(buf);
3728 if (bufr == NULL)
3729 {
3730 Py_DECREF(self);
3731 return NULL;
3732 }
3733 Py_INCREF(bufr);
3734
3735 self->buf = bufr;
3736 self->start = start;
3737 self->end = end;
3738
3739 return (PyObject *)(self);
3740}
3741
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003742 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003743RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003744{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003745 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003746 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003747 PyObject_GC_Del((void *)(self));
3748}
3749
3750 static int
3751RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3752{
3753 Py_VISIT(((PyObject *)(self->buf)));
3754 return 0;
3755}
3756
3757 static int
3758RangeClear(RangeObject *self)
3759{
3760 Py_CLEAR(self->buf);
3761 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003762}
3763
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003764 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003765RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003766{
3767 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003768 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003769 return -1; /* ??? */
3770
Bram Moolenaard6e39182013-05-21 18:30:34 +02003771 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003772}
3773
3774 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003775RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003776{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003777 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003778}
3779
3780 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003781RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003782{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003783 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003784}
3785
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003786static char *RangeAttrs[] = {
3787 "start", "end",
3788 NULL
3789};
3790
3791 static PyObject *
3792RangeDir(PyObject *self)
3793{
3794 return ObjectDir(self, RangeAttrs);
3795}
3796
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003797 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003798RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003799{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003800 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003801}
3802
3803 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003804RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003805{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003806 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003807 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
3808 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003809 else
3810 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003811 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003812
3813 if (name == NULL)
3814 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003815
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003816 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02003817 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003818 }
3819}
3820
3821static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003822 /* name, function, calling, documentation */
3823 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003824 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3825 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003826};
3827
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003828static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003829static PySequenceMethods BufferAsSeq;
3830static PyMappingMethods BufferAsMapping;
3831
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003832 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003833BufferNew(buf_T *buf)
3834{
3835 /* We need to handle deletion of buffers underneath us.
3836 * If we add a "b_python*_ref" field to the buf_T structure,
3837 * then we can get at it in buf_freeall() in vim. We then
3838 * need to create only ONE Python object per buffer - if
3839 * we try to create a second, just INCREF the existing one
3840 * and return it. The (single) Python object referring to
3841 * the buffer is stored in "b_python*_ref".
3842 * Question: what to do on a buf_freeall(). We'll probably
3843 * have to either delete the Python object (DECREF it to
3844 * zero - a bad idea, as it leaves dangling refs!) or
3845 * set the buf_T * value to an invalid value (-1?), which
3846 * means we need checks in all access functions... Bah.
3847 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003848 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003849 * b_python_ref and b_python3_ref fields respectively.
3850 */
3851
3852 BufferObject *self;
3853
3854 if (BUF_PYTHON_REF(buf) != NULL)
3855 {
3856 self = BUF_PYTHON_REF(buf);
3857 Py_INCREF(self);
3858 }
3859 else
3860 {
3861 self = PyObject_NEW(BufferObject, &BufferType);
3862 if (self == NULL)
3863 return NULL;
3864 self->buf = buf;
3865 BUF_PYTHON_REF(buf) = self;
3866 }
3867
3868 return (PyObject *)(self);
3869}
3870
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003871 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003872BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003873{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003874 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3875 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003876
3877 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003878}
3879
Bram Moolenaar971db462013-05-12 18:44:48 +02003880 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003881BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003882{
3883 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003884 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003885 return -1; /* ??? */
3886
Bram Moolenaard6e39182013-05-21 18:30:34 +02003887 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003888}
3889
3890 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003891BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003892{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003893 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003894}
3895
3896 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003897BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003898{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003899 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003900}
3901
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003902static char *BufferAttrs[] = {
3903 "name", "number", "vars", "options", "valid",
3904 NULL
3905};
3906
3907 static PyObject *
3908BufferDir(PyObject *self)
3909{
3910 return ObjectDir(self, BufferAttrs);
3911}
3912
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003913 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003914BufferAttrValid(BufferObject *self, char *name)
3915{
3916 PyObject *r;
3917
3918 if (strcmp(name, "valid") != 0)
3919 return NULL;
3920
3921 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
3922 Py_INCREF(r);
3923 return r;
3924}
3925
3926 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003927BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003928{
3929 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02003930 return PyString_FromString((self->buf->b_ffname == NULL
3931 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003932 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003933 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003934 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003935 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003936 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003937 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3938 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003939 else if (strcmp(name, "__members__") == 0)
3940 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003941 else
3942 return NULL;
3943}
3944
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003945 static int
3946BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3947{
3948 if (CheckBuffer(self))
3949 return -1;
3950
3951 if (strcmp(name, "name") == 0)
3952 {
3953 char_u *val;
3954 aco_save_T aco;
3955 int r;
3956 PyObject *todecref;
3957
3958 if (!(val = StringToChars(valObject, &todecref)))
3959 return -1;
3960
3961 VimTryStart();
3962 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3963 aucmd_prepbuf(&aco, self->buf);
3964 r = rename_buffer(val);
3965 aucmd_restbuf(&aco);
3966 Py_XDECREF(todecref);
3967 if (VimTryEnd())
3968 return -1;
3969
3970 if (r == FAIL)
3971 {
3972 PyErr_SetVim(_("failed to rename buffer"));
3973 return -1;
3974 }
3975 return 0;
3976 }
3977 else
3978 {
3979 PyErr_SetString(PyExc_AttributeError, name);
3980 return -1;
3981 }
3982}
3983
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003984 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003985BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003986{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003987 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003988}
3989
3990 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003991BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003992{
3993 pos_T *posp;
3994 char *pmark;
3995 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003996 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003997
Bram Moolenaard6e39182013-05-21 18:30:34 +02003998 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003999 return NULL;
4000
4001 if (!PyArg_ParseTuple(args, "s", &pmark))
4002 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004003
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004004 if (STRLEN(pmark) != 1)
4005 {
4006 PyErr_SetString(PyExc_ValueError,
4007 _("mark name must be a single character"));
4008 return NULL;
4009 }
4010
4011 mark = *pmark;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004012 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004013 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004014 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004015 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004016 if (VimTryEnd())
4017 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004018
4019 if (posp == NULL)
4020 {
4021 PyErr_SetVim(_("invalid mark name"));
4022 return NULL;
4023 }
4024
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004025 if (posp->lnum <= 0)
4026 {
4027 /* Or raise an error? */
4028 Py_INCREF(Py_None);
4029 return Py_None;
4030 }
4031
4032 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4033}
4034
4035 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004036BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004037{
4038 PyInt start;
4039 PyInt end;
4040
Bram Moolenaard6e39182013-05-21 18:30:34 +02004041 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004042 return NULL;
4043
4044 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4045 return NULL;
4046
Bram Moolenaard6e39182013-05-21 18:30:34 +02004047 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004048}
4049
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004050 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004051BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004052{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004053 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004054 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004055 else
4056 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004057 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004058
4059 if (name == NULL)
4060 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004061
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004062 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004063 }
4064}
4065
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004066static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004067 /* name, function, calling, documentation */
4068 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4069 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4070 {"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 +02004071 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4072 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004073};
4074
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004075/*
4076 * Buffer list object - Implementation
4077 */
4078
4079static PyTypeObject BufMapType;
4080
4081typedef struct
4082{
4083 PyObject_HEAD
4084} BufMapObject;
4085
4086 static PyInt
4087BufMapLength(PyObject *self UNUSED)
4088{
4089 buf_T *b = firstbuf;
4090 PyInt n = 0;
4091
4092 while (b)
4093 {
4094 ++n;
4095 b = b->b_next;
4096 }
4097
4098 return n;
4099}
4100
4101 static PyObject *
4102BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4103{
4104 buf_T *b;
4105 int bnr;
4106
4107#if PY_MAJOR_VERSION < 3
4108 if (PyInt_Check(keyObject))
4109 bnr = PyInt_AsLong(keyObject);
4110 else
4111#endif
4112 if (PyLong_Check(keyObject))
4113 bnr = PyLong_AsLong(keyObject);
4114 else
4115 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004116 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004117 return NULL;
4118 }
4119
4120 b = buflist_findnr(bnr);
4121
4122 if (b)
4123 return BufferNew(b);
4124 else
4125 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004126 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004127 return NULL;
4128 }
4129}
4130
4131 static void
4132BufMapIterDestruct(PyObject *buffer)
4133{
4134 /* Iteration was stopped before all buffers were processed */
4135 if (buffer)
4136 {
4137 Py_DECREF(buffer);
4138 }
4139}
4140
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004141 static int
4142BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4143{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004144 if (buffer)
4145 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004146 return 0;
4147}
4148
4149 static int
4150BufMapIterClear(PyObject **buffer)
4151{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004152 if (*buffer)
4153 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004154 return 0;
4155}
4156
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004157 static PyObject *
4158BufMapIterNext(PyObject **buffer)
4159{
4160 PyObject *next;
4161 PyObject *r;
4162
4163 if (!*buffer)
4164 return NULL;
4165
4166 r = *buffer;
4167
4168 if (CheckBuffer((BufferObject *)(r)))
4169 {
4170 *buffer = NULL;
4171 return NULL;
4172 }
4173
4174 if (!((BufferObject *)(r))->buf->b_next)
4175 next = NULL;
4176 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4177 return NULL;
4178 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004179 /* Do not increment reference: we no longer hold it (decref), but whoever
4180 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004181 return r;
4182}
4183
4184 static PyObject *
4185BufMapIter(PyObject *self UNUSED)
4186{
4187 PyObject *buffer;
4188
4189 buffer = BufferNew(firstbuf);
4190 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004191 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4192 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004193}
4194
4195static PyMappingMethods BufMapAsMapping = {
4196 (lenfunc) BufMapLength,
4197 (binaryfunc) BufMapItem,
4198 (objobjargproc) 0,
4199};
4200
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004201/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004202 */
4203
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004204static char *CurrentAttrs[] = {
4205 "buffer", "window", "line", "range", "tabpage",
4206 NULL
4207};
4208
4209 static PyObject *
4210CurrentDir(PyObject *self)
4211{
4212 return ObjectDir(self, CurrentAttrs);
4213}
4214
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004215 static PyObject *
4216CurrentGetattr(PyObject *self UNUSED, char *name)
4217{
4218 if (strcmp(name, "buffer") == 0)
4219 return (PyObject *)BufferNew(curbuf);
4220 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004221 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004222 else if (strcmp(name, "tabpage") == 0)
4223 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004224 else if (strcmp(name, "line") == 0)
4225 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4226 else if (strcmp(name, "range") == 0)
4227 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004228 else if (strcmp(name, "__members__") == 0)
4229 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004230 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004231#if PY_MAJOR_VERSION < 3
4232 return Py_FindMethod(WindowMethods, self, name);
4233#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004234 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004235#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004236}
4237
4238 static int
4239CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4240{
4241 if (strcmp(name, "line") == 0)
4242 {
4243 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4244 return -1;
4245
4246 return 0;
4247 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004248 else if (strcmp(name, "buffer") == 0)
4249 {
4250 int count;
4251
4252 if (value->ob_type != &BufferType)
4253 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004254 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004255 return -1;
4256 }
4257
4258 if (CheckBuffer((BufferObject *)(value)))
4259 return -1;
4260 count = ((BufferObject *)(value))->buf->b_fnum;
4261
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004262 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004263 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4264 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004265 if (VimTryEnd())
4266 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004267 PyErr_SetVim(_("failed to switch to given buffer"));
4268 return -1;
4269 }
4270
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004271 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004272 }
4273 else if (strcmp(name, "window") == 0)
4274 {
4275 int count;
4276
4277 if (value->ob_type != &WindowType)
4278 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004279 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004280 return -1;
4281 }
4282
4283 if (CheckWindow((WindowObject *)(value)))
4284 return -1;
4285 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4286
4287 if (!count)
4288 {
4289 PyErr_SetString(PyExc_ValueError,
4290 _("failed to find window in the current tab page"));
4291 return -1;
4292 }
4293
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004294 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004295 win_goto(((WindowObject *)(value))->win);
4296 if (((WindowObject *)(value))->win != curwin)
4297 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004298 if (VimTryEnd())
4299 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004300 PyErr_SetString(PyExc_RuntimeError,
4301 _("did not switch to the specified window"));
4302 return -1;
4303 }
4304
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004305 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004306 }
4307 else if (strcmp(name, "tabpage") == 0)
4308 {
4309 if (value->ob_type != &TabPageType)
4310 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004311 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004312 return -1;
4313 }
4314
4315 if (CheckTabPage((TabPageObject *)(value)))
4316 return -1;
4317
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004318 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004319 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4320 if (((TabPageObject *)(value))->tab != curtab)
4321 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004322 if (VimTryEnd())
4323 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004324 PyErr_SetString(PyExc_RuntimeError,
4325 _("did not switch to the specified tab page"));
4326 return -1;
4327 }
4328
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004329 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004330 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004331 else
4332 {
4333 PyErr_SetString(PyExc_AttributeError, name);
4334 return -1;
4335 }
4336}
4337
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004338static struct PyMethodDef CurrentMethods[] = {
4339 /* name, function, calling, documentation */
4340 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4341 { NULL, NULL, 0, NULL}
4342};
4343
Bram Moolenaardb913952012-06-29 12:54:53 +02004344 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004345init_range_cmd(exarg_T *eap)
4346{
4347 RangeStart = eap->line1;
4348 RangeEnd = eap->line2;
4349}
4350
4351 static void
4352init_range_eval(typval_T *rettv UNUSED)
4353{
4354 RangeStart = (PyInt) curwin->w_cursor.lnum;
4355 RangeEnd = RangeStart;
4356}
4357
4358 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004359run_cmd(const char *cmd, void *arg UNUSED
4360#ifdef PY_CAN_RECURSE
4361 , PyGILState_STATE *pygilstate UNUSED
4362#endif
4363 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004364{
4365 PyRun_SimpleString((char *) cmd);
4366}
4367
4368static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4369static int code_hdr_len = 30;
4370
4371 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004372run_do(const char *cmd, void *arg UNUSED
4373#ifdef PY_CAN_RECURSE
4374 , PyGILState_STATE *pygilstate
4375#endif
4376 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004377{
4378 PyInt lnum;
4379 size_t len;
4380 char *code;
4381 int status;
4382 PyObject *pyfunc, *pymain;
4383
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004384 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004385 {
4386 EMSG(_("cannot save undo information"));
4387 return;
4388 }
4389
4390 len = code_hdr_len + STRLEN(cmd);
4391 code = PyMem_New(char, len + 1);
4392 memcpy(code, code_hdr, code_hdr_len);
4393 STRCPY(code + code_hdr_len, cmd);
4394 status = PyRun_SimpleString(code);
4395 PyMem_Free(code);
4396
4397 if (status)
4398 {
4399 EMSG(_("failed to run the code"));
4400 return;
4401 }
4402
4403 status = 0;
4404 pymain = PyImport_AddModule("__main__");
4405 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004406#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004407 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004408#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004409
4410 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4411 {
4412 PyObject *line, *linenr, *ret;
4413
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004414#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004415 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004416#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004417 if (!(line = GetBufferLine(curbuf, lnum)))
4418 goto err;
4419 if (!(linenr = PyInt_FromLong((long) lnum)))
4420 {
4421 Py_DECREF(line);
4422 goto err;
4423 }
4424 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4425 Py_DECREF(line);
4426 Py_DECREF(linenr);
4427 if (!ret)
4428 goto err;
4429
4430 if (ret != Py_None)
4431 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4432 goto err;
4433
4434 Py_XDECREF(ret);
4435 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004436#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004437 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004438#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004439 }
4440 goto out;
4441err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004442#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004443 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004444#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004445 PyErr_PrintEx(0);
4446 PythonIO_Flush();
4447 status = 1;
4448out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004449#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004450 if (!status)
4451 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004452#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004453 Py_DECREF(pyfunc);
4454 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4455 if (status)
4456 return;
4457 check_cursor();
4458 update_curbuf(NOT_VALID);
4459}
4460
4461 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004462run_eval(const char *cmd, typval_T *rettv
4463#ifdef PY_CAN_RECURSE
4464 , PyGILState_STATE *pygilstate UNUSED
4465#endif
4466 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004467{
4468 PyObject *r;
4469
4470 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4471 if (r == NULL)
4472 {
4473 if (PyErr_Occurred() && !msg_silent)
4474 PyErr_PrintEx(0);
4475 EMSG(_("E858: Eval did not return a valid python object"));
4476 }
4477 else
4478 {
4479 if (ConvertFromPyObject(r, rettv) == -1)
4480 EMSG(_("E859: Failed to convert returned python object to vim value"));
4481 Py_DECREF(r);
4482 }
4483 PyErr_Clear();
4484}
4485
4486 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004487set_ref_in_py(const int copyID)
4488{
4489 pylinkedlist_T *cur;
4490 dict_T *dd;
4491 list_T *ll;
4492
4493 if (lastdict != NULL)
4494 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4495 {
4496 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4497 if (dd->dv_copyID != copyID)
4498 {
4499 dd->dv_copyID = copyID;
4500 set_ref_in_ht(&dd->dv_hashtab, copyID);
4501 }
4502 }
4503
4504 if (lastlist != NULL)
4505 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4506 {
4507 ll = ((ListObject *) (cur->pll_obj))->list;
4508 if (ll->lv_copyID != copyID)
4509 {
4510 ll->lv_copyID = copyID;
4511 set_ref_in_list(ll, copyID);
4512 }
4513 }
4514}
4515
4516 static int
4517set_string_copy(char_u *str, typval_T *tv)
4518{
4519 tv->vval.v_string = vim_strsave(str);
4520 if (tv->vval.v_string == NULL)
4521 {
4522 PyErr_NoMemory();
4523 return -1;
4524 }
4525 return 0;
4526}
4527
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004528 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004529pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004530{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004531 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004532 char_u *key;
4533 dictitem_T *di;
4534 PyObject *keyObject;
4535 PyObject *valObject;
4536 Py_ssize_t iter = 0;
4537
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004538 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004539 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004540
4541 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004542 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004543
4544 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4545 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004546 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004547
Bram Moolenaara03e6312013-05-29 22:49:26 +02004548 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004549 {
4550 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004551 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004552 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004553
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004554 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004555 {
4556 dict_unref(dict);
4557 return -1;
4558 }
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004559 if (*key == NUL)
4560 {
4561 dict_unref(dict);
4562 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004563 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004564 return -1;
4565 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004566
4567 di = dictitem_alloc(key);
4568
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004569 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004570
4571 if (di == NULL)
4572 {
4573 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004574 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004575 return -1;
4576 }
4577 di->di_tv.v_lock = 0;
4578
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004579 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004580 {
4581 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004582 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004583 return -1;
4584 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004585
4586 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004587 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004588 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004589 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004590 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004591 PyErr_SetVim(_("failed to add key to dictionary"));
4592 return -1;
4593 }
4594 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004595
4596 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004597 return 0;
4598}
4599
4600 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004601pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004602{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004603 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004604 char_u *key;
4605 dictitem_T *di;
4606 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004607 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004608 PyObject *keyObject;
4609 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004610
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004611 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004612 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004613
4614 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004615 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004616
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004617 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004618 {
4619 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004620 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004621 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004622
4623 if (!(iterator = PyObject_GetIter(list)))
4624 {
4625 dict_unref(dict);
4626 Py_DECREF(list);
4627 return -1;
4628 }
4629 Py_DECREF(list);
4630
4631 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004632 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004633 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004634
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004635 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004636 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004637 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004638 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004639 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004640 return -1;
4641 }
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004642 if (*key == NUL)
4643 {
4644 Py_DECREF(keyObject);
4645 Py_DECREF(iterator);
4646 Py_XDECREF(todecref);
4647 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004648 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004649 return -1;
4650 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004651
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004652 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004653 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004654 Py_DECREF(keyObject);
4655 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004656 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004657 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004658 return -1;
4659 }
4660
4661 di = dictitem_alloc(key);
4662
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004663 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004664 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004665
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004666 if (di == NULL)
4667 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004668 Py_DECREF(iterator);
4669 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004670 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004671 PyErr_NoMemory();
4672 return -1;
4673 }
4674 di->di_tv.v_lock = 0;
4675
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004676 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004677 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004678 Py_DECREF(iterator);
4679 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004680 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004681 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004682 return -1;
4683 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004684
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004685 Py_DECREF(valObject);
4686
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004687 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004688 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004689 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004690 dictitem_free(di);
4691 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004692 PyErr_SetVim(_("failed to add key to dictionary"));
4693 return -1;
4694 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004695 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004696 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004697 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004698 return 0;
4699}
4700
4701 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004702pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004703{
4704 list_T *l;
4705
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004706 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004707 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004708
4709 tv->v_type = VAR_LIST;
4710 tv->vval.v_list = l;
4711
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004712 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004713 {
4714 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004715 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004716 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004717
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004718 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004719 return 0;
4720}
4721
Bram Moolenaardb913952012-06-29 12:54:53 +02004722typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4723
4724 static int
4725convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004726 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004727{
4728 PyObject *capsule;
4729 char hexBuf[sizeof(void *) * 2 + 3];
4730
4731 sprintf(hexBuf, "%p", obj);
4732
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004733# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004734 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004735# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004736 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004737# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004738 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004739 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004740# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004741 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004742# else
4743 capsule = PyCObject_FromVoidPtr(tv, NULL);
4744# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004745 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4746 {
4747 Py_DECREF(capsule);
4748 tv->v_type = VAR_UNKNOWN;
4749 return -1;
4750 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004751 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004752 {
4753 tv->v_type = VAR_UNKNOWN;
4754 return -1;
4755 }
4756 /* As we are not using copy_tv which increments reference count we must
4757 * do it ourself. */
4758 switch(tv->v_type)
4759 {
4760 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4761 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4762 }
4763 }
4764 else
4765 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004766 typval_T *v;
4767
4768# ifdef PY_USE_CAPSULE
4769 v = PyCapsule_GetPointer(capsule, NULL);
4770# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004771 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004772# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004773 copy_tv(v, tv);
4774 }
4775 return 0;
4776}
4777
4778 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02004779ConvertFromPyMapping(PyObject *obj, typval_T *tv)
4780{
4781 PyObject *lookup_dict;
4782 int r;
4783
4784 if (!(lookup_dict = PyDict_New()))
4785 return -1;
4786
4787 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
4788 {
4789 tv->v_type = VAR_DICT;
4790 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4791 ++tv->vval.v_dict->dv_refcount;
4792 r = 0;
4793 }
4794 else if (PyDict_Check(obj))
4795 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
4796 else if (PyMapping_Check(obj))
4797 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
4798 else
4799 {
4800 PyErr_SetString(PyExc_TypeError,
4801 _("unable to convert object to vim dictionary"));
4802 r = -1;
4803 }
4804 Py_DECREF(lookup_dict);
4805 return r;
4806}
4807
4808 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02004809ConvertFromPyObject(PyObject *obj, typval_T *tv)
4810{
4811 PyObject *lookup_dict;
4812 int r;
4813
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004814 if (!(lookup_dict = PyDict_New()))
4815 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004816 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4817 Py_DECREF(lookup_dict);
4818 return r;
4819}
4820
4821 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004822_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004823{
Bram Moolenaara9922d62013-05-30 13:01:18 +02004824 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02004825 {
4826 tv->v_type = VAR_DICT;
4827 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4828 ++tv->vval.v_dict->dv_refcount;
4829 }
4830 else if (obj->ob_type == &ListType)
4831 {
4832 tv->v_type = VAR_LIST;
4833 tv->vval.v_list = (((ListObject *)(obj))->list);
4834 ++tv->vval.v_list->lv_refcount;
4835 }
4836 else if (obj->ob_type == &FunctionType)
4837 {
4838 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4839 return -1;
4840
4841 tv->v_type = VAR_FUNC;
4842 func_ref(tv->vval.v_string);
4843 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004844 else if (PyBytes_Check(obj))
4845 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004846 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004847
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004848 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4849 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004850 if (result == NULL)
4851 return -1;
4852
4853 if (set_string_copy(result, tv) == -1)
4854 return -1;
4855
4856 tv->v_type = VAR_STRING;
4857 }
4858 else if (PyUnicode_Check(obj))
4859 {
4860 PyObject *bytes;
4861 char_u *result;
4862
Bram Moolenaardb913952012-06-29 12:54:53 +02004863 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4864 if (bytes == NULL)
4865 return -1;
4866
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004867 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4868 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004869 if (result == NULL)
4870 return -1;
4871
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004872 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004873 {
4874 Py_XDECREF(bytes);
4875 return -1;
4876 }
4877 Py_XDECREF(bytes);
4878
4879 tv->v_type = VAR_STRING;
4880 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004881#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004882 else if (PyInt_Check(obj))
4883 {
4884 tv->v_type = VAR_NUMBER;
4885 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4886 }
4887#endif
4888 else if (PyLong_Check(obj))
4889 {
4890 tv->v_type = VAR_NUMBER;
4891 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4892 }
4893 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004894 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004895#ifdef FEAT_FLOAT
4896 else if (PyFloat_Check(obj))
4897 {
4898 tv->v_type = VAR_FLOAT;
4899 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4900 }
4901#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004902 else if (PyObject_HasAttrString(obj, "keys"))
4903 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004904 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004905 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004906 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004907 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004908 else
4909 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004910 PyErr_SetString(PyExc_TypeError,
4911 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004912 return -1;
4913 }
4914 return 0;
4915}
4916
4917 static PyObject *
4918ConvertToPyObject(typval_T *tv)
4919{
4920 if (tv == NULL)
4921 {
4922 PyErr_SetVim(_("NULL reference passed"));
4923 return NULL;
4924 }
4925 switch (tv->v_type)
4926 {
4927 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004928 return PyBytes_FromString(tv->vval.v_string == NULL
4929 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004930 case VAR_NUMBER:
4931 return PyLong_FromLong((long) tv->vval.v_number);
4932#ifdef FEAT_FLOAT
4933 case VAR_FLOAT:
4934 return PyFloat_FromDouble((double) tv->vval.v_float);
4935#endif
4936 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004937 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02004938 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02004939 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004940 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02004941 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004942 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004943 case VAR_UNKNOWN:
4944 Py_INCREF(Py_None);
4945 return Py_None;
4946 default:
4947 PyErr_SetVim(_("internal error: invalid value type"));
4948 return NULL;
4949 }
4950}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004951
4952typedef struct
4953{
4954 PyObject_HEAD
4955} CurrentObject;
4956static PyTypeObject CurrentType;
4957
4958 static void
4959init_structs(void)
4960{
4961 vim_memset(&OutputType, 0, sizeof(OutputType));
4962 OutputType.tp_name = "vim.message";
4963 OutputType.tp_basicsize = sizeof(OutputObject);
4964 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4965 OutputType.tp_doc = "vim message object";
4966 OutputType.tp_methods = OutputMethods;
4967#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004968 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4969 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004970 OutputType.tp_alloc = call_PyType_GenericAlloc;
4971 OutputType.tp_new = call_PyType_GenericNew;
4972 OutputType.tp_free = call_PyObject_Free;
4973#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004974 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4975 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004976#endif
4977
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004978 vim_memset(&IterType, 0, sizeof(IterType));
4979 IterType.tp_name = "vim.iter";
4980 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02004981 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004982 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004983 IterType.tp_iter = (getiterfunc)IterIter;
4984 IterType.tp_iternext = (iternextfunc)IterNext;
4985 IterType.tp_dealloc = (destructor)IterDestructor;
4986 IterType.tp_traverse = (traverseproc)IterTraverse;
4987 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004988
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004989 vim_memset(&BufferType, 0, sizeof(BufferType));
4990 BufferType.tp_name = "vim.buffer";
4991 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004992 BufferType.tp_dealloc = (destructor)BufferDestructor;
4993 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004994 BufferType.tp_as_sequence = &BufferAsSeq;
4995 BufferType.tp_as_mapping = &BufferAsMapping;
4996 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4997 BufferType.tp_doc = "vim buffer object";
4998 BufferType.tp_methods = BufferMethods;
4999#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005000 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005001 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005002 BufferType.tp_alloc = call_PyType_GenericAlloc;
5003 BufferType.tp_new = call_PyType_GenericNew;
5004 BufferType.tp_free = call_PyObject_Free;
5005#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005006 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005007 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005008#endif
5009
5010 vim_memset(&WindowType, 0, sizeof(WindowType));
5011 WindowType.tp_name = "vim.window";
5012 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005013 WindowType.tp_dealloc = (destructor)WindowDestructor;
5014 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005015 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005016 WindowType.tp_doc = "vim Window object";
5017 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005018 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5019 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005020#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005021 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5022 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005023 WindowType.tp_alloc = call_PyType_GenericAlloc;
5024 WindowType.tp_new = call_PyType_GenericNew;
5025 WindowType.tp_free = call_PyObject_Free;
5026#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005027 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5028 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005029#endif
5030
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005031 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5032 TabPageType.tp_name = "vim.tabpage";
5033 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005034 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5035 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005036 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5037 TabPageType.tp_doc = "vim tab page object";
5038 TabPageType.tp_methods = TabPageMethods;
5039#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005040 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005041 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5042 TabPageType.tp_new = call_PyType_GenericNew;
5043 TabPageType.tp_free = call_PyObject_Free;
5044#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005045 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005046#endif
5047
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005048 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5049 BufMapType.tp_name = "vim.bufferlist";
5050 BufMapType.tp_basicsize = sizeof(BufMapObject);
5051 BufMapType.tp_as_mapping = &BufMapAsMapping;
5052 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005053 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005054 BufferType.tp_doc = "vim buffer list";
5055
5056 vim_memset(&WinListType, 0, sizeof(WinListType));
5057 WinListType.tp_name = "vim.windowlist";
5058 WinListType.tp_basicsize = sizeof(WinListType);
5059 WinListType.tp_as_sequence = &WinListAsSeq;
5060 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5061 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005062 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005063
5064 vim_memset(&TabListType, 0, sizeof(TabListType));
5065 TabListType.tp_name = "vim.tabpagelist";
5066 TabListType.tp_basicsize = sizeof(TabListType);
5067 TabListType.tp_as_sequence = &TabListAsSeq;
5068 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5069 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005070
5071 vim_memset(&RangeType, 0, sizeof(RangeType));
5072 RangeType.tp_name = "vim.range";
5073 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005074 RangeType.tp_dealloc = (destructor)RangeDestructor;
5075 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005076 RangeType.tp_as_sequence = &RangeAsSeq;
5077 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005078 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005079 RangeType.tp_doc = "vim Range object";
5080 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005081 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5082 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005083#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005084 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005085 RangeType.tp_alloc = call_PyType_GenericAlloc;
5086 RangeType.tp_new = call_PyType_GenericNew;
5087 RangeType.tp_free = call_PyObject_Free;
5088#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005089 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005090#endif
5091
5092 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5093 CurrentType.tp_name = "vim.currentdata";
5094 CurrentType.tp_basicsize = sizeof(CurrentObject);
5095 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5096 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005097 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005098#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005099 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5100 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005101#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005102 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5103 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005104#endif
5105
5106 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5107 DictionaryType.tp_name = "vim.dictionary";
5108 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005109 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005110 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005111 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005112 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005113 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5114 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005115 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5116 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5117 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005118#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005119 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5120 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005121#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005122 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5123 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005124#endif
5125
5126 vim_memset(&ListType, 0, sizeof(ListType));
5127 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005128 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005129 ListType.tp_basicsize = sizeof(ListObject);
5130 ListType.tp_as_sequence = &ListAsSeq;
5131 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005132 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005133 ListType.tp_doc = "list pushing modifications to vim structure";
5134 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005135 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005136 ListType.tp_new = (newfunc)ListConstructor;
5137 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005138#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005139 ListType.tp_getattro = (getattrofunc)ListGetattro;
5140 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005141#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005142 ListType.tp_getattr = (getattrfunc)ListGetattr;
5143 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005144#endif
5145
5146 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005147 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005148 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005149 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5150 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005151 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005152 FunctionType.tp_doc = "object that calls vim function";
5153 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005154 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005155 FunctionType.tp_new = (newfunc)FunctionConstructor;
5156 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005157#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005158 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005159#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005160 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005161#endif
5162
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005163 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5164 OptionsType.tp_name = "vim.options";
5165 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005166 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005167 OptionsType.tp_doc = "object for manipulating options";
5168 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005169 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5170 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5171 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005172
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005173#if PY_MAJOR_VERSION >= 3
5174 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5175 vimmodule.m_name = "vim";
5176 vimmodule.m_doc = "Vim Python interface\n";
5177 vimmodule.m_size = -1;
5178 vimmodule.m_methods = VimMethods;
5179#endif
5180}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005181
5182#define PYTYPE_READY(type) \
5183 if (PyType_Ready(&type)) \
5184 return -1;
5185
5186 static int
5187init_types()
5188{
5189 PYTYPE_READY(IterType);
5190 PYTYPE_READY(BufferType);
5191 PYTYPE_READY(RangeType);
5192 PYTYPE_READY(WindowType);
5193 PYTYPE_READY(TabPageType);
5194 PYTYPE_READY(BufMapType);
5195 PYTYPE_READY(WinListType);
5196 PYTYPE_READY(TabListType);
5197 PYTYPE_READY(CurrentType);
5198 PYTYPE_READY(DictionaryType);
5199 PYTYPE_READY(ListType);
5200 PYTYPE_READY(FunctionType);
5201 PYTYPE_READY(OptionsType);
5202 PYTYPE_READY(OutputType);
5203 return 0;
5204}
5205
5206static BufMapObject TheBufferMap =
5207{
5208 PyObject_HEAD_INIT(&BufMapType)
5209};
5210
5211static WinListObject TheWindowList =
5212{
5213 PyObject_HEAD_INIT(&WinListType)
5214 NULL
5215};
5216
5217static CurrentObject TheCurrent =
5218{
5219 PyObject_HEAD_INIT(&CurrentType)
5220};
5221
5222static TabListObject TheTabPageList =
5223{
5224 PyObject_HEAD_INIT(&TabListType)
5225};
5226
5227static struct numeric_constant {
5228 char *name;
5229 int value;
5230} numeric_constants[] = {
5231 {"VAR_LOCKED", VAR_LOCKED},
5232 {"VAR_FIXED", VAR_FIXED},
5233 {"VAR_SCOPE", VAR_SCOPE},
5234 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5235};
5236
5237static struct object_constant {
5238 char *name;
5239 PyObject *value;
5240} object_constants[] = {
5241 {"buffers", (PyObject *)(void *)&TheBufferMap},
5242 {"windows", (PyObject *)(void *)&TheWindowList},
5243 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5244 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005245
5246 {"Buffer", (PyObject *)&BufferType},
5247 {"Range", (PyObject *)&RangeType},
5248 {"Window", (PyObject *)&WindowType},
5249 {"TabPage", (PyObject *)&TabPageType},
5250 {"Dictionary", (PyObject *)&DictionaryType},
5251 {"List", (PyObject *)&ListType},
5252 {"Function", (PyObject *)&FunctionType},
5253 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005254};
5255
5256typedef int (*object_adder)(PyObject *, const char *, PyObject *);
5257
5258#define ADD_OBJECT(m, name, obj) \
5259 if (add_object(m, name, obj)) \
5260 return -1;
5261
5262#define ADD_CHECKED_OBJECT(m, name, obj) \
5263 { \
5264 PyObject *value = obj; \
5265 if (!value) \
5266 return -1; \
5267 ADD_OBJECT(m, name, value); \
5268 }
5269
5270 static int
5271populate_module(PyObject *m, object_adder add_object)
5272{
5273 int i;
5274
5275 for (i = 0; i < (int)(sizeof(numeric_constants)
5276 / sizeof(struct numeric_constant));
5277 ++i)
5278 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5279 PyInt_FromLong(numeric_constants[i].value));
5280
5281 for (i = 0; i < (int)(sizeof(object_constants)
5282 / sizeof(struct object_constant));
5283 ++i)
5284 {
5285 PyObject *value;
5286
5287 value = object_constants[i].value;
5288 Py_INCREF(value);
5289 ADD_OBJECT(m, object_constants[i].name, value);
5290 }
5291
5292 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5293 return -1;
5294 ADD_OBJECT(m, "error", VimError);
5295
Bram Moolenaara9922d62013-05-30 13:01:18 +02005296 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5297 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005298 ADD_CHECKED_OBJECT(m, "options",
5299 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
5300 return 0;
5301}