blob: 21bf069c8c37d9f49bc6a9bff29e08d675d1bb0f [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;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001020 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001021 return NULL;
1022 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001023
Bram Moolenaara9922d62013-05-30 13:01:18 +02001024 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001025
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001026 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001027
Bram Moolenaara9922d62013-05-30 13:01:18 +02001028 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001029 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001030 if (defObject)
1031 {
1032 Py_INCREF(defObject);
1033 return defObject;
1034 }
1035 else
1036 {
1037 PyErr_SetObject(PyExc_KeyError, keyObject);
1038 return NULL;
1039 }
1040 }
1041 else if (flags & DICT_FLAG_RETURN_BOOL)
1042 {
1043 Py_INCREF(Py_True);
1044 return Py_True;
1045 }
1046
1047 di = dict_lookup(hi);
1048
1049 if (!(r = ConvertToPyObject(&di->di_tv)))
1050 return NULL;
1051
1052 if (flags & DICT_FLAG_POP)
1053 {
1054 if (dict->dv_lock)
1055 {
1056 PyErr_SetVim(_("dict is locked"));
1057 Py_DECREF(r);
1058 return NULL;
1059 }
1060
1061 hash_remove(&dict->dv_hashtab, hi);
1062 dictitem_free(di);
1063 }
1064
Bram Moolenaara9922d62013-05-30 13:01:18 +02001065 return r;
1066}
1067
1068 static PyObject *
1069DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1070{
1071 return _DictionaryItem(self, keyObject, 0);
1072}
1073
1074 static int
1075DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1076{
1077 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1078 int r;
1079
1080 r = (rObj == Py_True);
1081
1082 Py_DECREF(Py_True);
1083
1084 return r;
1085}
1086
1087typedef struct
1088{
1089 hashitem_T *ht_array;
1090 long_u ht_used;
1091 hashtab_T *ht;
1092 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001093 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001094} dictiterinfo_T;
1095
1096 static PyObject *
1097DictionaryIterNext(dictiterinfo_T **dii)
1098{
1099 PyObject *r;
1100
1101 if (!(*dii)->todo)
1102 return NULL;
1103
1104 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1105 (*dii)->ht->ht_used != (*dii)->ht_used)
1106 {
1107 PyErr_SetString(PyExc_RuntimeError,
1108 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001109 return NULL;
1110 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001111
Bram Moolenaara9922d62013-05-30 13:01:18 +02001112 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1113 ++((*dii)->hi);
1114
1115 --((*dii)->todo);
1116
1117 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1118 return NULL;
1119
1120 return r;
1121}
1122
1123 static PyObject *
1124DictionaryIter(DictionaryObject *self)
1125{
1126 dictiterinfo_T *dii;
1127 hashtab_T *ht;
1128
1129 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1130 {
1131 PyErr_NoMemory();
1132 return NULL;
1133 }
1134
1135 ht = &self->dict->dv_hashtab;
1136 dii->ht_array = ht->ht_array;
1137 dii->ht_used = ht->ht_used;
1138 dii->ht = ht;
1139 dii->hi = dii->ht_array;
1140 dii->todo = dii->ht_used;
1141
1142 return IterNew(dii,
1143 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1144 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001145}
1146
1147 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001148DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001149{
1150 char_u *key;
1151 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001152 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001153 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001154 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001155
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001156 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001157 {
1158 PyErr_SetVim(_("dict is locked"));
1159 return -1;
1160 }
1161
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001162 if (!(key = StringToChars(keyObject, &todecref)))
1163 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001164
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001165 if (*key == NUL)
1166 {
1167 RAISE_NO_EMPTY_KEYS;
1168 return -1;
1169 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001170
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001171 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001172
1173 if (valObject == NULL)
1174 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001175 hashitem_T *hi;
1176
Bram Moolenaardb913952012-06-29 12:54:53 +02001177 if (di == NULL)
1178 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001179 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001180 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001181 return -1;
1182 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001183 hi = hash_find(&dict->dv_hashtab, di->di_key);
1184 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001185 dictitem_free(di);
1186 return 0;
1187 }
1188
1189 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001190 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001191
1192 if (di == NULL)
1193 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001194 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001195 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001196 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001197 PyErr_NoMemory();
1198 return -1;
1199 }
1200 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001201 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001202
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001203 if (dict_add(dict, di) == FAIL)
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 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001207 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001208 PyErr_SetVim(_("failed to add key to dictionary"));
1209 return -1;
1210 }
1211 }
1212 else
1213 clear_tv(&di->di_tv);
1214
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001215 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001216
1217 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001218 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001219 return 0;
1220}
1221
Bram Moolenaara9922d62013-05-30 13:01:18 +02001222typedef PyObject *(*hi_to_py)(hashitem_T *);
1223
Bram Moolenaardb913952012-06-29 12:54:53 +02001224 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001225DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001226{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001227 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001228 long_u todo = dict->dv_hashtab.ht_used;
1229 Py_ssize_t i = 0;
1230 PyObject *r;
1231 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001232 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001233
1234 r = PyList_New(todo);
1235 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1236 {
1237 if (!HASHITEM_EMPTY(hi))
1238 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001239 if (!(newObj = hiconvert(hi)))
1240 {
1241 Py_DECREF(r);
1242 return NULL;
1243 }
1244 if (PyList_SetItem(r, i, newObj))
1245 {
1246 Py_DECREF(r);
1247 Py_DECREF(newObj);
1248 return NULL;
1249 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001250 --todo;
1251 ++i;
1252 }
1253 }
1254 return r;
1255}
1256
Bram Moolenaara9922d62013-05-30 13:01:18 +02001257 static PyObject *
1258dict_key(hashitem_T *hi)
1259{
1260 return PyBytes_FromString((char *)(hi->hi_key));
1261}
1262
1263 static PyObject *
1264DictionaryListKeys(DictionaryObject *self)
1265{
1266 return DictionaryListObjects(self, dict_key);
1267}
1268
1269 static PyObject *
1270dict_val(hashitem_T *hi)
1271{
1272 dictitem_T *di;
1273
1274 di = dict_lookup(hi);
1275 return ConvertToPyObject(&di->di_tv);
1276}
1277
1278 static PyObject *
1279DictionaryListValues(DictionaryObject *self)
1280{
1281 return DictionaryListObjects(self, dict_val);
1282}
1283
1284 static PyObject *
1285dict_item(hashitem_T *hi)
1286{
1287 PyObject *keyObject;
1288 PyObject *valObject;
1289 PyObject *r;
1290
1291 if (!(keyObject = dict_key(hi)))
1292 return NULL;
1293
1294 if (!(valObject = dict_val(hi)))
1295 {
1296 Py_DECREF(keyObject);
1297 return NULL;
1298 }
1299
1300 r = Py_BuildValue("(OO)", keyObject, valObject);
1301
1302 Py_DECREF(keyObject);
1303 Py_DECREF(valObject);
1304
1305 return r;
1306}
1307
1308 static PyObject *
1309DictionaryListItems(DictionaryObject *self)
1310{
1311 return DictionaryListObjects(self, dict_item);
1312}
1313
1314 static PyObject *
1315DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1316{
1317 dict_T *dict = self->dict;
1318
1319 if (dict->dv_lock)
1320 {
1321 PyErr_SetVim(_("dict is locked"));
1322 return NULL;
1323 }
1324
1325 if (kwargs)
1326 {
1327 typval_T tv;
1328
1329 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1330 return NULL;
1331
1332 VimTryStart();
1333 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1334 clear_tv(&tv);
1335 if (VimTryEnd())
1336 return NULL;
1337 }
1338 else
1339 {
1340 PyObject *object;
1341
1342 if (!PyArg_Parse(args, "(O)", &object))
1343 return NULL;
1344
1345 if (PyObject_HasAttrString(object, "keys"))
1346 return DictionaryUpdate(self, NULL, object);
1347 else
1348 {
1349 PyObject *iterator;
1350 PyObject *item;
1351
1352 if (!(iterator = PyObject_GetIter(object)))
1353 return NULL;
1354
1355 while ((item = PyIter_Next(iterator)))
1356 {
1357 PyObject *fast;
1358 PyObject *keyObject;
1359 PyObject *valObject;
1360 PyObject *todecref;
1361 char_u *key;
1362 dictitem_T *di;
1363
1364 if (!(fast = PySequence_Fast(item, "")))
1365 {
1366 Py_DECREF(iterator);
1367 Py_DECREF(item);
1368 return NULL;
1369 }
1370
1371 Py_DECREF(item);
1372
1373 if (PySequence_Fast_GET_SIZE(fast) != 2)
1374 {
1375 Py_DECREF(iterator);
1376 Py_DECREF(fast);
1377 PyErr_SetString(PyExc_ValueError,
1378 _("expected sequence element of size 2"));
1379 return NULL;
1380 }
1381
1382 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1383
1384 if (!(key = StringToChars(keyObject, &todecref)))
1385 {
1386 Py_DECREF(iterator);
1387 Py_DECREF(fast);
1388 return NULL;
1389 }
1390
1391 di = dictitem_alloc(key);
1392
1393 Py_XDECREF(todecref);
1394
1395 if (di == NULL)
1396 {
1397 Py_DECREF(fast);
1398 Py_DECREF(iterator);
1399 PyErr_NoMemory();
1400 return NULL;
1401 }
1402 di->di_tv.v_lock = 0;
1403 di->di_tv.v_type = VAR_UNKNOWN;
1404
1405 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1406
1407 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1408 {
1409 Py_DECREF(iterator);
1410 Py_DECREF(fast);
1411 dictitem_free(di);
1412 return NULL;
1413 }
1414
1415 Py_DECREF(fast);
1416
1417 if (dict_add(dict, di) == FAIL)
1418 {
1419 Py_DECREF(iterator);
1420 dictitem_free(di);
1421 PyErr_SetVim(_("failed to add key to dictionary"));
1422 return NULL;
1423 }
1424 }
1425
1426 Py_DECREF(iterator);
1427
1428 /* Iterator may have finished due to an exception */
1429 if (PyErr_Occurred())
1430 return NULL;
1431 }
1432 }
1433 Py_INCREF(Py_None);
1434 return Py_None;
1435}
1436
1437 static PyObject *
1438DictionaryGet(DictionaryObject *self, PyObject *args)
1439{
1440 return _DictionaryItem(self, args,
1441 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1442}
1443
1444 static PyObject *
1445DictionaryPop(DictionaryObject *self, PyObject *args)
1446{
1447 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1448}
1449
1450 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001451DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001452{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001453 hashitem_T *hi;
1454 PyObject *r;
1455 PyObject *valObject;
1456 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001457
Bram Moolenaarde71b562013-06-02 17:41:54 +02001458 if (self->dict->dv_hashtab.ht_used == 0)
1459 {
1460 PyErr_SetNone(PyExc_KeyError);
1461 return NULL;
1462 }
1463
1464 hi = self->dict->dv_hashtab.ht_array;
1465 while (HASHITEM_EMPTY(hi))
1466 ++hi;
1467
1468 di = dict_lookup(hi);
1469
1470 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001471 return NULL;
1472
Bram Moolenaarde71b562013-06-02 17:41:54 +02001473 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
1474 {
1475 Py_DECREF(valObject);
1476 return NULL;
1477 }
1478
1479 hash_remove(&self->dict->dv_hashtab, hi);
1480 dictitem_free(di);
1481
1482 return r;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001483}
1484
1485 static PyObject *
1486DictionaryHasKey(DictionaryObject *self, PyObject *args)
1487{
1488 PyObject *keyObject;
1489
1490 if (!PyArg_ParseTuple(args, "O", &keyObject))
1491 return NULL;
1492
1493 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1494}
1495
1496static PySequenceMethods DictionaryAsSeq = {
1497 0, /* sq_length */
1498 0, /* sq_concat */
1499 0, /* sq_repeat */
1500 0, /* sq_item */
1501 0, /* sq_slice */
1502 0, /* sq_ass_item */
1503 0, /* sq_ass_slice */
1504 (objobjproc) DictionaryContains, /* sq_contains */
1505 0, /* sq_inplace_concat */
1506 0, /* sq_inplace_repeat */
1507};
1508
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001509static PyMappingMethods DictionaryAsMapping = {
1510 (lenfunc) DictionaryLength,
1511 (binaryfunc) DictionaryItem,
1512 (objobjargproc) DictionaryAssItem,
1513};
1514
Bram Moolenaardb913952012-06-29 12:54:53 +02001515static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001516 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001517 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1518 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1519 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1520 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1521 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02001522 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001523 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001524 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1525 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001526};
1527
1528static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001529static PySequenceMethods ListAsSeq;
1530static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001531
1532typedef struct
1533{
1534 PyObject_HEAD
1535 list_T *list;
1536 pylinkedlist_T ref;
1537} ListObject;
1538
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001539#define NEW_LIST(list) ListNew(&ListType, list)
1540
Bram Moolenaardb913952012-06-29 12:54:53 +02001541 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001542ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001543{
1544 ListObject *self;
1545
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001546 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001547 if (self == NULL)
1548 return NULL;
1549 self->list = list;
1550 ++list->lv_refcount;
1551
1552 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1553
1554 return (PyObject *)(self);
1555}
1556
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001557 static list_T *
1558py_list_alloc()
1559{
1560 list_T *r;
1561
1562 if (!(r = list_alloc()))
1563 {
1564 PyErr_NoMemory();
1565 return NULL;
1566 }
1567 ++r->lv_refcount;
1568
1569 return r;
1570}
1571
1572 static int
1573list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1574{
1575 PyObject *iterator;
1576 PyObject *item;
1577 listitem_T *li;
1578
1579 if (!(iterator = PyObject_GetIter(obj)))
1580 return -1;
1581
1582 while ((item = PyIter_Next(iterator)))
1583 {
1584 if (!(li = listitem_alloc()))
1585 {
1586 PyErr_NoMemory();
1587 Py_DECREF(item);
1588 Py_DECREF(iterator);
1589 return -1;
1590 }
1591 li->li_tv.v_lock = 0;
1592 li->li_tv.v_type = VAR_UNKNOWN;
1593
1594 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1595 {
1596 Py_DECREF(item);
1597 Py_DECREF(iterator);
1598 listitem_free(li);
1599 return -1;
1600 }
1601
1602 Py_DECREF(item);
1603
1604 list_append(l, li);
1605 }
1606
1607 Py_DECREF(iterator);
1608
1609 /* Iterator may have finished due to an exception */
1610 if (PyErr_Occurred())
1611 return -1;
1612
1613 return 0;
1614}
1615
1616 static PyObject *
1617ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1618{
1619 list_T *list;
1620 PyObject *obj = NULL;
1621
1622 if (kwargs)
1623 {
1624 PyErr_SetString(PyExc_TypeError,
1625 _("list constructor does not accept keyword arguments"));
1626 return NULL;
1627 }
1628
1629 if (!PyArg_ParseTuple(args, "|O", &obj))
1630 return NULL;
1631
1632 if (!(list = py_list_alloc()))
1633 return NULL;
1634
1635 if (obj)
1636 {
1637 PyObject *lookup_dict;
1638
1639 if (!(lookup_dict = PyDict_New()))
1640 {
1641 list_unref(list);
1642 return NULL;
1643 }
1644
1645 if (list_py_concat(list, obj, lookup_dict) == -1)
1646 {
1647 Py_DECREF(lookup_dict);
1648 list_unref(list);
1649 return NULL;
1650 }
1651
1652 Py_DECREF(lookup_dict);
1653 }
1654
1655 return ListNew(subtype, list);
1656}
1657
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001658 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001659ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001660{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001661 pyll_remove(&self->ref, &lastlist);
1662 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001663
1664 DESTRUCTOR_FINISH(self);
1665}
1666
Bram Moolenaardb913952012-06-29 12:54:53 +02001667 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001668ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001669{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001670 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001671}
1672
1673 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001674ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001675{
1676 listitem_T *li;
1677
Bram Moolenaard6e39182013-05-21 18:30:34 +02001678 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001679 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001680 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001681 return NULL;
1682 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001683 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001684 if (li == NULL)
1685 {
1686 PyErr_SetVim(_("internal error: failed to get vim list item"));
1687 return NULL;
1688 }
1689 return ConvertToPyObject(&li->li_tv);
1690}
1691
1692#define PROC_RANGE \
1693 if (last < 0) {\
1694 if (last < -size) \
1695 last = 0; \
1696 else \
1697 last += size; \
1698 } \
1699 if (first < 0) \
1700 first = 0; \
1701 if (first > size) \
1702 first = size; \
1703 if (last > size) \
1704 last = size;
1705
1706 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001707ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001708{
1709 PyInt i;
1710 PyInt size = ListLength(self);
1711 PyInt n;
1712 PyObject *list;
1713 int reversed = 0;
1714
1715 PROC_RANGE
1716 if (first >= last)
1717 first = last;
1718
1719 n = last-first;
1720 list = PyList_New(n);
1721 if (list == NULL)
1722 return NULL;
1723
1724 for (i = 0; i < n; ++i)
1725 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001726 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001727 if (item == NULL)
1728 {
1729 Py_DECREF(list);
1730 return NULL;
1731 }
1732
1733 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1734 {
1735 Py_DECREF(item);
1736 Py_DECREF(list);
1737 return NULL;
1738 }
1739 }
1740
1741 return list;
1742}
1743
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001744typedef struct
1745{
1746 listwatch_T lw;
1747 list_T *list;
1748} listiterinfo_T;
1749
1750 static void
1751ListIterDestruct(listiterinfo_T *lii)
1752{
1753 list_rem_watch(lii->list, &lii->lw);
1754 PyMem_Free(lii);
1755}
1756
1757 static PyObject *
1758ListIterNext(listiterinfo_T **lii)
1759{
1760 PyObject *r;
1761
1762 if (!((*lii)->lw.lw_item))
1763 return NULL;
1764
1765 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1766 return NULL;
1767
1768 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1769
1770 return r;
1771}
1772
1773 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001774ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001775{
1776 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001777 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001778
1779 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1780 {
1781 PyErr_NoMemory();
1782 return NULL;
1783 }
1784
1785 list_add_watch(l, &lii->lw);
1786 lii->lw.lw_item = l->lv_first;
1787 lii->list = l;
1788
1789 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001790 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1791 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001792}
1793
Bram Moolenaardb913952012-06-29 12:54:53 +02001794 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001795ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001796{
1797 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001798 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001799 listitem_T *li;
1800 Py_ssize_t length = ListLength(self);
1801
1802 if (l->lv_lock)
1803 {
1804 PyErr_SetVim(_("list is locked"));
1805 return -1;
1806 }
1807 if (index>length || (index==length && obj==NULL))
1808 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001809 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001810 return -1;
1811 }
1812
1813 if (obj == NULL)
1814 {
1815 li = list_find(l, (long) index);
1816 list_remove(l, li, li);
1817 clear_tv(&li->li_tv);
1818 vim_free(li);
1819 return 0;
1820 }
1821
1822 if (ConvertFromPyObject(obj, &tv) == -1)
1823 return -1;
1824
1825 if (index == length)
1826 {
1827 if (list_append_tv(l, &tv) == FAIL)
1828 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001829 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001830 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001831 return -1;
1832 }
1833 }
1834 else
1835 {
1836 li = list_find(l, (long) index);
1837 clear_tv(&li->li_tv);
1838 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001839 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001840 }
1841 return 0;
1842}
1843
1844 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001845ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001846{
1847 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001848 PyObject *iterator;
1849 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02001850 listitem_T *li;
1851 listitem_T *next;
1852 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001853 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001854 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02001855
1856 if (l->lv_lock)
1857 {
1858 PyErr_SetVim(_("list is locked"));
1859 return -1;
1860 }
1861
1862 PROC_RANGE
1863
1864 if (first == size)
1865 li = NULL;
1866 else
1867 {
1868 li = list_find(l, (long) first);
1869 if (li == NULL)
1870 {
1871 PyErr_SetVim(_("internal error: no vim list item"));
1872 return -1;
1873 }
1874 if (last > first)
1875 {
1876 i = last - first;
1877 while (i-- && li != NULL)
1878 {
1879 next = li->li_next;
1880 listitem_remove(l, li);
1881 li = next;
1882 }
1883 }
1884 }
1885
1886 if (obj == NULL)
1887 return 0;
1888
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001889 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001890 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001891
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001892 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001893 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001894 if (ConvertFromPyObject(item, &v) == -1)
1895 {
1896 Py_DECREF(iterator);
1897 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001898 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001899 }
1900 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001901 if (list_insert_tv(l, &v, li) == FAIL)
1902 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001903 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001904 PyErr_SetVim(_("internal error: failed to add item to list"));
1905 return -1;
1906 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001907 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001908 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001909 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02001910 return 0;
1911}
1912
1913 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001914ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001915{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001916 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001917 PyObject *lookup_dict;
1918
1919 if (l->lv_lock)
1920 {
1921 PyErr_SetVim(_("list is locked"));
1922 return NULL;
1923 }
1924
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001925 if (!(lookup_dict = PyDict_New()))
1926 return NULL;
1927
Bram Moolenaardb913952012-06-29 12:54:53 +02001928 if (list_py_concat(l, obj, lookup_dict) == -1)
1929 {
1930 Py_DECREF(lookup_dict);
1931 return NULL;
1932 }
1933 Py_DECREF(lookup_dict);
1934
1935 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001936 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001937}
1938
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001939static char *ListAttrs[] = {
1940 "locked",
1941 NULL
1942};
1943
1944 static PyObject *
1945ListDir(PyObject *self)
1946{
1947 return ObjectDir(self, ListAttrs);
1948}
1949
Bram Moolenaar66b79852012-09-21 14:00:35 +02001950 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001951ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001952{
1953 if (val == NULL)
1954 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001955 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001956 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001957 return -1;
1958 }
1959
1960 if (strcmp(name, "locked") == 0)
1961 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001962 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001963 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001964 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001965 return -1;
1966 }
1967 else
1968 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001969 int istrue = PyObject_IsTrue(val);
1970 if (istrue == -1)
1971 return -1;
1972 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001973 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001974 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001975 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001976 }
1977 return 0;
1978 }
1979 else
1980 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001981 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001982 return -1;
1983 }
1984}
1985
Bram Moolenaardb913952012-06-29 12:54:53 +02001986static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001987 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1988 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
1989 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001990};
1991
1992typedef struct
1993{
1994 PyObject_HEAD
1995 char_u *name;
1996} FunctionObject;
1997
1998static PyTypeObject FunctionType;
1999
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002000#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2001
Bram Moolenaardb913952012-06-29 12:54:53 +02002002 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002003FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002004{
2005 FunctionObject *self;
2006
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002007 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2008
Bram Moolenaardb913952012-06-29 12:54:53 +02002009 if (self == NULL)
2010 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002011
2012 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002013 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002014 if (!translated_function_exists(name))
2015 {
2016 PyErr_SetString(PyExc_ValueError,
2017 _("unnamed function does not exist"));
2018 return NULL;
2019 }
2020 self->name = vim_strsave(name);
2021 func_ref(self->name);
2022 }
2023 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002024 if ((self->name = get_expanded_name(name,
2025 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2026 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002027 {
Bram Moolenaar018acca2013-05-30 13:37:28 +02002028 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2029 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002030 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002031
2032 return (PyObject *)(self);
2033}
2034
2035 static PyObject *
2036FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2037{
2038 PyObject *self;
2039 char_u *name;
2040
2041 if (kwargs)
2042 {
2043 PyErr_SetString(PyExc_TypeError,
2044 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002045 return NULL;
2046 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002047
2048 if (!PyArg_ParseTuple(args, "s", &name))
2049 return NULL;
2050
2051 self = FunctionNew(subtype, name);
2052
2053 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002054}
2055
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002056 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002057FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002058{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002059 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002060 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002061
2062 DESTRUCTOR_FINISH(self);
2063}
2064
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002065static char *FunctionAttrs[] = {
2066 "softspace",
2067 NULL
2068};
2069
2070 static PyObject *
2071FunctionDir(PyObject *self)
2072{
2073 return ObjectDir(self, FunctionAttrs);
2074}
2075
Bram Moolenaardb913952012-06-29 12:54:53 +02002076 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002077FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002078{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002079 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002080 typval_T args;
2081 typval_T selfdicttv;
2082 typval_T rettv;
2083 dict_T *selfdict = NULL;
2084 PyObject *selfdictObject;
2085 PyObject *result;
2086 int error;
2087
2088 if (ConvertFromPyObject(argsObject, &args) == -1)
2089 return NULL;
2090
2091 if (kwargs != NULL)
2092 {
2093 selfdictObject = PyDict_GetItemString(kwargs, "self");
2094 if (selfdictObject != NULL)
2095 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002096 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002097 {
2098 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002099 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002100 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002101 selfdict = selfdicttv.vval.v_dict;
2102 }
2103 }
2104
Bram Moolenaar71700b82013-05-15 17:49:05 +02002105 Py_BEGIN_ALLOW_THREADS
2106 Python_Lock_Vim();
2107
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002108 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002109 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002110
2111 Python_Release_Vim();
2112 Py_END_ALLOW_THREADS
2113
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002114 if (VimTryEnd())
2115 result = NULL;
2116 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002117 {
2118 result = NULL;
2119 PyErr_SetVim(_("failed to run function"));
2120 }
2121 else
2122 result = ConvertToPyObject(&rettv);
2123
Bram Moolenaardb913952012-06-29 12:54:53 +02002124 clear_tv(&args);
2125 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002126 if (selfdict != NULL)
2127 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002128
2129 return result;
2130}
2131
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002132 static PyObject *
2133FunctionRepr(FunctionObject *self)
2134{
2135 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2136}
2137
Bram Moolenaardb913952012-06-29 12:54:53 +02002138static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002139 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2140 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002141};
2142
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002143/*
2144 * Options object
2145 */
2146
2147static PyTypeObject OptionsType;
2148
2149typedef int (*checkfun)(void *);
2150
2151typedef struct
2152{
2153 PyObject_HEAD
2154 int opt_type;
2155 void *from;
2156 checkfun Check;
2157 PyObject *fromObj;
2158} OptionsObject;
2159
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002160 static int
2161dummy_check(void *arg UNUSED)
2162{
2163 return 0;
2164}
2165
2166 static PyObject *
2167OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2168{
2169 OptionsObject *self;
2170
Bram Moolenaar774267b2013-05-21 20:51:59 +02002171 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002172 if (self == NULL)
2173 return NULL;
2174
2175 self->opt_type = opt_type;
2176 self->from = from;
2177 self->Check = Check;
2178 self->fromObj = fromObj;
2179 if (fromObj)
2180 Py_INCREF(fromObj);
2181
2182 return (PyObject *)(self);
2183}
2184
2185 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002186OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002187{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002188 PyObject_GC_UnTrack((void *)(self));
2189 Py_XDECREF(self->fromObj);
2190 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002191}
2192
2193 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002194OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002195{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002196 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002197 return 0;
2198}
2199
2200 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002201OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002202{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002203 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002204 return 0;
2205}
2206
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002207 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002208OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002209{
2210 char_u *key;
2211 int flags;
2212 long numval;
2213 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002214 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002215
Bram Moolenaard6e39182013-05-21 18:30:34 +02002216 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002217 return NULL;
2218
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002219 if (!(key = StringToChars(keyObject, &todecref)))
2220 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002221
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002222 if (*key == NUL)
2223 {
2224 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002225 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002226 return NULL;
2227 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002228
2229 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002230 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002231
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002232 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002233
2234 if (flags == 0)
2235 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002236 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002237 return NULL;
2238 }
2239
2240 if (flags & SOPT_UNSET)
2241 {
2242 Py_INCREF(Py_None);
2243 return Py_None;
2244 }
2245 else if (flags & SOPT_BOOL)
2246 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002247 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002248 r = numval ? Py_True : Py_False;
2249 Py_INCREF(r);
2250 return r;
2251 }
2252 else if (flags & SOPT_NUM)
2253 return PyInt_FromLong(numval);
2254 else if (flags & SOPT_STRING)
2255 {
2256 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002257 {
2258 PyObject *r = PyBytes_FromString((char *) stringval);
2259 vim_free(stringval);
2260 return r;
2261 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002262 else
2263 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002264 PyErr_SetString(PyExc_RuntimeError,
2265 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002266 return NULL;
2267 }
2268 }
2269 else
2270 {
2271 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2272 return NULL;
2273 }
2274}
2275
2276 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002277set_option_value_err(key, numval, stringval, opt_flags)
2278 char_u *key;
2279 int numval;
2280 char_u *stringval;
2281 int opt_flags;
2282{
2283 char_u *errmsg;
2284
2285 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2286 {
2287 if (VimTryEnd())
2288 return FAIL;
2289 PyErr_SetVim((char *)errmsg);
2290 return FAIL;
2291 }
2292 return OK;
2293}
2294
2295 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002296set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2297 char_u *key;
2298 int numval;
2299 char_u *stringval;
2300 int opt_flags;
2301 int opt_type;
2302 void *from;
2303{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002304 win_T *save_curwin = NULL;
2305 tabpage_T *save_curtab = NULL;
2306 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002307 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002308
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002309 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002310 switch (opt_type)
2311 {
2312 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002313 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2314 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002315 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002316 if (VimTryEnd())
2317 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002318 PyErr_SetVim("Problem while switching windows.");
2319 return -1;
2320 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002321 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002322 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002323 if (r == FAIL)
2324 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002325 break;
2326 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002327 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002328 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002329 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002330 if (r == FAIL)
2331 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002332 break;
2333 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002334 r = set_option_value_err(key, numval, stringval, opt_flags);
2335 if (r == FAIL)
2336 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002337 break;
2338 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002339 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002340}
2341
2342 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002343OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002344{
2345 char_u *key;
2346 int flags;
2347 int opt_flags;
2348 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002349 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002350
Bram Moolenaard6e39182013-05-21 18:30:34 +02002351 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002352 return -1;
2353
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002354 if (!(key = StringToChars(keyObject, &todecref)))
2355 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002356
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002357 if (*key == NUL)
2358 {
2359 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002360 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002361 return -1;
2362 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002363
2364 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002365 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002366
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002367 if (flags == 0)
2368 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002369 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002370 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002371 return -1;
2372 }
2373
2374 if (valObject == NULL)
2375 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002376 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002377 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002378 PyErr_SetString(PyExc_ValueError,
2379 _("unable to unset global option"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002380 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002381 return -1;
2382 }
2383 else if (!(flags & SOPT_GLOBAL))
2384 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002385 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2386 "without global value"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002387 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002388 return -1;
2389 }
2390 else
2391 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002392 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002393 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002394 return 0;
2395 }
2396 }
2397
Bram Moolenaard6e39182013-05-21 18:30:34 +02002398 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002399
2400 if (flags & SOPT_BOOL)
2401 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002402 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002403
Bram Moolenaarb983f752013-05-15 16:11:50 +02002404 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002405 r = -1;
2406 else
2407 r = set_option_value_for(key, istrue, NULL,
2408 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002409 }
2410 else if (flags & SOPT_NUM)
2411 {
2412 int val;
2413
2414#if PY_MAJOR_VERSION < 3
2415 if (PyInt_Check(valObject))
2416 val = PyInt_AsLong(valObject);
2417 else
2418#endif
2419 if (PyLong_Check(valObject))
2420 val = PyLong_AsLong(valObject);
2421 else
2422 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002423 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002424 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002425 return -1;
2426 }
2427
2428 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002429 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002430 }
2431 else
2432 {
2433 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002434 PyObject *todecref;
2435
2436 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002437 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002438 r = set_option_value_for(key, 0, val, opt_flags,
2439 self->opt_type, self->from);
2440 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002441 }
2442 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002443 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002444 }
2445
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002446 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002447
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002448 return r;
2449}
2450
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002451static PyMappingMethods OptionsAsMapping = {
2452 (lenfunc) NULL,
2453 (binaryfunc) OptionsItem,
2454 (objobjargproc) OptionsAssItem,
2455};
2456
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002457/* Tabpage object
2458 */
2459
2460typedef struct
2461{
2462 PyObject_HEAD
2463 tabpage_T *tab;
2464} TabPageObject;
2465
2466static PyObject *WinListNew(TabPageObject *tabObject);
2467
2468static PyTypeObject TabPageType;
2469
2470 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002471CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002472{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002473 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002474 {
2475 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2476 return -1;
2477 }
2478
2479 return 0;
2480}
2481
2482 static PyObject *
2483TabPageNew(tabpage_T *tab)
2484{
2485 TabPageObject *self;
2486
2487 if (TAB_PYTHON_REF(tab))
2488 {
2489 self = TAB_PYTHON_REF(tab);
2490 Py_INCREF(self);
2491 }
2492 else
2493 {
2494 self = PyObject_NEW(TabPageObject, &TabPageType);
2495 if (self == NULL)
2496 return NULL;
2497 self->tab = tab;
2498 TAB_PYTHON_REF(tab) = self;
2499 }
2500
2501 return (PyObject *)(self);
2502}
2503
2504 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002505TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002506{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002507 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2508 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002509
2510 DESTRUCTOR_FINISH(self);
2511}
2512
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002513static char *TabPageAttrs[] = {
2514 "windows", "number", "vars", "window", "valid",
2515 NULL
2516};
2517
2518 static PyObject *
2519TabPageDir(PyObject *self)
2520{
2521 return ObjectDir(self, TabPageAttrs);
2522}
2523
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002524 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002525TabPageAttrValid(TabPageObject *self, char *name)
2526{
2527 PyObject *r;
2528
2529 if (strcmp(name, "valid") != 0)
2530 return NULL;
2531
2532 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2533 Py_INCREF(r);
2534 return r;
2535}
2536
2537 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002538TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002539{
2540 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002541 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002542 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002543 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002544 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002545 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002546 else if (strcmp(name, "window") == 0)
2547 {
2548 /* For current tab window.c does not bother to set or update tp_curwin
2549 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002550 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002551 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002552 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002553 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002554 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002555 else if (strcmp(name, "__members__") == 0)
2556 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002557 return NULL;
2558}
2559
2560 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002561TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002562{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002563 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002564 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002565 else
2566 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002567 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002568
2569 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002570 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2571 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002572 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002573 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002574 }
2575}
2576
2577static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002578 /* name, function, calling, documentation */
2579 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2580 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002581};
2582
2583/*
2584 * Window list object
2585 */
2586
2587static PyTypeObject TabListType;
2588static PySequenceMethods TabListAsSeq;
2589
2590typedef struct
2591{
2592 PyObject_HEAD
2593} TabListObject;
2594
2595 static PyInt
2596TabListLength(PyObject *self UNUSED)
2597{
2598 tabpage_T *tp = first_tabpage;
2599 PyInt n = 0;
2600
2601 while (tp != NULL)
2602 {
2603 ++n;
2604 tp = tp->tp_next;
2605 }
2606
2607 return n;
2608}
2609
2610 static PyObject *
2611TabListItem(PyObject *self UNUSED, PyInt n)
2612{
2613 tabpage_T *tp;
2614
2615 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2616 if (n == 0)
2617 return TabPageNew(tp);
2618
2619 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2620 return NULL;
2621}
2622
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002623/* Window object
2624 */
2625
2626typedef struct
2627{
2628 PyObject_HEAD
2629 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002630 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002631} WindowObject;
2632
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002633static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002634
2635 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002636CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002637{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002638 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002639 {
2640 PyErr_SetVim(_("attempt to refer to deleted window"));
2641 return -1;
2642 }
2643
2644 return 0;
2645}
2646
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002647 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002648WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002649{
2650 /* We need to handle deletion of windows underneath us.
2651 * If we add a "w_python*_ref" field to the win_T structure,
2652 * then we can get at it in win_free() in vim. We then
2653 * need to create only ONE Python object per window - if
2654 * we try to create a second, just INCREF the existing one
2655 * and return it. The (single) Python object referring to
2656 * the window is stored in "w_python*_ref".
2657 * On a win_free() we set the Python object's win_T* field
2658 * to an invalid value. We trap all uses of a window
2659 * object, and reject them if the win_T* field is invalid.
2660 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002661 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002662 * w_python_ref and w_python3_ref fields respectively.
2663 */
2664
2665 WindowObject *self;
2666
2667 if (WIN_PYTHON_REF(win))
2668 {
2669 self = WIN_PYTHON_REF(win);
2670 Py_INCREF(self);
2671 }
2672 else
2673 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002674 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002675 if (self == NULL)
2676 return NULL;
2677 self->win = win;
2678 WIN_PYTHON_REF(win) = self;
2679 }
2680
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002681 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2682
Bram Moolenaar971db462013-05-12 18:44:48 +02002683 return (PyObject *)(self);
2684}
2685
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002686 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002687WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002688{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002689 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002690 if (self->win && self->win != INVALID_WINDOW_VALUE)
2691 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002692 Py_XDECREF(((PyObject *)(self->tabObject)));
2693 PyObject_GC_Del((void *)(self));
2694}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002695
Bram Moolenaar774267b2013-05-21 20:51:59 +02002696 static int
2697WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2698{
2699 Py_VISIT(((PyObject *)(self->tabObject)));
2700 return 0;
2701}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002702
Bram Moolenaar774267b2013-05-21 20:51:59 +02002703 static int
2704WindowClear(WindowObject *self)
2705{
2706 Py_CLEAR(self->tabObject);
2707 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002708}
2709
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002710 static win_T *
2711get_firstwin(TabPageObject *tabObject)
2712{
2713 if (tabObject)
2714 {
2715 if (CheckTabPage(tabObject))
2716 return NULL;
2717 /* For current tab window.c does not bother to set or update tp_firstwin
2718 */
2719 else if (tabObject->tab == curtab)
2720 return firstwin;
2721 else
2722 return tabObject->tab->tp_firstwin;
2723 }
2724 else
2725 return firstwin;
2726}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002727static char *WindowAttrs[] = {
2728 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2729 "tabpage", "valid",
2730 NULL
2731};
2732
2733 static PyObject *
2734WindowDir(PyObject *self)
2735{
2736 return ObjectDir(self, WindowAttrs);
2737}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002738
Bram Moolenaar971db462013-05-12 18:44:48 +02002739 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002740WindowAttrValid(WindowObject *self, char *name)
2741{
2742 PyObject *r;
2743
2744 if (strcmp(name, "valid") != 0)
2745 return NULL;
2746
2747 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2748 Py_INCREF(r);
2749 return r;
2750}
2751
2752 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002753WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002754{
2755 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002756 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002757 else if (strcmp(name, "cursor") == 0)
2758 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002759 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002760
2761 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2762 }
2763 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002764 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002765#ifdef FEAT_WINDOWS
2766 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002767 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002768#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002769#ifdef FEAT_VERTSPLIT
2770 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002771 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002772 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002773 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002774#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002775 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002776 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002777 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002778 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2779 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002780 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002781 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002782 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002783 return NULL;
2784 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002785 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002786 }
2787 else if (strcmp(name, "tabpage") == 0)
2788 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002789 Py_INCREF(self->tabObject);
2790 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002791 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002792 else if (strcmp(name, "__members__") == 0)
2793 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002794 else
2795 return NULL;
2796}
2797
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002798 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002799WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002800{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002801 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002802 return -1;
2803
2804 if (strcmp(name, "buffer") == 0)
2805 {
2806 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2807 return -1;
2808 }
2809 else if (strcmp(name, "cursor") == 0)
2810 {
2811 long lnum;
2812 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002813
2814 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2815 return -1;
2816
Bram Moolenaard6e39182013-05-21 18:30:34 +02002817 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002818 {
2819 PyErr_SetVim(_("cursor position outside buffer"));
2820 return -1;
2821 }
2822
2823 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002824 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002825 return -1;
2826
Bram Moolenaard6e39182013-05-21 18:30:34 +02002827 self->win->w_cursor.lnum = lnum;
2828 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002829#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002830 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002831#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002832 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002833 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002834
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002835 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002836 return 0;
2837 }
2838 else if (strcmp(name, "height") == 0)
2839 {
2840 int height;
2841 win_T *savewin;
2842
2843 if (!PyArg_Parse(val, "i", &height))
2844 return -1;
2845
2846#ifdef FEAT_GUI
2847 need_mouse_correct = TRUE;
2848#endif
2849 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002850 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002851
2852 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002853 win_setheight(height);
2854 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002855 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002856 return -1;
2857
2858 return 0;
2859 }
2860#ifdef FEAT_VERTSPLIT
2861 else if (strcmp(name, "width") == 0)
2862 {
2863 int width;
2864 win_T *savewin;
2865
2866 if (!PyArg_Parse(val, "i", &width))
2867 return -1;
2868
2869#ifdef FEAT_GUI
2870 need_mouse_correct = TRUE;
2871#endif
2872 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002873 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002874
2875 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002876 win_setwidth(width);
2877 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002878 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002879 return -1;
2880
2881 return 0;
2882 }
2883#endif
2884 else
2885 {
2886 PyErr_SetString(PyExc_AttributeError, name);
2887 return -1;
2888 }
2889}
2890
2891 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002892WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002893{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002894 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002895 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002896 else
2897 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002898 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002899
Bram Moolenaar6d216452013-05-12 19:00:41 +02002900 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002901 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002902 (self));
2903 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002904 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002905 }
2906}
2907
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002908static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002909 /* name, function, calling, documentation */
2910 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2911 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002912};
2913
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002914/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002915 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002916 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002917
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002918static PyTypeObject WinListType;
2919static PySequenceMethods WinListAsSeq;
2920
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002921typedef struct
2922{
2923 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002924 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002925} WinListObject;
2926
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002927 static PyObject *
2928WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002929{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002930 WinListObject *self;
2931
2932 self = PyObject_NEW(WinListObject, &WinListType);
2933 self->tabObject = tabObject;
2934 Py_INCREF(tabObject);
2935
2936 return (PyObject *)(self);
2937}
2938
2939 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002940WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002941{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002942 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002943
2944 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02002945 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002946 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02002947 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002948
2949 DESTRUCTOR_FINISH(self);
2950}
2951
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002952 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002953WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002954{
2955 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002956 PyInt n = 0;
2957
Bram Moolenaard6e39182013-05-21 18:30:34 +02002958 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002959 return -1;
2960
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002961 while (w != NULL)
2962 {
2963 ++n;
2964 w = W_NEXT(w);
2965 }
2966
2967 return n;
2968}
2969
2970 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002971WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002972{
2973 win_T *w;
2974
Bram Moolenaard6e39182013-05-21 18:30:34 +02002975 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002976 return NULL;
2977
2978 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002979 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002980 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002981
2982 PyErr_SetString(PyExc_IndexError, _("no such window"));
2983 return NULL;
2984}
2985
2986/* Convert a Python string into a Vim line.
2987 *
2988 * The result is in allocated memory. All internal nulls are replaced by
2989 * newline characters. It is an error for the string to contain newline
2990 * characters.
2991 *
2992 * On errors, the Python exception data is set, and NULL is returned.
2993 */
2994 static char *
2995StringToLine(PyObject *obj)
2996{
2997 const char *str;
2998 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002999 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003000 PyInt len;
3001 PyInt i;
3002 char *p;
3003
3004 if (obj == NULL || !PyString_Check(obj))
3005 {
3006 PyErr_BadArgument();
3007 return NULL;
3008 }
3009
Bram Moolenaar19e60942011-06-19 00:27:51 +02003010 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
3011 str = PyString_AsString(bytes);
3012 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003013
3014 /*
3015 * Error checking: String must not contain newlines, as we
3016 * are replacing a single line, and we must replace it with
3017 * a single line.
3018 * A trailing newline is removed, so that append(f.readlines()) works.
3019 */
3020 p = memchr(str, '\n', len);
3021 if (p != NULL)
3022 {
3023 if (p == str + len - 1)
3024 --len;
3025 else
3026 {
3027 PyErr_SetVim(_("string cannot contain newlines"));
3028 return NULL;
3029 }
3030 }
3031
3032 /* Create a copy of the string, with internal nulls replaced by
3033 * newline characters, as is the vim convention.
3034 */
3035 save = (char *)alloc((unsigned)(len+1));
3036 if (save == NULL)
3037 {
3038 PyErr_NoMemory();
3039 return NULL;
3040 }
3041
3042 for (i = 0; i < len; ++i)
3043 {
3044 if (str[i] == '\0')
3045 save[i] = '\n';
3046 else
3047 save[i] = str[i];
3048 }
3049
3050 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003051 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003052
3053 return save;
3054}
3055
3056/* Get a line from the specified buffer. The line number is
3057 * in Vim format (1-based). The line is returned as a Python
3058 * string object.
3059 */
3060 static PyObject *
3061GetBufferLine(buf_T *buf, PyInt n)
3062{
3063 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3064}
3065
3066
3067/* Get a list of lines from the specified buffer. The line numbers
3068 * are in Vim format (1-based). The range is from lo up to, but not
3069 * including, hi. The list is returned as a Python list of string objects.
3070 */
3071 static PyObject *
3072GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3073{
3074 PyInt i;
3075 PyInt n = hi - lo;
3076 PyObject *list = PyList_New(n);
3077
3078 if (list == NULL)
3079 return NULL;
3080
3081 for (i = 0; i < n; ++i)
3082 {
3083 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3084
3085 /* Error check - was the Python string creation OK? */
3086 if (str == NULL)
3087 {
3088 Py_DECREF(list);
3089 return NULL;
3090 }
3091
3092 /* Set the list item */
3093 if (PyList_SetItem(list, i, str))
3094 {
3095 Py_DECREF(str);
3096 Py_DECREF(list);
3097 return NULL;
3098 }
3099 }
3100
3101 /* The ownership of the Python list is passed to the caller (ie,
3102 * the caller should Py_DECREF() the object when it is finished
3103 * with it).
3104 */
3105
3106 return list;
3107}
3108
3109/*
3110 * Check if deleting lines made the cursor position invalid.
3111 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3112 * deleted).
3113 */
3114 static void
3115py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3116{
3117 if (curwin->w_cursor.lnum >= lo)
3118 {
3119 /* Adjust the cursor position if it's in/after the changed
3120 * lines. */
3121 if (curwin->w_cursor.lnum >= hi)
3122 {
3123 curwin->w_cursor.lnum += extra;
3124 check_cursor_col();
3125 }
3126 else if (extra < 0)
3127 {
3128 curwin->w_cursor.lnum = lo;
3129 check_cursor();
3130 }
3131 else
3132 check_cursor_col();
3133 changed_cline_bef_curs();
3134 }
3135 invalidate_botline();
3136}
3137
Bram Moolenaar19e60942011-06-19 00:27:51 +02003138/*
3139 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003140 * in Vim format (1-based). The replacement line is given as
3141 * a Python string object. The object is checked for validity
3142 * and correct format. Errors are returned as a value of FAIL.
3143 * The return value is OK on success.
3144 * If OK is returned and len_change is not NULL, *len_change
3145 * is set to the change in the buffer length.
3146 */
3147 static int
3148SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3149{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003150 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003151 * There are three cases:
3152 * 1. NULL, or None - this is a deletion.
3153 * 2. A string - this is a replacement.
3154 * 3. Anything else - this is an error.
3155 */
3156 if (line == Py_None || line == NULL)
3157 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003158 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003159
3160 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003161 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003162
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003163 VimTryStart();
3164
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003165 if (u_savedel((linenr_T)n, 1L) == FAIL)
3166 PyErr_SetVim(_("cannot save undo information"));
3167 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3168 PyErr_SetVim(_("cannot delete line"));
3169 else
3170 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003171 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003172 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3173 deleted_lines_mark((linenr_T)n, 1L);
3174 }
3175
Bram Moolenaar105bc352013-05-17 16:03:57 +02003176 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003177
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003178 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003179 return FAIL;
3180
3181 if (len_change)
3182 *len_change = -1;
3183
3184 return OK;
3185 }
3186 else if (PyString_Check(line))
3187 {
3188 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003189 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003190
3191 if (save == NULL)
3192 return FAIL;
3193
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003194 VimTryStart();
3195
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003196 /* We do not need to free "save" if ml_replace() consumes it. */
3197 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003198 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003199
3200 if (u_savesub((linenr_T)n) == FAIL)
3201 {
3202 PyErr_SetVim(_("cannot save undo information"));
3203 vim_free(save);
3204 }
3205 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3206 {
3207 PyErr_SetVim(_("cannot replace line"));
3208 vim_free(save);
3209 }
3210 else
3211 changed_bytes((linenr_T)n, 0);
3212
Bram Moolenaar105bc352013-05-17 16:03:57 +02003213 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003214
3215 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003216 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003217 check_cursor_col();
3218
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003219 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003220 return FAIL;
3221
3222 if (len_change)
3223 *len_change = 0;
3224
3225 return OK;
3226 }
3227 else
3228 {
3229 PyErr_BadArgument();
3230 return FAIL;
3231 }
3232}
3233
Bram Moolenaar19e60942011-06-19 00:27:51 +02003234/* Replace a range of lines in the specified buffer. The line numbers are in
3235 * Vim format (1-based). The range is from lo up to, but not including, hi.
3236 * The replacement lines are given as a Python list of string objects. The
3237 * list is checked for validity and correct format. Errors are returned as a
3238 * value of FAIL. The return value is OK on success.
3239 * If OK is returned and len_change is not NULL, *len_change
3240 * is set to the change in the buffer length.
3241 */
3242 static int
3243SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3244{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003245 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003246 * There are three cases:
3247 * 1. NULL, or None - this is a deletion.
3248 * 2. A list - this is a replacement.
3249 * 3. Anything else - this is an error.
3250 */
3251 if (list == Py_None || list == NULL)
3252 {
3253 PyInt i;
3254 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003255 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003256
3257 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003258 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003259 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003260
3261 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3262 PyErr_SetVim(_("cannot save undo information"));
3263 else
3264 {
3265 for (i = 0; i < n; ++i)
3266 {
3267 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3268 {
3269 PyErr_SetVim(_("cannot delete line"));
3270 break;
3271 }
3272 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003273 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003274 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3275 deleted_lines_mark((linenr_T)lo, (long)i);
3276 }
3277
Bram Moolenaar105bc352013-05-17 16:03:57 +02003278 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003279
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003280 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003281 return FAIL;
3282
3283 if (len_change)
3284 *len_change = -n;
3285
3286 return OK;
3287 }
3288 else if (PyList_Check(list))
3289 {
3290 PyInt i;
3291 PyInt new_len = PyList_Size(list);
3292 PyInt old_len = hi - lo;
3293 PyInt extra = 0; /* lines added to text, can be negative */
3294 char **array;
3295 buf_T *savebuf;
3296
3297 if (new_len == 0) /* avoid allocating zero bytes */
3298 array = NULL;
3299 else
3300 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003301 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003302 if (array == NULL)
3303 {
3304 PyErr_NoMemory();
3305 return FAIL;
3306 }
3307 }
3308
3309 for (i = 0; i < new_len; ++i)
3310 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003311 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003312
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003313 if (!(line = PyList_GetItem(list, i)) ||
3314 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003315 {
3316 while (i)
3317 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003318 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003319 return FAIL;
3320 }
3321 }
3322
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003323 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003324 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003325
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003326 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003327 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003328
3329 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3330 PyErr_SetVim(_("cannot save undo information"));
3331
3332 /* If the size of the range is reducing (ie, new_len < old_len) we
3333 * need to delete some old_len. We do this at the start, by
3334 * repeatedly deleting line "lo".
3335 */
3336 if (!PyErr_Occurred())
3337 {
3338 for (i = 0; i < old_len - new_len; ++i)
3339 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3340 {
3341 PyErr_SetVim(_("cannot delete line"));
3342 break;
3343 }
3344 extra -= i;
3345 }
3346
3347 /* For as long as possible, replace the existing old_len with the
3348 * new old_len. This is a more efficient operation, as it requires
3349 * less memory allocation and freeing.
3350 */
3351 if (!PyErr_Occurred())
3352 {
3353 for (i = 0; i < old_len && i < new_len; ++i)
3354 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3355 == FAIL)
3356 {
3357 PyErr_SetVim(_("cannot replace line"));
3358 break;
3359 }
3360 }
3361 else
3362 i = 0;
3363
3364 /* Now we may need to insert the remaining new old_len. If we do, we
3365 * must free the strings as we finish with them (we can't pass the
3366 * responsibility to vim in this case).
3367 */
3368 if (!PyErr_Occurred())
3369 {
3370 while (i < new_len)
3371 {
3372 if (ml_append((linenr_T)(lo + i - 1),
3373 (char_u *)array[i], 0, FALSE) == FAIL)
3374 {
3375 PyErr_SetVim(_("cannot insert line"));
3376 break;
3377 }
3378 vim_free(array[i]);
3379 ++i;
3380 ++extra;
3381 }
3382 }
3383
3384 /* Free any left-over old_len, as a result of an error */
3385 while (i < new_len)
3386 {
3387 vim_free(array[i]);
3388 ++i;
3389 }
3390
3391 /* Free the array of old_len. All of its contents have now
3392 * been dealt with (either freed, or the responsibility passed
3393 * to vim.
3394 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003395 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003396
3397 /* Adjust marks. Invalidate any which lie in the
3398 * changed range, and move any in the remainder of the buffer.
3399 */
3400 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3401 (long)MAXLNUM, (long)extra);
3402 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3403
Bram Moolenaar105bc352013-05-17 16:03:57 +02003404 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003405 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3406
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003407 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003408 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003409
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003410 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003411 return FAIL;
3412
3413 if (len_change)
3414 *len_change = new_len - old_len;
3415
3416 return OK;
3417 }
3418 else
3419 {
3420 PyErr_BadArgument();
3421 return FAIL;
3422 }
3423}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003424
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003425/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003426 * The line number is in Vim format (1-based). The lines to be inserted are
3427 * given as a Python list of string objects or as a single string. The lines
3428 * to be added are checked for validity and correct format. Errors are
3429 * returned as a value of FAIL. The return value is OK on success.
3430 * If OK is returned and len_change is not NULL, *len_change
3431 * is set to the change in the buffer length.
3432 */
3433 static int
3434InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3435{
3436 /* First of all, we check the type of the supplied Python object.
3437 * It must be a string or a list, or the call is in error.
3438 */
3439 if (PyString_Check(lines))
3440 {
3441 char *str = StringToLine(lines);
3442 buf_T *savebuf;
3443
3444 if (str == NULL)
3445 return FAIL;
3446
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003447 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003448 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003449 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003450
3451 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3452 PyErr_SetVim(_("cannot save undo information"));
3453 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3454 PyErr_SetVim(_("cannot insert line"));
3455 else
3456 appended_lines_mark((linenr_T)n, 1L);
3457
3458 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003459 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003460 update_screen(VALID);
3461
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003462 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003463 return FAIL;
3464
3465 if (len_change)
3466 *len_change = 1;
3467
3468 return OK;
3469 }
3470 else if (PyList_Check(lines))
3471 {
3472 PyInt i;
3473 PyInt size = PyList_Size(lines);
3474 char **array;
3475 buf_T *savebuf;
3476
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003477 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003478 if (array == NULL)
3479 {
3480 PyErr_NoMemory();
3481 return FAIL;
3482 }
3483
3484 for (i = 0; i < size; ++i)
3485 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003486 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003487
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003488 if (!(line = PyList_GetItem(lines, i)) ||
3489 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003490 {
3491 while (i)
3492 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003493 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003494 return FAIL;
3495 }
3496 }
3497
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003498 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003499 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003500 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003501
3502 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3503 PyErr_SetVim(_("cannot save undo information"));
3504 else
3505 {
3506 for (i = 0; i < size; ++i)
3507 {
3508 if (ml_append((linenr_T)(n + i),
3509 (char_u *)array[i], 0, FALSE) == FAIL)
3510 {
3511 PyErr_SetVim(_("cannot insert line"));
3512
3513 /* Free the rest of the lines */
3514 while (i < size)
3515 vim_free(array[i++]);
3516
3517 break;
3518 }
3519 vim_free(array[i]);
3520 }
3521 if (i > 0)
3522 appended_lines_mark((linenr_T)n, (long)i);
3523 }
3524
3525 /* Free the array of lines. All of its contents have now
3526 * been freed.
3527 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003528 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003529
Bram Moolenaar105bc352013-05-17 16:03:57 +02003530 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003531 update_screen(VALID);
3532
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003533 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003534 return FAIL;
3535
3536 if (len_change)
3537 *len_change = size;
3538
3539 return OK;
3540 }
3541 else
3542 {
3543 PyErr_BadArgument();
3544 return FAIL;
3545 }
3546}
3547
3548/*
3549 * Common routines for buffers and line ranges
3550 * -------------------------------------------
3551 */
3552
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003553typedef struct
3554{
3555 PyObject_HEAD
3556 buf_T *buf;
3557} BufferObject;
3558
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003559 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003560CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003561{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003562 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003563 {
3564 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3565 return -1;
3566 }
3567
3568 return 0;
3569}
3570
3571 static PyObject *
3572RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3573{
3574 if (CheckBuffer(self))
3575 return NULL;
3576
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003577 if (end == -1)
3578 end = self->buf->b_ml.ml_line_count;
3579
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003580 if (n < 0)
3581 n += end - start + 1;
3582
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003583 if (n < 0 || n > end - start)
3584 {
3585 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3586 return NULL;
3587 }
3588
3589 return GetBufferLine(self->buf, n+start);
3590}
3591
3592 static PyObject *
3593RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3594{
3595 PyInt size;
3596
3597 if (CheckBuffer(self))
3598 return NULL;
3599
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003600 if (end == -1)
3601 end = self->buf->b_ml.ml_line_count;
3602
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003603 size = end - start + 1;
3604
3605 if (lo < 0)
3606 lo = 0;
3607 else if (lo > size)
3608 lo = size;
3609 if (hi < 0)
3610 hi = 0;
3611 if (hi < lo)
3612 hi = lo;
3613 else if (hi > size)
3614 hi = size;
3615
3616 return GetBufferLineList(self->buf, lo+start, hi+start);
3617}
3618
3619 static PyInt
3620RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3621{
3622 PyInt len_change;
3623
3624 if (CheckBuffer(self))
3625 return -1;
3626
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003627 if (end == -1)
3628 end = self->buf->b_ml.ml_line_count;
3629
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003630 if (n < 0)
3631 n += end - start + 1;
3632
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003633 if (n < 0 || n > end - start)
3634 {
3635 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3636 return -1;
3637 }
3638
3639 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3640 return -1;
3641
3642 if (new_end)
3643 *new_end = end + len_change;
3644
3645 return 0;
3646}
3647
Bram Moolenaar19e60942011-06-19 00:27:51 +02003648 static PyInt
3649RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3650{
3651 PyInt size;
3652 PyInt len_change;
3653
3654 /* Self must be a valid buffer */
3655 if (CheckBuffer(self))
3656 return -1;
3657
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003658 if (end == -1)
3659 end = self->buf->b_ml.ml_line_count;
3660
Bram Moolenaar19e60942011-06-19 00:27:51 +02003661 /* Sort out the slice range */
3662 size = end - start + 1;
3663
3664 if (lo < 0)
3665 lo = 0;
3666 else if (lo > size)
3667 lo = size;
3668 if (hi < 0)
3669 hi = 0;
3670 if (hi < lo)
3671 hi = lo;
3672 else if (hi > size)
3673 hi = size;
3674
3675 if (SetBufferLineList(self->buf, lo + start, hi + start,
3676 val, &len_change) == FAIL)
3677 return -1;
3678
3679 if (new_end)
3680 *new_end = end + len_change;
3681
3682 return 0;
3683}
3684
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003685
3686 static PyObject *
3687RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3688{
3689 PyObject *lines;
3690 PyInt len_change;
3691 PyInt max;
3692 PyInt n;
3693
3694 if (CheckBuffer(self))
3695 return NULL;
3696
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003697 if (end == -1)
3698 end = self->buf->b_ml.ml_line_count;
3699
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003700 max = n = end - start + 1;
3701
3702 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3703 return NULL;
3704
3705 if (n < 0 || n > max)
3706 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003707 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003708 return NULL;
3709 }
3710
3711 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3712 return NULL;
3713
3714 if (new_end)
3715 *new_end = end + len_change;
3716
3717 Py_INCREF(Py_None);
3718 return Py_None;
3719}
3720
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003721/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003722 */
3723
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003724static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003725static PySequenceMethods RangeAsSeq;
3726static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003727
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003728typedef struct
3729{
3730 PyObject_HEAD
3731 BufferObject *buf;
3732 PyInt start;
3733 PyInt end;
3734} RangeObject;
3735
3736 static PyObject *
3737RangeNew(buf_T *buf, PyInt start, PyInt end)
3738{
3739 BufferObject *bufr;
3740 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003741 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003742 if (self == NULL)
3743 return NULL;
3744
3745 bufr = (BufferObject *)BufferNew(buf);
3746 if (bufr == NULL)
3747 {
3748 Py_DECREF(self);
3749 return NULL;
3750 }
3751 Py_INCREF(bufr);
3752
3753 self->buf = bufr;
3754 self->start = start;
3755 self->end = end;
3756
3757 return (PyObject *)(self);
3758}
3759
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003760 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003761RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003762{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003763 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003764 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003765 PyObject_GC_Del((void *)(self));
3766}
3767
3768 static int
3769RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3770{
3771 Py_VISIT(((PyObject *)(self->buf)));
3772 return 0;
3773}
3774
3775 static int
3776RangeClear(RangeObject *self)
3777{
3778 Py_CLEAR(self->buf);
3779 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003780}
3781
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003782 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003783RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003784{
3785 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003786 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003787 return -1; /* ??? */
3788
Bram Moolenaard6e39182013-05-21 18:30:34 +02003789 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003790}
3791
3792 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003793RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003794{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003795 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003796}
3797
3798 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003799RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003800{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003801 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003802}
3803
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003804static char *RangeAttrs[] = {
3805 "start", "end",
3806 NULL
3807};
3808
3809 static PyObject *
3810RangeDir(PyObject *self)
3811{
3812 return ObjectDir(self, RangeAttrs);
3813}
3814
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003815 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003816RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003817{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003818 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003819}
3820
3821 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003822RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003823{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003824 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003825 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
3826 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003827 else
3828 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003829 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003830
3831 if (name == NULL)
3832 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003833
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003834 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02003835 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003836 }
3837}
3838
3839static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003840 /* name, function, calling, documentation */
3841 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003842 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3843 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003844};
3845
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003846static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003847static PySequenceMethods BufferAsSeq;
3848static PyMappingMethods BufferAsMapping;
3849
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003850 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003851BufferNew(buf_T *buf)
3852{
3853 /* We need to handle deletion of buffers underneath us.
3854 * If we add a "b_python*_ref" field to the buf_T structure,
3855 * then we can get at it in buf_freeall() in vim. We then
3856 * need to create only ONE Python object per buffer - if
3857 * we try to create a second, just INCREF the existing one
3858 * and return it. The (single) Python object referring to
3859 * the buffer is stored in "b_python*_ref".
3860 * Question: what to do on a buf_freeall(). We'll probably
3861 * have to either delete the Python object (DECREF it to
3862 * zero - a bad idea, as it leaves dangling refs!) or
3863 * set the buf_T * value to an invalid value (-1?), which
3864 * means we need checks in all access functions... Bah.
3865 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003866 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003867 * b_python_ref and b_python3_ref fields respectively.
3868 */
3869
3870 BufferObject *self;
3871
3872 if (BUF_PYTHON_REF(buf) != NULL)
3873 {
3874 self = BUF_PYTHON_REF(buf);
3875 Py_INCREF(self);
3876 }
3877 else
3878 {
3879 self = PyObject_NEW(BufferObject, &BufferType);
3880 if (self == NULL)
3881 return NULL;
3882 self->buf = buf;
3883 BUF_PYTHON_REF(buf) = self;
3884 }
3885
3886 return (PyObject *)(self);
3887}
3888
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003889 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003890BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003891{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003892 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3893 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003894
3895 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003896}
3897
Bram Moolenaar971db462013-05-12 18:44:48 +02003898 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003899BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003900{
3901 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003902 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003903 return -1; /* ??? */
3904
Bram Moolenaard6e39182013-05-21 18:30:34 +02003905 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003906}
3907
3908 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003909BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003910{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003911 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003912}
3913
3914 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003915BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003916{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003917 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003918}
3919
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003920static char *BufferAttrs[] = {
3921 "name", "number", "vars", "options", "valid",
3922 NULL
3923};
3924
3925 static PyObject *
3926BufferDir(PyObject *self)
3927{
3928 return ObjectDir(self, BufferAttrs);
3929}
3930
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003931 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003932BufferAttrValid(BufferObject *self, char *name)
3933{
3934 PyObject *r;
3935
3936 if (strcmp(name, "valid") != 0)
3937 return NULL;
3938
3939 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
3940 Py_INCREF(r);
3941 return r;
3942}
3943
3944 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003945BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003946{
3947 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02003948 return PyString_FromString((self->buf->b_ffname == NULL
3949 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003950 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003951 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003952 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003953 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003954 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003955 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3956 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003957 else if (strcmp(name, "__members__") == 0)
3958 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003959 else
3960 return NULL;
3961}
3962
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003963 static int
3964BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3965{
3966 if (CheckBuffer(self))
3967 return -1;
3968
3969 if (strcmp(name, "name") == 0)
3970 {
3971 char_u *val;
3972 aco_save_T aco;
3973 int r;
3974 PyObject *todecref;
3975
3976 if (!(val = StringToChars(valObject, &todecref)))
3977 return -1;
3978
3979 VimTryStart();
3980 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3981 aucmd_prepbuf(&aco, self->buf);
3982 r = rename_buffer(val);
3983 aucmd_restbuf(&aco);
3984 Py_XDECREF(todecref);
3985 if (VimTryEnd())
3986 return -1;
3987
3988 if (r == FAIL)
3989 {
3990 PyErr_SetVim(_("failed to rename buffer"));
3991 return -1;
3992 }
3993 return 0;
3994 }
3995 else
3996 {
3997 PyErr_SetString(PyExc_AttributeError, name);
3998 return -1;
3999 }
4000}
4001
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004002 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004003BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004004{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004005 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004006}
4007
4008 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004009BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004010{
4011 pos_T *posp;
4012 char *pmark;
4013 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004014 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004015
Bram Moolenaard6e39182013-05-21 18:30:34 +02004016 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004017 return NULL;
4018
4019 if (!PyArg_ParseTuple(args, "s", &pmark))
4020 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004021
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004022 if (STRLEN(pmark) != 1)
4023 {
4024 PyErr_SetString(PyExc_ValueError,
4025 _("mark name must be a single character"));
4026 return NULL;
4027 }
4028
4029 mark = *pmark;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004030 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004031 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004032 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004033 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004034 if (VimTryEnd())
4035 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036
4037 if (posp == NULL)
4038 {
4039 PyErr_SetVim(_("invalid mark name"));
4040 return NULL;
4041 }
4042
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004043 if (posp->lnum <= 0)
4044 {
4045 /* Or raise an error? */
4046 Py_INCREF(Py_None);
4047 return Py_None;
4048 }
4049
4050 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4051}
4052
4053 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004054BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004055{
4056 PyInt start;
4057 PyInt end;
4058
Bram Moolenaard6e39182013-05-21 18:30:34 +02004059 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004060 return NULL;
4061
4062 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4063 return NULL;
4064
Bram Moolenaard6e39182013-05-21 18:30:34 +02004065 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004066}
4067
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004068 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004069BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004070{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004071 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004072 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004073 else
4074 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004075 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004076
4077 if (name == NULL)
4078 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004079
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004080 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004081 }
4082}
4083
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004084static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004085 /* name, function, calling, documentation */
4086 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4087 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4088 {"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 +02004089 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4090 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091};
4092
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004093/*
4094 * Buffer list object - Implementation
4095 */
4096
4097static PyTypeObject BufMapType;
4098
4099typedef struct
4100{
4101 PyObject_HEAD
4102} BufMapObject;
4103
4104 static PyInt
4105BufMapLength(PyObject *self UNUSED)
4106{
4107 buf_T *b = firstbuf;
4108 PyInt n = 0;
4109
4110 while (b)
4111 {
4112 ++n;
4113 b = b->b_next;
4114 }
4115
4116 return n;
4117}
4118
4119 static PyObject *
4120BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4121{
4122 buf_T *b;
4123 int bnr;
4124
4125#if PY_MAJOR_VERSION < 3
4126 if (PyInt_Check(keyObject))
4127 bnr = PyInt_AsLong(keyObject);
4128 else
4129#endif
4130 if (PyLong_Check(keyObject))
4131 bnr = PyLong_AsLong(keyObject);
4132 else
4133 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004134 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004135 return NULL;
4136 }
4137
4138 b = buflist_findnr(bnr);
4139
4140 if (b)
4141 return BufferNew(b);
4142 else
4143 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004144 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004145 return NULL;
4146 }
4147}
4148
4149 static void
4150BufMapIterDestruct(PyObject *buffer)
4151{
4152 /* Iteration was stopped before all buffers were processed */
4153 if (buffer)
4154 {
4155 Py_DECREF(buffer);
4156 }
4157}
4158
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004159 static int
4160BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4161{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004162 if (buffer)
4163 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004164 return 0;
4165}
4166
4167 static int
4168BufMapIterClear(PyObject **buffer)
4169{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004170 if (*buffer)
4171 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004172 return 0;
4173}
4174
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004175 static PyObject *
4176BufMapIterNext(PyObject **buffer)
4177{
4178 PyObject *next;
4179 PyObject *r;
4180
4181 if (!*buffer)
4182 return NULL;
4183
4184 r = *buffer;
4185
4186 if (CheckBuffer((BufferObject *)(r)))
4187 {
4188 *buffer = NULL;
4189 return NULL;
4190 }
4191
4192 if (!((BufferObject *)(r))->buf->b_next)
4193 next = NULL;
4194 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4195 return NULL;
4196 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004197 /* Do not increment reference: we no longer hold it (decref), but whoever
4198 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004199 return r;
4200}
4201
4202 static PyObject *
4203BufMapIter(PyObject *self UNUSED)
4204{
4205 PyObject *buffer;
4206
4207 buffer = BufferNew(firstbuf);
4208 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004209 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4210 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004211}
4212
4213static PyMappingMethods BufMapAsMapping = {
4214 (lenfunc) BufMapLength,
4215 (binaryfunc) BufMapItem,
4216 (objobjargproc) 0,
4217};
4218
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004219/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004220 */
4221
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004222static char *CurrentAttrs[] = {
4223 "buffer", "window", "line", "range", "tabpage",
4224 NULL
4225};
4226
4227 static PyObject *
4228CurrentDir(PyObject *self)
4229{
4230 return ObjectDir(self, CurrentAttrs);
4231}
4232
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004233 static PyObject *
4234CurrentGetattr(PyObject *self UNUSED, char *name)
4235{
4236 if (strcmp(name, "buffer") == 0)
4237 return (PyObject *)BufferNew(curbuf);
4238 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004239 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004240 else if (strcmp(name, "tabpage") == 0)
4241 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004242 else if (strcmp(name, "line") == 0)
4243 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4244 else if (strcmp(name, "range") == 0)
4245 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004246 else if (strcmp(name, "__members__") == 0)
4247 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004248 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004249#if PY_MAJOR_VERSION < 3
4250 return Py_FindMethod(WindowMethods, self, name);
4251#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004252 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004253#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004254}
4255
4256 static int
4257CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4258{
4259 if (strcmp(name, "line") == 0)
4260 {
4261 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4262 return -1;
4263
4264 return 0;
4265 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004266 else if (strcmp(name, "buffer") == 0)
4267 {
4268 int count;
4269
4270 if (value->ob_type != &BufferType)
4271 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004272 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004273 return -1;
4274 }
4275
4276 if (CheckBuffer((BufferObject *)(value)))
4277 return -1;
4278 count = ((BufferObject *)(value))->buf->b_fnum;
4279
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004280 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004281 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4282 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004283 if (VimTryEnd())
4284 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004285 PyErr_SetVim(_("failed to switch to given buffer"));
4286 return -1;
4287 }
4288
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004289 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004290 }
4291 else if (strcmp(name, "window") == 0)
4292 {
4293 int count;
4294
4295 if (value->ob_type != &WindowType)
4296 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004297 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004298 return -1;
4299 }
4300
4301 if (CheckWindow((WindowObject *)(value)))
4302 return -1;
4303 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4304
4305 if (!count)
4306 {
4307 PyErr_SetString(PyExc_ValueError,
4308 _("failed to find window in the current tab page"));
4309 return -1;
4310 }
4311
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004312 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004313 win_goto(((WindowObject *)(value))->win);
4314 if (((WindowObject *)(value))->win != curwin)
4315 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004316 if (VimTryEnd())
4317 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004318 PyErr_SetString(PyExc_RuntimeError,
4319 _("did not switch to the specified window"));
4320 return -1;
4321 }
4322
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004323 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004324 }
4325 else if (strcmp(name, "tabpage") == 0)
4326 {
4327 if (value->ob_type != &TabPageType)
4328 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004329 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004330 return -1;
4331 }
4332
4333 if (CheckTabPage((TabPageObject *)(value)))
4334 return -1;
4335
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004336 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004337 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4338 if (((TabPageObject *)(value))->tab != curtab)
4339 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004340 if (VimTryEnd())
4341 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004342 PyErr_SetString(PyExc_RuntimeError,
4343 _("did not switch to the specified tab page"));
4344 return -1;
4345 }
4346
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004347 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004348 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004349 else
4350 {
4351 PyErr_SetString(PyExc_AttributeError, name);
4352 return -1;
4353 }
4354}
4355
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004356static struct PyMethodDef CurrentMethods[] = {
4357 /* name, function, calling, documentation */
4358 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4359 { NULL, NULL, 0, NULL}
4360};
4361
Bram Moolenaardb913952012-06-29 12:54:53 +02004362 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004363init_range_cmd(exarg_T *eap)
4364{
4365 RangeStart = eap->line1;
4366 RangeEnd = eap->line2;
4367}
4368
4369 static void
4370init_range_eval(typval_T *rettv UNUSED)
4371{
4372 RangeStart = (PyInt) curwin->w_cursor.lnum;
4373 RangeEnd = RangeStart;
4374}
4375
4376 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004377run_cmd(const char *cmd, void *arg UNUSED
4378#ifdef PY_CAN_RECURSE
4379 , PyGILState_STATE *pygilstate UNUSED
4380#endif
4381 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004382{
4383 PyRun_SimpleString((char *) cmd);
4384}
4385
4386static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4387static int code_hdr_len = 30;
4388
4389 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004390run_do(const char *cmd, void *arg UNUSED
4391#ifdef PY_CAN_RECURSE
4392 , PyGILState_STATE *pygilstate
4393#endif
4394 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004395{
4396 PyInt lnum;
4397 size_t len;
4398 char *code;
4399 int status;
4400 PyObject *pyfunc, *pymain;
4401
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004402 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004403 {
4404 EMSG(_("cannot save undo information"));
4405 return;
4406 }
4407
4408 len = code_hdr_len + STRLEN(cmd);
4409 code = PyMem_New(char, len + 1);
4410 memcpy(code, code_hdr, code_hdr_len);
4411 STRCPY(code + code_hdr_len, cmd);
4412 status = PyRun_SimpleString(code);
4413 PyMem_Free(code);
4414
4415 if (status)
4416 {
4417 EMSG(_("failed to run the code"));
4418 return;
4419 }
4420
4421 status = 0;
4422 pymain = PyImport_AddModule("__main__");
4423 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004424#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004425 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004426#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004427
4428 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4429 {
4430 PyObject *line, *linenr, *ret;
4431
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004432#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004433 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004434#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004435 if (!(line = GetBufferLine(curbuf, lnum)))
4436 goto err;
4437 if (!(linenr = PyInt_FromLong((long) lnum)))
4438 {
4439 Py_DECREF(line);
4440 goto err;
4441 }
4442 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4443 Py_DECREF(line);
4444 Py_DECREF(linenr);
4445 if (!ret)
4446 goto err;
4447
4448 if (ret != Py_None)
4449 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4450 goto err;
4451
4452 Py_XDECREF(ret);
4453 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004454#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004455 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004456#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004457 }
4458 goto out;
4459err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004460#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004461 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004462#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004463 PyErr_PrintEx(0);
4464 PythonIO_Flush();
4465 status = 1;
4466out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004467#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004468 if (!status)
4469 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004470#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004471 Py_DECREF(pyfunc);
4472 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4473 if (status)
4474 return;
4475 check_cursor();
4476 update_curbuf(NOT_VALID);
4477}
4478
4479 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004480run_eval(const char *cmd, typval_T *rettv
4481#ifdef PY_CAN_RECURSE
4482 , PyGILState_STATE *pygilstate UNUSED
4483#endif
4484 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004485{
4486 PyObject *r;
4487
4488 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4489 if (r == NULL)
4490 {
4491 if (PyErr_Occurred() && !msg_silent)
4492 PyErr_PrintEx(0);
4493 EMSG(_("E858: Eval did not return a valid python object"));
4494 }
4495 else
4496 {
4497 if (ConvertFromPyObject(r, rettv) == -1)
4498 EMSG(_("E859: Failed to convert returned python object to vim value"));
4499 Py_DECREF(r);
4500 }
4501 PyErr_Clear();
4502}
4503
4504 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004505set_ref_in_py(const int copyID)
4506{
4507 pylinkedlist_T *cur;
4508 dict_T *dd;
4509 list_T *ll;
4510
4511 if (lastdict != NULL)
4512 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4513 {
4514 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4515 if (dd->dv_copyID != copyID)
4516 {
4517 dd->dv_copyID = copyID;
4518 set_ref_in_ht(&dd->dv_hashtab, copyID);
4519 }
4520 }
4521
4522 if (lastlist != NULL)
4523 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4524 {
4525 ll = ((ListObject *) (cur->pll_obj))->list;
4526 if (ll->lv_copyID != copyID)
4527 {
4528 ll->lv_copyID = copyID;
4529 set_ref_in_list(ll, copyID);
4530 }
4531 }
4532}
4533
4534 static int
4535set_string_copy(char_u *str, typval_T *tv)
4536{
4537 tv->vval.v_string = vim_strsave(str);
4538 if (tv->vval.v_string == NULL)
4539 {
4540 PyErr_NoMemory();
4541 return -1;
4542 }
4543 return 0;
4544}
4545
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004546 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004547pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004548{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004549 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004550 char_u *key;
4551 dictitem_T *di;
4552 PyObject *keyObject;
4553 PyObject *valObject;
4554 Py_ssize_t iter = 0;
4555
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004556 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004557 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004558
4559 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004560 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004561
4562 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4563 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004564 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004565
Bram Moolenaara03e6312013-05-29 22:49:26 +02004566 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004567 {
4568 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004569 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004570 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004571
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004572 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004573 {
4574 dict_unref(dict);
4575 return -1;
4576 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004577
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004578 if (*key == NUL)
4579 {
4580 dict_unref(dict);
4581 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004582 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004583 return -1;
4584 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004585
4586 di = dictitem_alloc(key);
4587
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004588 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004589
4590 if (di == NULL)
4591 {
4592 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004593 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004594 return -1;
4595 }
4596 di->di_tv.v_lock = 0;
4597
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004598 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004599 {
4600 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004601 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004602 return -1;
4603 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004604
4605 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004606 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004607 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004608 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004609 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004610 PyErr_SetVim(_("failed to add key to dictionary"));
4611 return -1;
4612 }
4613 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004614
4615 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004616 return 0;
4617}
4618
4619 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004620pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004621{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004622 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004623 char_u *key;
4624 dictitem_T *di;
4625 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004626 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004627 PyObject *keyObject;
4628 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004629
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004630 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004631 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004632
4633 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004634 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004635
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004636 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004637 {
4638 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004639 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004640 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004641
4642 if (!(iterator = PyObject_GetIter(list)))
4643 {
4644 dict_unref(dict);
4645 Py_DECREF(list);
4646 return -1;
4647 }
4648 Py_DECREF(list);
4649
4650 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004651 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004652 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004653
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004654 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004655 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004656 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004657 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004658 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004659 return -1;
4660 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004661
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004662 if (*key == NUL)
4663 {
4664 Py_DECREF(keyObject);
4665 Py_DECREF(iterator);
4666 Py_XDECREF(todecref);
4667 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004668 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004669 return -1;
4670 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004671
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004672 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004673 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004674 Py_DECREF(keyObject);
4675 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004676 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004677 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004678 return -1;
4679 }
4680
4681 di = dictitem_alloc(key);
4682
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004683 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004684 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004685
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004686 if (di == NULL)
4687 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004688 Py_DECREF(iterator);
4689 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004690 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004691 PyErr_NoMemory();
4692 return -1;
4693 }
4694 di->di_tv.v_lock = 0;
4695
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004696 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004697 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004698 Py_DECREF(iterator);
4699 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004700 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004701 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004702 return -1;
4703 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004704
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004705 Py_DECREF(valObject);
4706
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004707 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004708 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004709 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004710 dictitem_free(di);
4711 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004712 PyErr_SetVim(_("failed to add key to dictionary"));
4713 return -1;
4714 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004715 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004716 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004717 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004718 return 0;
4719}
4720
4721 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004722pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004723{
4724 list_T *l;
4725
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004726 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004727 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004728
4729 tv->v_type = VAR_LIST;
4730 tv->vval.v_list = l;
4731
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004732 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004733 {
4734 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004735 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004736 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004737
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004738 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004739 return 0;
4740}
4741
Bram Moolenaardb913952012-06-29 12:54:53 +02004742typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4743
4744 static int
4745convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004746 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004747{
4748 PyObject *capsule;
4749 char hexBuf[sizeof(void *) * 2 + 3];
4750
4751 sprintf(hexBuf, "%p", obj);
4752
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004753# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004754 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004755# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004756 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004757# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004758 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004759 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004760# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004761 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004762# else
4763 capsule = PyCObject_FromVoidPtr(tv, NULL);
4764# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004765 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4766 {
4767 Py_DECREF(capsule);
4768 tv->v_type = VAR_UNKNOWN;
4769 return -1;
4770 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004771 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004772 {
4773 tv->v_type = VAR_UNKNOWN;
4774 return -1;
4775 }
4776 /* As we are not using copy_tv which increments reference count we must
4777 * do it ourself. */
4778 switch(tv->v_type)
4779 {
4780 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4781 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4782 }
4783 }
4784 else
4785 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004786 typval_T *v;
4787
4788# ifdef PY_USE_CAPSULE
4789 v = PyCapsule_GetPointer(capsule, NULL);
4790# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004791 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004792# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004793 copy_tv(v, tv);
4794 }
4795 return 0;
4796}
4797
4798 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02004799ConvertFromPyMapping(PyObject *obj, typval_T *tv)
4800{
4801 PyObject *lookup_dict;
4802 int r;
4803
4804 if (!(lookup_dict = PyDict_New()))
4805 return -1;
4806
4807 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
4808 {
4809 tv->v_type = VAR_DICT;
4810 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4811 ++tv->vval.v_dict->dv_refcount;
4812 r = 0;
4813 }
4814 else if (PyDict_Check(obj))
4815 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
4816 else if (PyMapping_Check(obj))
4817 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
4818 else
4819 {
4820 PyErr_SetString(PyExc_TypeError,
4821 _("unable to convert object to vim dictionary"));
4822 r = -1;
4823 }
4824 Py_DECREF(lookup_dict);
4825 return r;
4826}
4827
4828 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02004829ConvertFromPyObject(PyObject *obj, typval_T *tv)
4830{
4831 PyObject *lookup_dict;
4832 int r;
4833
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004834 if (!(lookup_dict = PyDict_New()))
4835 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004836 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4837 Py_DECREF(lookup_dict);
4838 return r;
4839}
4840
4841 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004842_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004843{
Bram Moolenaara9922d62013-05-30 13:01:18 +02004844 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02004845 {
4846 tv->v_type = VAR_DICT;
4847 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4848 ++tv->vval.v_dict->dv_refcount;
4849 }
4850 else if (obj->ob_type == &ListType)
4851 {
4852 tv->v_type = VAR_LIST;
4853 tv->vval.v_list = (((ListObject *)(obj))->list);
4854 ++tv->vval.v_list->lv_refcount;
4855 }
4856 else if (obj->ob_type == &FunctionType)
4857 {
4858 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4859 return -1;
4860
4861 tv->v_type = VAR_FUNC;
4862 func_ref(tv->vval.v_string);
4863 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004864 else if (PyBytes_Check(obj))
4865 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004866 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004867
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004868 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4869 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004870 if (result == NULL)
4871 return -1;
4872
4873 if (set_string_copy(result, tv) == -1)
4874 return -1;
4875
4876 tv->v_type = VAR_STRING;
4877 }
4878 else if (PyUnicode_Check(obj))
4879 {
4880 PyObject *bytes;
4881 char_u *result;
4882
Bram Moolenaardb913952012-06-29 12:54:53 +02004883 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4884 if (bytes == NULL)
4885 return -1;
4886
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004887 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4888 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004889 if (result == NULL)
4890 return -1;
4891
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004892 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004893 {
4894 Py_XDECREF(bytes);
4895 return -1;
4896 }
4897 Py_XDECREF(bytes);
4898
4899 tv->v_type = VAR_STRING;
4900 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004901#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004902 else if (PyInt_Check(obj))
4903 {
4904 tv->v_type = VAR_NUMBER;
4905 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4906 }
4907#endif
4908 else if (PyLong_Check(obj))
4909 {
4910 tv->v_type = VAR_NUMBER;
4911 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4912 }
4913 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004914 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004915#ifdef FEAT_FLOAT
4916 else if (PyFloat_Check(obj))
4917 {
4918 tv->v_type = VAR_FLOAT;
4919 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4920 }
4921#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004922 else if (PyObject_HasAttrString(obj, "keys"))
4923 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004924 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004925 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004926 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004927 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004928 else
4929 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004930 PyErr_SetString(PyExc_TypeError,
4931 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004932 return -1;
4933 }
4934 return 0;
4935}
4936
4937 static PyObject *
4938ConvertToPyObject(typval_T *tv)
4939{
4940 if (tv == NULL)
4941 {
4942 PyErr_SetVim(_("NULL reference passed"));
4943 return NULL;
4944 }
4945 switch (tv->v_type)
4946 {
4947 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004948 return PyBytes_FromString(tv->vval.v_string == NULL
4949 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004950 case VAR_NUMBER:
4951 return PyLong_FromLong((long) tv->vval.v_number);
4952#ifdef FEAT_FLOAT
4953 case VAR_FLOAT:
4954 return PyFloat_FromDouble((double) tv->vval.v_float);
4955#endif
4956 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004957 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02004958 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02004959 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004960 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02004961 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004962 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004963 case VAR_UNKNOWN:
4964 Py_INCREF(Py_None);
4965 return Py_None;
4966 default:
4967 PyErr_SetVim(_("internal error: invalid value type"));
4968 return NULL;
4969 }
4970}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004971
4972typedef struct
4973{
4974 PyObject_HEAD
4975} CurrentObject;
4976static PyTypeObject CurrentType;
4977
4978 static void
4979init_structs(void)
4980{
4981 vim_memset(&OutputType, 0, sizeof(OutputType));
4982 OutputType.tp_name = "vim.message";
4983 OutputType.tp_basicsize = sizeof(OutputObject);
4984 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4985 OutputType.tp_doc = "vim message object";
4986 OutputType.tp_methods = OutputMethods;
4987#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004988 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4989 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004990 OutputType.tp_alloc = call_PyType_GenericAlloc;
4991 OutputType.tp_new = call_PyType_GenericNew;
4992 OutputType.tp_free = call_PyObject_Free;
4993#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004994 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4995 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004996#endif
4997
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004998 vim_memset(&IterType, 0, sizeof(IterType));
4999 IterType.tp_name = "vim.iter";
5000 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005001 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005002 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005003 IterType.tp_iter = (getiterfunc)IterIter;
5004 IterType.tp_iternext = (iternextfunc)IterNext;
5005 IterType.tp_dealloc = (destructor)IterDestructor;
5006 IterType.tp_traverse = (traverseproc)IterTraverse;
5007 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005008
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005009 vim_memset(&BufferType, 0, sizeof(BufferType));
5010 BufferType.tp_name = "vim.buffer";
5011 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005012 BufferType.tp_dealloc = (destructor)BufferDestructor;
5013 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005014 BufferType.tp_as_sequence = &BufferAsSeq;
5015 BufferType.tp_as_mapping = &BufferAsMapping;
5016 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5017 BufferType.tp_doc = "vim buffer object";
5018 BufferType.tp_methods = BufferMethods;
5019#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005020 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005021 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005022 BufferType.tp_alloc = call_PyType_GenericAlloc;
5023 BufferType.tp_new = call_PyType_GenericNew;
5024 BufferType.tp_free = call_PyObject_Free;
5025#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005026 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005027 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005028#endif
5029
5030 vim_memset(&WindowType, 0, sizeof(WindowType));
5031 WindowType.tp_name = "vim.window";
5032 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005033 WindowType.tp_dealloc = (destructor)WindowDestructor;
5034 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005035 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005036 WindowType.tp_doc = "vim Window object";
5037 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005038 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5039 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005040#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005041 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5042 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005043 WindowType.tp_alloc = call_PyType_GenericAlloc;
5044 WindowType.tp_new = call_PyType_GenericNew;
5045 WindowType.tp_free = call_PyObject_Free;
5046#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005047 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5048 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005049#endif
5050
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005051 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5052 TabPageType.tp_name = "vim.tabpage";
5053 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005054 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5055 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005056 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5057 TabPageType.tp_doc = "vim tab page object";
5058 TabPageType.tp_methods = TabPageMethods;
5059#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005060 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005061 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5062 TabPageType.tp_new = call_PyType_GenericNew;
5063 TabPageType.tp_free = call_PyObject_Free;
5064#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005065 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005066#endif
5067
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005068 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5069 BufMapType.tp_name = "vim.bufferlist";
5070 BufMapType.tp_basicsize = sizeof(BufMapObject);
5071 BufMapType.tp_as_mapping = &BufMapAsMapping;
5072 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005073 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005074 BufferType.tp_doc = "vim buffer list";
5075
5076 vim_memset(&WinListType, 0, sizeof(WinListType));
5077 WinListType.tp_name = "vim.windowlist";
5078 WinListType.tp_basicsize = sizeof(WinListType);
5079 WinListType.tp_as_sequence = &WinListAsSeq;
5080 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5081 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005082 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005083
5084 vim_memset(&TabListType, 0, sizeof(TabListType));
5085 TabListType.tp_name = "vim.tabpagelist";
5086 TabListType.tp_basicsize = sizeof(TabListType);
5087 TabListType.tp_as_sequence = &TabListAsSeq;
5088 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5089 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005090
5091 vim_memset(&RangeType, 0, sizeof(RangeType));
5092 RangeType.tp_name = "vim.range";
5093 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005094 RangeType.tp_dealloc = (destructor)RangeDestructor;
5095 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005096 RangeType.tp_as_sequence = &RangeAsSeq;
5097 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005098 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005099 RangeType.tp_doc = "vim Range object";
5100 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005101 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5102 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005103#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005104 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005105 RangeType.tp_alloc = call_PyType_GenericAlloc;
5106 RangeType.tp_new = call_PyType_GenericNew;
5107 RangeType.tp_free = call_PyObject_Free;
5108#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005109 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005110#endif
5111
5112 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5113 CurrentType.tp_name = "vim.currentdata";
5114 CurrentType.tp_basicsize = sizeof(CurrentObject);
5115 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5116 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005117 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005118#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005119 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5120 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005121#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005122 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5123 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005124#endif
5125
5126 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5127 DictionaryType.tp_name = "vim.dictionary";
5128 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005129 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005130 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005131 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005132 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005133 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5134 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005135 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5136 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5137 DictionaryType.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 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5140 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005141#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005142 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5143 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005144#endif
5145
5146 vim_memset(&ListType, 0, sizeof(ListType));
5147 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005148 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005149 ListType.tp_basicsize = sizeof(ListObject);
5150 ListType.tp_as_sequence = &ListAsSeq;
5151 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005152 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005153 ListType.tp_doc = "list pushing modifications to vim structure";
5154 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005155 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005156 ListType.tp_new = (newfunc)ListConstructor;
5157 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005158#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005159 ListType.tp_getattro = (getattrofunc)ListGetattro;
5160 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005161#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005162 ListType.tp_getattr = (getattrfunc)ListGetattr;
5163 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005164#endif
5165
5166 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005167 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005168 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005169 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5170 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005171 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005172 FunctionType.tp_doc = "object that calls vim function";
5173 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005174 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005175 FunctionType.tp_new = (newfunc)FunctionConstructor;
5176 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005177#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005178 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005179#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005180 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005181#endif
5182
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005183 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5184 OptionsType.tp_name = "vim.options";
5185 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005186 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005187 OptionsType.tp_doc = "object for manipulating options";
5188 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005189 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5190 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5191 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005192
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005193#if PY_MAJOR_VERSION >= 3
5194 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5195 vimmodule.m_name = "vim";
5196 vimmodule.m_doc = "Vim Python interface\n";
5197 vimmodule.m_size = -1;
5198 vimmodule.m_methods = VimMethods;
5199#endif
5200}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005201
5202#define PYTYPE_READY(type) \
5203 if (PyType_Ready(&type)) \
5204 return -1;
5205
5206 static int
5207init_types()
5208{
5209 PYTYPE_READY(IterType);
5210 PYTYPE_READY(BufferType);
5211 PYTYPE_READY(RangeType);
5212 PYTYPE_READY(WindowType);
5213 PYTYPE_READY(TabPageType);
5214 PYTYPE_READY(BufMapType);
5215 PYTYPE_READY(WinListType);
5216 PYTYPE_READY(TabListType);
5217 PYTYPE_READY(CurrentType);
5218 PYTYPE_READY(DictionaryType);
5219 PYTYPE_READY(ListType);
5220 PYTYPE_READY(FunctionType);
5221 PYTYPE_READY(OptionsType);
5222 PYTYPE_READY(OutputType);
5223 return 0;
5224}
5225
5226static BufMapObject TheBufferMap =
5227{
5228 PyObject_HEAD_INIT(&BufMapType)
5229};
5230
5231static WinListObject TheWindowList =
5232{
5233 PyObject_HEAD_INIT(&WinListType)
5234 NULL
5235};
5236
5237static CurrentObject TheCurrent =
5238{
5239 PyObject_HEAD_INIT(&CurrentType)
5240};
5241
5242static TabListObject TheTabPageList =
5243{
5244 PyObject_HEAD_INIT(&TabListType)
5245};
5246
5247static struct numeric_constant {
5248 char *name;
5249 int value;
5250} numeric_constants[] = {
5251 {"VAR_LOCKED", VAR_LOCKED},
5252 {"VAR_FIXED", VAR_FIXED},
5253 {"VAR_SCOPE", VAR_SCOPE},
5254 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5255};
5256
5257static struct object_constant {
5258 char *name;
5259 PyObject *value;
5260} object_constants[] = {
5261 {"buffers", (PyObject *)(void *)&TheBufferMap},
5262 {"windows", (PyObject *)(void *)&TheWindowList},
5263 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5264 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005265
5266 {"Buffer", (PyObject *)&BufferType},
5267 {"Range", (PyObject *)&RangeType},
5268 {"Window", (PyObject *)&WindowType},
5269 {"TabPage", (PyObject *)&TabPageType},
5270 {"Dictionary", (PyObject *)&DictionaryType},
5271 {"List", (PyObject *)&ListType},
5272 {"Function", (PyObject *)&FunctionType},
5273 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005274};
5275
5276typedef int (*object_adder)(PyObject *, const char *, PyObject *);
5277
5278#define ADD_OBJECT(m, name, obj) \
5279 if (add_object(m, name, obj)) \
5280 return -1;
5281
5282#define ADD_CHECKED_OBJECT(m, name, obj) \
5283 { \
5284 PyObject *value = obj; \
5285 if (!value) \
5286 return -1; \
5287 ADD_OBJECT(m, name, value); \
5288 }
5289
5290 static int
5291populate_module(PyObject *m, object_adder add_object)
5292{
5293 int i;
5294
5295 for (i = 0; i < (int)(sizeof(numeric_constants)
5296 / sizeof(struct numeric_constant));
5297 ++i)
5298 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5299 PyInt_FromLong(numeric_constants[i].value));
5300
5301 for (i = 0; i < (int)(sizeof(object_constants)
5302 / sizeof(struct object_constant));
5303 ++i)
5304 {
5305 PyObject *value;
5306
5307 value = object_constants[i].value;
5308 Py_INCREF(value);
5309 ADD_OBJECT(m, object_constants[i].name, value);
5310 }
5311
5312 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5313 return -1;
5314 ADD_OBJECT(m, "error", VimError);
5315
Bram Moolenaara9922d62013-05-30 13:01:18 +02005316 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5317 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005318 ADD_CHECKED_OBJECT(m, "options",
5319 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
5320 return 0;
5321}