blob: e5d8d5e37d98f96a8bfabf334bc7950595737f5e [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 Moolenaarc09a6d62013-06-10 21:27:29 +020027static const char *vim_special_path = "_vim_path_";
28
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020029#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
30
Bram Moolenaar35eacd72013-05-30 22:06:33 +020031#define RAISE_NO_EMPTY_KEYS PyErr_SetString(PyExc_ValueError, \
32 _("empty keys are not allowed"))
33
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020034#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
35#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020036#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020038typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020039typedef void (*runner)(const char *, void *
40#ifdef PY_CAN_RECURSE
41 , PyGILState_STATE *
42#endif
43 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020044
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020045static int ConvertFromPyObject(PyObject *, typval_T *);
46static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020047static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020048static PyObject *WindowNew(win_T *, tabpage_T *);
49static PyObject *BufferNew (buf_T *);
50static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020051
52static PyInt RangeStart;
53static PyInt RangeEnd;
54
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020055static PyObject *globals;
56
Bram Moolenaarf4258302013-06-02 18:20:17 +020057static PyObject *py_chdir;
58static PyObject *py_fchdir;
59static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020060static PyObject *vim_module;
61static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020062
Bram Moolenaar81c40c52013-06-12 14:41:04 +020063static PyObject *py_find_module;
64static PyObject *py_load_module;
65
66static PyObject *VimError;
67
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020068/*
69 * obtain a lock on the Vim data structures
70 */
71 static void
72Python_Lock_Vim(void)
73{
74}
75
76/*
77 * release a lock on the Vim data structures
78 */
79 static void
80Python_Release_Vim(void)
81{
82}
83
Bram Moolenaare9ba5162013-05-29 22:02:22 +020084/*
85 * The "todecref" argument holds a pointer to PyObject * that must be
86 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
87 * was needed to generate returned value is object.
88 *
89 * Use Py_XDECREF to decrement reference count.
90 */
91 static char_u *
92StringToChars(PyObject *object, PyObject **todecref)
93{
94 char_u *p;
95 PyObject *bytes = NULL;
96
97 if (PyBytes_Check(object))
98 {
99
100 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
101 return NULL;
102 if (p == NULL)
103 return NULL;
104
105 *todecref = NULL;
106 }
107 else if (PyUnicode_Check(object))
108 {
109 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
110 if (bytes == NULL)
111 return NULL;
112
113 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
114 return NULL;
115 if (p == NULL)
116 return NULL;
117
118 *todecref = bytes;
119 }
120 else
121 {
122 PyErr_SetString(PyExc_TypeError, _("object must be string"));
123 return NULL;
124 }
125
126 return (char_u *) p;
127}
128
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200129 static int
130add_string(PyObject *list, char *s)
131{
132 PyObject *string;
133
134 if (!(string = PyString_FromString(s)))
135 return -1;
136 if (PyList_Append(list, string))
137 {
138 Py_DECREF(string);
139 return -1;
140 }
141
142 Py_DECREF(string);
143 return 0;
144}
145
146 static PyObject *
147ObjectDir(PyObject *self, char **attributes)
148{
149 PyMethodDef *method;
150 char **attr;
151 PyObject *r;
152
153 if (!(r = PyList_New(0)))
154 return NULL;
155
156 if (self)
157 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
158 if (add_string(r, (char *) method->ml_name))
159 {
160 Py_DECREF(r);
161 return NULL;
162 }
163
164 for (attr = attributes ; *attr ; ++attr)
165 if (add_string(r, *attr))
166 {
167 Py_DECREF(r);
168 return NULL;
169 }
170
171#if PY_MAJOR_VERSION < 3
172 if (add_string(r, "__members__"))
173 {
174 Py_DECREF(r);
175 return NULL;
176 }
177#endif
178
179 return r;
180}
181
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200182/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200183 */
184
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200185/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200186typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200187
188static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200189
190typedef struct
191{
192 PyObject_HEAD
193 long softspace;
194 long error;
195} OutputObject;
196
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200197static char *OutputAttrs[] = {
198 "softspace",
199 NULL
200};
201
202 static PyObject *
203OutputDir(PyObject *self)
204{
205 return ObjectDir(self, OutputAttrs);
206}
207
Bram Moolenaar77045652012-09-21 13:46:06 +0200208 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200209OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200210{
211 if (val == NULL)
212 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200213 PyErr_SetString(PyExc_AttributeError,
214 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200215 return -1;
216 }
217
218 if (strcmp(name, "softspace") == 0)
219 {
220 if (!PyInt_Check(val))
221 {
222 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
223 return -1;
224 }
225
Bram Moolenaard6e39182013-05-21 18:30:34 +0200226 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200227 return 0;
228 }
229
230 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
231 return -1;
232}
233
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200234/* Buffer IO, we write one whole line at a time. */
235static garray_T io_ga = {0, 0, 1, 80, NULL};
236static writefn old_fn = NULL;
237
238 static void
239PythonIO_Flush(void)
240{
241 if (old_fn != NULL && io_ga.ga_len > 0)
242 {
243 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
244 old_fn((char_u *)io_ga.ga_data);
245 }
246 io_ga.ga_len = 0;
247}
248
249 static void
250writer(writefn fn, char_u *str, PyInt n)
251{
252 char_u *ptr;
253
254 /* Flush when switching output function. */
255 if (fn != old_fn)
256 PythonIO_Flush();
257 old_fn = fn;
258
259 /* Write each NL separated line. Text after the last NL is kept for
260 * writing later. */
261 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
262 {
263 PyInt len = ptr - str;
264
265 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
266 break;
267
268 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
269 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
270 fn((char_u *)io_ga.ga_data);
271 str = ptr + 1;
272 n -= len + 1;
273 io_ga.ga_len = 0;
274 }
275
276 /* Put the remaining text into io_ga for later printing. */
277 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
278 {
279 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
280 io_ga.ga_len += (int)n;
281 }
282}
283
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200284 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200285OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200286{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200287 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200288 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200289 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200290
Bram Moolenaar27564802011-09-07 19:30:21 +0200291 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200292 return NULL;
293
294 Py_BEGIN_ALLOW_THREADS
295 Python_Lock_Vim();
296 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
297 Python_Release_Vim();
298 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200299 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200300
301 Py_INCREF(Py_None);
302 return Py_None;
303}
304
305 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200306OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200307{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200308 PyObject *seq;
309 PyObject *iterator;
310 PyObject *item;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200311 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200312
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200313 if (!PyArg_ParseTuple(args, "O", &seq))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200314 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200316 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200317 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200318
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200319 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200320 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200321 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200322 PyInt len;
323
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200324 if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
Bram Moolenaardb913952012-06-29 12:54:53 +0200325 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200326 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200327 Py_DECREF(iterator);
328 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200329 return NULL;
330 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200331 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200332
333 Py_BEGIN_ALLOW_THREADS
334 Python_Lock_Vim();
335 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
336 Python_Release_Vim();
337 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200338 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200339 }
340
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200341 Py_DECREF(iterator);
342
343 /* Iterator may have finished due to an exception */
344 if (PyErr_Occurred())
345 return NULL;
346
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200347 Py_INCREF(Py_None);
348 return Py_None;
349}
350
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100351 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200352OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100353{
354 /* do nothing */
355 Py_INCREF(Py_None);
356 return Py_None;
357}
358
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200359/***************/
360
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200361static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200362 /* name, function, calling, doc */
363 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
364 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
365 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200366 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200367 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200368};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200369
370static OutputObject Output =
371{
372 PyObject_HEAD_INIT(&OutputType)
373 0,
374 0
375};
376
377static OutputObject Error =
378{
379 PyObject_HEAD_INIT(&OutputType)
380 0,
381 1
382};
383
384 static int
385PythonIO_Init_io(void)
386{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200387 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
388 return -1;
389 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
390 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200391
392 if (PyErr_Occurred())
393 {
394 EMSG(_("E264: Python: Error initialising I/O objects"));
395 return -1;
396 }
397
398 return 0;
399}
400
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200401typedef struct
402{
403 PyObject_HEAD
404 PyObject *module;
405} LoaderObject;
406static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200407
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200408 static void
409LoaderDestructor(LoaderObject *self)
410{
411 Py_DECREF(self->module);
412 DESTRUCTOR_FINISH(self);
413}
414
415 static PyObject *
416LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
417{
418 PyObject *r = self->module;
419
420 Py_INCREF(r);
421 return r;
422}
423
424static struct PyMethodDef LoaderMethods[] = {
425 /* name, function, calling, doc */
426 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
427 { NULL, NULL, 0, NULL}
428};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200429
430/* Check to see whether a Vim error has been reported, or a keyboard
431 * interrupt has been detected.
432 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200433
434 static void
435VimTryStart(void)
436{
437 ++trylevel;
438}
439
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200441VimTryEnd(void)
442{
443 --trylevel;
444 if (got_int)
445 {
446 PyErr_SetNone(PyExc_KeyboardInterrupt);
447 return 1;
448 }
449 else if (!did_throw)
450 return 0;
451 else if (PyErr_Occurred())
452 return 1;
453 else
454 {
455 PyErr_SetVim((char *) current_exception->value);
456 discard_current_exception();
457 return 1;
458 }
459}
460
461 static int
462VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200463{
464 if (got_int)
465 {
466 PyErr_SetNone(PyExc_KeyboardInterrupt);
467 return 1;
468 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200469 return 0;
470}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200471
472/* Vim module - Implementation
473 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200474
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200475 static PyObject *
476VimCommand(PyObject *self UNUSED, PyObject *args)
477{
478 char *cmd;
479 PyObject *result;
480
481 if (!PyArg_ParseTuple(args, "s", &cmd))
482 return NULL;
483
484 PyErr_Clear();
485
486 Py_BEGIN_ALLOW_THREADS
487 Python_Lock_Vim();
488
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200489 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200490 do_cmdline_cmd((char_u *)cmd);
491 update_screen(VALID);
492
493 Python_Release_Vim();
494 Py_END_ALLOW_THREADS
495
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200496 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200497 result = NULL;
498 else
499 result = Py_None;
500
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200501
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200502 Py_XINCREF(result);
503 return result;
504}
505
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200506/*
507 * Function to translate a typval_T into a PyObject; this will recursively
508 * translate lists/dictionaries into their Python equivalents.
509 *
510 * The depth parameter is to avoid infinite recursion, set it to 1 when
511 * you call VimToPython.
512 */
513 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200514VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200515{
516 PyObject *result;
517 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200518 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200519
520 /* Avoid infinite recursion */
521 if (depth > 100)
522 {
523 Py_INCREF(Py_None);
524 result = Py_None;
525 return result;
526 }
527
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200528 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200529 * then and we can use it again. */
530 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
531 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
532 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200533 sprintf(ptrBuf, "%p",
534 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
535 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200536
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200537 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200538 {
539 Py_INCREF(result);
540 return result;
541 }
542 }
543
544 if (our_tv->v_type == VAR_STRING)
545 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200546 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200547 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200548 }
549 else if (our_tv->v_type == VAR_NUMBER)
550 {
551 char buf[NUMBUFLEN];
552
553 /* For backwards compatibility numbers are stored as strings. */
554 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200555 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200556 }
557# ifdef FEAT_FLOAT
558 else if (our_tv->v_type == VAR_FLOAT)
559 {
560 char buf[NUMBUFLEN];
561
562 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200563 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200564 }
565# endif
566 else if (our_tv->v_type == VAR_LIST)
567 {
568 list_T *list = our_tv->vval.v_list;
569 listitem_T *curr;
570
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200571 if (list == NULL)
572 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200573
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200574 if (!(result = PyList_New(0)))
575 return NULL;
576
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200577 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200578 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200579 Py_DECREF(result);
580 return NULL;
581 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200582
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200583 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
584 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200585 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200586 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200587 Py_DECREF(result);
588 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200589 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200590 if (PyList_Append(result, newObj))
591 {
592 Py_DECREF(newObj);
593 Py_DECREF(result);
594 return NULL;
595 }
596 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200597 }
598 }
599 else if (our_tv->v_type == VAR_DICT)
600 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200601
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200602 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
603 long_u todo = ht->ht_used;
604 hashitem_T *hi;
605 dictitem_T *di;
606 if (our_tv->vval.v_dict == NULL)
607 return NULL;
608
609 if (!(result = PyDict_New()))
610 return NULL;
611
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200612 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200614 Py_DECREF(result);
615 return NULL;
616 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200617
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200618 for (hi = ht->ht_array; todo > 0; ++hi)
619 {
620 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200621 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200622 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200623
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200624 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200625 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200626 {
627 Py_DECREF(result);
628 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200629 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200630 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
631 {
632 Py_DECREF(result);
633 Py_DECREF(newObj);
634 return NULL;
635 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200636 }
637 }
638 }
639 else
640 {
641 Py_INCREF(Py_None);
642 result = Py_None;
643 }
644
645 return result;
646}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200647
648 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200649VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200650{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200651 char *expr;
652 typval_T *our_tv;
653 PyObject *result;
654 PyObject *lookup_dict;
655
656 if (!PyArg_ParseTuple(args, "s", &expr))
657 return NULL;
658
659 Py_BEGIN_ALLOW_THREADS
660 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200661 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200662 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200663 Python_Release_Vim();
664 Py_END_ALLOW_THREADS
665
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200666 if (VimTryEnd())
667 return NULL;
668
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200669 if (our_tv == NULL)
670 {
671 PyErr_SetVim(_("invalid expression"));
672 return NULL;
673 }
674
675 /* Convert the Vim type into a Python type. Create a dictionary that's
676 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200677 if (!(lookup_dict = PyDict_New()))
678 result = NULL;
679 else
680 {
681 result = VimToPython(our_tv, 1, lookup_dict);
682 Py_DECREF(lookup_dict);
683 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200684
685
686 Py_BEGIN_ALLOW_THREADS
687 Python_Lock_Vim();
688 free_tv(our_tv);
689 Python_Release_Vim();
690 Py_END_ALLOW_THREADS
691
692 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693}
694
Bram Moolenaardb913952012-06-29 12:54:53 +0200695static PyObject *ConvertToPyObject(typval_T *);
696
697 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200698VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200699{
Bram Moolenaardb913952012-06-29 12:54:53 +0200700 char *expr;
701 typval_T *our_tv;
702 PyObject *result;
703
704 if (!PyArg_ParseTuple(args, "s", &expr))
705 return NULL;
706
707 Py_BEGIN_ALLOW_THREADS
708 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200709 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200710 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200711 Python_Release_Vim();
712 Py_END_ALLOW_THREADS
713
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200714 if (VimTryEnd())
715 return NULL;
716
Bram Moolenaardb913952012-06-29 12:54:53 +0200717 if (our_tv == NULL)
718 {
719 PyErr_SetVim(_("invalid expression"));
720 return NULL;
721 }
722
723 result = ConvertToPyObject(our_tv);
724 Py_BEGIN_ALLOW_THREADS
725 Python_Lock_Vim();
726 free_tv(our_tv);
727 Python_Release_Vim();
728 Py_END_ALLOW_THREADS
729
730 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200731}
732
733 static PyObject *
734VimStrwidth(PyObject *self UNUSED, PyObject *args)
735{
736 char *expr;
737
738 if (!PyArg_ParseTuple(args, "s", &expr))
739 return NULL;
740
Bram Moolenaara54bf402012-12-05 16:30:07 +0100741 return PyLong_FromLong(
742#ifdef FEAT_MBYTE
743 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
744#else
745 STRLEN(expr)
746#endif
747 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200748}
749
Bram Moolenaarf4258302013-06-02 18:20:17 +0200750 static PyObject *
751_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
752{
753 PyObject *r;
754 PyObject *newwd;
755 PyObject *todecref;
756 char_u *new_dir;
757
Bram Moolenaard4209d22013-06-05 20:34:15 +0200758 if (_chdir == NULL)
759 return NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200760 if (!(r = PyObject_Call(_chdir, args, kwargs)))
761 return NULL;
762
763 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
764 {
765 Py_DECREF(r);
766 return NULL;
767 }
768
769 if (!(new_dir = StringToChars(newwd, &todecref)))
770 {
771 Py_DECREF(r);
772 Py_DECREF(newwd);
773 return NULL;
774 }
775
776 VimTryStart();
777
778 if (vim_chdir(new_dir))
779 {
780 Py_DECREF(r);
781 Py_DECREF(newwd);
782 Py_XDECREF(todecref);
783
784 if (VimTryEnd())
785 return NULL;
786
787 PyErr_SetVim(_("failed to change directory"));
788 return NULL;
789 }
790
791 Py_DECREF(newwd);
792 Py_XDECREF(todecref);
793
794 post_chdir(FALSE);
795
796 if (VimTryEnd())
797 {
798 Py_DECREF(r);
799 return NULL;
800 }
801
802 return r;
803}
804
805 static PyObject *
806VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
807{
808 return _VimChdir(py_chdir, args, kwargs);
809}
810
811 static PyObject *
812VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
813{
814 return _VimChdir(py_fchdir, args, kwargs);
815}
816
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200817typedef struct {
818 PyObject *callable;
819 PyObject *result;
820} map_rtp_data;
821
822 static void
823map_rtp_callback(char_u *path, void *_data)
824{
825 void **data = (void **) _data;
826 PyObject *pathObject;
827 map_rtp_data *mr_data = *((map_rtp_data **) data);
828
829 if (!(pathObject = PyString_FromString((char *) path)))
830 {
831 *data = NULL;
832 return;
833 }
834
835 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
836 pathObject, NULL);
837
838 Py_DECREF(pathObject);
839
840 if (!mr_data->result || mr_data->result != Py_None)
841 *data = NULL;
842 else
843 {
844 Py_DECREF(mr_data->result);
845 mr_data->result = NULL;
846 }
847}
848
849 static PyObject *
850VimForeachRTP(PyObject *self UNUSED, PyObject *args)
851{
852 map_rtp_data data;
853
854 if (!PyArg_ParseTuple(args, "O", &data.callable))
855 return NULL;
856
857 data.result = NULL;
858
859 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
860
861 if (data.result == NULL)
862 {
863 if (PyErr_Occurred())
864 return NULL;
865 else
866 {
867 Py_INCREF(Py_None);
868 return Py_None;
869 }
870 }
871 return data.result;
872}
873
874/*
875 * _vim_runtimepath_ special path implementation.
876 */
877
878 static void
879map_finder_callback(char_u *path, void *_data)
880{
881 void **data = (void **) _data;
882 PyObject *list = *((PyObject **) data);
883 PyObject *pathObject1, *pathObject2;
884 char *pathbuf;
885 size_t pathlen;
886
887 pathlen = STRLEN(path);
888
889#if PY_MAJOR_VERSION < 3
890# define PY_MAIN_DIR_STRING "python2"
891#else
892# define PY_MAIN_DIR_STRING "python3"
893#endif
894#define PY_ALTERNATE_DIR_STRING "pythonx"
895
896#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
897 if (!(pathbuf = PyMem_New(char,
898 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
899 {
900 PyErr_NoMemory();
901 *data = NULL;
902 return;
903 }
904
905 mch_memmove(pathbuf, path, pathlen + 1);
906 add_pathsep((char_u *) pathbuf);
907
908 pathlen = STRLEN(pathbuf);
909 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
910 PYTHONX_STRING_LENGTH + 1);
911
912 if (!(pathObject1 = PyString_FromString(pathbuf)))
913 {
914 *data = NULL;
915 PyMem_Free(pathbuf);
916 return;
917 }
918
919 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
920 PYTHONX_STRING_LENGTH + 1);
921
922 if (!(pathObject2 = PyString_FromString(pathbuf)))
923 {
924 Py_DECREF(pathObject1);
925 PyMem_Free(pathbuf);
926 *data = NULL;
927 return;
928 }
929
930 PyMem_Free(pathbuf);
931
932 if (PyList_Append(list, pathObject1)
933 || PyList_Append(list, pathObject2))
934 *data = NULL;
935
936 Py_DECREF(pathObject1);
937 Py_DECREF(pathObject2);
938}
939
940 static PyObject *
941Vim_GetPaths(PyObject *self UNUSED)
942{
943 PyObject *r;
944
945 if (!(r = PyList_New(0)))
946 return NULL;
947
948 do_in_runtimepath(NULL, FALSE, &map_finder_callback, r);
949
950 if (PyErr_Occurred())
951 {
952 Py_DECREF(r);
953 return NULL;
954 }
955
956 return r;
957}
958
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200959 static PyObject *
960call_load_module(char *name, int len, PyObject *find_module_result)
961{
962 PyObject *fd, *pathname, *description;
963
964 if (!PyTuple_Check(find_module_result)
965 || PyTuple_GET_SIZE(find_module_result) != 3)
966 {
967 PyErr_SetString(PyExc_TypeError,
968 _("expected 3-tuple as imp.find_module() result"));
969 return NULL;
970 }
971
972 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
973 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
974 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
975 {
976 PyErr_SetString(PyExc_RuntimeError,
977 _("internal error: imp.find_module returned tuple with NULL"));
978 return NULL;
979 }
980
981 return PyObject_CallFunction(py_load_module,
982 "s#OOO", name, len, fd, pathname, description);
983}
984
985 static PyObject *
986find_module(char *fullname, char *tail, PyObject *new_path)
987{
988 PyObject *find_module_result;
989 PyObject *module;
990 char *dot;
991
992 if ((dot = (char *) vim_strchr((char_u *) tail, '.')))
993 {
994 /*
995 * There is a dot in the name: call find_module recursively without the
996 * first component
997 */
998 PyObject *newest_path;
999 int partlen = (int) (dot - 1 - tail);
1000
1001 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1002 "s#O", tail, partlen, new_path)))
1003 return NULL;
1004
1005 if (!(module = call_load_module(
1006 fullname,
1007 ((int) (tail - fullname)) + partlen,
1008 find_module_result)))
1009 {
1010 Py_DECREF(find_module_result);
1011 return NULL;
1012 }
1013
1014 Py_DECREF(find_module_result);
1015
1016 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1017 {
1018 Py_DECREF(module);
1019 return NULL;
1020 }
1021
1022 Py_DECREF(module);
1023
1024 module = find_module(fullname, dot + 1, newest_path);
1025
1026 Py_DECREF(newest_path);
1027
1028 return module;
1029 }
1030 else
1031 {
1032 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1033 "sO", tail, new_path)))
1034 return NULL;
1035
1036 if (!(module = call_load_module(
1037 fullname,
1038 STRLEN(fullname),
1039 find_module_result)))
1040 {
1041 Py_DECREF(find_module_result);
1042 return NULL;
1043 }
1044
1045 Py_DECREF(find_module_result);
1046
1047 return module;
1048 }
1049}
1050
1051 static PyObject *
1052FinderFindModule(PyObject *self, PyObject *args)
1053{
1054 char *fullname;
1055 PyObject *module;
1056 PyObject *new_path;
1057 LoaderObject *loader;
1058
1059 if (!PyArg_ParseTuple(args, "s", &fullname))
1060 return NULL;
1061
1062 if (!(new_path = Vim_GetPaths(self)))
1063 return NULL;
1064
1065 module = find_module(fullname, fullname, new_path);
1066
1067 Py_DECREF(new_path);
1068
1069 if (!module)
1070 {
1071 Py_INCREF(Py_None);
1072 return Py_None;
1073 }
1074
1075 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1076 {
1077 Py_DECREF(module);
1078 return NULL;
1079 }
1080
1081 loader->module = module;
1082
1083 return (PyObject *) loader;
1084}
1085
1086 static PyObject *
1087VimPathHook(PyObject *self UNUSED, PyObject *args)
1088{
1089 char *path;
1090
1091 if (PyArg_ParseTuple(args, "s", &path)
1092 && STRCMP(path, vim_special_path) == 0)
1093 {
1094 Py_INCREF(vim_module);
1095 return vim_module;
1096 }
1097
1098 PyErr_Clear();
1099 PyErr_SetNone(PyExc_ImportError);
1100 return NULL;
1101}
1102
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001103/*
1104 * Vim module - Definitions
1105 */
1106
1107static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001108 /* name, function, calling, documentation */
1109 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
1110 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
1111 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
1112 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
1113 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1114 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1115 {"foreach_rtp", VimForeachRTP, METH_VARARGS, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001116 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001117 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1118 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1119 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001120};
1121
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001122/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001123 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001124 */
1125
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001126static PyTypeObject IterType;
1127
1128typedef PyObject *(*nextfun)(void **);
1129typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001130typedef int (*traversefun)(void *, visitproc, void *);
1131typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001132
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001133/* Main purpose of this object is removing the need for do python
1134 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1135 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001136
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001137typedef struct
1138{
1139 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001140 void *cur;
1141 nextfun next;
1142 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001143 traversefun traverse;
1144 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001145} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001146
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001147 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001148IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1149 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001150{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001151 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001152
Bram Moolenaar774267b2013-05-21 20:51:59 +02001153 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001154 self->cur = start;
1155 self->next = next;
1156 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001157 self->traverse = traverse;
1158 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001159
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001160 return (PyObject *)(self);
1161}
1162
1163 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001164IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001165{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001166 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001167 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001168 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001169}
1170
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001171 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001172IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001173{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001174 if (self->traverse != NULL)
1175 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001176 else
1177 return 0;
1178}
1179
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001180/* Mac OSX defines clear() somewhere. */
1181#ifdef clear
1182# undef clear
1183#endif
1184
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001185 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001186IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001187{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001188 if (self->clear != NULL)
1189 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001190 else
1191 return 0;
1192}
1193
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001194 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001195IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001196{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001197 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001198}
1199
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001200 static PyObject *
1201IterIter(PyObject *self)
1202{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001203 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001204 return self;
1205}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001206
Bram Moolenaardb913952012-06-29 12:54:53 +02001207typedef struct pylinkedlist_S {
1208 struct pylinkedlist_S *pll_next;
1209 struct pylinkedlist_S *pll_prev;
1210 PyObject *pll_obj;
1211} pylinkedlist_T;
1212
1213static pylinkedlist_T *lastdict = NULL;
1214static pylinkedlist_T *lastlist = NULL;
1215
1216 static void
1217pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1218{
1219 if (ref->pll_prev == NULL)
1220 {
1221 if (ref->pll_next == NULL)
1222 {
1223 *last = NULL;
1224 return;
1225 }
1226 }
1227 else
1228 ref->pll_prev->pll_next = ref->pll_next;
1229
1230 if (ref->pll_next == NULL)
1231 *last = ref->pll_prev;
1232 else
1233 ref->pll_next->pll_prev = ref->pll_prev;
1234}
1235
1236 static void
1237pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1238{
1239 if (*last == NULL)
1240 ref->pll_prev = NULL;
1241 else
1242 {
1243 (*last)->pll_next = ref;
1244 ref->pll_prev = *last;
1245 }
1246 ref->pll_next = NULL;
1247 ref->pll_obj = self;
1248 *last = ref;
1249}
1250
1251static PyTypeObject DictionaryType;
1252
1253typedef struct
1254{
1255 PyObject_HEAD
1256 dict_T *dict;
1257 pylinkedlist_T ref;
1258} DictionaryObject;
1259
Bram Moolenaara9922d62013-05-30 13:01:18 +02001260static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1261
1262#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1263
Bram Moolenaardb913952012-06-29 12:54:53 +02001264 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001265DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001266{
1267 DictionaryObject *self;
1268
Bram Moolenaara9922d62013-05-30 13:01:18 +02001269 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001270 if (self == NULL)
1271 return NULL;
1272 self->dict = dict;
1273 ++dict->dv_refcount;
1274
1275 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1276
1277 return (PyObject *)(self);
1278}
1279
Bram Moolenaara9922d62013-05-30 13:01:18 +02001280 static dict_T *
1281py_dict_alloc()
1282{
1283 dict_T *r;
1284
1285 if (!(r = dict_alloc()))
1286 {
1287 PyErr_NoMemory();
1288 return NULL;
1289 }
1290 ++r->dv_refcount;
1291
1292 return r;
1293}
1294
1295 static PyObject *
1296DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1297{
1298 DictionaryObject *self;
1299 dict_T *dict;
1300
1301 if (!(dict = py_dict_alloc()))
1302 return NULL;
1303
1304 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1305
1306 --dict->dv_refcount;
1307
1308 if (kwargs || PyTuple_Size(args))
1309 {
1310 PyObject *tmp;
1311 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1312 {
1313 Py_DECREF(self);
1314 return NULL;
1315 }
1316
1317 Py_DECREF(tmp);
1318 }
1319
1320 return (PyObject *)(self);
1321}
1322
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001323 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001324DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001325{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001326 pyll_remove(&self->ref, &lastdict);
1327 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001328
1329 DESTRUCTOR_FINISH(self);
1330}
1331
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001332static char *DictionaryAttrs[] = {
1333 "locked", "scope",
1334 NULL
1335};
1336
1337 static PyObject *
1338DictionaryDir(PyObject *self)
1339{
1340 return ObjectDir(self, DictionaryAttrs);
1341}
1342
Bram Moolenaardb913952012-06-29 12:54:53 +02001343 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001344DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001345{
1346 if (val == NULL)
1347 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001348 PyErr_SetString(PyExc_AttributeError,
1349 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001350 return -1;
1351 }
1352
1353 if (strcmp(name, "locked") == 0)
1354 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001355 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001356 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001357 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001358 return -1;
1359 }
1360 else
1361 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001362 int istrue = PyObject_IsTrue(val);
1363 if (istrue == -1)
1364 return -1;
1365 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001366 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001367 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001368 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001369 }
1370 return 0;
1371 }
1372 else
1373 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001374 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001375 return -1;
1376 }
1377}
1378
1379 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001380DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001381{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001382 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001383}
1384
Bram Moolenaara9922d62013-05-30 13:01:18 +02001385#define DICT_FLAG_HAS_DEFAULT 0x01
1386#define DICT_FLAG_POP 0x02
1387#define DICT_FLAG_NONE_DEFAULT 0x04
1388#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1389#define DICT_FLAG_RETURN_PAIR 0x10
1390
Bram Moolenaardb913952012-06-29 12:54:53 +02001391 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001392_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001393{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001394 PyObject *keyObject;
1395 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1396 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001397 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001398 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001399 dict_T *dict = self->dict;
1400 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001401 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001402
Bram Moolenaara9922d62013-05-30 13:01:18 +02001403 if (flags & DICT_FLAG_HAS_DEFAULT)
1404 {
1405 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1406 return NULL;
1407 }
1408 else
1409 keyObject = args;
1410
1411 if (flags & DICT_FLAG_RETURN_BOOL)
1412 defObject = Py_False;
1413
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001414 if (!(key = StringToChars(keyObject, &todecref)))
1415 return NULL;
1416
1417 if (*key == NUL)
1418 {
1419 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001420 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001421 return NULL;
1422 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001423
Bram Moolenaara9922d62013-05-30 13:01:18 +02001424 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001425
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001426 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001427
Bram Moolenaara9922d62013-05-30 13:01:18 +02001428 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001429 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001430 if (defObject)
1431 {
1432 Py_INCREF(defObject);
1433 return defObject;
1434 }
1435 else
1436 {
1437 PyErr_SetObject(PyExc_KeyError, keyObject);
1438 return NULL;
1439 }
1440 }
1441 else if (flags & DICT_FLAG_RETURN_BOOL)
1442 {
1443 Py_INCREF(Py_True);
1444 return Py_True;
1445 }
1446
1447 di = dict_lookup(hi);
1448
1449 if (!(r = ConvertToPyObject(&di->di_tv)))
1450 return NULL;
1451
1452 if (flags & DICT_FLAG_POP)
1453 {
1454 if (dict->dv_lock)
1455 {
1456 PyErr_SetVim(_("dict is locked"));
1457 Py_DECREF(r);
1458 return NULL;
1459 }
1460
1461 hash_remove(&dict->dv_hashtab, hi);
1462 dictitem_free(di);
1463 }
1464
Bram Moolenaara9922d62013-05-30 13:01:18 +02001465 return r;
1466}
1467
1468 static PyObject *
1469DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1470{
1471 return _DictionaryItem(self, keyObject, 0);
1472}
1473
1474 static int
1475DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1476{
1477 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1478 int r;
1479
1480 r = (rObj == Py_True);
1481
1482 Py_DECREF(Py_True);
1483
1484 return r;
1485}
1486
1487typedef struct
1488{
1489 hashitem_T *ht_array;
1490 long_u ht_used;
1491 hashtab_T *ht;
1492 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001493 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001494} dictiterinfo_T;
1495
1496 static PyObject *
1497DictionaryIterNext(dictiterinfo_T **dii)
1498{
1499 PyObject *r;
1500
1501 if (!(*dii)->todo)
1502 return NULL;
1503
1504 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1505 (*dii)->ht->ht_used != (*dii)->ht_used)
1506 {
1507 PyErr_SetString(PyExc_RuntimeError,
1508 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001509 return NULL;
1510 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001511
Bram Moolenaara9922d62013-05-30 13:01:18 +02001512 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1513 ++((*dii)->hi);
1514
1515 --((*dii)->todo);
1516
1517 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1518 return NULL;
1519
1520 return r;
1521}
1522
1523 static PyObject *
1524DictionaryIter(DictionaryObject *self)
1525{
1526 dictiterinfo_T *dii;
1527 hashtab_T *ht;
1528
1529 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1530 {
1531 PyErr_NoMemory();
1532 return NULL;
1533 }
1534
1535 ht = &self->dict->dv_hashtab;
1536 dii->ht_array = ht->ht_array;
1537 dii->ht_used = ht->ht_used;
1538 dii->ht = ht;
1539 dii->hi = dii->ht_array;
1540 dii->todo = dii->ht_used;
1541
1542 return IterNew(dii,
1543 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1544 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001545}
1546
1547 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001548DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001549{
1550 char_u *key;
1551 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001552 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001553 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001554 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001555
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001556 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001557 {
1558 PyErr_SetVim(_("dict is locked"));
1559 return -1;
1560 }
1561
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001562 if (!(key = StringToChars(keyObject, &todecref)))
1563 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001564
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001565 if (*key == NUL)
1566 {
1567 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001568 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001569 return -1;
1570 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001571
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001572 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001573
1574 if (valObject == NULL)
1575 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001576 hashitem_T *hi;
1577
Bram Moolenaardb913952012-06-29 12:54:53 +02001578 if (di == NULL)
1579 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001580 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001581 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001582 return -1;
1583 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001584 hi = hash_find(&dict->dv_hashtab, di->di_key);
1585 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001586 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001587 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001588 return 0;
1589 }
1590
1591 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001592 {
1593 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001594 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001595 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001596
1597 if (di == NULL)
1598 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001599 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001600 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001601 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001602 PyErr_NoMemory();
1603 return -1;
1604 }
1605 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001606 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001607
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001608 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001609 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001610 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001611 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001612 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001613 PyErr_SetVim(_("failed to add key to dictionary"));
1614 return -1;
1615 }
1616 }
1617 else
1618 clear_tv(&di->di_tv);
1619
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001620 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001621
1622 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001623 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001624 return 0;
1625}
1626
Bram Moolenaara9922d62013-05-30 13:01:18 +02001627typedef PyObject *(*hi_to_py)(hashitem_T *);
1628
Bram Moolenaardb913952012-06-29 12:54:53 +02001629 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001630DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001631{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001632 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001633 long_u todo = dict->dv_hashtab.ht_used;
1634 Py_ssize_t i = 0;
1635 PyObject *r;
1636 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001637 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001638
1639 r = PyList_New(todo);
1640 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1641 {
1642 if (!HASHITEM_EMPTY(hi))
1643 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001644 if (!(newObj = hiconvert(hi)))
1645 {
1646 Py_DECREF(r);
1647 return NULL;
1648 }
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02001649 PyList_SET_ITEM(r, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001650 --todo;
1651 ++i;
1652 }
1653 }
1654 return r;
1655}
1656
Bram Moolenaara9922d62013-05-30 13:01:18 +02001657 static PyObject *
1658dict_key(hashitem_T *hi)
1659{
1660 return PyBytes_FromString((char *)(hi->hi_key));
1661}
1662
1663 static PyObject *
1664DictionaryListKeys(DictionaryObject *self)
1665{
1666 return DictionaryListObjects(self, dict_key);
1667}
1668
1669 static PyObject *
1670dict_val(hashitem_T *hi)
1671{
1672 dictitem_T *di;
1673
1674 di = dict_lookup(hi);
1675 return ConvertToPyObject(&di->di_tv);
1676}
1677
1678 static PyObject *
1679DictionaryListValues(DictionaryObject *self)
1680{
1681 return DictionaryListObjects(self, dict_val);
1682}
1683
1684 static PyObject *
1685dict_item(hashitem_T *hi)
1686{
1687 PyObject *keyObject;
1688 PyObject *valObject;
1689 PyObject *r;
1690
1691 if (!(keyObject = dict_key(hi)))
1692 return NULL;
1693
1694 if (!(valObject = dict_val(hi)))
1695 {
1696 Py_DECREF(keyObject);
1697 return NULL;
1698 }
1699
1700 r = Py_BuildValue("(OO)", keyObject, valObject);
1701
1702 Py_DECREF(keyObject);
1703 Py_DECREF(valObject);
1704
1705 return r;
1706}
1707
1708 static PyObject *
1709DictionaryListItems(DictionaryObject *self)
1710{
1711 return DictionaryListObjects(self, dict_item);
1712}
1713
1714 static PyObject *
1715DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1716{
1717 dict_T *dict = self->dict;
1718
1719 if (dict->dv_lock)
1720 {
1721 PyErr_SetVim(_("dict is locked"));
1722 return NULL;
1723 }
1724
1725 if (kwargs)
1726 {
1727 typval_T tv;
1728
1729 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1730 return NULL;
1731
1732 VimTryStart();
1733 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1734 clear_tv(&tv);
1735 if (VimTryEnd())
1736 return NULL;
1737 }
1738 else
1739 {
1740 PyObject *object;
1741
1742 if (!PyArg_Parse(args, "(O)", &object))
1743 return NULL;
1744
1745 if (PyObject_HasAttrString(object, "keys"))
1746 return DictionaryUpdate(self, NULL, object);
1747 else
1748 {
1749 PyObject *iterator;
1750 PyObject *item;
1751
1752 if (!(iterator = PyObject_GetIter(object)))
1753 return NULL;
1754
1755 while ((item = PyIter_Next(iterator)))
1756 {
1757 PyObject *fast;
1758 PyObject *keyObject;
1759 PyObject *valObject;
1760 PyObject *todecref;
1761 char_u *key;
1762 dictitem_T *di;
1763
1764 if (!(fast = PySequence_Fast(item, "")))
1765 {
1766 Py_DECREF(iterator);
1767 Py_DECREF(item);
1768 return NULL;
1769 }
1770
1771 Py_DECREF(item);
1772
1773 if (PySequence_Fast_GET_SIZE(fast) != 2)
1774 {
1775 Py_DECREF(iterator);
1776 Py_DECREF(fast);
1777 PyErr_SetString(PyExc_ValueError,
1778 _("expected sequence element of size 2"));
1779 return NULL;
1780 }
1781
1782 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1783
1784 if (!(key = StringToChars(keyObject, &todecref)))
1785 {
1786 Py_DECREF(iterator);
1787 Py_DECREF(fast);
1788 return NULL;
1789 }
1790
1791 di = dictitem_alloc(key);
1792
1793 Py_XDECREF(todecref);
1794
1795 if (di == NULL)
1796 {
1797 Py_DECREF(fast);
1798 Py_DECREF(iterator);
1799 PyErr_NoMemory();
1800 return NULL;
1801 }
1802 di->di_tv.v_lock = 0;
1803 di->di_tv.v_type = VAR_UNKNOWN;
1804
1805 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1806
1807 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1808 {
1809 Py_DECREF(iterator);
1810 Py_DECREF(fast);
1811 dictitem_free(di);
1812 return NULL;
1813 }
1814
1815 Py_DECREF(fast);
1816
1817 if (dict_add(dict, di) == FAIL)
1818 {
1819 Py_DECREF(iterator);
1820 dictitem_free(di);
1821 PyErr_SetVim(_("failed to add key to dictionary"));
1822 return NULL;
1823 }
1824 }
1825
1826 Py_DECREF(iterator);
1827
1828 /* Iterator may have finished due to an exception */
1829 if (PyErr_Occurred())
1830 return NULL;
1831 }
1832 }
1833 Py_INCREF(Py_None);
1834 return Py_None;
1835}
1836
1837 static PyObject *
1838DictionaryGet(DictionaryObject *self, PyObject *args)
1839{
1840 return _DictionaryItem(self, args,
1841 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1842}
1843
1844 static PyObject *
1845DictionaryPop(DictionaryObject *self, PyObject *args)
1846{
1847 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1848}
1849
1850 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001851DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001852{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001853 hashitem_T *hi;
1854 PyObject *r;
1855 PyObject *valObject;
1856 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001857
Bram Moolenaarde71b562013-06-02 17:41:54 +02001858 if (self->dict->dv_hashtab.ht_used == 0)
1859 {
1860 PyErr_SetNone(PyExc_KeyError);
1861 return NULL;
1862 }
1863
1864 hi = self->dict->dv_hashtab.ht_array;
1865 while (HASHITEM_EMPTY(hi))
1866 ++hi;
1867
1868 di = dict_lookup(hi);
1869
1870 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001871 return NULL;
1872
Bram Moolenaarde71b562013-06-02 17:41:54 +02001873 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
1874 {
1875 Py_DECREF(valObject);
1876 return NULL;
1877 }
1878
1879 hash_remove(&self->dict->dv_hashtab, hi);
1880 dictitem_free(di);
1881
1882 return r;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001883}
1884
1885 static PyObject *
1886DictionaryHasKey(DictionaryObject *self, PyObject *args)
1887{
1888 PyObject *keyObject;
1889
1890 if (!PyArg_ParseTuple(args, "O", &keyObject))
1891 return NULL;
1892
1893 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1894}
1895
1896static PySequenceMethods DictionaryAsSeq = {
1897 0, /* sq_length */
1898 0, /* sq_concat */
1899 0, /* sq_repeat */
1900 0, /* sq_item */
1901 0, /* sq_slice */
1902 0, /* sq_ass_item */
1903 0, /* sq_ass_slice */
1904 (objobjproc) DictionaryContains, /* sq_contains */
1905 0, /* sq_inplace_concat */
1906 0, /* sq_inplace_repeat */
1907};
1908
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001909static PyMappingMethods DictionaryAsMapping = {
1910 (lenfunc) DictionaryLength,
1911 (binaryfunc) DictionaryItem,
1912 (objobjargproc) DictionaryAssItem,
1913};
1914
Bram Moolenaardb913952012-06-29 12:54:53 +02001915static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001916 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001917 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1918 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1919 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1920 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1921 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02001922 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001923 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001924 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1925 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001926};
1927
1928static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001929static PySequenceMethods ListAsSeq;
1930static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001931
1932typedef struct
1933{
1934 PyObject_HEAD
1935 list_T *list;
1936 pylinkedlist_T ref;
1937} ListObject;
1938
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001939#define NEW_LIST(list) ListNew(&ListType, list)
1940
Bram Moolenaardb913952012-06-29 12:54:53 +02001941 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001942ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001943{
1944 ListObject *self;
1945
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001946 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001947 if (self == NULL)
1948 return NULL;
1949 self->list = list;
1950 ++list->lv_refcount;
1951
1952 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1953
1954 return (PyObject *)(self);
1955}
1956
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001957 static list_T *
1958py_list_alloc()
1959{
1960 list_T *r;
1961
1962 if (!(r = list_alloc()))
1963 {
1964 PyErr_NoMemory();
1965 return NULL;
1966 }
1967 ++r->lv_refcount;
1968
1969 return r;
1970}
1971
1972 static int
1973list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1974{
1975 PyObject *iterator;
1976 PyObject *item;
1977 listitem_T *li;
1978
1979 if (!(iterator = PyObject_GetIter(obj)))
1980 return -1;
1981
1982 while ((item = PyIter_Next(iterator)))
1983 {
1984 if (!(li = listitem_alloc()))
1985 {
1986 PyErr_NoMemory();
1987 Py_DECREF(item);
1988 Py_DECREF(iterator);
1989 return -1;
1990 }
1991 li->li_tv.v_lock = 0;
1992 li->li_tv.v_type = VAR_UNKNOWN;
1993
1994 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1995 {
1996 Py_DECREF(item);
1997 Py_DECREF(iterator);
1998 listitem_free(li);
1999 return -1;
2000 }
2001
2002 Py_DECREF(item);
2003
2004 list_append(l, li);
2005 }
2006
2007 Py_DECREF(iterator);
2008
2009 /* Iterator may have finished due to an exception */
2010 if (PyErr_Occurred())
2011 return -1;
2012
2013 return 0;
2014}
2015
2016 static PyObject *
2017ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2018{
2019 list_T *list;
2020 PyObject *obj = NULL;
2021
2022 if (kwargs)
2023 {
2024 PyErr_SetString(PyExc_TypeError,
2025 _("list constructor does not accept keyword arguments"));
2026 return NULL;
2027 }
2028
2029 if (!PyArg_ParseTuple(args, "|O", &obj))
2030 return NULL;
2031
2032 if (!(list = py_list_alloc()))
2033 return NULL;
2034
2035 if (obj)
2036 {
2037 PyObject *lookup_dict;
2038
2039 if (!(lookup_dict = PyDict_New()))
2040 {
2041 list_unref(list);
2042 return NULL;
2043 }
2044
2045 if (list_py_concat(list, obj, lookup_dict) == -1)
2046 {
2047 Py_DECREF(lookup_dict);
2048 list_unref(list);
2049 return NULL;
2050 }
2051
2052 Py_DECREF(lookup_dict);
2053 }
2054
2055 return ListNew(subtype, list);
2056}
2057
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002058 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002059ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002060{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002061 pyll_remove(&self->ref, &lastlist);
2062 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002063
2064 DESTRUCTOR_FINISH(self);
2065}
2066
Bram Moolenaardb913952012-06-29 12:54:53 +02002067 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002068ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002069{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002070 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002071}
2072
2073 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002074ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002075{
2076 listitem_T *li;
2077
Bram Moolenaard6e39182013-05-21 18:30:34 +02002078 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002079 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002080 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002081 return NULL;
2082 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002083 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002084 if (li == NULL)
2085 {
2086 PyErr_SetVim(_("internal error: failed to get vim list item"));
2087 return NULL;
2088 }
2089 return ConvertToPyObject(&li->li_tv);
2090}
2091
2092#define PROC_RANGE \
2093 if (last < 0) {\
2094 if (last < -size) \
2095 last = 0; \
2096 else \
2097 last += size; \
2098 } \
2099 if (first < 0) \
2100 first = 0; \
2101 if (first > size) \
2102 first = size; \
2103 if (last > size) \
2104 last = size;
2105
2106 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002107ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002108{
2109 PyInt i;
2110 PyInt size = ListLength(self);
2111 PyInt n;
2112 PyObject *list;
2113 int reversed = 0;
2114
2115 PROC_RANGE
2116 if (first >= last)
2117 first = last;
2118
2119 n = last-first;
2120 list = PyList_New(n);
2121 if (list == NULL)
2122 return NULL;
2123
2124 for (i = 0; i < n; ++i)
2125 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002126 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002127 if (item == NULL)
2128 {
2129 Py_DECREF(list);
2130 return NULL;
2131 }
2132
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002133 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002134 }
2135
2136 return list;
2137}
2138
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002139typedef struct
2140{
2141 listwatch_T lw;
2142 list_T *list;
2143} listiterinfo_T;
2144
2145 static void
2146ListIterDestruct(listiterinfo_T *lii)
2147{
2148 list_rem_watch(lii->list, &lii->lw);
2149 PyMem_Free(lii);
2150}
2151
2152 static PyObject *
2153ListIterNext(listiterinfo_T **lii)
2154{
2155 PyObject *r;
2156
2157 if (!((*lii)->lw.lw_item))
2158 return NULL;
2159
2160 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
2161 return NULL;
2162
2163 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2164
2165 return r;
2166}
2167
2168 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002169ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002170{
2171 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002172 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002173
2174 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2175 {
2176 PyErr_NoMemory();
2177 return NULL;
2178 }
2179
2180 list_add_watch(l, &lii->lw);
2181 lii->lw.lw_item = l->lv_first;
2182 lii->list = l;
2183
2184 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002185 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2186 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002187}
2188
Bram Moolenaardb913952012-06-29 12:54:53 +02002189 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002190ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002191{
2192 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002193 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002194 listitem_T *li;
2195 Py_ssize_t length = ListLength(self);
2196
2197 if (l->lv_lock)
2198 {
2199 PyErr_SetVim(_("list is locked"));
2200 return -1;
2201 }
2202 if (index>length || (index==length && obj==NULL))
2203 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002204 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002205 return -1;
2206 }
2207
2208 if (obj == NULL)
2209 {
2210 li = list_find(l, (long) index);
2211 list_remove(l, li, li);
2212 clear_tv(&li->li_tv);
2213 vim_free(li);
2214 return 0;
2215 }
2216
2217 if (ConvertFromPyObject(obj, &tv) == -1)
2218 return -1;
2219
2220 if (index == length)
2221 {
2222 if (list_append_tv(l, &tv) == FAIL)
2223 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002224 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002225 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002226 return -1;
2227 }
2228 }
2229 else
2230 {
2231 li = list_find(l, (long) index);
2232 clear_tv(&li->li_tv);
2233 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002234 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002235 }
2236 return 0;
2237}
2238
2239 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002240ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002241{
2242 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002243 PyObject *iterator;
2244 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002245 listitem_T *li;
2246 listitem_T *next;
2247 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002248 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002249 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002250
2251 if (l->lv_lock)
2252 {
2253 PyErr_SetVim(_("list is locked"));
2254 return -1;
2255 }
2256
2257 PROC_RANGE
2258
2259 if (first == size)
2260 li = NULL;
2261 else
2262 {
2263 li = list_find(l, (long) first);
2264 if (li == NULL)
2265 {
2266 PyErr_SetVim(_("internal error: no vim list item"));
2267 return -1;
2268 }
2269 if (last > first)
2270 {
2271 i = last - first;
2272 while (i-- && li != NULL)
2273 {
2274 next = li->li_next;
2275 listitem_remove(l, li);
2276 li = next;
2277 }
2278 }
2279 }
2280
2281 if (obj == NULL)
2282 return 0;
2283
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002284 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002285 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002286
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002287 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002288 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002289 if (ConvertFromPyObject(item, &v) == -1)
2290 {
2291 Py_DECREF(iterator);
2292 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002293 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002294 }
2295 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002296 if (list_insert_tv(l, &v, li) == FAIL)
2297 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002298 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002299 PyErr_SetVim(_("internal error: failed to add item to list"));
2300 return -1;
2301 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002302 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002303 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002304 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02002305 return 0;
2306}
2307
2308 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002309ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002310{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002311 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002312 PyObject *lookup_dict;
2313
2314 if (l->lv_lock)
2315 {
2316 PyErr_SetVim(_("list is locked"));
2317 return NULL;
2318 }
2319
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002320 if (!(lookup_dict = PyDict_New()))
2321 return NULL;
2322
Bram Moolenaardb913952012-06-29 12:54:53 +02002323 if (list_py_concat(l, obj, lookup_dict) == -1)
2324 {
2325 Py_DECREF(lookup_dict);
2326 return NULL;
2327 }
2328 Py_DECREF(lookup_dict);
2329
2330 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002331 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002332}
2333
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002334static char *ListAttrs[] = {
2335 "locked",
2336 NULL
2337};
2338
2339 static PyObject *
2340ListDir(PyObject *self)
2341{
2342 return ObjectDir(self, ListAttrs);
2343}
2344
Bram Moolenaar66b79852012-09-21 14:00:35 +02002345 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002346ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002347{
2348 if (val == NULL)
2349 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002350 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002351 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002352 return -1;
2353 }
2354
2355 if (strcmp(name, "locked") == 0)
2356 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002357 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002358 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002359 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002360 return -1;
2361 }
2362 else
2363 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002364 int istrue = PyObject_IsTrue(val);
2365 if (istrue == -1)
2366 return -1;
2367 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002368 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002369 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002370 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002371 }
2372 return 0;
2373 }
2374 else
2375 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002376 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002377 return -1;
2378 }
2379}
2380
Bram Moolenaardb913952012-06-29 12:54:53 +02002381static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002382 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2383 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2384 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002385};
2386
2387typedef struct
2388{
2389 PyObject_HEAD
2390 char_u *name;
2391} FunctionObject;
2392
2393static PyTypeObject FunctionType;
2394
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002395#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2396
Bram Moolenaardb913952012-06-29 12:54:53 +02002397 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002398FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002399{
2400 FunctionObject *self;
2401
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002402 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2403
Bram Moolenaardb913952012-06-29 12:54:53 +02002404 if (self == NULL)
2405 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002406
2407 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002408 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002409 if (!translated_function_exists(name))
2410 {
2411 PyErr_SetString(PyExc_ValueError,
2412 _("unnamed function does not exist"));
2413 return NULL;
2414 }
2415 self->name = vim_strsave(name);
2416 func_ref(self->name);
2417 }
2418 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002419 if ((self->name = get_expanded_name(name,
2420 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2421 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002422 {
Bram Moolenaar018acca2013-05-30 13:37:28 +02002423 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2424 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002425 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002426
2427 return (PyObject *)(self);
2428}
2429
2430 static PyObject *
2431FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2432{
2433 PyObject *self;
2434 char_u *name;
2435
2436 if (kwargs)
2437 {
2438 PyErr_SetString(PyExc_TypeError,
2439 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002440 return NULL;
2441 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002442
2443 if (!PyArg_ParseTuple(args, "s", &name))
2444 return NULL;
2445
2446 self = FunctionNew(subtype, name);
2447
2448 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002449}
2450
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002451 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002452FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002453{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002454 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002455 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002456
2457 DESTRUCTOR_FINISH(self);
2458}
2459
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002460static char *FunctionAttrs[] = {
2461 "softspace",
2462 NULL
2463};
2464
2465 static PyObject *
2466FunctionDir(PyObject *self)
2467{
2468 return ObjectDir(self, FunctionAttrs);
2469}
2470
Bram Moolenaardb913952012-06-29 12:54:53 +02002471 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002472FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002473{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002474 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002475 typval_T args;
2476 typval_T selfdicttv;
2477 typval_T rettv;
2478 dict_T *selfdict = NULL;
2479 PyObject *selfdictObject;
2480 PyObject *result;
2481 int error;
2482
2483 if (ConvertFromPyObject(argsObject, &args) == -1)
2484 return NULL;
2485
2486 if (kwargs != NULL)
2487 {
2488 selfdictObject = PyDict_GetItemString(kwargs, "self");
2489 if (selfdictObject != NULL)
2490 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002491 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002492 {
2493 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002494 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002495 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002496 selfdict = selfdicttv.vval.v_dict;
2497 }
2498 }
2499
Bram Moolenaar71700b82013-05-15 17:49:05 +02002500 Py_BEGIN_ALLOW_THREADS
2501 Python_Lock_Vim();
2502
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002503 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002504 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002505
2506 Python_Release_Vim();
2507 Py_END_ALLOW_THREADS
2508
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002509 if (VimTryEnd())
2510 result = NULL;
2511 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002512 {
2513 result = NULL;
2514 PyErr_SetVim(_("failed to run function"));
2515 }
2516 else
2517 result = ConvertToPyObject(&rettv);
2518
Bram Moolenaardb913952012-06-29 12:54:53 +02002519 clear_tv(&args);
2520 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002521 if (selfdict != NULL)
2522 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002523
2524 return result;
2525}
2526
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002527 static PyObject *
2528FunctionRepr(FunctionObject *self)
2529{
2530 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2531}
2532
Bram Moolenaardb913952012-06-29 12:54:53 +02002533static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002534 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2535 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002536};
2537
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002538/*
2539 * Options object
2540 */
2541
2542static PyTypeObject OptionsType;
2543
2544typedef int (*checkfun)(void *);
2545
2546typedef struct
2547{
2548 PyObject_HEAD
2549 int opt_type;
2550 void *from;
2551 checkfun Check;
2552 PyObject *fromObj;
2553} OptionsObject;
2554
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002555 static int
2556dummy_check(void *arg UNUSED)
2557{
2558 return 0;
2559}
2560
2561 static PyObject *
2562OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2563{
2564 OptionsObject *self;
2565
Bram Moolenaar774267b2013-05-21 20:51:59 +02002566 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002567 if (self == NULL)
2568 return NULL;
2569
2570 self->opt_type = opt_type;
2571 self->from = from;
2572 self->Check = Check;
2573 self->fromObj = fromObj;
2574 if (fromObj)
2575 Py_INCREF(fromObj);
2576
2577 return (PyObject *)(self);
2578}
2579
2580 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002581OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002582{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002583 PyObject_GC_UnTrack((void *)(self));
2584 Py_XDECREF(self->fromObj);
2585 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002586}
2587
2588 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002589OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002590{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002591 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002592 return 0;
2593}
2594
2595 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002596OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002597{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002598 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002599 return 0;
2600}
2601
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002602 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002603OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002604{
2605 char_u *key;
2606 int flags;
2607 long numval;
2608 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002609 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002610
Bram Moolenaard6e39182013-05-21 18:30:34 +02002611 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002612 return NULL;
2613
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002614 if (!(key = StringToChars(keyObject, &todecref)))
2615 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002616
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002617 if (*key == NUL)
2618 {
2619 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002620 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002621 return NULL;
2622 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002623
2624 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002625 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002626
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002627 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002628
2629 if (flags == 0)
2630 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002631 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002632 return NULL;
2633 }
2634
2635 if (flags & SOPT_UNSET)
2636 {
2637 Py_INCREF(Py_None);
2638 return Py_None;
2639 }
2640 else if (flags & SOPT_BOOL)
2641 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002642 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002643 r = numval ? Py_True : Py_False;
2644 Py_INCREF(r);
2645 return r;
2646 }
2647 else if (flags & SOPT_NUM)
2648 return PyInt_FromLong(numval);
2649 else if (flags & SOPT_STRING)
2650 {
2651 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002652 {
2653 PyObject *r = PyBytes_FromString((char *) stringval);
2654 vim_free(stringval);
2655 return r;
2656 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002657 else
2658 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002659 PyErr_SetString(PyExc_RuntimeError,
2660 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002661 return NULL;
2662 }
2663 }
2664 else
2665 {
2666 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2667 return NULL;
2668 }
2669}
2670
2671 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002672set_option_value_err(key, numval, stringval, opt_flags)
2673 char_u *key;
2674 int numval;
2675 char_u *stringval;
2676 int opt_flags;
2677{
2678 char_u *errmsg;
2679
2680 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2681 {
2682 if (VimTryEnd())
2683 return FAIL;
2684 PyErr_SetVim((char *)errmsg);
2685 return FAIL;
2686 }
2687 return OK;
2688}
2689
2690 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002691set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2692 char_u *key;
2693 int numval;
2694 char_u *stringval;
2695 int opt_flags;
2696 int opt_type;
2697 void *from;
2698{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002699 win_T *save_curwin = NULL;
2700 tabpage_T *save_curtab = NULL;
2701 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002702 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002703
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002704 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002705 switch (opt_type)
2706 {
2707 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002708 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2709 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002710 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002711 if (VimTryEnd())
2712 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002713 PyErr_SetVim("Problem while switching windows.");
2714 return -1;
2715 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002716 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002717 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002718 if (r == FAIL)
2719 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002720 break;
2721 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002722 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002723 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002724 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002725 if (r == FAIL)
2726 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002727 break;
2728 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002729 r = set_option_value_err(key, numval, stringval, opt_flags);
2730 if (r == FAIL)
2731 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002732 break;
2733 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002734 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002735}
2736
2737 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002738OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002739{
2740 char_u *key;
2741 int flags;
2742 int opt_flags;
2743 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002744 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002745
Bram Moolenaard6e39182013-05-21 18:30:34 +02002746 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002747 return -1;
2748
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002749 if (!(key = StringToChars(keyObject, &todecref)))
2750 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002751
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002752 if (*key == NUL)
2753 {
2754 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002755 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002756 return -1;
2757 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002758
2759 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002760 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002761
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002762 if (flags == 0)
2763 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002764 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002765 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002766 return -1;
2767 }
2768
2769 if (valObject == NULL)
2770 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002771 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002772 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002773 PyErr_SetString(PyExc_ValueError,
2774 _("unable to unset global option"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002775 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002776 return -1;
2777 }
2778 else if (!(flags & SOPT_GLOBAL))
2779 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002780 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2781 "without global value"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002782 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002783 return -1;
2784 }
2785 else
2786 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002787 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002788 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002789 return 0;
2790 }
2791 }
2792
Bram Moolenaard6e39182013-05-21 18:30:34 +02002793 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002794
2795 if (flags & SOPT_BOOL)
2796 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002797 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002798
Bram Moolenaarb983f752013-05-15 16:11:50 +02002799 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002800 r = -1;
2801 else
2802 r = set_option_value_for(key, istrue, NULL,
2803 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002804 }
2805 else if (flags & SOPT_NUM)
2806 {
2807 int val;
2808
2809#if PY_MAJOR_VERSION < 3
2810 if (PyInt_Check(valObject))
2811 val = PyInt_AsLong(valObject);
2812 else
2813#endif
2814 if (PyLong_Check(valObject))
2815 val = PyLong_AsLong(valObject);
2816 else
2817 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002818 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002819 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002820 return -1;
2821 }
2822
2823 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002824 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002825 }
2826 else
2827 {
2828 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002829 PyObject *todecref;
2830
2831 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002832 r = set_option_value_for(key, 0, val, opt_flags,
2833 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002834 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002835 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002836 }
2837
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002838 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002839
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002840 return r;
2841}
2842
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002843static PyMappingMethods OptionsAsMapping = {
2844 (lenfunc) NULL,
2845 (binaryfunc) OptionsItem,
2846 (objobjargproc) OptionsAssItem,
2847};
2848
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002849/* Tabpage object
2850 */
2851
2852typedef struct
2853{
2854 PyObject_HEAD
2855 tabpage_T *tab;
2856} TabPageObject;
2857
2858static PyObject *WinListNew(TabPageObject *tabObject);
2859
2860static PyTypeObject TabPageType;
2861
2862 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002863CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002864{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002865 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002866 {
2867 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2868 return -1;
2869 }
2870
2871 return 0;
2872}
2873
2874 static PyObject *
2875TabPageNew(tabpage_T *tab)
2876{
2877 TabPageObject *self;
2878
2879 if (TAB_PYTHON_REF(tab))
2880 {
2881 self = TAB_PYTHON_REF(tab);
2882 Py_INCREF(self);
2883 }
2884 else
2885 {
2886 self = PyObject_NEW(TabPageObject, &TabPageType);
2887 if (self == NULL)
2888 return NULL;
2889 self->tab = tab;
2890 TAB_PYTHON_REF(tab) = self;
2891 }
2892
2893 return (PyObject *)(self);
2894}
2895
2896 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002897TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002898{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002899 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2900 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002901
2902 DESTRUCTOR_FINISH(self);
2903}
2904
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002905static char *TabPageAttrs[] = {
2906 "windows", "number", "vars", "window", "valid",
2907 NULL
2908};
2909
2910 static PyObject *
2911TabPageDir(PyObject *self)
2912{
2913 return ObjectDir(self, TabPageAttrs);
2914}
2915
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002916 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002917TabPageAttrValid(TabPageObject *self, char *name)
2918{
2919 PyObject *r;
2920
2921 if (strcmp(name, "valid") != 0)
2922 return NULL;
2923
2924 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2925 Py_INCREF(r);
2926 return r;
2927}
2928
2929 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002930TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002931{
2932 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002933 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002934 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002935 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002936 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002937 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002938 else if (strcmp(name, "window") == 0)
2939 {
2940 /* For current tab window.c does not bother to set or update tp_curwin
2941 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002942 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002943 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002944 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002945 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002946 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002947 else if (strcmp(name, "__members__") == 0)
2948 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002949 return NULL;
2950}
2951
2952 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002953TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002954{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002955 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002956 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002957 else
2958 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002959 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002960
2961 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002962 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2963 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002964 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002965 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002966 }
2967}
2968
2969static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002970 /* name, function, calling, documentation */
2971 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2972 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002973};
2974
2975/*
2976 * Window list object
2977 */
2978
2979static PyTypeObject TabListType;
2980static PySequenceMethods TabListAsSeq;
2981
2982typedef struct
2983{
2984 PyObject_HEAD
2985} TabListObject;
2986
2987 static PyInt
2988TabListLength(PyObject *self UNUSED)
2989{
2990 tabpage_T *tp = first_tabpage;
2991 PyInt n = 0;
2992
2993 while (tp != NULL)
2994 {
2995 ++n;
2996 tp = tp->tp_next;
2997 }
2998
2999 return n;
3000}
3001
3002 static PyObject *
3003TabListItem(PyObject *self UNUSED, PyInt n)
3004{
3005 tabpage_T *tp;
3006
3007 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3008 if (n == 0)
3009 return TabPageNew(tp);
3010
3011 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
3012 return NULL;
3013}
3014
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003015/* Window object
3016 */
3017
3018typedef struct
3019{
3020 PyObject_HEAD
3021 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003022 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003023} WindowObject;
3024
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003025static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003026
3027 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003028CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003029{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003030 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003031 {
3032 PyErr_SetVim(_("attempt to refer to deleted window"));
3033 return -1;
3034 }
3035
3036 return 0;
3037}
3038
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003039 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003040WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003041{
3042 /* We need to handle deletion of windows underneath us.
3043 * If we add a "w_python*_ref" field to the win_T structure,
3044 * then we can get at it in win_free() in vim. We then
3045 * need to create only ONE Python object per window - if
3046 * we try to create a second, just INCREF the existing one
3047 * and return it. The (single) Python object referring to
3048 * the window is stored in "w_python*_ref".
3049 * On a win_free() we set the Python object's win_T* field
3050 * to an invalid value. We trap all uses of a window
3051 * object, and reject them if the win_T* field is invalid.
3052 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003053 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003054 * w_python_ref and w_python3_ref fields respectively.
3055 */
3056
3057 WindowObject *self;
3058
3059 if (WIN_PYTHON_REF(win))
3060 {
3061 self = WIN_PYTHON_REF(win);
3062 Py_INCREF(self);
3063 }
3064 else
3065 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003066 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003067 if (self == NULL)
3068 return NULL;
3069 self->win = win;
3070 WIN_PYTHON_REF(win) = self;
3071 }
3072
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003073 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3074
Bram Moolenaar971db462013-05-12 18:44:48 +02003075 return (PyObject *)(self);
3076}
3077
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003078 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003079WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003080{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003081 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003082 if (self->win && self->win != INVALID_WINDOW_VALUE)
3083 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003084 Py_XDECREF(((PyObject *)(self->tabObject)));
3085 PyObject_GC_Del((void *)(self));
3086}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003087
Bram Moolenaar774267b2013-05-21 20:51:59 +02003088 static int
3089WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3090{
3091 Py_VISIT(((PyObject *)(self->tabObject)));
3092 return 0;
3093}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003094
Bram Moolenaar774267b2013-05-21 20:51:59 +02003095 static int
3096WindowClear(WindowObject *self)
3097{
3098 Py_CLEAR(self->tabObject);
3099 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003100}
3101
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003102 static win_T *
3103get_firstwin(TabPageObject *tabObject)
3104{
3105 if (tabObject)
3106 {
3107 if (CheckTabPage(tabObject))
3108 return NULL;
3109 /* For current tab window.c does not bother to set or update tp_firstwin
3110 */
3111 else if (tabObject->tab == curtab)
3112 return firstwin;
3113 else
3114 return tabObject->tab->tp_firstwin;
3115 }
3116 else
3117 return firstwin;
3118}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003119static char *WindowAttrs[] = {
3120 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3121 "tabpage", "valid",
3122 NULL
3123};
3124
3125 static PyObject *
3126WindowDir(PyObject *self)
3127{
3128 return ObjectDir(self, WindowAttrs);
3129}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003130
Bram Moolenaar971db462013-05-12 18:44:48 +02003131 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003132WindowAttrValid(WindowObject *self, char *name)
3133{
3134 PyObject *r;
3135
3136 if (strcmp(name, "valid") != 0)
3137 return NULL;
3138
3139 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3140 Py_INCREF(r);
3141 return r;
3142}
3143
3144 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003145WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003146{
3147 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003148 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003149 else if (strcmp(name, "cursor") == 0)
3150 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003151 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003152
3153 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3154 }
3155 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003156 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003157#ifdef FEAT_WINDOWS
3158 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003159 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003160#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003161#ifdef FEAT_VERTSPLIT
3162 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003163 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003164 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003165 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003166#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003167 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003168 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003169 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003170 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3171 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003172 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003173 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003174 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003175 return NULL;
3176 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003177 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003178 }
3179 else if (strcmp(name, "tabpage") == 0)
3180 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003181 Py_INCREF(self->tabObject);
3182 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003183 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003184 else if (strcmp(name, "__members__") == 0)
3185 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003186 else
3187 return NULL;
3188}
3189
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003190 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003191WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003192{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003193 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003194 return -1;
3195
3196 if (strcmp(name, "buffer") == 0)
3197 {
3198 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
3199 return -1;
3200 }
3201 else if (strcmp(name, "cursor") == 0)
3202 {
3203 long lnum;
3204 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003205
3206 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
3207 return -1;
3208
Bram Moolenaard6e39182013-05-21 18:30:34 +02003209 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003210 {
3211 PyErr_SetVim(_("cursor position outside buffer"));
3212 return -1;
3213 }
3214
3215 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003216 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003217 return -1;
3218
Bram Moolenaard6e39182013-05-21 18:30:34 +02003219 self->win->w_cursor.lnum = lnum;
3220 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003221#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003222 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003223#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003224 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003225 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003226
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003227 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003228 return 0;
3229 }
3230 else if (strcmp(name, "height") == 0)
3231 {
3232 int height;
3233 win_T *savewin;
3234
3235 if (!PyArg_Parse(val, "i", &height))
3236 return -1;
3237
3238#ifdef FEAT_GUI
3239 need_mouse_correct = TRUE;
3240#endif
3241 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003242 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003243
3244 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003245 win_setheight(height);
3246 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003247 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003248 return -1;
3249
3250 return 0;
3251 }
3252#ifdef FEAT_VERTSPLIT
3253 else if (strcmp(name, "width") == 0)
3254 {
3255 int width;
3256 win_T *savewin;
3257
3258 if (!PyArg_Parse(val, "i", &width))
3259 return -1;
3260
3261#ifdef FEAT_GUI
3262 need_mouse_correct = TRUE;
3263#endif
3264 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003265 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003266
3267 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003268 win_setwidth(width);
3269 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003270 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003271 return -1;
3272
3273 return 0;
3274 }
3275#endif
3276 else
3277 {
3278 PyErr_SetString(PyExc_AttributeError, name);
3279 return -1;
3280 }
3281}
3282
3283 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003284WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003285{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003286 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003287 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003288 else
3289 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003290 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003291
Bram Moolenaar6d216452013-05-12 19:00:41 +02003292 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003293 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003294 (self));
3295 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003296 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003297 }
3298}
3299
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003300static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003301 /* name, function, calling, documentation */
3302 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3303 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003304};
3305
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003306/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003307 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003308 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003309
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003310static PyTypeObject WinListType;
3311static PySequenceMethods WinListAsSeq;
3312
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003313typedef struct
3314{
3315 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003316 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003317} WinListObject;
3318
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003319 static PyObject *
3320WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003321{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003322 WinListObject *self;
3323
3324 self = PyObject_NEW(WinListObject, &WinListType);
3325 self->tabObject = tabObject;
3326 Py_INCREF(tabObject);
3327
3328 return (PyObject *)(self);
3329}
3330
3331 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003332WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003333{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003334 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003335
3336 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003337 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003338 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003339 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003340
3341 DESTRUCTOR_FINISH(self);
3342}
3343
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003344 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003345WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003346{
3347 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003348 PyInt n = 0;
3349
Bram Moolenaard6e39182013-05-21 18:30:34 +02003350 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003351 return -1;
3352
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003353 while (w != NULL)
3354 {
3355 ++n;
3356 w = W_NEXT(w);
3357 }
3358
3359 return n;
3360}
3361
3362 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003363WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003364{
3365 win_T *w;
3366
Bram Moolenaard6e39182013-05-21 18:30:34 +02003367 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003368 return NULL;
3369
3370 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003371 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003372 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003373
3374 PyErr_SetString(PyExc_IndexError, _("no such window"));
3375 return NULL;
3376}
3377
3378/* Convert a Python string into a Vim line.
3379 *
3380 * The result is in allocated memory. All internal nulls are replaced by
3381 * newline characters. It is an error for the string to contain newline
3382 * characters.
3383 *
3384 * On errors, the Python exception data is set, and NULL is returned.
3385 */
3386 static char *
3387StringToLine(PyObject *obj)
3388{
3389 const char *str;
3390 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003391 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003392 PyInt len;
3393 PyInt i;
3394 char *p;
3395
3396 if (obj == NULL || !PyString_Check(obj))
3397 {
3398 PyErr_BadArgument();
3399 return NULL;
3400 }
3401
Bram Moolenaar19e60942011-06-19 00:27:51 +02003402 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
3403 str = PyString_AsString(bytes);
3404 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003405
3406 /*
3407 * Error checking: String must not contain newlines, as we
3408 * are replacing a single line, and we must replace it with
3409 * a single line.
3410 * A trailing newline is removed, so that append(f.readlines()) works.
3411 */
3412 p = memchr(str, '\n', len);
3413 if (p != NULL)
3414 {
3415 if (p == str + len - 1)
3416 --len;
3417 else
3418 {
3419 PyErr_SetVim(_("string cannot contain newlines"));
3420 return NULL;
3421 }
3422 }
3423
3424 /* Create a copy of the string, with internal nulls replaced by
3425 * newline characters, as is the vim convention.
3426 */
3427 save = (char *)alloc((unsigned)(len+1));
3428 if (save == NULL)
3429 {
3430 PyErr_NoMemory();
3431 return NULL;
3432 }
3433
3434 for (i = 0; i < len; ++i)
3435 {
3436 if (str[i] == '\0')
3437 save[i] = '\n';
3438 else
3439 save[i] = str[i];
3440 }
3441
3442 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003443 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003444
3445 return save;
3446}
3447
3448/* Get a line from the specified buffer. The line number is
3449 * in Vim format (1-based). The line is returned as a Python
3450 * string object.
3451 */
3452 static PyObject *
3453GetBufferLine(buf_T *buf, PyInt n)
3454{
3455 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3456}
3457
3458
3459/* Get a list of lines from the specified buffer. The line numbers
3460 * are in Vim format (1-based). The range is from lo up to, but not
3461 * including, hi. The list is returned as a Python list of string objects.
3462 */
3463 static PyObject *
3464GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3465{
3466 PyInt i;
3467 PyInt n = hi - lo;
3468 PyObject *list = PyList_New(n);
3469
3470 if (list == NULL)
3471 return NULL;
3472
3473 for (i = 0; i < n; ++i)
3474 {
3475 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3476
3477 /* Error check - was the Python string creation OK? */
3478 if (str == NULL)
3479 {
3480 Py_DECREF(list);
3481 return NULL;
3482 }
3483
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02003484 PyList_SET_ITEM(list, i, str);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003485 }
3486
3487 /* The ownership of the Python list is passed to the caller (ie,
3488 * the caller should Py_DECREF() the object when it is finished
3489 * with it).
3490 */
3491
3492 return list;
3493}
3494
3495/*
3496 * Check if deleting lines made the cursor position invalid.
3497 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3498 * deleted).
3499 */
3500 static void
3501py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3502{
3503 if (curwin->w_cursor.lnum >= lo)
3504 {
3505 /* Adjust the cursor position if it's in/after the changed
3506 * lines. */
3507 if (curwin->w_cursor.lnum >= hi)
3508 {
3509 curwin->w_cursor.lnum += extra;
3510 check_cursor_col();
3511 }
3512 else if (extra < 0)
3513 {
3514 curwin->w_cursor.lnum = lo;
3515 check_cursor();
3516 }
3517 else
3518 check_cursor_col();
3519 changed_cline_bef_curs();
3520 }
3521 invalidate_botline();
3522}
3523
Bram Moolenaar19e60942011-06-19 00:27:51 +02003524/*
3525 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003526 * in Vim format (1-based). The replacement line is given as
3527 * a Python string object. The object is checked for validity
3528 * and correct format. Errors are returned as a value of FAIL.
3529 * The return value is OK on success.
3530 * If OK is returned and len_change is not NULL, *len_change
3531 * is set to the change in the buffer length.
3532 */
3533 static int
3534SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3535{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003536 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003537 * There are three cases:
3538 * 1. NULL, or None - this is a deletion.
3539 * 2. A string - this is a replacement.
3540 * 3. Anything else - this is an error.
3541 */
3542 if (line == Py_None || line == NULL)
3543 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003544 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003545
3546 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003547 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003548
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003549 VimTryStart();
3550
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003551 if (u_savedel((linenr_T)n, 1L) == FAIL)
3552 PyErr_SetVim(_("cannot save undo information"));
3553 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3554 PyErr_SetVim(_("cannot delete line"));
3555 else
3556 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003557 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003558 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3559 deleted_lines_mark((linenr_T)n, 1L);
3560 }
3561
Bram Moolenaar105bc352013-05-17 16:03:57 +02003562 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003563
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003564 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003565 return FAIL;
3566
3567 if (len_change)
3568 *len_change = -1;
3569
3570 return OK;
3571 }
3572 else if (PyString_Check(line))
3573 {
3574 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003575 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003576
3577 if (save == NULL)
3578 return FAIL;
3579
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003580 VimTryStart();
3581
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003582 /* We do not need to free "save" if ml_replace() consumes it. */
3583 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003584 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003585
3586 if (u_savesub((linenr_T)n) == FAIL)
3587 {
3588 PyErr_SetVim(_("cannot save undo information"));
3589 vim_free(save);
3590 }
3591 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3592 {
3593 PyErr_SetVim(_("cannot replace line"));
3594 vim_free(save);
3595 }
3596 else
3597 changed_bytes((linenr_T)n, 0);
3598
Bram Moolenaar105bc352013-05-17 16:03:57 +02003599 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003600
3601 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003602 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003603 check_cursor_col();
3604
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003605 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003606 return FAIL;
3607
3608 if (len_change)
3609 *len_change = 0;
3610
3611 return OK;
3612 }
3613 else
3614 {
3615 PyErr_BadArgument();
3616 return FAIL;
3617 }
3618}
3619
Bram Moolenaar19e60942011-06-19 00:27:51 +02003620/* Replace a range of lines in the specified buffer. The line numbers are in
3621 * Vim format (1-based). The range is from lo up to, but not including, hi.
3622 * The replacement lines are given as a Python list of string objects. The
3623 * list is checked for validity and correct format. Errors are returned as a
3624 * value of FAIL. The return value is OK on success.
3625 * If OK is returned and len_change is not NULL, *len_change
3626 * is set to the change in the buffer length.
3627 */
3628 static int
3629SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3630{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003631 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003632 * There are three cases:
3633 * 1. NULL, or None - this is a deletion.
3634 * 2. A list - this is a replacement.
3635 * 3. Anything else - this is an error.
3636 */
3637 if (list == Py_None || list == NULL)
3638 {
3639 PyInt i;
3640 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003641 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003642
3643 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003644 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003645 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003646
3647 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3648 PyErr_SetVim(_("cannot save undo information"));
3649 else
3650 {
3651 for (i = 0; i < n; ++i)
3652 {
3653 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3654 {
3655 PyErr_SetVim(_("cannot delete line"));
3656 break;
3657 }
3658 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003659 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003660 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3661 deleted_lines_mark((linenr_T)lo, (long)i);
3662 }
3663
Bram Moolenaar105bc352013-05-17 16:03:57 +02003664 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003665
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003666 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003667 return FAIL;
3668
3669 if (len_change)
3670 *len_change = -n;
3671
3672 return OK;
3673 }
3674 else if (PyList_Check(list))
3675 {
3676 PyInt i;
3677 PyInt new_len = PyList_Size(list);
3678 PyInt old_len = hi - lo;
3679 PyInt extra = 0; /* lines added to text, can be negative */
3680 char **array;
3681 buf_T *savebuf;
3682
3683 if (new_len == 0) /* avoid allocating zero bytes */
3684 array = NULL;
3685 else
3686 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003687 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003688 if (array == NULL)
3689 {
3690 PyErr_NoMemory();
3691 return FAIL;
3692 }
3693 }
3694
3695 for (i = 0; i < new_len; ++i)
3696 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003697 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003698
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003699 if (!(line = PyList_GetItem(list, i)) ||
3700 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003701 {
3702 while (i)
3703 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003704 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003705 return FAIL;
3706 }
3707 }
3708
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003709 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003710 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003711
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003712 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003713 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003714
3715 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3716 PyErr_SetVim(_("cannot save undo information"));
3717
3718 /* If the size of the range is reducing (ie, new_len < old_len) we
3719 * need to delete some old_len. We do this at the start, by
3720 * repeatedly deleting line "lo".
3721 */
3722 if (!PyErr_Occurred())
3723 {
3724 for (i = 0; i < old_len - new_len; ++i)
3725 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3726 {
3727 PyErr_SetVim(_("cannot delete line"));
3728 break;
3729 }
3730 extra -= i;
3731 }
3732
3733 /* For as long as possible, replace the existing old_len with the
3734 * new old_len. This is a more efficient operation, as it requires
3735 * less memory allocation and freeing.
3736 */
3737 if (!PyErr_Occurred())
3738 {
3739 for (i = 0; i < old_len && i < new_len; ++i)
3740 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3741 == FAIL)
3742 {
3743 PyErr_SetVim(_("cannot replace line"));
3744 break;
3745 }
3746 }
3747 else
3748 i = 0;
3749
3750 /* Now we may need to insert the remaining new old_len. If we do, we
3751 * must free the strings as we finish with them (we can't pass the
3752 * responsibility to vim in this case).
3753 */
3754 if (!PyErr_Occurred())
3755 {
3756 while (i < new_len)
3757 {
3758 if (ml_append((linenr_T)(lo + i - 1),
3759 (char_u *)array[i], 0, FALSE) == FAIL)
3760 {
3761 PyErr_SetVim(_("cannot insert line"));
3762 break;
3763 }
3764 vim_free(array[i]);
3765 ++i;
3766 ++extra;
3767 }
3768 }
3769
3770 /* Free any left-over old_len, as a result of an error */
3771 while (i < new_len)
3772 {
3773 vim_free(array[i]);
3774 ++i;
3775 }
3776
3777 /* Free the array of old_len. All of its contents have now
3778 * been dealt with (either freed, or the responsibility passed
3779 * to vim.
3780 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003781 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003782
3783 /* Adjust marks. Invalidate any which lie in the
3784 * changed range, and move any in the remainder of the buffer.
3785 */
3786 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3787 (long)MAXLNUM, (long)extra);
3788 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3789
Bram Moolenaar105bc352013-05-17 16:03:57 +02003790 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003791 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3792
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003793 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003794 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003795
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003796 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003797 return FAIL;
3798
3799 if (len_change)
3800 *len_change = new_len - old_len;
3801
3802 return OK;
3803 }
3804 else
3805 {
3806 PyErr_BadArgument();
3807 return FAIL;
3808 }
3809}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003810
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003811/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003812 * The line number is in Vim format (1-based). The lines to be inserted are
3813 * given as a Python list of string objects or as a single string. The lines
3814 * to be added are checked for validity and correct format. Errors are
3815 * returned as a value of FAIL. The return value is OK on success.
3816 * If OK is returned and len_change is not NULL, *len_change
3817 * is set to the change in the buffer length.
3818 */
3819 static int
3820InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3821{
3822 /* First of all, we check the type of the supplied Python object.
3823 * It must be a string or a list, or the call is in error.
3824 */
3825 if (PyString_Check(lines))
3826 {
3827 char *str = StringToLine(lines);
3828 buf_T *savebuf;
3829
3830 if (str == NULL)
3831 return FAIL;
3832
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003833 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003834 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003835 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003836
3837 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3838 PyErr_SetVim(_("cannot save undo information"));
3839 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3840 PyErr_SetVim(_("cannot insert line"));
3841 else
3842 appended_lines_mark((linenr_T)n, 1L);
3843
3844 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003845 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003846 update_screen(VALID);
3847
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003848 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003849 return FAIL;
3850
3851 if (len_change)
3852 *len_change = 1;
3853
3854 return OK;
3855 }
3856 else if (PyList_Check(lines))
3857 {
3858 PyInt i;
3859 PyInt size = PyList_Size(lines);
3860 char **array;
3861 buf_T *savebuf;
3862
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003863 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003864 if (array == NULL)
3865 {
3866 PyErr_NoMemory();
3867 return FAIL;
3868 }
3869
3870 for (i = 0; i < size; ++i)
3871 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003872 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003873
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003874 if (!(line = PyList_GetItem(lines, i)) ||
3875 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003876 {
3877 while (i)
3878 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003879 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003880 return FAIL;
3881 }
3882 }
3883
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003884 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003885 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003886 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003887
3888 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3889 PyErr_SetVim(_("cannot save undo information"));
3890 else
3891 {
3892 for (i = 0; i < size; ++i)
3893 {
3894 if (ml_append((linenr_T)(n + i),
3895 (char_u *)array[i], 0, FALSE) == FAIL)
3896 {
3897 PyErr_SetVim(_("cannot insert line"));
3898
3899 /* Free the rest of the lines */
3900 while (i < size)
3901 vim_free(array[i++]);
3902
3903 break;
3904 }
3905 vim_free(array[i]);
3906 }
3907 if (i > 0)
3908 appended_lines_mark((linenr_T)n, (long)i);
3909 }
3910
3911 /* Free the array of lines. All of its contents have now
3912 * been freed.
3913 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003914 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003915
Bram Moolenaar105bc352013-05-17 16:03:57 +02003916 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003917 update_screen(VALID);
3918
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003919 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003920 return FAIL;
3921
3922 if (len_change)
3923 *len_change = size;
3924
3925 return OK;
3926 }
3927 else
3928 {
3929 PyErr_BadArgument();
3930 return FAIL;
3931 }
3932}
3933
3934/*
3935 * Common routines for buffers and line ranges
3936 * -------------------------------------------
3937 */
3938
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003939typedef struct
3940{
3941 PyObject_HEAD
3942 buf_T *buf;
3943} BufferObject;
3944
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003945 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003946CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003947{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003948 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003949 {
3950 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3951 return -1;
3952 }
3953
3954 return 0;
3955}
3956
3957 static PyObject *
3958RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3959{
3960 if (CheckBuffer(self))
3961 return NULL;
3962
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003963 if (end == -1)
3964 end = self->buf->b_ml.ml_line_count;
3965
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003966 if (n < 0)
3967 n += end - start + 1;
3968
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003969 if (n < 0 || n > end - start)
3970 {
3971 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3972 return NULL;
3973 }
3974
3975 return GetBufferLine(self->buf, n+start);
3976}
3977
3978 static PyObject *
3979RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3980{
3981 PyInt size;
3982
3983 if (CheckBuffer(self))
3984 return NULL;
3985
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003986 if (end == -1)
3987 end = self->buf->b_ml.ml_line_count;
3988
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003989 size = end - start + 1;
3990
3991 if (lo < 0)
3992 lo = 0;
3993 else if (lo > size)
3994 lo = size;
3995 if (hi < 0)
3996 hi = 0;
3997 if (hi < lo)
3998 hi = lo;
3999 else if (hi > size)
4000 hi = size;
4001
4002 return GetBufferLineList(self->buf, lo+start, hi+start);
4003}
4004
4005 static PyInt
4006RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
4007{
4008 PyInt len_change;
4009
4010 if (CheckBuffer(self))
4011 return -1;
4012
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004013 if (end == -1)
4014 end = self->buf->b_ml.ml_line_count;
4015
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004016 if (n < 0)
4017 n += end - start + 1;
4018
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004019 if (n < 0 || n > end - start)
4020 {
4021 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
4022 return -1;
4023 }
4024
4025 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
4026 return -1;
4027
4028 if (new_end)
4029 *new_end = end + len_change;
4030
4031 return 0;
4032}
4033
Bram Moolenaar19e60942011-06-19 00:27:51 +02004034 static PyInt
4035RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
4036{
4037 PyInt size;
4038 PyInt len_change;
4039
4040 /* Self must be a valid buffer */
4041 if (CheckBuffer(self))
4042 return -1;
4043
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004044 if (end == -1)
4045 end = self->buf->b_ml.ml_line_count;
4046
Bram Moolenaar19e60942011-06-19 00:27:51 +02004047 /* Sort out the slice range */
4048 size = end - start + 1;
4049
4050 if (lo < 0)
4051 lo = 0;
4052 else if (lo > size)
4053 lo = size;
4054 if (hi < 0)
4055 hi = 0;
4056 if (hi < lo)
4057 hi = lo;
4058 else if (hi > size)
4059 hi = size;
4060
4061 if (SetBufferLineList(self->buf, lo + start, hi + start,
4062 val, &len_change) == FAIL)
4063 return -1;
4064
4065 if (new_end)
4066 *new_end = end + len_change;
4067
4068 return 0;
4069}
4070
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004071
4072 static PyObject *
4073RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
4074{
4075 PyObject *lines;
4076 PyInt len_change;
4077 PyInt max;
4078 PyInt n;
4079
4080 if (CheckBuffer(self))
4081 return NULL;
4082
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004083 if (end == -1)
4084 end = self->buf->b_ml.ml_line_count;
4085
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086 max = n = end - start + 1;
4087
4088 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4089 return NULL;
4090
4091 if (n < 0 || n > max)
4092 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004093 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004094 return NULL;
4095 }
4096
4097 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4098 return NULL;
4099
4100 if (new_end)
4101 *new_end = end + len_change;
4102
4103 Py_INCREF(Py_None);
4104 return Py_None;
4105}
4106
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004107/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004108 */
4109
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004110static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004111static PySequenceMethods RangeAsSeq;
4112static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004113
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114typedef struct
4115{
4116 PyObject_HEAD
4117 BufferObject *buf;
4118 PyInt start;
4119 PyInt end;
4120} RangeObject;
4121
4122 static PyObject *
4123RangeNew(buf_T *buf, PyInt start, PyInt end)
4124{
4125 BufferObject *bufr;
4126 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004127 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004128 if (self == NULL)
4129 return NULL;
4130
4131 bufr = (BufferObject *)BufferNew(buf);
4132 if (bufr == NULL)
4133 {
4134 Py_DECREF(self);
4135 return NULL;
4136 }
4137 Py_INCREF(bufr);
4138
4139 self->buf = bufr;
4140 self->start = start;
4141 self->end = end;
4142
4143 return (PyObject *)(self);
4144}
4145
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004146 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004147RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004148{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004149 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004150 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004151 PyObject_GC_Del((void *)(self));
4152}
4153
4154 static int
4155RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4156{
4157 Py_VISIT(((PyObject *)(self->buf)));
4158 return 0;
4159}
4160
4161 static int
4162RangeClear(RangeObject *self)
4163{
4164 Py_CLEAR(self->buf);
4165 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004166}
4167
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004168 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004169RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004170{
4171 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004172 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004173 return -1; /* ??? */
4174
Bram Moolenaard6e39182013-05-21 18:30:34 +02004175 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004176}
4177
4178 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004179RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004180{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004181 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004182}
4183
4184 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004185RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004186{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004187 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004188}
4189
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004190static char *RangeAttrs[] = {
4191 "start", "end",
4192 NULL
4193};
4194
4195 static PyObject *
4196RangeDir(PyObject *self)
4197{
4198 return ObjectDir(self, RangeAttrs);
4199}
4200
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004201 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004202RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004203{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004204 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004205}
4206
4207 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004208RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004209{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004210 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004211 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4212 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004213 else
4214 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004215 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004216
4217 if (name == NULL)
4218 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004219
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004220 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004221 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004222 }
4223}
4224
4225static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004226 /* name, function, calling, documentation */
4227 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004228 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4229 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004230};
4231
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004232static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004233static PySequenceMethods BufferAsSeq;
4234static PyMappingMethods BufferAsMapping;
4235
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004236 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004237BufferNew(buf_T *buf)
4238{
4239 /* We need to handle deletion of buffers underneath us.
4240 * If we add a "b_python*_ref" field to the buf_T structure,
4241 * then we can get at it in buf_freeall() in vim. We then
4242 * need to create only ONE Python object per buffer - if
4243 * we try to create a second, just INCREF the existing one
4244 * and return it. The (single) Python object referring to
4245 * the buffer is stored in "b_python*_ref".
4246 * Question: what to do on a buf_freeall(). We'll probably
4247 * have to either delete the Python object (DECREF it to
4248 * zero - a bad idea, as it leaves dangling refs!) or
4249 * set the buf_T * value to an invalid value (-1?), which
4250 * means we need checks in all access functions... Bah.
4251 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004252 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004253 * b_python_ref and b_python3_ref fields respectively.
4254 */
4255
4256 BufferObject *self;
4257
4258 if (BUF_PYTHON_REF(buf) != NULL)
4259 {
4260 self = BUF_PYTHON_REF(buf);
4261 Py_INCREF(self);
4262 }
4263 else
4264 {
4265 self = PyObject_NEW(BufferObject, &BufferType);
4266 if (self == NULL)
4267 return NULL;
4268 self->buf = buf;
4269 BUF_PYTHON_REF(buf) = self;
4270 }
4271
4272 return (PyObject *)(self);
4273}
4274
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004275 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004276BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004277{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004278 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4279 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004280
4281 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004282}
4283
Bram Moolenaar971db462013-05-12 18:44:48 +02004284 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004285BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004286{
4287 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004288 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004289 return -1; /* ??? */
4290
Bram Moolenaard6e39182013-05-21 18:30:34 +02004291 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004292}
4293
4294 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004295BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004296{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004297 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004298}
4299
4300 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004301BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004302{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004303 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004304}
4305
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004306static char *BufferAttrs[] = {
4307 "name", "number", "vars", "options", "valid",
4308 NULL
4309};
4310
4311 static PyObject *
4312BufferDir(PyObject *self)
4313{
4314 return ObjectDir(self, BufferAttrs);
4315}
4316
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004317 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004318BufferAttrValid(BufferObject *self, char *name)
4319{
4320 PyObject *r;
4321
4322 if (strcmp(name, "valid") != 0)
4323 return NULL;
4324
4325 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4326 Py_INCREF(r);
4327 return r;
4328}
4329
4330 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004331BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004332{
4333 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004334 return PyString_FromString((self->buf->b_ffname == NULL
4335 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004336 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004337 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004338 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004339 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004340 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004341 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4342 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004343 else if (strcmp(name, "__members__") == 0)
4344 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004345 else
4346 return NULL;
4347}
4348
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004349 static int
4350BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4351{
4352 if (CheckBuffer(self))
4353 return -1;
4354
4355 if (strcmp(name, "name") == 0)
4356 {
4357 char_u *val;
4358 aco_save_T aco;
4359 int r;
4360 PyObject *todecref;
4361
4362 if (!(val = StringToChars(valObject, &todecref)))
4363 return -1;
4364
4365 VimTryStart();
4366 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4367 aucmd_prepbuf(&aco, self->buf);
4368 r = rename_buffer(val);
4369 aucmd_restbuf(&aco);
4370 Py_XDECREF(todecref);
4371 if (VimTryEnd())
4372 return -1;
4373
4374 if (r == FAIL)
4375 {
4376 PyErr_SetVim(_("failed to rename buffer"));
4377 return -1;
4378 }
4379 return 0;
4380 }
4381 else
4382 {
4383 PyErr_SetString(PyExc_AttributeError, name);
4384 return -1;
4385 }
4386}
4387
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004388 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004389BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004391 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004392}
4393
4394 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004395BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004396{
4397 pos_T *posp;
4398 char *pmark;
4399 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004400 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004401
Bram Moolenaard6e39182013-05-21 18:30:34 +02004402 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004403 return NULL;
4404
4405 if (!PyArg_ParseTuple(args, "s", &pmark))
4406 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004407
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004408 if (STRLEN(pmark) != 1)
4409 {
4410 PyErr_SetString(PyExc_ValueError,
4411 _("mark name must be a single character"));
4412 return NULL;
4413 }
4414
4415 mark = *pmark;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004416 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004417 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004418 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004419 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004420 if (VimTryEnd())
4421 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422
4423 if (posp == NULL)
4424 {
4425 PyErr_SetVim(_("invalid mark name"));
4426 return NULL;
4427 }
4428
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004429 if (posp->lnum <= 0)
4430 {
4431 /* Or raise an error? */
4432 Py_INCREF(Py_None);
4433 return Py_None;
4434 }
4435
4436 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4437}
4438
4439 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004440BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004441{
4442 PyInt start;
4443 PyInt end;
4444
Bram Moolenaard6e39182013-05-21 18:30:34 +02004445 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446 return NULL;
4447
4448 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4449 return NULL;
4450
Bram Moolenaard6e39182013-05-21 18:30:34 +02004451 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004452}
4453
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004454 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004455BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004456{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004457 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004458 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004459 else
4460 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004461 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004462
4463 if (name == NULL)
4464 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004465
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004466 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004467 }
4468}
4469
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004470static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004471 /* name, function, calling, documentation */
4472 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4473 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4474 {"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 +02004475 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4476 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004477};
4478
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004479/*
4480 * Buffer list object - Implementation
4481 */
4482
4483static PyTypeObject BufMapType;
4484
4485typedef struct
4486{
4487 PyObject_HEAD
4488} BufMapObject;
4489
4490 static PyInt
4491BufMapLength(PyObject *self UNUSED)
4492{
4493 buf_T *b = firstbuf;
4494 PyInt n = 0;
4495
4496 while (b)
4497 {
4498 ++n;
4499 b = b->b_next;
4500 }
4501
4502 return n;
4503}
4504
4505 static PyObject *
4506BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4507{
4508 buf_T *b;
4509 int bnr;
4510
4511#if PY_MAJOR_VERSION < 3
4512 if (PyInt_Check(keyObject))
4513 bnr = PyInt_AsLong(keyObject);
4514 else
4515#endif
4516 if (PyLong_Check(keyObject))
4517 bnr = PyLong_AsLong(keyObject);
4518 else
4519 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004520 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004521 return NULL;
4522 }
4523
4524 b = buflist_findnr(bnr);
4525
4526 if (b)
4527 return BufferNew(b);
4528 else
4529 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004530 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004531 return NULL;
4532 }
4533}
4534
4535 static void
4536BufMapIterDestruct(PyObject *buffer)
4537{
4538 /* Iteration was stopped before all buffers were processed */
4539 if (buffer)
4540 {
4541 Py_DECREF(buffer);
4542 }
4543}
4544
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004545 static int
4546BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4547{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004548 if (buffer)
4549 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004550 return 0;
4551}
4552
4553 static int
4554BufMapIterClear(PyObject **buffer)
4555{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004556 if (*buffer)
4557 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004558 return 0;
4559}
4560
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004561 static PyObject *
4562BufMapIterNext(PyObject **buffer)
4563{
4564 PyObject *next;
4565 PyObject *r;
4566
4567 if (!*buffer)
4568 return NULL;
4569
4570 r = *buffer;
4571
4572 if (CheckBuffer((BufferObject *)(r)))
4573 {
4574 *buffer = NULL;
4575 return NULL;
4576 }
4577
4578 if (!((BufferObject *)(r))->buf->b_next)
4579 next = NULL;
4580 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4581 return NULL;
4582 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004583 /* Do not increment reference: we no longer hold it (decref), but whoever
4584 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004585 return r;
4586}
4587
4588 static PyObject *
4589BufMapIter(PyObject *self UNUSED)
4590{
4591 PyObject *buffer;
4592
4593 buffer = BufferNew(firstbuf);
4594 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004595 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4596 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004597}
4598
4599static PyMappingMethods BufMapAsMapping = {
4600 (lenfunc) BufMapLength,
4601 (binaryfunc) BufMapItem,
4602 (objobjargproc) 0,
4603};
4604
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004605/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004606 */
4607
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004608static char *CurrentAttrs[] = {
4609 "buffer", "window", "line", "range", "tabpage",
4610 NULL
4611};
4612
4613 static PyObject *
4614CurrentDir(PyObject *self)
4615{
4616 return ObjectDir(self, CurrentAttrs);
4617}
4618
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004619 static PyObject *
4620CurrentGetattr(PyObject *self UNUSED, char *name)
4621{
4622 if (strcmp(name, "buffer") == 0)
4623 return (PyObject *)BufferNew(curbuf);
4624 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004625 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004626 else if (strcmp(name, "tabpage") == 0)
4627 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004628 else if (strcmp(name, "line") == 0)
4629 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4630 else if (strcmp(name, "range") == 0)
4631 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004632 else if (strcmp(name, "__members__") == 0)
4633 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004634 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004635#if PY_MAJOR_VERSION < 3
4636 return Py_FindMethod(WindowMethods, self, name);
4637#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004638 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004639#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004640}
4641
4642 static int
4643CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4644{
4645 if (strcmp(name, "line") == 0)
4646 {
4647 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4648 return -1;
4649
4650 return 0;
4651 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004652 else if (strcmp(name, "buffer") == 0)
4653 {
4654 int count;
4655
4656 if (value->ob_type != &BufferType)
4657 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004658 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004659 return -1;
4660 }
4661
4662 if (CheckBuffer((BufferObject *)(value)))
4663 return -1;
4664 count = ((BufferObject *)(value))->buf->b_fnum;
4665
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004666 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004667 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4668 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004669 if (VimTryEnd())
4670 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004671 PyErr_SetVim(_("failed to switch to given buffer"));
4672 return -1;
4673 }
4674
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004675 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004676 }
4677 else if (strcmp(name, "window") == 0)
4678 {
4679 int count;
4680
4681 if (value->ob_type != &WindowType)
4682 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004683 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004684 return -1;
4685 }
4686
4687 if (CheckWindow((WindowObject *)(value)))
4688 return -1;
4689 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4690
4691 if (!count)
4692 {
4693 PyErr_SetString(PyExc_ValueError,
4694 _("failed to find window in the current tab page"));
4695 return -1;
4696 }
4697
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004698 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004699 win_goto(((WindowObject *)(value))->win);
4700 if (((WindowObject *)(value))->win != curwin)
4701 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004702 if (VimTryEnd())
4703 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004704 PyErr_SetString(PyExc_RuntimeError,
4705 _("did not switch to the specified window"));
4706 return -1;
4707 }
4708
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004709 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004710 }
4711 else if (strcmp(name, "tabpage") == 0)
4712 {
4713 if (value->ob_type != &TabPageType)
4714 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004715 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004716 return -1;
4717 }
4718
4719 if (CheckTabPage((TabPageObject *)(value)))
4720 return -1;
4721
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004722 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004723 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4724 if (((TabPageObject *)(value))->tab != curtab)
4725 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004726 if (VimTryEnd())
4727 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004728 PyErr_SetString(PyExc_RuntimeError,
4729 _("did not switch to the specified tab page"));
4730 return -1;
4731 }
4732
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004733 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004734 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004735 else
4736 {
4737 PyErr_SetString(PyExc_AttributeError, name);
4738 return -1;
4739 }
4740}
4741
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004742static struct PyMethodDef CurrentMethods[] = {
4743 /* name, function, calling, documentation */
4744 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4745 { NULL, NULL, 0, NULL}
4746};
4747
Bram Moolenaardb913952012-06-29 12:54:53 +02004748 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004749init_range_cmd(exarg_T *eap)
4750{
4751 RangeStart = eap->line1;
4752 RangeEnd = eap->line2;
4753}
4754
4755 static void
4756init_range_eval(typval_T *rettv UNUSED)
4757{
4758 RangeStart = (PyInt) curwin->w_cursor.lnum;
4759 RangeEnd = RangeStart;
4760}
4761
4762 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004763run_cmd(const char *cmd, void *arg UNUSED
4764#ifdef PY_CAN_RECURSE
4765 , PyGILState_STATE *pygilstate UNUSED
4766#endif
4767 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004768{
4769 PyRun_SimpleString((char *) cmd);
4770}
4771
4772static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4773static int code_hdr_len = 30;
4774
4775 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004776run_do(const char *cmd, void *arg UNUSED
4777#ifdef PY_CAN_RECURSE
4778 , PyGILState_STATE *pygilstate
4779#endif
4780 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004781{
4782 PyInt lnum;
4783 size_t len;
4784 char *code;
4785 int status;
4786 PyObject *pyfunc, *pymain;
4787
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004788 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004789 {
4790 EMSG(_("cannot save undo information"));
4791 return;
4792 }
4793
4794 len = code_hdr_len + STRLEN(cmd);
4795 code = PyMem_New(char, len + 1);
4796 memcpy(code, code_hdr, code_hdr_len);
4797 STRCPY(code + code_hdr_len, cmd);
4798 status = PyRun_SimpleString(code);
4799 PyMem_Free(code);
4800
4801 if (status)
4802 {
4803 EMSG(_("failed to run the code"));
4804 return;
4805 }
4806
4807 status = 0;
4808 pymain = PyImport_AddModule("__main__");
4809 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004810#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004811 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004812#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004813
4814 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4815 {
4816 PyObject *line, *linenr, *ret;
4817
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004818#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004819 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004820#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004821 if (!(line = GetBufferLine(curbuf, lnum)))
4822 goto err;
4823 if (!(linenr = PyInt_FromLong((long) lnum)))
4824 {
4825 Py_DECREF(line);
4826 goto err;
4827 }
4828 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4829 Py_DECREF(line);
4830 Py_DECREF(linenr);
4831 if (!ret)
4832 goto err;
4833
4834 if (ret != Py_None)
4835 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4836 goto err;
4837
4838 Py_XDECREF(ret);
4839 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004840#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004841 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004842#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004843 }
4844 goto out;
4845err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004846#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004847 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004848#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004849 PyErr_PrintEx(0);
4850 PythonIO_Flush();
4851 status = 1;
4852out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004853#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004854 if (!status)
4855 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004856#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004857 Py_DECREF(pyfunc);
4858 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4859 if (status)
4860 return;
4861 check_cursor();
4862 update_curbuf(NOT_VALID);
4863}
4864
4865 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004866run_eval(const char *cmd, typval_T *rettv
4867#ifdef PY_CAN_RECURSE
4868 , PyGILState_STATE *pygilstate UNUSED
4869#endif
4870 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004871{
4872 PyObject *r;
4873
4874 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4875 if (r == NULL)
4876 {
4877 if (PyErr_Occurred() && !msg_silent)
4878 PyErr_PrintEx(0);
4879 EMSG(_("E858: Eval did not return a valid python object"));
4880 }
4881 else
4882 {
4883 if (ConvertFromPyObject(r, rettv) == -1)
4884 EMSG(_("E859: Failed to convert returned python object to vim value"));
4885 Py_DECREF(r);
4886 }
4887 PyErr_Clear();
4888}
4889
4890 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004891set_ref_in_py(const int copyID)
4892{
4893 pylinkedlist_T *cur;
4894 dict_T *dd;
4895 list_T *ll;
4896
4897 if (lastdict != NULL)
4898 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4899 {
4900 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4901 if (dd->dv_copyID != copyID)
4902 {
4903 dd->dv_copyID = copyID;
4904 set_ref_in_ht(&dd->dv_hashtab, copyID);
4905 }
4906 }
4907
4908 if (lastlist != NULL)
4909 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4910 {
4911 ll = ((ListObject *) (cur->pll_obj))->list;
4912 if (ll->lv_copyID != copyID)
4913 {
4914 ll->lv_copyID = copyID;
4915 set_ref_in_list(ll, copyID);
4916 }
4917 }
4918}
4919
4920 static int
4921set_string_copy(char_u *str, typval_T *tv)
4922{
4923 tv->vval.v_string = vim_strsave(str);
4924 if (tv->vval.v_string == NULL)
4925 {
4926 PyErr_NoMemory();
4927 return -1;
4928 }
4929 return 0;
4930}
4931
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004932 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004933pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004934{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004935 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004936 char_u *key;
4937 dictitem_T *di;
4938 PyObject *keyObject;
4939 PyObject *valObject;
4940 Py_ssize_t iter = 0;
4941
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004942 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004943 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004944
4945 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004946 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004947
4948 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4949 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004950 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004951
Bram Moolenaara03e6312013-05-29 22:49:26 +02004952 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004953 {
4954 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004955 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004956 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004957
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004958 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004959 {
4960 dict_unref(dict);
4961 return -1;
4962 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004963
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004964 if (*key == NUL)
4965 {
4966 dict_unref(dict);
4967 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004968 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004969 return -1;
4970 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004971
4972 di = dictitem_alloc(key);
4973
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004974 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004975
4976 if (di == NULL)
4977 {
4978 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004979 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004980 return -1;
4981 }
4982 di->di_tv.v_lock = 0;
4983
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004984 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004985 {
4986 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004987 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004988 return -1;
4989 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004990
4991 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004992 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004993 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004994 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004995 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004996 PyErr_SetVim(_("failed to add key to dictionary"));
4997 return -1;
4998 }
4999 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005000
5001 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005002 return 0;
5003}
5004
5005 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005006pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005007{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005008 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005009 char_u *key;
5010 dictitem_T *di;
5011 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005012 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005013 PyObject *keyObject;
5014 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005015
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005016 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005017 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005018
5019 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005020 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005021
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005022 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005023 {
5024 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005025 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005026 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005027
5028 if (!(iterator = PyObject_GetIter(list)))
5029 {
5030 dict_unref(dict);
5031 Py_DECREF(list);
5032 return -1;
5033 }
5034 Py_DECREF(list);
5035
5036 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005037 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005038 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005039
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005040 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005041 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005042 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005043 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005044 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005045 return -1;
5046 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005047
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005048 if (*key == NUL)
5049 {
5050 Py_DECREF(keyObject);
5051 Py_DECREF(iterator);
5052 Py_XDECREF(todecref);
5053 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005054 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005055 return -1;
5056 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005057
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005058 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005059 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005060 Py_DECREF(keyObject);
5061 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005062 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005063 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005064 return -1;
5065 }
5066
5067 di = dictitem_alloc(key);
5068
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005069 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005070 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005072 if (di == NULL)
5073 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005074 Py_DECREF(iterator);
5075 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005076 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005077 PyErr_NoMemory();
5078 return -1;
5079 }
5080 di->di_tv.v_lock = 0;
5081
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005082 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005083 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005084 Py_DECREF(iterator);
5085 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005086 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005087 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005088 return -1;
5089 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005090
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005091 Py_DECREF(valObject);
5092
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005093 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005094 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005095 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005096 dictitem_free(di);
5097 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005098 PyErr_SetVim(_("failed to add key to dictionary"));
5099 return -1;
5100 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005102 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005103 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005104 return 0;
5105}
5106
5107 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005108pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005109{
5110 list_T *l;
5111
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005112 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005113 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005114
5115 tv->v_type = VAR_LIST;
5116 tv->vval.v_list = l;
5117
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005118 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005119 {
5120 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005121 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005122 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005123
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005124 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005125 return 0;
5126}
5127
Bram Moolenaardb913952012-06-29 12:54:53 +02005128typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5129
5130 static int
5131convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005132 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005133{
5134 PyObject *capsule;
5135 char hexBuf[sizeof(void *) * 2 + 3];
5136
5137 sprintf(hexBuf, "%p", obj);
5138
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005139# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005140 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005141# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005142 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005143# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005144 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005145 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005146# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005147 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005148# else
5149 capsule = PyCObject_FromVoidPtr(tv, NULL);
5150# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005151 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5152 {
5153 Py_DECREF(capsule);
5154 tv->v_type = VAR_UNKNOWN;
5155 return -1;
5156 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005157 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005158 {
5159 tv->v_type = VAR_UNKNOWN;
5160 return -1;
5161 }
5162 /* As we are not using copy_tv which increments reference count we must
5163 * do it ourself. */
5164 switch(tv->v_type)
5165 {
5166 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5167 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5168 }
5169 }
5170 else
5171 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005172 typval_T *v;
5173
5174# ifdef PY_USE_CAPSULE
5175 v = PyCapsule_GetPointer(capsule, NULL);
5176# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005177 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005178# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005179 copy_tv(v, tv);
5180 }
5181 return 0;
5182}
5183
5184 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005185ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5186{
5187 PyObject *lookup_dict;
5188 int r;
5189
5190 if (!(lookup_dict = PyDict_New()))
5191 return -1;
5192
5193 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5194 {
5195 tv->v_type = VAR_DICT;
5196 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5197 ++tv->vval.v_dict->dv_refcount;
5198 r = 0;
5199 }
5200 else if (PyDict_Check(obj))
5201 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
5202 else if (PyMapping_Check(obj))
5203 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5204 else
5205 {
5206 PyErr_SetString(PyExc_TypeError,
5207 _("unable to convert object to vim dictionary"));
5208 r = -1;
5209 }
5210 Py_DECREF(lookup_dict);
5211 return r;
5212}
5213
5214 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005215ConvertFromPyObject(PyObject *obj, typval_T *tv)
5216{
5217 PyObject *lookup_dict;
5218 int r;
5219
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005220 if (!(lookup_dict = PyDict_New()))
5221 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005222 r = _ConvertFromPyObject(obj, tv, lookup_dict);
5223 Py_DECREF(lookup_dict);
5224 return r;
5225}
5226
5227 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005228_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005229{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005230 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005231 {
5232 tv->v_type = VAR_DICT;
5233 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5234 ++tv->vval.v_dict->dv_refcount;
5235 }
5236 else if (obj->ob_type == &ListType)
5237 {
5238 tv->v_type = VAR_LIST;
5239 tv->vval.v_list = (((ListObject *)(obj))->list);
5240 ++tv->vval.v_list->lv_refcount;
5241 }
5242 else if (obj->ob_type == &FunctionType)
5243 {
5244 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5245 return -1;
5246
5247 tv->v_type = VAR_FUNC;
5248 func_ref(tv->vval.v_string);
5249 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005250 else if (PyBytes_Check(obj))
5251 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005252 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02005253
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005254 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
5255 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005256 if (result == NULL)
5257 return -1;
5258
5259 if (set_string_copy(result, tv) == -1)
5260 return -1;
5261
5262 tv->v_type = VAR_STRING;
5263 }
5264 else if (PyUnicode_Check(obj))
5265 {
5266 PyObject *bytes;
5267 char_u *result;
5268
Bram Moolenaardb913952012-06-29 12:54:53 +02005269 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
5270 if (bytes == NULL)
5271 return -1;
5272
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005273 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
5274 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005275 if (result == NULL)
5276 return -1;
5277
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005278 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005279 {
5280 Py_XDECREF(bytes);
5281 return -1;
5282 }
5283 Py_XDECREF(bytes);
5284
5285 tv->v_type = VAR_STRING;
5286 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005287#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005288 else if (PyInt_Check(obj))
5289 {
5290 tv->v_type = VAR_NUMBER;
5291 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
5292 }
5293#endif
5294 else if (PyLong_Check(obj))
5295 {
5296 tv->v_type = VAR_NUMBER;
5297 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
5298 }
5299 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005300 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005301#ifdef FEAT_FLOAT
5302 else if (PyFloat_Check(obj))
5303 {
5304 tv->v_type = VAR_FLOAT;
5305 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5306 }
5307#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005308 else if (PyObject_HasAttrString(obj, "keys"))
5309 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005310 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005311 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005312 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005313 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005314 else
5315 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02005316 PyErr_SetString(PyExc_TypeError,
5317 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005318 return -1;
5319 }
5320 return 0;
5321}
5322
5323 static PyObject *
5324ConvertToPyObject(typval_T *tv)
5325{
5326 if (tv == NULL)
5327 {
5328 PyErr_SetVim(_("NULL reference passed"));
5329 return NULL;
5330 }
5331 switch (tv->v_type)
5332 {
5333 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005334 return PyBytes_FromString(tv->vval.v_string == NULL
5335 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005336 case VAR_NUMBER:
5337 return PyLong_FromLong((long) tv->vval.v_number);
5338#ifdef FEAT_FLOAT
5339 case VAR_FLOAT:
5340 return PyFloat_FromDouble((double) tv->vval.v_float);
5341#endif
5342 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005343 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005344 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005345 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005346 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005347 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005348 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005349 case VAR_UNKNOWN:
5350 Py_INCREF(Py_None);
5351 return Py_None;
5352 default:
5353 PyErr_SetVim(_("internal error: invalid value type"));
5354 return NULL;
5355 }
5356}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005357
5358typedef struct
5359{
5360 PyObject_HEAD
5361} CurrentObject;
5362static PyTypeObject CurrentType;
5363
5364 static void
5365init_structs(void)
5366{
5367 vim_memset(&OutputType, 0, sizeof(OutputType));
5368 OutputType.tp_name = "vim.message";
5369 OutputType.tp_basicsize = sizeof(OutputObject);
5370 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5371 OutputType.tp_doc = "vim message object";
5372 OutputType.tp_methods = OutputMethods;
5373#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005374 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5375 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005376 OutputType.tp_alloc = call_PyType_GenericAlloc;
5377 OutputType.tp_new = call_PyType_GenericNew;
5378 OutputType.tp_free = call_PyObject_Free;
5379#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005380 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5381 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005382#endif
5383
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005384 vim_memset(&IterType, 0, sizeof(IterType));
5385 IterType.tp_name = "vim.iter";
5386 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005387 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005388 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005389 IterType.tp_iter = (getiterfunc)IterIter;
5390 IterType.tp_iternext = (iternextfunc)IterNext;
5391 IterType.tp_dealloc = (destructor)IterDestructor;
5392 IterType.tp_traverse = (traverseproc)IterTraverse;
5393 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005394
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005395 vim_memset(&BufferType, 0, sizeof(BufferType));
5396 BufferType.tp_name = "vim.buffer";
5397 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005398 BufferType.tp_dealloc = (destructor)BufferDestructor;
5399 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005400 BufferType.tp_as_sequence = &BufferAsSeq;
5401 BufferType.tp_as_mapping = &BufferAsMapping;
5402 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5403 BufferType.tp_doc = "vim buffer object";
5404 BufferType.tp_methods = BufferMethods;
5405#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005406 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005407 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005408 BufferType.tp_alloc = call_PyType_GenericAlloc;
5409 BufferType.tp_new = call_PyType_GenericNew;
5410 BufferType.tp_free = call_PyObject_Free;
5411#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005412 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005413 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005414#endif
5415
5416 vim_memset(&WindowType, 0, sizeof(WindowType));
5417 WindowType.tp_name = "vim.window";
5418 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005419 WindowType.tp_dealloc = (destructor)WindowDestructor;
5420 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005421 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005422 WindowType.tp_doc = "vim Window object";
5423 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005424 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5425 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005426#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005427 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5428 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005429 WindowType.tp_alloc = call_PyType_GenericAlloc;
5430 WindowType.tp_new = call_PyType_GenericNew;
5431 WindowType.tp_free = call_PyObject_Free;
5432#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005433 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5434 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005435#endif
5436
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005437 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5438 TabPageType.tp_name = "vim.tabpage";
5439 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005440 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5441 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005442 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5443 TabPageType.tp_doc = "vim tab page object";
5444 TabPageType.tp_methods = TabPageMethods;
5445#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005446 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005447 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5448 TabPageType.tp_new = call_PyType_GenericNew;
5449 TabPageType.tp_free = call_PyObject_Free;
5450#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005451 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005452#endif
5453
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005454 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5455 BufMapType.tp_name = "vim.bufferlist";
5456 BufMapType.tp_basicsize = sizeof(BufMapObject);
5457 BufMapType.tp_as_mapping = &BufMapAsMapping;
5458 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005459 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005460 BufferType.tp_doc = "vim buffer list";
5461
5462 vim_memset(&WinListType, 0, sizeof(WinListType));
5463 WinListType.tp_name = "vim.windowlist";
5464 WinListType.tp_basicsize = sizeof(WinListType);
5465 WinListType.tp_as_sequence = &WinListAsSeq;
5466 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5467 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005468 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005469
5470 vim_memset(&TabListType, 0, sizeof(TabListType));
5471 TabListType.tp_name = "vim.tabpagelist";
5472 TabListType.tp_basicsize = sizeof(TabListType);
5473 TabListType.tp_as_sequence = &TabListAsSeq;
5474 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5475 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005476
5477 vim_memset(&RangeType, 0, sizeof(RangeType));
5478 RangeType.tp_name = "vim.range";
5479 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005480 RangeType.tp_dealloc = (destructor)RangeDestructor;
5481 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005482 RangeType.tp_as_sequence = &RangeAsSeq;
5483 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005484 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005485 RangeType.tp_doc = "vim Range object";
5486 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005487 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5488 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005489#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005490 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005491 RangeType.tp_alloc = call_PyType_GenericAlloc;
5492 RangeType.tp_new = call_PyType_GenericNew;
5493 RangeType.tp_free = call_PyObject_Free;
5494#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005495 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005496#endif
5497
5498 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5499 CurrentType.tp_name = "vim.currentdata";
5500 CurrentType.tp_basicsize = sizeof(CurrentObject);
5501 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5502 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005503 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005504#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005505 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5506 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005507#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005508 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5509 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005510#endif
5511
5512 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5513 DictionaryType.tp_name = "vim.dictionary";
5514 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005515 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005516 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005517 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005518 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005519 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5520 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005521 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5522 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5523 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005524#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005525 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5526 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005527#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005528 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5529 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005530#endif
5531
5532 vim_memset(&ListType, 0, sizeof(ListType));
5533 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005534 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005535 ListType.tp_basicsize = sizeof(ListObject);
5536 ListType.tp_as_sequence = &ListAsSeq;
5537 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005538 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005539 ListType.tp_doc = "list pushing modifications to vim structure";
5540 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005541 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005542 ListType.tp_new = (newfunc)ListConstructor;
5543 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005544#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005545 ListType.tp_getattro = (getattrofunc)ListGetattro;
5546 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005547#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005548 ListType.tp_getattr = (getattrfunc)ListGetattr;
5549 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005550#endif
5551
5552 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005553 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005554 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005555 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5556 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005557 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005558 FunctionType.tp_doc = "object that calls vim function";
5559 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005560 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005561 FunctionType.tp_new = (newfunc)FunctionConstructor;
5562 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005563#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005564 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005565#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005566 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005567#endif
5568
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005569 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5570 OptionsType.tp_name = "vim.options";
5571 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005572 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005573 OptionsType.tp_doc = "object for manipulating options";
5574 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005575 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5576 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5577 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005578
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005579 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5580 LoaderType.tp_name = "vim.Loader";
5581 LoaderType.tp_basicsize = sizeof(LoaderObject);
5582 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5583 LoaderType.tp_doc = "vim message object";
5584 LoaderType.tp_methods = LoaderMethods;
5585 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5586
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005587#if PY_MAJOR_VERSION >= 3
5588 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5589 vimmodule.m_name = "vim";
5590 vimmodule.m_doc = "Vim Python interface\n";
5591 vimmodule.m_size = -1;
5592 vimmodule.m_methods = VimMethods;
5593#endif
5594}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005595
5596#define PYTYPE_READY(type) \
5597 if (PyType_Ready(&type)) \
5598 return -1;
5599
5600 static int
5601init_types()
5602{
5603 PYTYPE_READY(IterType);
5604 PYTYPE_READY(BufferType);
5605 PYTYPE_READY(RangeType);
5606 PYTYPE_READY(WindowType);
5607 PYTYPE_READY(TabPageType);
5608 PYTYPE_READY(BufMapType);
5609 PYTYPE_READY(WinListType);
5610 PYTYPE_READY(TabListType);
5611 PYTYPE_READY(CurrentType);
5612 PYTYPE_READY(DictionaryType);
5613 PYTYPE_READY(ListType);
5614 PYTYPE_READY(FunctionType);
5615 PYTYPE_READY(OptionsType);
5616 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005617 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005618 return 0;
5619}
5620
5621 static int
5622init_sys_path()
5623{
5624 PyObject *path;
5625 PyObject *path_hook;
5626 PyObject *path_hooks;
5627
5628 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5629 return -1;
5630
5631 if (!(path_hooks = PySys_GetObject("path_hooks")))
5632 {
5633 PyErr_Clear();
5634 path_hooks = PyList_New(1);
5635 PyList_SET_ITEM(path_hooks, 0, path_hook);
5636 if (PySys_SetObject("path_hooks", path_hooks))
5637 {
5638 Py_DECREF(path_hooks);
5639 return -1;
5640 }
5641 Py_DECREF(path_hooks);
5642 }
5643 else if (PyList_Check(path_hooks))
5644 {
5645 if (PyList_Append(path_hooks, path_hook))
5646 {
5647 Py_DECREF(path_hook);
5648 return -1;
5649 }
5650 Py_DECREF(path_hook);
5651 }
5652 else
5653 {
5654 VimTryStart();
5655 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5656 "You should now do the following:\n"
5657 "- append vim.path_hook to sys.path_hooks\n"
5658 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5659 VimTryEnd(); /* Discard the error */
5660 Py_DECREF(path_hook);
5661 return 0;
5662 }
5663
5664 if (!(path = PySys_GetObject("path")))
5665 {
5666 PyErr_Clear();
5667 path = PyList_New(1);
5668 Py_INCREF(vim_special_path_object);
5669 PyList_SET_ITEM(path, 0, vim_special_path_object);
5670 if (PySys_SetObject("path", path))
5671 {
5672 Py_DECREF(path);
5673 return -1;
5674 }
5675 Py_DECREF(path);
5676 }
5677 else if (PyList_Check(path))
5678 {
5679 if (PyList_Append(path, vim_special_path_object))
5680 return -1;
5681 }
5682 else
5683 {
5684 VimTryStart();
5685 EMSG(_("Failed to set path: sys.path is not a list\n"
5686 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5687 VimTryEnd(); /* Discard the error */
5688 }
5689
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005690 return 0;
5691}
5692
5693static BufMapObject TheBufferMap =
5694{
5695 PyObject_HEAD_INIT(&BufMapType)
5696};
5697
5698static WinListObject TheWindowList =
5699{
5700 PyObject_HEAD_INIT(&WinListType)
5701 NULL
5702};
5703
5704static CurrentObject TheCurrent =
5705{
5706 PyObject_HEAD_INIT(&CurrentType)
5707};
5708
5709static TabListObject TheTabPageList =
5710{
5711 PyObject_HEAD_INIT(&TabListType)
5712};
5713
5714static struct numeric_constant {
5715 char *name;
5716 int value;
5717} numeric_constants[] = {
5718 {"VAR_LOCKED", VAR_LOCKED},
5719 {"VAR_FIXED", VAR_FIXED},
5720 {"VAR_SCOPE", VAR_SCOPE},
5721 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5722};
5723
5724static struct object_constant {
5725 char *name;
5726 PyObject *value;
5727} object_constants[] = {
5728 {"buffers", (PyObject *)(void *)&TheBufferMap},
5729 {"windows", (PyObject *)(void *)&TheWindowList},
5730 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5731 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005732
5733 {"Buffer", (PyObject *)&BufferType},
5734 {"Range", (PyObject *)&RangeType},
5735 {"Window", (PyObject *)&WindowType},
5736 {"TabPage", (PyObject *)&TabPageType},
5737 {"Dictionary", (PyObject *)&DictionaryType},
5738 {"List", (PyObject *)&ListType},
5739 {"Function", (PyObject *)&FunctionType},
5740 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005741 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005742};
5743
5744typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005745typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005746
5747#define ADD_OBJECT(m, name, obj) \
5748 if (add_object(m, name, obj)) \
5749 return -1;
5750
5751#define ADD_CHECKED_OBJECT(m, name, obj) \
5752 { \
5753 PyObject *value = obj; \
5754 if (!value) \
5755 return -1; \
5756 ADD_OBJECT(m, name, value); \
5757 }
5758
5759 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005760populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005761{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005762 int i;
5763 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005764 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005765 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005766
5767 for (i = 0; i < (int)(sizeof(numeric_constants)
5768 / sizeof(struct numeric_constant));
5769 ++i)
5770 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5771 PyInt_FromLong(numeric_constants[i].value));
5772
5773 for (i = 0; i < (int)(sizeof(object_constants)
5774 / sizeof(struct object_constant));
5775 ++i)
5776 {
5777 PyObject *value;
5778
5779 value = object_constants[i].value;
5780 Py_INCREF(value);
5781 ADD_OBJECT(m, object_constants[i].name, value);
5782 }
5783
5784 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5785 return -1;
5786 ADD_OBJECT(m, "error", VimError);
5787
Bram Moolenaara9922d62013-05-30 13:01:18 +02005788 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5789 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005790 ADD_CHECKED_OBJECT(m, "options",
5791 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005792
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005793 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005794 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005795 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005796
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005797 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005798 return -1;
5799 ADD_OBJECT(m, "_getcwd", py_getcwd)
5800
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005801 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005802 return -1;
5803 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005804 if (!(attr = get_attr(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005805 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005806 if (PyObject_SetAttrString(other_module, "chdir", attr))
5807 {
5808 Py_DECREF(attr);
5809 return -1;
5810 }
5811 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005812
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005813 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005814 {
5815 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005816 if (!(attr = get_attr(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005817 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005818 if (PyObject_SetAttrString(other_module, "fchdir", attr))
5819 {
5820 Py_DECREF(attr);
5821 return -1;
5822 }
5823 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005824 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02005825 else
5826 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02005827
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005828 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
5829 return -1;
5830
5831 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
5832
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005833 if (!(imp = PyImport_ImportModule("imp")))
5834 return -1;
5835
5836 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
5837 {
5838 Py_DECREF(imp);
5839 return -1;
5840 }
5841
5842 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
5843 {
5844 Py_DECREF(py_find_module);
5845 Py_DECREF(imp);
5846 return -1;
5847 }
5848
5849 Py_DECREF(imp);
5850
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005851 ADD_OBJECT(m, "_find_module", py_find_module);
5852 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005853
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005854 return 0;
5855}