blob: 64b7e542f259c14455a4daea391c898040494967 [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 Moolenaar6c85e7f2013-06-23 12:51:32 +0200284 static int
285write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200286{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200287 Py_ssize_t len = 0;
288 char *str = NULL;
289 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200290
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200291 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
292 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200293
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
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200301 return 0;
302}
303
304 static PyObject *
305OutputWrite(OutputObject *self, PyObject *string)
306{
307 if (write_output(self, string))
308 return NULL;
309
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200310 Py_INCREF(Py_None);
311 return Py_None;
312}
313
314 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200315OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200316{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200317 PyObject *iterator;
318 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200319
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200320 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200321 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200322
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200323 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200324 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200325 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200326 {
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
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200334 Py_DECREF(iterator);
335
336 /* Iterator may have finished due to an exception */
337 if (PyErr_Occurred())
338 return NULL;
339
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200340 Py_INCREF(Py_None);
341 return Py_None;
342}
343
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100344 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200345OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100346{
347 /* do nothing */
348 Py_INCREF(Py_None);
349 return Py_None;
350}
351
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200352/***************/
353
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200354static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200355 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200356 {"write", (PyCFunction)OutputWrite, METH_O, ""},
357 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200358 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200359 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200360 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200361};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200362
363static OutputObject Output =
364{
365 PyObject_HEAD_INIT(&OutputType)
366 0,
367 0
368};
369
370static OutputObject Error =
371{
372 PyObject_HEAD_INIT(&OutputType)
373 0,
374 1
375};
376
377 static int
378PythonIO_Init_io(void)
379{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200380 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
381 return -1;
382 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
383 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200384
385 if (PyErr_Occurred())
386 {
387 EMSG(_("E264: Python: Error initialising I/O objects"));
388 return -1;
389 }
390
391 return 0;
392}
393
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200394typedef struct
395{
396 PyObject_HEAD
397 PyObject *module;
398} LoaderObject;
399static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200400
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200401 static void
402LoaderDestructor(LoaderObject *self)
403{
404 Py_DECREF(self->module);
405 DESTRUCTOR_FINISH(self);
406}
407
408 static PyObject *
409LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
410{
411 PyObject *r = self->module;
412
413 Py_INCREF(r);
414 return r;
415}
416
417static struct PyMethodDef LoaderMethods[] = {
418 /* name, function, calling, doc */
419 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
420 { NULL, NULL, 0, NULL}
421};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200422
423/* Check to see whether a Vim error has been reported, or a keyboard
424 * interrupt has been detected.
425 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200426
427 static void
428VimTryStart(void)
429{
430 ++trylevel;
431}
432
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200433 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200434VimTryEnd(void)
435{
436 --trylevel;
437 if (got_int)
438 {
439 PyErr_SetNone(PyExc_KeyboardInterrupt);
440 return 1;
441 }
442 else if (!did_throw)
443 return 0;
444 else if (PyErr_Occurred())
445 return 1;
446 else
447 {
448 PyErr_SetVim((char *) current_exception->value);
449 discard_current_exception();
450 return 1;
451 }
452}
453
454 static int
455VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200456{
457 if (got_int)
458 {
459 PyErr_SetNone(PyExc_KeyboardInterrupt);
460 return 1;
461 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200462 return 0;
463}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200464
465/* Vim module - Implementation
466 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200467
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200468 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200469VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200470{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200471 char_u *cmd;
472 PyObject *result;
473 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200474
Bram Moolenaar389a1792013-06-23 13:00:44 +0200475 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200476 return NULL;
477
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200478 Py_BEGIN_ALLOW_THREADS
479 Python_Lock_Vim();
480
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200481 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200482 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200483 update_screen(VALID);
484
485 Python_Release_Vim();
486 Py_END_ALLOW_THREADS
487
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200488 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200489 result = NULL;
490 else
491 result = Py_None;
492
493 Py_XINCREF(result);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200494 Py_XDECREF(todecref);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200495 return result;
496}
497
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200498/*
499 * Function to translate a typval_T into a PyObject; this will recursively
500 * translate lists/dictionaries into their Python equivalents.
501 *
502 * The depth parameter is to avoid infinite recursion, set it to 1 when
503 * you call VimToPython.
504 */
505 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200506VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200507{
508 PyObject *result;
509 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200510 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200511
512 /* Avoid infinite recursion */
513 if (depth > 100)
514 {
515 Py_INCREF(Py_None);
516 result = Py_None;
517 return result;
518 }
519
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200520 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200521 * then and we can use it again. */
522 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
523 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
524 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200525 sprintf(ptrBuf, "%p",
526 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
527 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200528
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200529 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200530 {
531 Py_INCREF(result);
532 return result;
533 }
534 }
535
536 if (our_tv->v_type == VAR_STRING)
537 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200538 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200539 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200540 }
541 else if (our_tv->v_type == VAR_NUMBER)
542 {
543 char buf[NUMBUFLEN];
544
545 /* For backwards compatibility numbers are stored as strings. */
546 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200547 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200548 }
549# ifdef FEAT_FLOAT
550 else if (our_tv->v_type == VAR_FLOAT)
551 {
552 char buf[NUMBUFLEN];
553
554 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200555 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200556 }
557# endif
558 else if (our_tv->v_type == VAR_LIST)
559 {
560 list_T *list = our_tv->vval.v_list;
561 listitem_T *curr;
562
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200563 if (list == NULL)
564 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200565
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200566 if (!(result = PyList_New(0)))
567 return NULL;
568
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200569 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200570 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200571 Py_DECREF(result);
572 return NULL;
573 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200574
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200575 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
576 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200577 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200578 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200579 Py_DECREF(result);
580 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200581 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200582 if (PyList_Append(result, newObj))
583 {
584 Py_DECREF(newObj);
585 Py_DECREF(result);
586 return NULL;
587 }
588 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200589 }
590 }
591 else if (our_tv->v_type == VAR_DICT)
592 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200593
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200594 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
595 long_u todo = ht->ht_used;
596 hashitem_T *hi;
597 dictitem_T *di;
598 if (our_tv->vval.v_dict == NULL)
599 return NULL;
600
601 if (!(result = PyDict_New()))
602 return NULL;
603
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200604 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200605 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200606 Py_DECREF(result);
607 return NULL;
608 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200609
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200610 for (hi = ht->ht_array; todo > 0; ++hi)
611 {
612 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200614 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200615
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200616 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200617 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200618 {
619 Py_DECREF(result);
620 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200621 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200622 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
623 {
624 Py_DECREF(result);
625 Py_DECREF(newObj);
626 return NULL;
627 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628 }
629 }
630 }
631 else
632 {
633 Py_INCREF(Py_None);
634 result = Py_None;
635 }
636
637 return result;
638}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200639
640 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200641VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200642{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200643 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200644 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200645 PyObject *string;
646 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200647 PyObject *result;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200648 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200649
Bram Moolenaar389a1792013-06-23 13:00:44 +0200650 if (!PyArg_ParseTuple(args, "O", &string))
651 return NULL;
652
653 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200654 return NULL;
655
656 Py_BEGIN_ALLOW_THREADS
657 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200658 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200659 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200660 Python_Release_Vim();
661 Py_END_ALLOW_THREADS
662
Bram Moolenaar389a1792013-06-23 13:00:44 +0200663 Py_XDECREF(todecref);
664
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200665 if (VimTryEnd())
666 return NULL;
667
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200668 if (our_tv == NULL)
669 {
670 PyErr_SetVim(_("invalid expression"));
671 return NULL;
672 }
673
674 /* Convert the Vim type into a Python type. Create a dictionary that's
675 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200676 if (!(lookup_dict = PyDict_New()))
677 result = NULL;
678 else
679 {
680 result = VimToPython(our_tv, 1, lookup_dict);
681 Py_DECREF(lookup_dict);
682 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200683
684
685 Py_BEGIN_ALLOW_THREADS
686 Python_Lock_Vim();
687 free_tv(our_tv);
688 Python_Release_Vim();
689 Py_END_ALLOW_THREADS
690
691 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200692}
693
Bram Moolenaardb913952012-06-29 12:54:53 +0200694static PyObject *ConvertToPyObject(typval_T *);
695
696 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200697VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200698{
Bram Moolenaardb913952012-06-29 12:54:53 +0200699 typval_T *our_tv;
700 PyObject *result;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200701 char_u *expr;
702 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200703
Bram Moolenaar389a1792013-06-23 13:00:44 +0200704 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200705 return NULL;
706
707 Py_BEGIN_ALLOW_THREADS
708 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200709 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200710 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200711 Python_Release_Vim();
712 Py_END_ALLOW_THREADS
713
Bram Moolenaar389a1792013-06-23 13:00:44 +0200714 Py_XDECREF(todecref);
715
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200716 if (VimTryEnd())
717 return NULL;
718
Bram Moolenaardb913952012-06-29 12:54:53 +0200719 if (our_tv == NULL)
720 {
721 PyErr_SetVim(_("invalid expression"));
722 return NULL;
723 }
724
725 result = ConvertToPyObject(our_tv);
726 Py_BEGIN_ALLOW_THREADS
727 Python_Lock_Vim();
728 free_tv(our_tv);
729 Python_Release_Vim();
730 Py_END_ALLOW_THREADS
731
732 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200733}
734
735 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200736VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200737{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200738 char_u *str;
739 PyObject *todecref;
740 int result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200741
Bram Moolenaar389a1792013-06-23 13:00:44 +0200742 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200743 return NULL;
744
Bram Moolenaara54bf402012-12-05 16:30:07 +0100745#ifdef FEAT_MBYTE
Bram Moolenaar389a1792013-06-23 13:00:44 +0200746 result = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100747#else
Bram Moolenaar389a1792013-06-23 13:00:44 +0200748 result = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100749#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200750
751 Py_XDECREF(todecref);
752
753 return PyLong_FromLong(result);
Bram Moolenaardb913952012-06-29 12:54:53 +0200754}
755
Bram Moolenaarf4258302013-06-02 18:20:17 +0200756 static PyObject *
757_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
758{
759 PyObject *r;
760 PyObject *newwd;
761 PyObject *todecref;
762 char_u *new_dir;
763
Bram Moolenaard4209d22013-06-05 20:34:15 +0200764 if (_chdir == NULL)
765 return NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200766 if (!(r = PyObject_Call(_chdir, args, kwargs)))
767 return NULL;
768
769 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
770 {
771 Py_DECREF(r);
772 return NULL;
773 }
774
775 if (!(new_dir = StringToChars(newwd, &todecref)))
776 {
777 Py_DECREF(r);
778 Py_DECREF(newwd);
779 return NULL;
780 }
781
782 VimTryStart();
783
784 if (vim_chdir(new_dir))
785 {
786 Py_DECREF(r);
787 Py_DECREF(newwd);
788 Py_XDECREF(todecref);
789
790 if (VimTryEnd())
791 return NULL;
792
793 PyErr_SetVim(_("failed to change directory"));
794 return NULL;
795 }
796
797 Py_DECREF(newwd);
798 Py_XDECREF(todecref);
799
800 post_chdir(FALSE);
801
802 if (VimTryEnd())
803 {
804 Py_DECREF(r);
805 return NULL;
806 }
807
808 return r;
809}
810
811 static PyObject *
812VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
813{
814 return _VimChdir(py_chdir, args, kwargs);
815}
816
817 static PyObject *
818VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
819{
820 return _VimChdir(py_fchdir, args, kwargs);
821}
822
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200823typedef struct {
824 PyObject *callable;
825 PyObject *result;
826} map_rtp_data;
827
828 static void
829map_rtp_callback(char_u *path, void *_data)
830{
831 void **data = (void **) _data;
832 PyObject *pathObject;
833 map_rtp_data *mr_data = *((map_rtp_data **) data);
834
835 if (!(pathObject = PyString_FromString((char *) path)))
836 {
837 *data = NULL;
838 return;
839 }
840
841 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
842 pathObject, NULL);
843
844 Py_DECREF(pathObject);
845
846 if (!mr_data->result || mr_data->result != Py_None)
847 *data = NULL;
848 else
849 {
850 Py_DECREF(mr_data->result);
851 mr_data->result = NULL;
852 }
853}
854
855 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200856VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200857{
858 map_rtp_data data;
859
Bram Moolenaar389a1792013-06-23 13:00:44 +0200860 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200861 data.result = NULL;
862
863 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
864
865 if (data.result == NULL)
866 {
867 if (PyErr_Occurred())
868 return NULL;
869 else
870 {
871 Py_INCREF(Py_None);
872 return Py_None;
873 }
874 }
875 return data.result;
876}
877
878/*
879 * _vim_runtimepath_ special path implementation.
880 */
881
882 static void
883map_finder_callback(char_u *path, void *_data)
884{
885 void **data = (void **) _data;
886 PyObject *list = *((PyObject **) data);
887 PyObject *pathObject1, *pathObject2;
888 char *pathbuf;
889 size_t pathlen;
890
891 pathlen = STRLEN(path);
892
893#if PY_MAJOR_VERSION < 3
894# define PY_MAIN_DIR_STRING "python2"
895#else
896# define PY_MAIN_DIR_STRING "python3"
897#endif
898#define PY_ALTERNATE_DIR_STRING "pythonx"
899
900#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
901 if (!(pathbuf = PyMem_New(char,
902 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
903 {
904 PyErr_NoMemory();
905 *data = NULL;
906 return;
907 }
908
909 mch_memmove(pathbuf, path, pathlen + 1);
910 add_pathsep((char_u *) pathbuf);
911
912 pathlen = STRLEN(pathbuf);
913 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
914 PYTHONX_STRING_LENGTH + 1);
915
916 if (!(pathObject1 = PyString_FromString(pathbuf)))
917 {
918 *data = NULL;
919 PyMem_Free(pathbuf);
920 return;
921 }
922
923 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
924 PYTHONX_STRING_LENGTH + 1);
925
926 if (!(pathObject2 = PyString_FromString(pathbuf)))
927 {
928 Py_DECREF(pathObject1);
929 PyMem_Free(pathbuf);
930 *data = NULL;
931 return;
932 }
933
934 PyMem_Free(pathbuf);
935
936 if (PyList_Append(list, pathObject1)
937 || PyList_Append(list, pathObject2))
938 *data = NULL;
939
940 Py_DECREF(pathObject1);
941 Py_DECREF(pathObject2);
942}
943
944 static PyObject *
945Vim_GetPaths(PyObject *self UNUSED)
946{
947 PyObject *r;
948
949 if (!(r = PyList_New(0)))
950 return NULL;
951
952 do_in_runtimepath(NULL, FALSE, &map_finder_callback, r);
953
954 if (PyErr_Occurred())
955 {
956 Py_DECREF(r);
957 return NULL;
958 }
959
960 return r;
961}
962
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200963 static PyObject *
964call_load_module(char *name, int len, PyObject *find_module_result)
965{
966 PyObject *fd, *pathname, *description;
967
968 if (!PyTuple_Check(find_module_result)
969 || PyTuple_GET_SIZE(find_module_result) != 3)
970 {
971 PyErr_SetString(PyExc_TypeError,
972 _("expected 3-tuple as imp.find_module() result"));
973 return NULL;
974 }
975
976 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
977 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
978 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
979 {
980 PyErr_SetString(PyExc_RuntimeError,
981 _("internal error: imp.find_module returned tuple with NULL"));
982 return NULL;
983 }
984
985 return PyObject_CallFunction(py_load_module,
986 "s#OOO", name, len, fd, pathname, description);
987}
988
989 static PyObject *
990find_module(char *fullname, char *tail, PyObject *new_path)
991{
992 PyObject *find_module_result;
993 PyObject *module;
994 char *dot;
995
996 if ((dot = (char *) vim_strchr((char_u *) tail, '.')))
997 {
998 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +0200999 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001000 * first component
1001 */
1002 PyObject *newest_path;
1003 int partlen = (int) (dot - 1 - tail);
1004
1005 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1006 "s#O", tail, partlen, new_path)))
1007 return NULL;
1008
1009 if (!(module = call_load_module(
1010 fullname,
1011 ((int) (tail - fullname)) + partlen,
1012 find_module_result)))
1013 {
1014 Py_DECREF(find_module_result);
1015 return NULL;
1016 }
1017
1018 Py_DECREF(find_module_result);
1019
1020 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1021 {
1022 Py_DECREF(module);
1023 return NULL;
1024 }
1025
1026 Py_DECREF(module);
1027
1028 module = find_module(fullname, dot + 1, newest_path);
1029
1030 Py_DECREF(newest_path);
1031
1032 return module;
1033 }
1034 else
1035 {
1036 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1037 "sO", tail, new_path)))
1038 return NULL;
1039
1040 if (!(module = call_load_module(
1041 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001042 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001043 find_module_result)))
1044 {
1045 Py_DECREF(find_module_result);
1046 return NULL;
1047 }
1048
1049 Py_DECREF(find_module_result);
1050
1051 return module;
1052 }
1053}
1054
1055 static PyObject *
1056FinderFindModule(PyObject *self, PyObject *args)
1057{
1058 char *fullname;
1059 PyObject *module;
1060 PyObject *new_path;
1061 LoaderObject *loader;
1062
1063 if (!PyArg_ParseTuple(args, "s", &fullname))
1064 return NULL;
1065
1066 if (!(new_path = Vim_GetPaths(self)))
1067 return NULL;
1068
1069 module = find_module(fullname, fullname, new_path);
1070
1071 Py_DECREF(new_path);
1072
1073 if (!module)
1074 {
1075 Py_INCREF(Py_None);
1076 return Py_None;
1077 }
1078
1079 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1080 {
1081 Py_DECREF(module);
1082 return NULL;
1083 }
1084
1085 loader->module = module;
1086
1087 return (PyObject *) loader;
1088}
1089
1090 static PyObject *
1091VimPathHook(PyObject *self UNUSED, PyObject *args)
1092{
1093 char *path;
1094
1095 if (PyArg_ParseTuple(args, "s", &path)
1096 && STRCMP(path, vim_special_path) == 0)
1097 {
1098 Py_INCREF(vim_module);
1099 return vim_module;
1100 }
1101
1102 PyErr_Clear();
1103 PyErr_SetNone(PyExc_ImportError);
1104 return NULL;
1105}
1106
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001107/*
1108 * Vim module - Definitions
1109 */
1110
1111static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001112 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001113 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001114 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001115 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1116 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001117 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1118 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001119 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001120 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001121 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1122 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1123 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001124};
1125
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001126/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001127 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001128 */
1129
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001130static PyTypeObject IterType;
1131
1132typedef PyObject *(*nextfun)(void **);
1133typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001134typedef int (*traversefun)(void *, visitproc, void *);
1135typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001136
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001137/* Main purpose of this object is removing the need for do python
1138 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1139 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001140
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001141typedef struct
1142{
1143 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001144 void *cur;
1145 nextfun next;
1146 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001147 traversefun traverse;
1148 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001149} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001150
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001151 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001152IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1153 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001154{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001155 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001156
Bram Moolenaar774267b2013-05-21 20:51:59 +02001157 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001158 self->cur = start;
1159 self->next = next;
1160 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001161 self->traverse = traverse;
1162 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001163
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001164 return (PyObject *)(self);
1165}
1166
1167 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001168IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001169{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001170 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001171 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001172 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001173}
1174
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001175 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001176IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001177{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001178 if (self->traverse != NULL)
1179 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001180 else
1181 return 0;
1182}
1183
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001184/* Mac OSX defines clear() somewhere. */
1185#ifdef clear
1186# undef clear
1187#endif
1188
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001189 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001190IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001191{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001192 if (self->clear != NULL)
1193 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001194 else
1195 return 0;
1196}
1197
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001198 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001199IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001200{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001201 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001202}
1203
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001204 static PyObject *
1205IterIter(PyObject *self)
1206{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001207 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001208 return self;
1209}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001210
Bram Moolenaardb913952012-06-29 12:54:53 +02001211typedef struct pylinkedlist_S {
1212 struct pylinkedlist_S *pll_next;
1213 struct pylinkedlist_S *pll_prev;
1214 PyObject *pll_obj;
1215} pylinkedlist_T;
1216
1217static pylinkedlist_T *lastdict = NULL;
1218static pylinkedlist_T *lastlist = NULL;
1219
1220 static void
1221pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1222{
1223 if (ref->pll_prev == NULL)
1224 {
1225 if (ref->pll_next == NULL)
1226 {
1227 *last = NULL;
1228 return;
1229 }
1230 }
1231 else
1232 ref->pll_prev->pll_next = ref->pll_next;
1233
1234 if (ref->pll_next == NULL)
1235 *last = ref->pll_prev;
1236 else
1237 ref->pll_next->pll_prev = ref->pll_prev;
1238}
1239
1240 static void
1241pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1242{
1243 if (*last == NULL)
1244 ref->pll_prev = NULL;
1245 else
1246 {
1247 (*last)->pll_next = ref;
1248 ref->pll_prev = *last;
1249 }
1250 ref->pll_next = NULL;
1251 ref->pll_obj = self;
1252 *last = ref;
1253}
1254
1255static PyTypeObject DictionaryType;
1256
1257typedef struct
1258{
1259 PyObject_HEAD
1260 dict_T *dict;
1261 pylinkedlist_T ref;
1262} DictionaryObject;
1263
Bram Moolenaara9922d62013-05-30 13:01:18 +02001264static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1265
1266#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1267
Bram Moolenaardb913952012-06-29 12:54:53 +02001268 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001269DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001270{
1271 DictionaryObject *self;
1272
Bram Moolenaara9922d62013-05-30 13:01:18 +02001273 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001274 if (self == NULL)
1275 return NULL;
1276 self->dict = dict;
1277 ++dict->dv_refcount;
1278
1279 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1280
1281 return (PyObject *)(self);
1282}
1283
Bram Moolenaara9922d62013-05-30 13:01:18 +02001284 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001285py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001286{
1287 dict_T *r;
1288
1289 if (!(r = dict_alloc()))
1290 {
1291 PyErr_NoMemory();
1292 return NULL;
1293 }
1294 ++r->dv_refcount;
1295
1296 return r;
1297}
1298
1299 static PyObject *
1300DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1301{
1302 DictionaryObject *self;
1303 dict_T *dict;
1304
1305 if (!(dict = py_dict_alloc()))
1306 return NULL;
1307
1308 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1309
1310 --dict->dv_refcount;
1311
1312 if (kwargs || PyTuple_Size(args))
1313 {
1314 PyObject *tmp;
1315 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1316 {
1317 Py_DECREF(self);
1318 return NULL;
1319 }
1320
1321 Py_DECREF(tmp);
1322 }
1323
1324 return (PyObject *)(self);
1325}
1326
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001327 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001328DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001329{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001330 pyll_remove(&self->ref, &lastdict);
1331 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001332
1333 DESTRUCTOR_FINISH(self);
1334}
1335
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001336static char *DictionaryAttrs[] = {
1337 "locked", "scope",
1338 NULL
1339};
1340
1341 static PyObject *
1342DictionaryDir(PyObject *self)
1343{
1344 return ObjectDir(self, DictionaryAttrs);
1345}
1346
Bram Moolenaardb913952012-06-29 12:54:53 +02001347 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001348DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001349{
1350 if (val == NULL)
1351 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001352 PyErr_SetString(PyExc_AttributeError,
1353 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001354 return -1;
1355 }
1356
1357 if (strcmp(name, "locked") == 0)
1358 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001359 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001360 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001361 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001362 return -1;
1363 }
1364 else
1365 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001366 int istrue = PyObject_IsTrue(val);
1367 if (istrue == -1)
1368 return -1;
1369 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001370 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001371 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001372 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001373 }
1374 return 0;
1375 }
1376 else
1377 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001378 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001379 return -1;
1380 }
1381}
1382
1383 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001384DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001385{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001386 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001387}
1388
Bram Moolenaara9922d62013-05-30 13:01:18 +02001389#define DICT_FLAG_HAS_DEFAULT 0x01
1390#define DICT_FLAG_POP 0x02
1391#define DICT_FLAG_NONE_DEFAULT 0x04
1392#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1393#define DICT_FLAG_RETURN_PAIR 0x10
1394
Bram Moolenaardb913952012-06-29 12:54:53 +02001395 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001396_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001397{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001398 PyObject *keyObject;
1399 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1400 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001401 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001402 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001403 dict_T *dict = self->dict;
1404 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001405 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001406
Bram Moolenaara9922d62013-05-30 13:01:18 +02001407 if (flags & DICT_FLAG_HAS_DEFAULT)
1408 {
1409 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1410 return NULL;
1411 }
1412 else
1413 keyObject = args;
1414
1415 if (flags & DICT_FLAG_RETURN_BOOL)
1416 defObject = Py_False;
1417
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001418 if (!(key = StringToChars(keyObject, &todecref)))
1419 return NULL;
1420
1421 if (*key == NUL)
1422 {
1423 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001424 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001425 return NULL;
1426 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001427
Bram Moolenaara9922d62013-05-30 13:01:18 +02001428 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001429
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001430 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001431
Bram Moolenaara9922d62013-05-30 13:01:18 +02001432 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001433 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001434 if (defObject)
1435 {
1436 Py_INCREF(defObject);
1437 return defObject;
1438 }
1439 else
1440 {
1441 PyErr_SetObject(PyExc_KeyError, keyObject);
1442 return NULL;
1443 }
1444 }
1445 else if (flags & DICT_FLAG_RETURN_BOOL)
1446 {
1447 Py_INCREF(Py_True);
1448 return Py_True;
1449 }
1450
1451 di = dict_lookup(hi);
1452
1453 if (!(r = ConvertToPyObject(&di->di_tv)))
1454 return NULL;
1455
1456 if (flags & DICT_FLAG_POP)
1457 {
1458 if (dict->dv_lock)
1459 {
1460 PyErr_SetVim(_("dict is locked"));
1461 Py_DECREF(r);
1462 return NULL;
1463 }
1464
1465 hash_remove(&dict->dv_hashtab, hi);
1466 dictitem_free(di);
1467 }
1468
Bram Moolenaara9922d62013-05-30 13:01:18 +02001469 return r;
1470}
1471
1472 static PyObject *
1473DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1474{
1475 return _DictionaryItem(self, keyObject, 0);
1476}
1477
1478 static int
1479DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1480{
1481 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1482 int r;
1483
1484 r = (rObj == Py_True);
1485
1486 Py_DECREF(Py_True);
1487
1488 return r;
1489}
1490
1491typedef struct
1492{
1493 hashitem_T *ht_array;
1494 long_u ht_used;
1495 hashtab_T *ht;
1496 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001497 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001498} dictiterinfo_T;
1499
1500 static PyObject *
1501DictionaryIterNext(dictiterinfo_T **dii)
1502{
1503 PyObject *r;
1504
1505 if (!(*dii)->todo)
1506 return NULL;
1507
1508 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1509 (*dii)->ht->ht_used != (*dii)->ht_used)
1510 {
1511 PyErr_SetString(PyExc_RuntimeError,
1512 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001513 return NULL;
1514 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001515
Bram Moolenaara9922d62013-05-30 13:01:18 +02001516 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1517 ++((*dii)->hi);
1518
1519 --((*dii)->todo);
1520
1521 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1522 return NULL;
1523
1524 return r;
1525}
1526
1527 static PyObject *
1528DictionaryIter(DictionaryObject *self)
1529{
1530 dictiterinfo_T *dii;
1531 hashtab_T *ht;
1532
1533 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1534 {
1535 PyErr_NoMemory();
1536 return NULL;
1537 }
1538
1539 ht = &self->dict->dv_hashtab;
1540 dii->ht_array = ht->ht_array;
1541 dii->ht_used = ht->ht_used;
1542 dii->ht = ht;
1543 dii->hi = dii->ht_array;
1544 dii->todo = dii->ht_used;
1545
1546 return IterNew(dii,
1547 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1548 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001549}
1550
1551 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001552DictionaryAssItem(
1553 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001554{
1555 char_u *key;
1556 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001557 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001558 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001559 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001560
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001561 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001562 {
1563 PyErr_SetVim(_("dict is locked"));
1564 return -1;
1565 }
1566
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001567 if (!(key = StringToChars(keyObject, &todecref)))
1568 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001569
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001570 if (*key == NUL)
1571 {
1572 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001573 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001574 return -1;
1575 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001576
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001577 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001578
1579 if (valObject == NULL)
1580 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001581 hashitem_T *hi;
1582
Bram Moolenaardb913952012-06-29 12:54:53 +02001583 if (di == NULL)
1584 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001585 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001586 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001587 return -1;
1588 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001589 hi = hash_find(&dict->dv_hashtab, di->di_key);
1590 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001591 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001592 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001593 return 0;
1594 }
1595
1596 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001597 {
1598 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001599 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001600 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001601
1602 if (di == NULL)
1603 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001604 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001605 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001606 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001607 PyErr_NoMemory();
1608 return -1;
1609 }
1610 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001611 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001612
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001613 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001614 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001615 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001616 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001617 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001618 PyErr_SetVim(_("failed to add key to dictionary"));
1619 return -1;
1620 }
1621 }
1622 else
1623 clear_tv(&di->di_tv);
1624
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001625 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001626
1627 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001628 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001629 return 0;
1630}
1631
Bram Moolenaara9922d62013-05-30 13:01:18 +02001632typedef PyObject *(*hi_to_py)(hashitem_T *);
1633
Bram Moolenaardb913952012-06-29 12:54:53 +02001634 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001635DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001636{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001637 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001638 long_u todo = dict->dv_hashtab.ht_used;
1639 Py_ssize_t i = 0;
1640 PyObject *r;
1641 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001642 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001643
1644 r = PyList_New(todo);
1645 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1646 {
1647 if (!HASHITEM_EMPTY(hi))
1648 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001649 if (!(newObj = hiconvert(hi)))
1650 {
1651 Py_DECREF(r);
1652 return NULL;
1653 }
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02001654 PyList_SET_ITEM(r, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001655 --todo;
1656 ++i;
1657 }
1658 }
1659 return r;
1660}
1661
Bram Moolenaara9922d62013-05-30 13:01:18 +02001662 static PyObject *
1663dict_key(hashitem_T *hi)
1664{
1665 return PyBytes_FromString((char *)(hi->hi_key));
1666}
1667
1668 static PyObject *
1669DictionaryListKeys(DictionaryObject *self)
1670{
1671 return DictionaryListObjects(self, dict_key);
1672}
1673
1674 static PyObject *
1675dict_val(hashitem_T *hi)
1676{
1677 dictitem_T *di;
1678
1679 di = dict_lookup(hi);
1680 return ConvertToPyObject(&di->di_tv);
1681}
1682
1683 static PyObject *
1684DictionaryListValues(DictionaryObject *self)
1685{
1686 return DictionaryListObjects(self, dict_val);
1687}
1688
1689 static PyObject *
1690dict_item(hashitem_T *hi)
1691{
1692 PyObject *keyObject;
1693 PyObject *valObject;
1694 PyObject *r;
1695
1696 if (!(keyObject = dict_key(hi)))
1697 return NULL;
1698
1699 if (!(valObject = dict_val(hi)))
1700 {
1701 Py_DECREF(keyObject);
1702 return NULL;
1703 }
1704
1705 r = Py_BuildValue("(OO)", keyObject, valObject);
1706
1707 Py_DECREF(keyObject);
1708 Py_DECREF(valObject);
1709
1710 return r;
1711}
1712
1713 static PyObject *
1714DictionaryListItems(DictionaryObject *self)
1715{
1716 return DictionaryListObjects(self, dict_item);
1717}
1718
1719 static PyObject *
1720DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1721{
1722 dict_T *dict = self->dict;
1723
1724 if (dict->dv_lock)
1725 {
1726 PyErr_SetVim(_("dict is locked"));
1727 return NULL;
1728 }
1729
1730 if (kwargs)
1731 {
1732 typval_T tv;
1733
1734 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1735 return NULL;
1736
1737 VimTryStart();
1738 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1739 clear_tv(&tv);
1740 if (VimTryEnd())
1741 return NULL;
1742 }
1743 else
1744 {
1745 PyObject *object;
1746
Bram Moolenaar389a1792013-06-23 13:00:44 +02001747 if (!PyArg_ParseTuple(args, "O", &object))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001748 return NULL;
1749
1750 if (PyObject_HasAttrString(object, "keys"))
1751 return DictionaryUpdate(self, NULL, object);
1752 else
1753 {
1754 PyObject *iterator;
1755 PyObject *item;
1756
1757 if (!(iterator = PyObject_GetIter(object)))
1758 return NULL;
1759
1760 while ((item = PyIter_Next(iterator)))
1761 {
1762 PyObject *fast;
1763 PyObject *keyObject;
1764 PyObject *valObject;
1765 PyObject *todecref;
1766 char_u *key;
1767 dictitem_T *di;
1768
1769 if (!(fast = PySequence_Fast(item, "")))
1770 {
1771 Py_DECREF(iterator);
1772 Py_DECREF(item);
1773 return NULL;
1774 }
1775
1776 Py_DECREF(item);
1777
1778 if (PySequence_Fast_GET_SIZE(fast) != 2)
1779 {
1780 Py_DECREF(iterator);
1781 Py_DECREF(fast);
1782 PyErr_SetString(PyExc_ValueError,
1783 _("expected sequence element of size 2"));
1784 return NULL;
1785 }
1786
1787 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1788
1789 if (!(key = StringToChars(keyObject, &todecref)))
1790 {
1791 Py_DECREF(iterator);
1792 Py_DECREF(fast);
1793 return NULL;
1794 }
1795
1796 di = dictitem_alloc(key);
1797
1798 Py_XDECREF(todecref);
1799
1800 if (di == NULL)
1801 {
1802 Py_DECREF(fast);
1803 Py_DECREF(iterator);
1804 PyErr_NoMemory();
1805 return NULL;
1806 }
1807 di->di_tv.v_lock = 0;
1808 di->di_tv.v_type = VAR_UNKNOWN;
1809
1810 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1811
1812 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1813 {
1814 Py_DECREF(iterator);
1815 Py_DECREF(fast);
1816 dictitem_free(di);
1817 return NULL;
1818 }
1819
1820 Py_DECREF(fast);
1821
1822 if (dict_add(dict, di) == FAIL)
1823 {
1824 Py_DECREF(iterator);
1825 dictitem_free(di);
1826 PyErr_SetVim(_("failed to add key to dictionary"));
1827 return NULL;
1828 }
1829 }
1830
1831 Py_DECREF(iterator);
1832
1833 /* Iterator may have finished due to an exception */
1834 if (PyErr_Occurred())
1835 return NULL;
1836 }
1837 }
1838 Py_INCREF(Py_None);
1839 return Py_None;
1840}
1841
1842 static PyObject *
1843DictionaryGet(DictionaryObject *self, PyObject *args)
1844{
1845 return _DictionaryItem(self, args,
1846 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1847}
1848
1849 static PyObject *
1850DictionaryPop(DictionaryObject *self, PyObject *args)
1851{
1852 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1853}
1854
1855 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001856DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001857{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001858 hashitem_T *hi;
1859 PyObject *r;
1860 PyObject *valObject;
1861 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001862
Bram Moolenaarde71b562013-06-02 17:41:54 +02001863 if (self->dict->dv_hashtab.ht_used == 0)
1864 {
1865 PyErr_SetNone(PyExc_KeyError);
1866 return NULL;
1867 }
1868
1869 hi = self->dict->dv_hashtab.ht_array;
1870 while (HASHITEM_EMPTY(hi))
1871 ++hi;
1872
1873 di = dict_lookup(hi);
1874
1875 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001876 return NULL;
1877
Bram Moolenaarde71b562013-06-02 17:41:54 +02001878 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
1879 {
1880 Py_DECREF(valObject);
1881 return NULL;
1882 }
1883
1884 hash_remove(&self->dict->dv_hashtab, hi);
1885 dictitem_free(di);
1886
1887 return r;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001888}
1889
1890 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001891DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001892{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001893 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 Moolenaar389a1792013-06-23 13:00:44 +02001923 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
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
Bram Moolenaar389a1792013-06-23 13:00:44 +02002443 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002444 return NULL;
2445
2446 self = FunctionNew(subtype, name);
2447
Bram Moolenaar389a1792013-06-23 13:00:44 +02002448 PyMem_Free(name);
2449
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002450 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002451}
2452
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002453 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002454FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002455{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002456 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002457 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002458
2459 DESTRUCTOR_FINISH(self);
2460}
2461
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002462static char *FunctionAttrs[] = {
2463 "softspace",
2464 NULL
2465};
2466
2467 static PyObject *
2468FunctionDir(PyObject *self)
2469{
2470 return ObjectDir(self, FunctionAttrs);
2471}
2472
Bram Moolenaardb913952012-06-29 12:54:53 +02002473 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002474FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002475{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002476 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002477 typval_T args;
2478 typval_T selfdicttv;
2479 typval_T rettv;
2480 dict_T *selfdict = NULL;
2481 PyObject *selfdictObject;
2482 PyObject *result;
2483 int error;
2484
2485 if (ConvertFromPyObject(argsObject, &args) == -1)
2486 return NULL;
2487
2488 if (kwargs != NULL)
2489 {
2490 selfdictObject = PyDict_GetItemString(kwargs, "self");
2491 if (selfdictObject != NULL)
2492 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002493 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002494 {
2495 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002496 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002497 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002498 selfdict = selfdicttv.vval.v_dict;
2499 }
2500 }
2501
Bram Moolenaar71700b82013-05-15 17:49:05 +02002502 Py_BEGIN_ALLOW_THREADS
2503 Python_Lock_Vim();
2504
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002505 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002506 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002507
2508 Python_Release_Vim();
2509 Py_END_ALLOW_THREADS
2510
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002511 if (VimTryEnd())
2512 result = NULL;
2513 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002514 {
2515 result = NULL;
2516 PyErr_SetVim(_("failed to run function"));
2517 }
2518 else
2519 result = ConvertToPyObject(&rettv);
2520
Bram Moolenaardb913952012-06-29 12:54:53 +02002521 clear_tv(&args);
2522 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002523 if (selfdict != NULL)
2524 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002525
2526 return result;
2527}
2528
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002529 static PyObject *
2530FunctionRepr(FunctionObject *self)
2531{
2532 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2533}
2534
Bram Moolenaardb913952012-06-29 12:54:53 +02002535static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002536 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2537 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002538};
2539
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002540/*
2541 * Options object
2542 */
2543
2544static PyTypeObject OptionsType;
2545
2546typedef int (*checkfun)(void *);
2547
2548typedef struct
2549{
2550 PyObject_HEAD
2551 int opt_type;
2552 void *from;
2553 checkfun Check;
2554 PyObject *fromObj;
2555} OptionsObject;
2556
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002557 static int
2558dummy_check(void *arg UNUSED)
2559{
2560 return 0;
2561}
2562
2563 static PyObject *
2564OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2565{
2566 OptionsObject *self;
2567
Bram Moolenaar774267b2013-05-21 20:51:59 +02002568 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002569 if (self == NULL)
2570 return NULL;
2571
2572 self->opt_type = opt_type;
2573 self->from = from;
2574 self->Check = Check;
2575 self->fromObj = fromObj;
2576 if (fromObj)
2577 Py_INCREF(fromObj);
2578
2579 return (PyObject *)(self);
2580}
2581
2582 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002583OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002584{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002585 PyObject_GC_UnTrack((void *)(self));
2586 Py_XDECREF(self->fromObj);
2587 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002588}
2589
2590 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002591OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002592{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002593 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002594 return 0;
2595}
2596
2597 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002598OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002599{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002600 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002601 return 0;
2602}
2603
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002604 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002605OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002606{
2607 char_u *key;
2608 int flags;
2609 long numval;
2610 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002611 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002612
Bram Moolenaard6e39182013-05-21 18:30:34 +02002613 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002614 return NULL;
2615
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002616 if (!(key = StringToChars(keyObject, &todecref)))
2617 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002618
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002619 if (*key == NUL)
2620 {
2621 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002622 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002623 return NULL;
2624 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002625
2626 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002627 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002628
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002629 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002630
2631 if (flags == 0)
2632 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002633 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002634 return NULL;
2635 }
2636
2637 if (flags & SOPT_UNSET)
2638 {
2639 Py_INCREF(Py_None);
2640 return Py_None;
2641 }
2642 else if (flags & SOPT_BOOL)
2643 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002644 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002645 r = numval ? Py_True : Py_False;
2646 Py_INCREF(r);
2647 return r;
2648 }
2649 else if (flags & SOPT_NUM)
2650 return PyInt_FromLong(numval);
2651 else if (flags & SOPT_STRING)
2652 {
2653 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002654 {
2655 PyObject *r = PyBytes_FromString((char *) stringval);
2656 vim_free(stringval);
2657 return r;
2658 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002659 else
2660 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002661 PyErr_SetString(PyExc_RuntimeError,
2662 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002663 return NULL;
2664 }
2665 }
2666 else
2667 {
2668 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2669 return NULL;
2670 }
2671}
2672
2673 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002674set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002675{
2676 char_u *errmsg;
2677
2678 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2679 {
2680 if (VimTryEnd())
2681 return FAIL;
2682 PyErr_SetVim((char *)errmsg);
2683 return FAIL;
2684 }
2685 return OK;
2686}
2687
2688 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002689set_option_value_for(
2690 char_u *key,
2691 int numval,
2692 char_u *stringval,
2693 int opt_flags,
2694 int opt_type,
2695 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002696{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002697 win_T *save_curwin = NULL;
2698 tabpage_T *save_curtab = NULL;
2699 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002700 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002701
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002702 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002703 switch (opt_type)
2704 {
2705 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002706 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002707 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002708 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002709 if (VimTryEnd())
2710 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002711 PyErr_SetVim("Problem while switching windows.");
2712 return -1;
2713 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002714 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaard6949742013-06-16 14:18:28 +02002715 restore_win(save_curwin, save_curtab, FALSE);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002716 if (r == FAIL)
2717 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002718 break;
2719 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002720 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002721 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002722 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002723 if (r == FAIL)
2724 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002725 break;
2726 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002727 r = set_option_value_err(key, numval, stringval, opt_flags);
2728 if (r == FAIL)
2729 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002730 break;
2731 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002732 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002733}
2734
2735 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002736OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002737{
2738 char_u *key;
2739 int flags;
2740 int opt_flags;
2741 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002742 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002743
Bram Moolenaard6e39182013-05-21 18:30:34 +02002744 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002745 return -1;
2746
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002747 if (!(key = StringToChars(keyObject, &todecref)))
2748 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002749
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002750 if (*key == NUL)
2751 {
2752 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002753 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002754 return -1;
2755 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002756
2757 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002758 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002759
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002760 if (flags == 0)
2761 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002762 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002763 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002764 return -1;
2765 }
2766
2767 if (valObject == NULL)
2768 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002769 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002770 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002771 PyErr_SetString(PyExc_ValueError,
2772 _("unable to unset global option"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002773 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002774 return -1;
2775 }
2776 else if (!(flags & SOPT_GLOBAL))
2777 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002778 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2779 "without global value"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002780 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002781 return -1;
2782 }
2783 else
2784 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002785 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002786 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002787 return 0;
2788 }
2789 }
2790
Bram Moolenaard6e39182013-05-21 18:30:34 +02002791 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002792
2793 if (flags & SOPT_BOOL)
2794 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002795 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002796
Bram Moolenaarb983f752013-05-15 16:11:50 +02002797 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002798 r = -1;
2799 else
2800 r = set_option_value_for(key, istrue, NULL,
2801 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002802 }
2803 else if (flags & SOPT_NUM)
2804 {
2805 int val;
2806
2807#if PY_MAJOR_VERSION < 3
2808 if (PyInt_Check(valObject))
2809 val = PyInt_AsLong(valObject);
2810 else
2811#endif
2812 if (PyLong_Check(valObject))
2813 val = PyLong_AsLong(valObject);
2814 else
2815 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002816 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002817 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002818 return -1;
2819 }
2820
2821 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002822 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002823 }
2824 else
2825 {
2826 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002827 PyObject *todecref;
2828
2829 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002830 r = set_option_value_for(key, 0, val, opt_flags,
2831 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002832 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002833 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002834 }
2835
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002836 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002837
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002838 return r;
2839}
2840
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002841static PyMappingMethods OptionsAsMapping = {
2842 (lenfunc) NULL,
2843 (binaryfunc) OptionsItem,
2844 (objobjargproc) OptionsAssItem,
2845};
2846
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002847/* Tabpage object
2848 */
2849
2850typedef struct
2851{
2852 PyObject_HEAD
2853 tabpage_T *tab;
2854} TabPageObject;
2855
2856static PyObject *WinListNew(TabPageObject *tabObject);
2857
2858static PyTypeObject TabPageType;
2859
2860 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002861CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002862{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002863 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002864 {
2865 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2866 return -1;
2867 }
2868
2869 return 0;
2870}
2871
2872 static PyObject *
2873TabPageNew(tabpage_T *tab)
2874{
2875 TabPageObject *self;
2876
2877 if (TAB_PYTHON_REF(tab))
2878 {
2879 self = TAB_PYTHON_REF(tab);
2880 Py_INCREF(self);
2881 }
2882 else
2883 {
2884 self = PyObject_NEW(TabPageObject, &TabPageType);
2885 if (self == NULL)
2886 return NULL;
2887 self->tab = tab;
2888 TAB_PYTHON_REF(tab) = self;
2889 }
2890
2891 return (PyObject *)(self);
2892}
2893
2894 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002895TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002896{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002897 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2898 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002899
2900 DESTRUCTOR_FINISH(self);
2901}
2902
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002903static char *TabPageAttrs[] = {
2904 "windows", "number", "vars", "window", "valid",
2905 NULL
2906};
2907
2908 static PyObject *
2909TabPageDir(PyObject *self)
2910{
2911 return ObjectDir(self, TabPageAttrs);
2912}
2913
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002914 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002915TabPageAttrValid(TabPageObject *self, char *name)
2916{
2917 PyObject *r;
2918
2919 if (strcmp(name, "valid") != 0)
2920 return NULL;
2921
2922 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2923 Py_INCREF(r);
2924 return r;
2925}
2926
2927 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002928TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002929{
2930 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002931 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002932 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002933 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002934 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002935 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002936 else if (strcmp(name, "window") == 0)
2937 {
2938 /* For current tab window.c does not bother to set or update tp_curwin
2939 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002940 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002941 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002942 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002943 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002944 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002945 else if (strcmp(name, "__members__") == 0)
2946 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002947 return NULL;
2948}
2949
2950 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002951TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002952{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002953 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002954 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002955 else
2956 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002957 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002958
2959 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002960 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2961 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002962 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002963 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002964 }
2965}
2966
2967static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002968 /* name, function, calling, documentation */
2969 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2970 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002971};
2972
2973/*
2974 * Window list object
2975 */
2976
2977static PyTypeObject TabListType;
2978static PySequenceMethods TabListAsSeq;
2979
2980typedef struct
2981{
2982 PyObject_HEAD
2983} TabListObject;
2984
2985 static PyInt
2986TabListLength(PyObject *self UNUSED)
2987{
2988 tabpage_T *tp = first_tabpage;
2989 PyInt n = 0;
2990
2991 while (tp != NULL)
2992 {
2993 ++n;
2994 tp = tp->tp_next;
2995 }
2996
2997 return n;
2998}
2999
3000 static PyObject *
3001TabListItem(PyObject *self UNUSED, PyInt n)
3002{
3003 tabpage_T *tp;
3004
3005 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3006 if (n == 0)
3007 return TabPageNew(tp);
3008
3009 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
3010 return NULL;
3011}
3012
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003013/*
3014 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003015 */
3016
3017typedef struct
3018{
3019 PyObject_HEAD
3020 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003021 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003022} WindowObject;
3023
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003024static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003025
3026 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003027CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003028{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003029 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003030 {
3031 PyErr_SetVim(_("attempt to refer to deleted window"));
3032 return -1;
3033 }
3034
3035 return 0;
3036}
3037
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003038 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003039WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003040{
3041 /* We need to handle deletion of windows underneath us.
3042 * If we add a "w_python*_ref" field to the win_T structure,
3043 * then we can get at it in win_free() in vim. We then
3044 * need to create only ONE Python object per window - if
3045 * we try to create a second, just INCREF the existing one
3046 * and return it. The (single) Python object referring to
3047 * the window is stored in "w_python*_ref".
3048 * On a win_free() we set the Python object's win_T* field
3049 * to an invalid value. We trap all uses of a window
3050 * object, and reject them if the win_T* field is invalid.
3051 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003052 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003053 * w_python_ref and w_python3_ref fields respectively.
3054 */
3055
3056 WindowObject *self;
3057
3058 if (WIN_PYTHON_REF(win))
3059 {
3060 self = WIN_PYTHON_REF(win);
3061 Py_INCREF(self);
3062 }
3063 else
3064 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003065 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003066 if (self == NULL)
3067 return NULL;
3068 self->win = win;
3069 WIN_PYTHON_REF(win) = self;
3070 }
3071
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003072 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3073
Bram Moolenaar971db462013-05-12 18:44:48 +02003074 return (PyObject *)(self);
3075}
3076
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003077 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003078WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003079{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003080 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003081 if (self->win && self->win != INVALID_WINDOW_VALUE)
3082 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003083 Py_XDECREF(((PyObject *)(self->tabObject)));
3084 PyObject_GC_Del((void *)(self));
3085}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003086
Bram Moolenaar774267b2013-05-21 20:51:59 +02003087 static int
3088WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3089{
3090 Py_VISIT(((PyObject *)(self->tabObject)));
3091 return 0;
3092}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003093
Bram Moolenaar774267b2013-05-21 20:51:59 +02003094 static int
3095WindowClear(WindowObject *self)
3096{
3097 Py_CLEAR(self->tabObject);
3098 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003099}
3100
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003101 static win_T *
3102get_firstwin(TabPageObject *tabObject)
3103{
3104 if (tabObject)
3105 {
3106 if (CheckTabPage(tabObject))
3107 return NULL;
3108 /* For current tab window.c does not bother to set or update tp_firstwin
3109 */
3110 else if (tabObject->tab == curtab)
3111 return firstwin;
3112 else
3113 return tabObject->tab->tp_firstwin;
3114 }
3115 else
3116 return firstwin;
3117}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003118static char *WindowAttrs[] = {
3119 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3120 "tabpage", "valid",
3121 NULL
3122};
3123
3124 static PyObject *
3125WindowDir(PyObject *self)
3126{
3127 return ObjectDir(self, WindowAttrs);
3128}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003129
Bram Moolenaar971db462013-05-12 18:44:48 +02003130 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003131WindowAttrValid(WindowObject *self, char *name)
3132{
3133 PyObject *r;
3134
3135 if (strcmp(name, "valid") != 0)
3136 return NULL;
3137
3138 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3139 Py_INCREF(r);
3140 return r;
3141}
3142
3143 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003144WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003145{
3146 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003147 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003148 else if (strcmp(name, "cursor") == 0)
3149 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003150 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003151
3152 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3153 }
3154 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003155 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003156#ifdef FEAT_WINDOWS
3157 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003158 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003159#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003160#ifdef FEAT_VERTSPLIT
3161 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003162 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003163 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003164 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003165#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003166 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003167 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003168 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003169 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3170 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003171 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003172 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003173 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003174 return NULL;
3175 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003176 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003177 }
3178 else if (strcmp(name, "tabpage") == 0)
3179 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003180 Py_INCREF(self->tabObject);
3181 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003182 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003183 else if (strcmp(name, "__members__") == 0)
3184 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003185 else
3186 return NULL;
3187}
3188
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003189 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003190WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003191{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003192 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003193 return -1;
3194
3195 if (strcmp(name, "buffer") == 0)
3196 {
3197 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
3198 return -1;
3199 }
3200 else if (strcmp(name, "cursor") == 0)
3201 {
3202 long lnum;
3203 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003204
3205 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
3206 return -1;
3207
Bram Moolenaard6e39182013-05-21 18:30:34 +02003208 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003209 {
3210 PyErr_SetVim(_("cursor position outside buffer"));
3211 return -1;
3212 }
3213
3214 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003215 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003216 return -1;
3217
Bram Moolenaard6e39182013-05-21 18:30:34 +02003218 self->win->w_cursor.lnum = lnum;
3219 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003220#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003221 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003222#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003223 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003224 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003225
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003226 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003227 return 0;
3228 }
3229 else if (strcmp(name, "height") == 0)
3230 {
3231 int height;
3232 win_T *savewin;
3233
3234 if (!PyArg_Parse(val, "i", &height))
3235 return -1;
3236
3237#ifdef FEAT_GUI
3238 need_mouse_correct = TRUE;
3239#endif
3240 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003241 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003242
3243 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003244 win_setheight(height);
3245 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003246 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003247 return -1;
3248
3249 return 0;
3250 }
3251#ifdef FEAT_VERTSPLIT
3252 else if (strcmp(name, "width") == 0)
3253 {
3254 int width;
3255 win_T *savewin;
3256
3257 if (!PyArg_Parse(val, "i", &width))
3258 return -1;
3259
3260#ifdef FEAT_GUI
3261 need_mouse_correct = TRUE;
3262#endif
3263 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003264 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003265
3266 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003267 win_setwidth(width);
3268 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003269 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003270 return -1;
3271
3272 return 0;
3273 }
3274#endif
3275 else
3276 {
3277 PyErr_SetString(PyExc_AttributeError, name);
3278 return -1;
3279 }
3280}
3281
3282 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003283WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003284{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003285 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003286 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003287 else
3288 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003289 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003290
Bram Moolenaar6d216452013-05-12 19:00:41 +02003291 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003292 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003293 (self));
3294 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003295 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003296 }
3297}
3298
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003299static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003300 /* name, function, calling, documentation */
3301 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3302 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003303};
3304
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003305/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003306 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003307 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003308
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003309static PyTypeObject WinListType;
3310static PySequenceMethods WinListAsSeq;
3311
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003312typedef struct
3313{
3314 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003315 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003316} WinListObject;
3317
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003318 static PyObject *
3319WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003320{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003321 WinListObject *self;
3322
3323 self = PyObject_NEW(WinListObject, &WinListType);
3324 self->tabObject = tabObject;
3325 Py_INCREF(tabObject);
3326
3327 return (PyObject *)(self);
3328}
3329
3330 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003331WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003332{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003333 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003334
3335 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003336 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003337 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003338 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003339
3340 DESTRUCTOR_FINISH(self);
3341}
3342
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003343 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003344WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003345{
3346 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003347 PyInt n = 0;
3348
Bram Moolenaard6e39182013-05-21 18:30:34 +02003349 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003350 return -1;
3351
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003352 while (w != NULL)
3353 {
3354 ++n;
3355 w = W_NEXT(w);
3356 }
3357
3358 return n;
3359}
3360
3361 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003362WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003363{
3364 win_T *w;
3365
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003367 return NULL;
3368
3369 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003370 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003371 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003372
3373 PyErr_SetString(PyExc_IndexError, _("no such window"));
3374 return NULL;
3375}
3376
3377/* Convert a Python string into a Vim line.
3378 *
3379 * The result is in allocated memory. All internal nulls are replaced by
3380 * newline characters. It is an error for the string to contain newline
3381 * characters.
3382 *
3383 * On errors, the Python exception data is set, and NULL is returned.
3384 */
3385 static char *
3386StringToLine(PyObject *obj)
3387{
3388 const char *str;
3389 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003390 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003391 PyInt len;
3392 PyInt i;
3393 char *p;
3394
3395 if (obj == NULL || !PyString_Check(obj))
3396 {
3397 PyErr_BadArgument();
3398 return NULL;
3399 }
3400
Bram Moolenaar19e60942011-06-19 00:27:51 +02003401 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
3402 str = PyString_AsString(bytes);
3403 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003404
3405 /*
3406 * Error checking: String must not contain newlines, as we
3407 * are replacing a single line, and we must replace it with
3408 * a single line.
3409 * A trailing newline is removed, so that append(f.readlines()) works.
3410 */
3411 p = memchr(str, '\n', len);
3412 if (p != NULL)
3413 {
3414 if (p == str + len - 1)
3415 --len;
3416 else
3417 {
3418 PyErr_SetVim(_("string cannot contain newlines"));
3419 return NULL;
3420 }
3421 }
3422
3423 /* Create a copy of the string, with internal nulls replaced by
3424 * newline characters, as is the vim convention.
3425 */
3426 save = (char *)alloc((unsigned)(len+1));
3427 if (save == NULL)
3428 {
3429 PyErr_NoMemory();
3430 return NULL;
3431 }
3432
3433 for (i = 0; i < len; ++i)
3434 {
3435 if (str[i] == '\0')
3436 save[i] = '\n';
3437 else
3438 save[i] = str[i];
3439 }
3440
3441 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003442 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003443
3444 return save;
3445}
3446
3447/* Get a line from the specified buffer. The line number is
3448 * in Vim format (1-based). The line is returned as a Python
3449 * string object.
3450 */
3451 static PyObject *
3452GetBufferLine(buf_T *buf, PyInt n)
3453{
3454 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3455}
3456
3457
3458/* Get a list of lines from the specified buffer. The line numbers
3459 * are in Vim format (1-based). The range is from lo up to, but not
3460 * including, hi. The list is returned as a Python list of string objects.
3461 */
3462 static PyObject *
3463GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3464{
3465 PyInt i;
3466 PyInt n = hi - lo;
3467 PyObject *list = PyList_New(n);
3468
3469 if (list == NULL)
3470 return NULL;
3471
3472 for (i = 0; i < n; ++i)
3473 {
3474 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3475
3476 /* Error check - was the Python string creation OK? */
3477 if (str == NULL)
3478 {
3479 Py_DECREF(list);
3480 return NULL;
3481 }
3482
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02003483 PyList_SET_ITEM(list, i, str);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003484 }
3485
3486 /* The ownership of the Python list is passed to the caller (ie,
3487 * the caller should Py_DECREF() the object when it is finished
3488 * with it).
3489 */
3490
3491 return list;
3492}
3493
3494/*
3495 * Check if deleting lines made the cursor position invalid.
3496 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3497 * deleted).
3498 */
3499 static void
3500py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3501{
3502 if (curwin->w_cursor.lnum >= lo)
3503 {
3504 /* Adjust the cursor position if it's in/after the changed
3505 * lines. */
3506 if (curwin->w_cursor.lnum >= hi)
3507 {
3508 curwin->w_cursor.lnum += extra;
3509 check_cursor_col();
3510 }
3511 else if (extra < 0)
3512 {
3513 curwin->w_cursor.lnum = lo;
3514 check_cursor();
3515 }
3516 else
3517 check_cursor_col();
3518 changed_cline_bef_curs();
3519 }
3520 invalidate_botline();
3521}
3522
Bram Moolenaar19e60942011-06-19 00:27:51 +02003523/*
3524 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003525 * in Vim format (1-based). The replacement line is given as
3526 * a Python string object. The object is checked for validity
3527 * and correct format. Errors are returned as a value of FAIL.
3528 * The return value is OK on success.
3529 * If OK is returned and len_change is not NULL, *len_change
3530 * is set to the change in the buffer length.
3531 */
3532 static int
3533SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3534{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003535 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003536 * There are three cases:
3537 * 1. NULL, or None - this is a deletion.
3538 * 2. A string - this is a replacement.
3539 * 3. Anything else - this is an error.
3540 */
3541 if (line == Py_None || line == NULL)
3542 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003543 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003544
3545 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003546 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003547
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003548 VimTryStart();
3549
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003550 if (u_savedel((linenr_T)n, 1L) == FAIL)
3551 PyErr_SetVim(_("cannot save undo information"));
3552 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3553 PyErr_SetVim(_("cannot delete line"));
3554 else
3555 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003556 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003557 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3558 deleted_lines_mark((linenr_T)n, 1L);
3559 }
3560
Bram Moolenaar105bc352013-05-17 16:03:57 +02003561 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003562
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003563 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003564 return FAIL;
3565
3566 if (len_change)
3567 *len_change = -1;
3568
3569 return OK;
3570 }
3571 else if (PyString_Check(line))
3572 {
3573 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003574 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003575
3576 if (save == NULL)
3577 return FAIL;
3578
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003579 VimTryStart();
3580
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003581 /* We do not need to free "save" if ml_replace() consumes it. */
3582 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003583 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003584
3585 if (u_savesub((linenr_T)n) == FAIL)
3586 {
3587 PyErr_SetVim(_("cannot save undo information"));
3588 vim_free(save);
3589 }
3590 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3591 {
3592 PyErr_SetVim(_("cannot replace line"));
3593 vim_free(save);
3594 }
3595 else
3596 changed_bytes((linenr_T)n, 0);
3597
Bram Moolenaar105bc352013-05-17 16:03:57 +02003598 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003599
3600 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003601 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003602 check_cursor_col();
3603
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003604 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003605 return FAIL;
3606
3607 if (len_change)
3608 *len_change = 0;
3609
3610 return OK;
3611 }
3612 else
3613 {
3614 PyErr_BadArgument();
3615 return FAIL;
3616 }
3617}
3618
Bram Moolenaar19e60942011-06-19 00:27:51 +02003619/* Replace a range of lines in the specified buffer. The line numbers are in
3620 * Vim format (1-based). The range is from lo up to, but not including, hi.
3621 * The replacement lines are given as a Python list of string objects. The
3622 * list is checked for validity and correct format. Errors are returned as a
3623 * value of FAIL. The return value is OK on success.
3624 * If OK is returned and len_change is not NULL, *len_change
3625 * is set to the change in the buffer length.
3626 */
3627 static int
3628SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3629{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003630 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003631 * There are three cases:
3632 * 1. NULL, or None - this is a deletion.
3633 * 2. A list - this is a replacement.
3634 * 3. Anything else - this is an error.
3635 */
3636 if (list == Py_None || list == NULL)
3637 {
3638 PyInt i;
3639 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003640 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003641
3642 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003643 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003644 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003645
3646 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3647 PyErr_SetVim(_("cannot save undo information"));
3648 else
3649 {
3650 for (i = 0; i < n; ++i)
3651 {
3652 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3653 {
3654 PyErr_SetVim(_("cannot delete line"));
3655 break;
3656 }
3657 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003658 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003659 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3660 deleted_lines_mark((linenr_T)lo, (long)i);
3661 }
3662
Bram Moolenaar105bc352013-05-17 16:03:57 +02003663 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003664
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003665 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003666 return FAIL;
3667
3668 if (len_change)
3669 *len_change = -n;
3670
3671 return OK;
3672 }
3673 else if (PyList_Check(list))
3674 {
3675 PyInt i;
3676 PyInt new_len = PyList_Size(list);
3677 PyInt old_len = hi - lo;
3678 PyInt extra = 0; /* lines added to text, can be negative */
3679 char **array;
3680 buf_T *savebuf;
3681
3682 if (new_len == 0) /* avoid allocating zero bytes */
3683 array = NULL;
3684 else
3685 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003686 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003687 if (array == NULL)
3688 {
3689 PyErr_NoMemory();
3690 return FAIL;
3691 }
3692 }
3693
3694 for (i = 0; i < new_len; ++i)
3695 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003696 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003697
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003698 if (!(line = PyList_GetItem(list, i)) ||
3699 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003700 {
3701 while (i)
3702 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003703 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003704 return FAIL;
3705 }
3706 }
3707
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003708 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003709 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003710
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003711 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003712 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003713
3714 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3715 PyErr_SetVim(_("cannot save undo information"));
3716
3717 /* If the size of the range is reducing (ie, new_len < old_len) we
3718 * need to delete some old_len. We do this at the start, by
3719 * repeatedly deleting line "lo".
3720 */
3721 if (!PyErr_Occurred())
3722 {
3723 for (i = 0; i < old_len - new_len; ++i)
3724 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3725 {
3726 PyErr_SetVim(_("cannot delete line"));
3727 break;
3728 }
3729 extra -= i;
3730 }
3731
3732 /* For as long as possible, replace the existing old_len with the
3733 * new old_len. This is a more efficient operation, as it requires
3734 * less memory allocation and freeing.
3735 */
3736 if (!PyErr_Occurred())
3737 {
3738 for (i = 0; i < old_len && i < new_len; ++i)
3739 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3740 == FAIL)
3741 {
3742 PyErr_SetVim(_("cannot replace line"));
3743 break;
3744 }
3745 }
3746 else
3747 i = 0;
3748
3749 /* Now we may need to insert the remaining new old_len. If we do, we
3750 * must free the strings as we finish with them (we can't pass the
3751 * responsibility to vim in this case).
3752 */
3753 if (!PyErr_Occurred())
3754 {
3755 while (i < new_len)
3756 {
3757 if (ml_append((linenr_T)(lo + i - 1),
3758 (char_u *)array[i], 0, FALSE) == FAIL)
3759 {
3760 PyErr_SetVim(_("cannot insert line"));
3761 break;
3762 }
3763 vim_free(array[i]);
3764 ++i;
3765 ++extra;
3766 }
3767 }
3768
3769 /* Free any left-over old_len, as a result of an error */
3770 while (i < new_len)
3771 {
3772 vim_free(array[i]);
3773 ++i;
3774 }
3775
3776 /* Free the array of old_len. All of its contents have now
3777 * been dealt with (either freed, or the responsibility passed
3778 * to vim.
3779 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003780 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003781
3782 /* Adjust marks. Invalidate any which lie in the
3783 * changed range, and move any in the remainder of the buffer.
3784 */
3785 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3786 (long)MAXLNUM, (long)extra);
3787 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3788
Bram Moolenaar105bc352013-05-17 16:03:57 +02003789 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003790 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3791
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003792 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003793 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003794
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003795 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003796 return FAIL;
3797
3798 if (len_change)
3799 *len_change = new_len - old_len;
3800
3801 return OK;
3802 }
3803 else
3804 {
3805 PyErr_BadArgument();
3806 return FAIL;
3807 }
3808}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003809
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003810/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003811 * The line number is in Vim format (1-based). The lines to be inserted are
3812 * given as a Python list of string objects or as a single string. The lines
3813 * to be added are checked for validity and correct format. Errors are
3814 * returned as a value of FAIL. The return value is OK on success.
3815 * If OK is returned and len_change is not NULL, *len_change
3816 * is set to the change in the buffer length.
3817 */
3818 static int
3819InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3820{
3821 /* First of all, we check the type of the supplied Python object.
3822 * It must be a string or a list, or the call is in error.
3823 */
3824 if (PyString_Check(lines))
3825 {
3826 char *str = StringToLine(lines);
3827 buf_T *savebuf;
3828
3829 if (str == NULL)
3830 return FAIL;
3831
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003832 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003833 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003834 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003835
3836 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3837 PyErr_SetVim(_("cannot save undo information"));
3838 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3839 PyErr_SetVim(_("cannot insert line"));
3840 else
3841 appended_lines_mark((linenr_T)n, 1L);
3842
3843 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003844 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003845 update_screen(VALID);
3846
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003847 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003848 return FAIL;
3849
3850 if (len_change)
3851 *len_change = 1;
3852
3853 return OK;
3854 }
3855 else if (PyList_Check(lines))
3856 {
3857 PyInt i;
3858 PyInt size = PyList_Size(lines);
3859 char **array;
3860 buf_T *savebuf;
3861
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003862 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003863 if (array == NULL)
3864 {
3865 PyErr_NoMemory();
3866 return FAIL;
3867 }
3868
3869 for (i = 0; i < size; ++i)
3870 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003871 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003872
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003873 if (!(line = PyList_GetItem(lines, i)) ||
3874 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003875 {
3876 while (i)
3877 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003878 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003879 return FAIL;
3880 }
3881 }
3882
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003883 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003884 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003885 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003886
3887 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3888 PyErr_SetVim(_("cannot save undo information"));
3889 else
3890 {
3891 for (i = 0; i < size; ++i)
3892 {
3893 if (ml_append((linenr_T)(n + i),
3894 (char_u *)array[i], 0, FALSE) == FAIL)
3895 {
3896 PyErr_SetVim(_("cannot insert line"));
3897
3898 /* Free the rest of the lines */
3899 while (i < size)
3900 vim_free(array[i++]);
3901
3902 break;
3903 }
3904 vim_free(array[i]);
3905 }
3906 if (i > 0)
3907 appended_lines_mark((linenr_T)n, (long)i);
3908 }
3909
3910 /* Free the array of lines. All of its contents have now
3911 * been freed.
3912 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003913 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003914
Bram Moolenaar105bc352013-05-17 16:03:57 +02003915 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003916 update_screen(VALID);
3917
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003918 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003919 return FAIL;
3920
3921 if (len_change)
3922 *len_change = size;
3923
3924 return OK;
3925 }
3926 else
3927 {
3928 PyErr_BadArgument();
3929 return FAIL;
3930 }
3931}
3932
3933/*
3934 * Common routines for buffers and line ranges
3935 * -------------------------------------------
3936 */
3937
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003938typedef struct
3939{
3940 PyObject_HEAD
3941 buf_T *buf;
3942} BufferObject;
3943
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003944 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003945CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003946{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003947 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003948 {
3949 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3950 return -1;
3951 }
3952
3953 return 0;
3954}
3955
3956 static PyObject *
3957RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3958{
3959 if (CheckBuffer(self))
3960 return NULL;
3961
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003962 if (end == -1)
3963 end = self->buf->b_ml.ml_line_count;
3964
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003965 if (n < 0)
3966 n += end - start + 1;
3967
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003968 if (n < 0 || n > end - start)
3969 {
3970 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3971 return NULL;
3972 }
3973
3974 return GetBufferLine(self->buf, n+start);
3975}
3976
3977 static PyObject *
3978RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3979{
3980 PyInt size;
3981
3982 if (CheckBuffer(self))
3983 return NULL;
3984
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003985 if (end == -1)
3986 end = self->buf->b_ml.ml_line_count;
3987
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003988 size = end - start + 1;
3989
3990 if (lo < 0)
3991 lo = 0;
3992 else if (lo > size)
3993 lo = size;
3994 if (hi < 0)
3995 hi = 0;
3996 if (hi < lo)
3997 hi = lo;
3998 else if (hi > size)
3999 hi = size;
4000
4001 return GetBufferLineList(self->buf, lo+start, hi+start);
4002}
4003
4004 static PyInt
4005RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
4006{
4007 PyInt len_change;
4008
4009 if (CheckBuffer(self))
4010 return -1;
4011
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004012 if (end == -1)
4013 end = self->buf->b_ml.ml_line_count;
4014
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004015 if (n < 0)
4016 n += end - start + 1;
4017
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004018 if (n < 0 || n > end - start)
4019 {
4020 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
4021 return -1;
4022 }
4023
4024 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
4025 return -1;
4026
4027 if (new_end)
4028 *new_end = end + len_change;
4029
4030 return 0;
4031}
4032
Bram Moolenaar19e60942011-06-19 00:27:51 +02004033 static PyInt
4034RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
4035{
4036 PyInt size;
4037 PyInt len_change;
4038
4039 /* Self must be a valid buffer */
4040 if (CheckBuffer(self))
4041 return -1;
4042
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004043 if (end == -1)
4044 end = self->buf->b_ml.ml_line_count;
4045
Bram Moolenaar19e60942011-06-19 00:27:51 +02004046 /* Sort out the slice range */
4047 size = end - start + 1;
4048
4049 if (lo < 0)
4050 lo = 0;
4051 else if (lo > size)
4052 lo = size;
4053 if (hi < 0)
4054 hi = 0;
4055 if (hi < lo)
4056 hi = lo;
4057 else if (hi > size)
4058 hi = size;
4059
4060 if (SetBufferLineList(self->buf, lo + start, hi + start,
4061 val, &len_change) == FAIL)
4062 return -1;
4063
4064 if (new_end)
4065 *new_end = end + len_change;
4066
4067 return 0;
4068}
4069
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004070
4071 static PyObject *
4072RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
4073{
4074 PyObject *lines;
4075 PyInt len_change;
4076 PyInt max;
4077 PyInt n;
4078
4079 if (CheckBuffer(self))
4080 return NULL;
4081
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004082 if (end == -1)
4083 end = self->buf->b_ml.ml_line_count;
4084
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085 max = n = end - start + 1;
4086
4087 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4088 return NULL;
4089
4090 if (n < 0 || n > max)
4091 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004092 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093 return NULL;
4094 }
4095
4096 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4097 return NULL;
4098
4099 if (new_end)
4100 *new_end = end + len_change;
4101
4102 Py_INCREF(Py_None);
4103 return Py_None;
4104}
4105
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004106/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107 */
4108
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004109static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004110static PySequenceMethods RangeAsSeq;
4111static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004112
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113typedef struct
4114{
4115 PyObject_HEAD
4116 BufferObject *buf;
4117 PyInt start;
4118 PyInt end;
4119} RangeObject;
4120
4121 static PyObject *
4122RangeNew(buf_T *buf, PyInt start, PyInt end)
4123{
4124 BufferObject *bufr;
4125 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004126 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004127 if (self == NULL)
4128 return NULL;
4129
4130 bufr = (BufferObject *)BufferNew(buf);
4131 if (bufr == NULL)
4132 {
4133 Py_DECREF(self);
4134 return NULL;
4135 }
4136 Py_INCREF(bufr);
4137
4138 self->buf = bufr;
4139 self->start = start;
4140 self->end = end;
4141
4142 return (PyObject *)(self);
4143}
4144
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004145 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004146RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004147{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004148 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004149 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004150 PyObject_GC_Del((void *)(self));
4151}
4152
4153 static int
4154RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4155{
4156 Py_VISIT(((PyObject *)(self->buf)));
4157 return 0;
4158}
4159
4160 static int
4161RangeClear(RangeObject *self)
4162{
4163 Py_CLEAR(self->buf);
4164 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004165}
4166
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004167 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004168RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004169{
4170 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004171 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004172 return -1; /* ??? */
4173
Bram Moolenaard6e39182013-05-21 18:30:34 +02004174 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004175}
4176
4177 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004178RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004179{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004180 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004181}
4182
4183 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004184RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004185{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004186 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004187}
4188
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004189static char *RangeAttrs[] = {
4190 "start", "end",
4191 NULL
4192};
4193
4194 static PyObject *
4195RangeDir(PyObject *self)
4196{
4197 return ObjectDir(self, RangeAttrs);
4198}
4199
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004200 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004201RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004202{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004203 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004204}
4205
4206 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004207RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004208{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004209 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004210 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4211 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004212 else
4213 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004214 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004215
4216 if (name == NULL)
4217 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004218
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004219 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004220 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004221 }
4222}
4223
4224static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004225 /* name, function, calling, documentation */
4226 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004227 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4228 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004229};
4230
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004231static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004232static PySequenceMethods BufferAsSeq;
4233static PyMappingMethods BufferAsMapping;
4234
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004235 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004236BufferNew(buf_T *buf)
4237{
4238 /* We need to handle deletion of buffers underneath us.
4239 * If we add a "b_python*_ref" field to the buf_T structure,
4240 * then we can get at it in buf_freeall() in vim. We then
4241 * need to create only ONE Python object per buffer - if
4242 * we try to create a second, just INCREF the existing one
4243 * and return it. The (single) Python object referring to
4244 * the buffer is stored in "b_python*_ref".
4245 * Question: what to do on a buf_freeall(). We'll probably
4246 * have to either delete the Python object (DECREF it to
4247 * zero - a bad idea, as it leaves dangling refs!) or
4248 * set the buf_T * value to an invalid value (-1?), which
4249 * means we need checks in all access functions... Bah.
4250 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004251 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004252 * b_python_ref and b_python3_ref fields respectively.
4253 */
4254
4255 BufferObject *self;
4256
4257 if (BUF_PYTHON_REF(buf) != NULL)
4258 {
4259 self = BUF_PYTHON_REF(buf);
4260 Py_INCREF(self);
4261 }
4262 else
4263 {
4264 self = PyObject_NEW(BufferObject, &BufferType);
4265 if (self == NULL)
4266 return NULL;
4267 self->buf = buf;
4268 BUF_PYTHON_REF(buf) = self;
4269 }
4270
4271 return (PyObject *)(self);
4272}
4273
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004274 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004275BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004276{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004277 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4278 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004279
4280 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004281}
4282
Bram Moolenaar971db462013-05-12 18:44:48 +02004283 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004284BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004285{
4286 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004287 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004288 return -1; /* ??? */
4289
Bram Moolenaard6e39182013-05-21 18:30:34 +02004290 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004291}
4292
4293 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004294BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004295{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004296 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004297}
4298
4299 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004300BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004301{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004302 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004303}
4304
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004305static char *BufferAttrs[] = {
4306 "name", "number", "vars", "options", "valid",
4307 NULL
4308};
4309
4310 static PyObject *
4311BufferDir(PyObject *self)
4312{
4313 return ObjectDir(self, BufferAttrs);
4314}
4315
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004316 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004317BufferAttrValid(BufferObject *self, char *name)
4318{
4319 PyObject *r;
4320
4321 if (strcmp(name, "valid") != 0)
4322 return NULL;
4323
4324 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4325 Py_INCREF(r);
4326 return r;
4327}
4328
4329 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004330BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004331{
4332 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004333 return PyString_FromString((self->buf->b_ffname == NULL
4334 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004335 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004336 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004337 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004338 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004339 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004340 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4341 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004342 else if (strcmp(name, "__members__") == 0)
4343 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004344 else
4345 return NULL;
4346}
4347
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004348 static int
4349BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4350{
4351 if (CheckBuffer(self))
4352 return -1;
4353
4354 if (strcmp(name, "name") == 0)
4355 {
4356 char_u *val;
4357 aco_save_T aco;
4358 int r;
4359 PyObject *todecref;
4360
4361 if (!(val = StringToChars(valObject, &todecref)))
4362 return -1;
4363
4364 VimTryStart();
4365 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4366 aucmd_prepbuf(&aco, self->buf);
4367 r = rename_buffer(val);
4368 aucmd_restbuf(&aco);
4369 Py_XDECREF(todecref);
4370 if (VimTryEnd())
4371 return -1;
4372
4373 if (r == FAIL)
4374 {
4375 PyErr_SetVim(_("failed to rename buffer"));
4376 return -1;
4377 }
4378 return 0;
4379 }
4380 else
4381 {
4382 PyErr_SetString(PyExc_AttributeError, name);
4383 return -1;
4384 }
4385}
4386
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004387 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004388BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004389{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004390 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004391}
4392
4393 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004394BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004395{
4396 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004397 char_u *pmark;
4398 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004399 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004400 PyObject *todecref;
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
Bram Moolenaar389a1792013-06-23 13:00:44 +02004405 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004406 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004407
Bram Moolenaar389a1792013-06-23 13:00:44 +02004408 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004409 {
4410 PyErr_SetString(PyExc_ValueError,
4411 _("mark name must be a single character"));
4412 return NULL;
4413 }
4414
4415 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004416
4417 Py_XDECREF(todecref);
4418
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004419 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004420 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004421 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004422 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004423 if (VimTryEnd())
4424 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425
4426 if (posp == NULL)
4427 {
4428 PyErr_SetVim(_("invalid mark name"));
4429 return NULL;
4430 }
4431
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432 if (posp->lnum <= 0)
4433 {
4434 /* Or raise an error? */
4435 Py_INCREF(Py_None);
4436 return Py_None;
4437 }
4438
4439 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4440}
4441
4442 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004443BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004444{
4445 PyInt start;
4446 PyInt end;
4447
Bram Moolenaard6e39182013-05-21 18:30:34 +02004448 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004449 return NULL;
4450
4451 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4452 return NULL;
4453
Bram Moolenaard6e39182013-05-21 18:30:34 +02004454 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004455}
4456
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004457 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004458BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004459{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004460 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004461 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004462 else
4463 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004464 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004465
4466 if (name == NULL)
4467 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004468
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004469 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004470 }
4471}
4472
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004473static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004474 /* name, function, calling, documentation */
4475 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004476 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004477 {"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 +02004478 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4479 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004480};
4481
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004482/*
4483 * Buffer list object - Implementation
4484 */
4485
4486static PyTypeObject BufMapType;
4487
4488typedef struct
4489{
4490 PyObject_HEAD
4491} BufMapObject;
4492
4493 static PyInt
4494BufMapLength(PyObject *self UNUSED)
4495{
4496 buf_T *b = firstbuf;
4497 PyInt n = 0;
4498
4499 while (b)
4500 {
4501 ++n;
4502 b = b->b_next;
4503 }
4504
4505 return n;
4506}
4507
4508 static PyObject *
4509BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4510{
4511 buf_T *b;
4512 int bnr;
4513
4514#if PY_MAJOR_VERSION < 3
4515 if (PyInt_Check(keyObject))
4516 bnr = PyInt_AsLong(keyObject);
4517 else
4518#endif
4519 if (PyLong_Check(keyObject))
4520 bnr = PyLong_AsLong(keyObject);
4521 else
4522 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004523 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004524 return NULL;
4525 }
4526
4527 b = buflist_findnr(bnr);
4528
4529 if (b)
4530 return BufferNew(b);
4531 else
4532 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004533 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004534 return NULL;
4535 }
4536}
4537
4538 static void
4539BufMapIterDestruct(PyObject *buffer)
4540{
4541 /* Iteration was stopped before all buffers were processed */
4542 if (buffer)
4543 {
4544 Py_DECREF(buffer);
4545 }
4546}
4547
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004548 static int
4549BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4550{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004551 if (buffer)
4552 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004553 return 0;
4554}
4555
4556 static int
4557BufMapIterClear(PyObject **buffer)
4558{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004559 if (*buffer)
4560 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004561 return 0;
4562}
4563
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004564 static PyObject *
4565BufMapIterNext(PyObject **buffer)
4566{
4567 PyObject *next;
4568 PyObject *r;
4569
4570 if (!*buffer)
4571 return NULL;
4572
4573 r = *buffer;
4574
4575 if (CheckBuffer((BufferObject *)(r)))
4576 {
4577 *buffer = NULL;
4578 return NULL;
4579 }
4580
4581 if (!((BufferObject *)(r))->buf->b_next)
4582 next = NULL;
4583 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4584 return NULL;
4585 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004586 /* Do not increment reference: we no longer hold it (decref), but whoever
4587 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004588 return r;
4589}
4590
4591 static PyObject *
4592BufMapIter(PyObject *self UNUSED)
4593{
4594 PyObject *buffer;
4595
4596 buffer = BufferNew(firstbuf);
4597 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004598 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4599 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004600}
4601
4602static PyMappingMethods BufMapAsMapping = {
4603 (lenfunc) BufMapLength,
4604 (binaryfunc) BufMapItem,
4605 (objobjargproc) 0,
4606};
4607
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004608/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004609 */
4610
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004611static char *CurrentAttrs[] = {
4612 "buffer", "window", "line", "range", "tabpage",
4613 NULL
4614};
4615
4616 static PyObject *
4617CurrentDir(PyObject *self)
4618{
4619 return ObjectDir(self, CurrentAttrs);
4620}
4621
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004622 static PyObject *
4623CurrentGetattr(PyObject *self UNUSED, char *name)
4624{
4625 if (strcmp(name, "buffer") == 0)
4626 return (PyObject *)BufferNew(curbuf);
4627 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004628 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004629 else if (strcmp(name, "tabpage") == 0)
4630 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004631 else if (strcmp(name, "line") == 0)
4632 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4633 else if (strcmp(name, "range") == 0)
4634 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004635 else if (strcmp(name, "__members__") == 0)
4636 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004637 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004638#if PY_MAJOR_VERSION < 3
4639 return Py_FindMethod(WindowMethods, self, name);
4640#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004641 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004642#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004643}
4644
4645 static int
4646CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4647{
4648 if (strcmp(name, "line") == 0)
4649 {
4650 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4651 return -1;
4652
4653 return 0;
4654 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004655 else if (strcmp(name, "buffer") == 0)
4656 {
4657 int count;
4658
4659 if (value->ob_type != &BufferType)
4660 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004661 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004662 return -1;
4663 }
4664
4665 if (CheckBuffer((BufferObject *)(value)))
4666 return -1;
4667 count = ((BufferObject *)(value))->buf->b_fnum;
4668
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004669 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004670 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4671 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004672 if (VimTryEnd())
4673 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004674 PyErr_SetVim(_("failed to switch to given buffer"));
4675 return -1;
4676 }
4677
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004678 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004679 }
4680 else if (strcmp(name, "window") == 0)
4681 {
4682 int count;
4683
4684 if (value->ob_type != &WindowType)
4685 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004686 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004687 return -1;
4688 }
4689
4690 if (CheckWindow((WindowObject *)(value)))
4691 return -1;
4692 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4693
4694 if (!count)
4695 {
4696 PyErr_SetString(PyExc_ValueError,
4697 _("failed to find window in the current tab page"));
4698 return -1;
4699 }
4700
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004701 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004702 win_goto(((WindowObject *)(value))->win);
4703 if (((WindowObject *)(value))->win != curwin)
4704 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004705 if (VimTryEnd())
4706 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004707 PyErr_SetString(PyExc_RuntimeError,
4708 _("did not switch to the specified window"));
4709 return -1;
4710 }
4711
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004712 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004713 }
4714 else if (strcmp(name, "tabpage") == 0)
4715 {
4716 if (value->ob_type != &TabPageType)
4717 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004718 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004719 return -1;
4720 }
4721
4722 if (CheckTabPage((TabPageObject *)(value)))
4723 return -1;
4724
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004725 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004726 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4727 if (((TabPageObject *)(value))->tab != curtab)
4728 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004729 if (VimTryEnd())
4730 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004731 PyErr_SetString(PyExc_RuntimeError,
4732 _("did not switch to the specified tab page"));
4733 return -1;
4734 }
4735
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004736 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004737 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004738 else
4739 {
4740 PyErr_SetString(PyExc_AttributeError, name);
4741 return -1;
4742 }
4743}
4744
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004745static struct PyMethodDef CurrentMethods[] = {
4746 /* name, function, calling, documentation */
4747 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4748 { NULL, NULL, 0, NULL}
4749};
4750
Bram Moolenaardb913952012-06-29 12:54:53 +02004751 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004752init_range_cmd(exarg_T *eap)
4753{
4754 RangeStart = eap->line1;
4755 RangeEnd = eap->line2;
4756}
4757
4758 static void
4759init_range_eval(typval_T *rettv UNUSED)
4760{
4761 RangeStart = (PyInt) curwin->w_cursor.lnum;
4762 RangeEnd = RangeStart;
4763}
4764
4765 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004766run_cmd(const char *cmd, void *arg UNUSED
4767#ifdef PY_CAN_RECURSE
4768 , PyGILState_STATE *pygilstate UNUSED
4769#endif
4770 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004771{
4772 PyRun_SimpleString((char *) cmd);
4773}
4774
4775static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4776static int code_hdr_len = 30;
4777
4778 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004779run_do(const char *cmd, void *arg UNUSED
4780#ifdef PY_CAN_RECURSE
4781 , PyGILState_STATE *pygilstate
4782#endif
4783 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004784{
4785 PyInt lnum;
4786 size_t len;
4787 char *code;
4788 int status;
4789 PyObject *pyfunc, *pymain;
4790
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004791 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004792 {
4793 EMSG(_("cannot save undo information"));
4794 return;
4795 }
4796
4797 len = code_hdr_len + STRLEN(cmd);
4798 code = PyMem_New(char, len + 1);
4799 memcpy(code, code_hdr, code_hdr_len);
4800 STRCPY(code + code_hdr_len, cmd);
4801 status = PyRun_SimpleString(code);
4802 PyMem_Free(code);
4803
4804 if (status)
4805 {
4806 EMSG(_("failed to run the code"));
4807 return;
4808 }
4809
4810 status = 0;
4811 pymain = PyImport_AddModule("__main__");
4812 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004813#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004814 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004815#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004816
4817 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4818 {
4819 PyObject *line, *linenr, *ret;
4820
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004821#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004822 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004823#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004824 if (!(line = GetBufferLine(curbuf, lnum)))
4825 goto err;
4826 if (!(linenr = PyInt_FromLong((long) lnum)))
4827 {
4828 Py_DECREF(line);
4829 goto err;
4830 }
4831 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4832 Py_DECREF(line);
4833 Py_DECREF(linenr);
4834 if (!ret)
4835 goto err;
4836
4837 if (ret != Py_None)
4838 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4839 goto err;
4840
4841 Py_XDECREF(ret);
4842 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004843#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004844 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004845#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004846 }
4847 goto out;
4848err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004849#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004850 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004851#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004852 PyErr_PrintEx(0);
4853 PythonIO_Flush();
4854 status = 1;
4855out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004856#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004857 if (!status)
4858 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004859#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004860 Py_DECREF(pyfunc);
4861 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4862 if (status)
4863 return;
4864 check_cursor();
4865 update_curbuf(NOT_VALID);
4866}
4867
4868 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004869run_eval(const char *cmd, typval_T *rettv
4870#ifdef PY_CAN_RECURSE
4871 , PyGILState_STATE *pygilstate UNUSED
4872#endif
4873 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004874{
4875 PyObject *r;
4876
4877 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4878 if (r == NULL)
4879 {
4880 if (PyErr_Occurred() && !msg_silent)
4881 PyErr_PrintEx(0);
4882 EMSG(_("E858: Eval did not return a valid python object"));
4883 }
4884 else
4885 {
4886 if (ConvertFromPyObject(r, rettv) == -1)
4887 EMSG(_("E859: Failed to convert returned python object to vim value"));
4888 Py_DECREF(r);
4889 }
4890 PyErr_Clear();
4891}
4892
4893 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004894set_ref_in_py(const int copyID)
4895{
4896 pylinkedlist_T *cur;
4897 dict_T *dd;
4898 list_T *ll;
4899
4900 if (lastdict != NULL)
4901 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4902 {
4903 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4904 if (dd->dv_copyID != copyID)
4905 {
4906 dd->dv_copyID = copyID;
4907 set_ref_in_ht(&dd->dv_hashtab, copyID);
4908 }
4909 }
4910
4911 if (lastlist != NULL)
4912 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4913 {
4914 ll = ((ListObject *) (cur->pll_obj))->list;
4915 if (ll->lv_copyID != copyID)
4916 {
4917 ll->lv_copyID = copyID;
4918 set_ref_in_list(ll, copyID);
4919 }
4920 }
4921}
4922
4923 static int
4924set_string_copy(char_u *str, typval_T *tv)
4925{
4926 tv->vval.v_string = vim_strsave(str);
4927 if (tv->vval.v_string == NULL)
4928 {
4929 PyErr_NoMemory();
4930 return -1;
4931 }
4932 return 0;
4933}
4934
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004935 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004936pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004937{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004938 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004939 char_u *key;
4940 dictitem_T *di;
4941 PyObject *keyObject;
4942 PyObject *valObject;
4943 Py_ssize_t iter = 0;
4944
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004945 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004946 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004947
4948 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004949 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004950
4951 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4952 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004953 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004954
Bram Moolenaara03e6312013-05-29 22:49:26 +02004955 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004956 {
4957 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004958 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004959 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004960
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004961 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004962 {
4963 dict_unref(dict);
4964 return -1;
4965 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004966
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004967 if (*key == NUL)
4968 {
4969 dict_unref(dict);
4970 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004971 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004972 return -1;
4973 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004974
4975 di = dictitem_alloc(key);
4976
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004977 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004978
4979 if (di == NULL)
4980 {
4981 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004982 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004983 return -1;
4984 }
4985 di->di_tv.v_lock = 0;
4986
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004987 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004988 {
4989 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004990 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004991 return -1;
4992 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004993
4994 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004995 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004996 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004997 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004998 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004999 PyErr_SetVim(_("failed to add key to dictionary"));
5000 return -1;
5001 }
5002 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005003
5004 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005005 return 0;
5006}
5007
5008 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005009pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005010{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005011 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005012 char_u *key;
5013 dictitem_T *di;
5014 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005015 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005016 PyObject *keyObject;
5017 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005018
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005019 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005020 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005021
5022 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005023 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005024
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005025 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005026 {
5027 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005028 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005029 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005030
5031 if (!(iterator = PyObject_GetIter(list)))
5032 {
5033 dict_unref(dict);
5034 Py_DECREF(list);
5035 return -1;
5036 }
5037 Py_DECREF(list);
5038
5039 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005040 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005041 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005042
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005043 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005044 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005045 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005046 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005047 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005048 return -1;
5049 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005050
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005051 if (*key == NUL)
5052 {
5053 Py_DECREF(keyObject);
5054 Py_DECREF(iterator);
5055 Py_XDECREF(todecref);
5056 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005057 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005058 return -1;
5059 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005060
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005061 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005062 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005063 Py_DECREF(keyObject);
5064 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005065 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005066 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005067 return -1;
5068 }
5069
5070 di = dictitem_alloc(key);
5071
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005072 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005073 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005074
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005075 if (di == NULL)
5076 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005077 Py_DECREF(iterator);
5078 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005079 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005080 PyErr_NoMemory();
5081 return -1;
5082 }
5083 di->di_tv.v_lock = 0;
5084
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005085 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005086 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005087 Py_DECREF(iterator);
5088 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005090 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005091 return -1;
5092 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005093
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005094 Py_DECREF(valObject);
5095
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005096 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005098 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005099 dictitem_free(di);
5100 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101 PyErr_SetVim(_("failed to add key to dictionary"));
5102 return -1;
5103 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005104 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005105 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005106 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005107 return 0;
5108}
5109
5110 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005111pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005112{
5113 list_T *l;
5114
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005115 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005117
5118 tv->v_type = VAR_LIST;
5119 tv->vval.v_list = l;
5120
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005121 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005122 {
5123 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005124 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005125 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005126
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005127 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005128 return 0;
5129}
5130
Bram Moolenaardb913952012-06-29 12:54:53 +02005131typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5132
5133 static int
5134convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005135 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005136{
5137 PyObject *capsule;
5138 char hexBuf[sizeof(void *) * 2 + 3];
5139
5140 sprintf(hexBuf, "%p", obj);
5141
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005142# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005143 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005144# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005145 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005146# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005147 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005148 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005149# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005150 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005151# else
5152 capsule = PyCObject_FromVoidPtr(tv, NULL);
5153# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005154 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5155 {
5156 Py_DECREF(capsule);
5157 tv->v_type = VAR_UNKNOWN;
5158 return -1;
5159 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005160 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005161 {
5162 tv->v_type = VAR_UNKNOWN;
5163 return -1;
5164 }
5165 /* As we are not using copy_tv which increments reference count we must
5166 * do it ourself. */
5167 switch(tv->v_type)
5168 {
5169 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5170 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5171 }
5172 }
5173 else
5174 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005175 typval_T *v;
5176
5177# ifdef PY_USE_CAPSULE
5178 v = PyCapsule_GetPointer(capsule, NULL);
5179# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005180 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005181# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005182 copy_tv(v, tv);
5183 }
5184 return 0;
5185}
5186
5187 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005188ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5189{
5190 PyObject *lookup_dict;
5191 int r;
5192
5193 if (!(lookup_dict = PyDict_New()))
5194 return -1;
5195
5196 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5197 {
5198 tv->v_type = VAR_DICT;
5199 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5200 ++tv->vval.v_dict->dv_refcount;
5201 r = 0;
5202 }
5203 else if (PyDict_Check(obj))
5204 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
5205 else if (PyMapping_Check(obj))
5206 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5207 else
5208 {
5209 PyErr_SetString(PyExc_TypeError,
5210 _("unable to convert object to vim dictionary"));
5211 r = -1;
5212 }
5213 Py_DECREF(lookup_dict);
5214 return r;
5215}
5216
5217 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005218ConvertFromPyObject(PyObject *obj, typval_T *tv)
5219{
5220 PyObject *lookup_dict;
5221 int r;
5222
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005223 if (!(lookup_dict = PyDict_New()))
5224 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005225 r = _ConvertFromPyObject(obj, tv, lookup_dict);
5226 Py_DECREF(lookup_dict);
5227 return r;
5228}
5229
5230 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005231_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005232{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005233 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005234 {
5235 tv->v_type = VAR_DICT;
5236 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5237 ++tv->vval.v_dict->dv_refcount;
5238 }
5239 else if (obj->ob_type == &ListType)
5240 {
5241 tv->v_type = VAR_LIST;
5242 tv->vval.v_list = (((ListObject *)(obj))->list);
5243 ++tv->vval.v_list->lv_refcount;
5244 }
5245 else if (obj->ob_type == &FunctionType)
5246 {
5247 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5248 return -1;
5249
5250 tv->v_type = VAR_FUNC;
5251 func_ref(tv->vval.v_string);
5252 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005253 else if (PyBytes_Check(obj))
5254 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005255 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02005256
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005257 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
5258 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005259 if (result == NULL)
5260 return -1;
5261
5262 if (set_string_copy(result, tv) == -1)
5263 return -1;
5264
5265 tv->v_type = VAR_STRING;
5266 }
5267 else if (PyUnicode_Check(obj))
5268 {
5269 PyObject *bytes;
5270 char_u *result;
5271
Bram Moolenaardb913952012-06-29 12:54:53 +02005272 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
5273 if (bytes == NULL)
5274 return -1;
5275
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005276 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
5277 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005278 if (result == NULL)
5279 return -1;
5280
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005281 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005282 {
5283 Py_XDECREF(bytes);
5284 return -1;
5285 }
5286 Py_XDECREF(bytes);
5287
5288 tv->v_type = VAR_STRING;
5289 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005290#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005291 else if (PyInt_Check(obj))
5292 {
5293 tv->v_type = VAR_NUMBER;
5294 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
5295 }
5296#endif
5297 else if (PyLong_Check(obj))
5298 {
5299 tv->v_type = VAR_NUMBER;
5300 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
5301 }
5302 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005303 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005304#ifdef FEAT_FLOAT
5305 else if (PyFloat_Check(obj))
5306 {
5307 tv->v_type = VAR_FLOAT;
5308 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5309 }
5310#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005311 else if (PyObject_HasAttrString(obj, "keys"))
5312 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005313 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005314 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005315 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005316 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005317 else
5318 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02005319 PyErr_SetString(PyExc_TypeError,
5320 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005321 return -1;
5322 }
5323 return 0;
5324}
5325
5326 static PyObject *
5327ConvertToPyObject(typval_T *tv)
5328{
5329 if (tv == NULL)
5330 {
5331 PyErr_SetVim(_("NULL reference passed"));
5332 return NULL;
5333 }
5334 switch (tv->v_type)
5335 {
5336 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005337 return PyBytes_FromString(tv->vval.v_string == NULL
5338 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005339 case VAR_NUMBER:
5340 return PyLong_FromLong((long) tv->vval.v_number);
5341#ifdef FEAT_FLOAT
5342 case VAR_FLOAT:
5343 return PyFloat_FromDouble((double) tv->vval.v_float);
5344#endif
5345 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005346 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005347 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005348 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005349 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005350 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005351 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005352 case VAR_UNKNOWN:
5353 Py_INCREF(Py_None);
5354 return Py_None;
5355 default:
5356 PyErr_SetVim(_("internal error: invalid value type"));
5357 return NULL;
5358 }
5359}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005360
5361typedef struct
5362{
5363 PyObject_HEAD
5364} CurrentObject;
5365static PyTypeObject CurrentType;
5366
5367 static void
5368init_structs(void)
5369{
5370 vim_memset(&OutputType, 0, sizeof(OutputType));
5371 OutputType.tp_name = "vim.message";
5372 OutputType.tp_basicsize = sizeof(OutputObject);
5373 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5374 OutputType.tp_doc = "vim message object";
5375 OutputType.tp_methods = OutputMethods;
5376#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005377 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5378 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005379 OutputType.tp_alloc = call_PyType_GenericAlloc;
5380 OutputType.tp_new = call_PyType_GenericNew;
5381 OutputType.tp_free = call_PyObject_Free;
5382#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005383 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5384 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005385#endif
5386
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005387 vim_memset(&IterType, 0, sizeof(IterType));
5388 IterType.tp_name = "vim.iter";
5389 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005390 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005391 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005392 IterType.tp_iter = (getiterfunc)IterIter;
5393 IterType.tp_iternext = (iternextfunc)IterNext;
5394 IterType.tp_dealloc = (destructor)IterDestructor;
5395 IterType.tp_traverse = (traverseproc)IterTraverse;
5396 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005397
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005398 vim_memset(&BufferType, 0, sizeof(BufferType));
5399 BufferType.tp_name = "vim.buffer";
5400 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005401 BufferType.tp_dealloc = (destructor)BufferDestructor;
5402 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005403 BufferType.tp_as_sequence = &BufferAsSeq;
5404 BufferType.tp_as_mapping = &BufferAsMapping;
5405 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5406 BufferType.tp_doc = "vim buffer object";
5407 BufferType.tp_methods = BufferMethods;
5408#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005409 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005410 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005411 BufferType.tp_alloc = call_PyType_GenericAlloc;
5412 BufferType.tp_new = call_PyType_GenericNew;
5413 BufferType.tp_free = call_PyObject_Free;
5414#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005415 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005416 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005417#endif
5418
5419 vim_memset(&WindowType, 0, sizeof(WindowType));
5420 WindowType.tp_name = "vim.window";
5421 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005422 WindowType.tp_dealloc = (destructor)WindowDestructor;
5423 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005424 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005425 WindowType.tp_doc = "vim Window object";
5426 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005427 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5428 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005429#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005430 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5431 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005432 WindowType.tp_alloc = call_PyType_GenericAlloc;
5433 WindowType.tp_new = call_PyType_GenericNew;
5434 WindowType.tp_free = call_PyObject_Free;
5435#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005436 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5437 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005438#endif
5439
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005440 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5441 TabPageType.tp_name = "vim.tabpage";
5442 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005443 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5444 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005445 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5446 TabPageType.tp_doc = "vim tab page object";
5447 TabPageType.tp_methods = TabPageMethods;
5448#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005449 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005450 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5451 TabPageType.tp_new = call_PyType_GenericNew;
5452 TabPageType.tp_free = call_PyObject_Free;
5453#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005454 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005455#endif
5456
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005457 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5458 BufMapType.tp_name = "vim.bufferlist";
5459 BufMapType.tp_basicsize = sizeof(BufMapObject);
5460 BufMapType.tp_as_mapping = &BufMapAsMapping;
5461 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005462 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005463 BufferType.tp_doc = "vim buffer list";
5464
5465 vim_memset(&WinListType, 0, sizeof(WinListType));
5466 WinListType.tp_name = "vim.windowlist";
5467 WinListType.tp_basicsize = sizeof(WinListType);
5468 WinListType.tp_as_sequence = &WinListAsSeq;
5469 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5470 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005471 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005472
5473 vim_memset(&TabListType, 0, sizeof(TabListType));
5474 TabListType.tp_name = "vim.tabpagelist";
5475 TabListType.tp_basicsize = sizeof(TabListType);
5476 TabListType.tp_as_sequence = &TabListAsSeq;
5477 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5478 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005479
5480 vim_memset(&RangeType, 0, sizeof(RangeType));
5481 RangeType.tp_name = "vim.range";
5482 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005483 RangeType.tp_dealloc = (destructor)RangeDestructor;
5484 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005485 RangeType.tp_as_sequence = &RangeAsSeq;
5486 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005487 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005488 RangeType.tp_doc = "vim Range object";
5489 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005490 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5491 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005492#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005493 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005494 RangeType.tp_alloc = call_PyType_GenericAlloc;
5495 RangeType.tp_new = call_PyType_GenericNew;
5496 RangeType.tp_free = call_PyObject_Free;
5497#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005498 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005499#endif
5500
5501 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5502 CurrentType.tp_name = "vim.currentdata";
5503 CurrentType.tp_basicsize = sizeof(CurrentObject);
5504 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5505 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005506 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005507#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005508 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5509 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005510#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005511 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5512 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005513#endif
5514
5515 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5516 DictionaryType.tp_name = "vim.dictionary";
5517 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005518 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005519 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005520 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005521 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005522 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5523 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005524 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5525 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5526 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005527#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005528 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5529 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005530#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005531 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5532 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005533#endif
5534
5535 vim_memset(&ListType, 0, sizeof(ListType));
5536 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005537 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005538 ListType.tp_basicsize = sizeof(ListObject);
5539 ListType.tp_as_sequence = &ListAsSeq;
5540 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005541 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005542 ListType.tp_doc = "list pushing modifications to vim structure";
5543 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005544 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005545 ListType.tp_new = (newfunc)ListConstructor;
5546 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005547#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005548 ListType.tp_getattro = (getattrofunc)ListGetattro;
5549 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005550#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005551 ListType.tp_getattr = (getattrfunc)ListGetattr;
5552 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005553#endif
5554
5555 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005556 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005557 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005558 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5559 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005560 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005561 FunctionType.tp_doc = "object that calls vim function";
5562 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005563 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005564 FunctionType.tp_new = (newfunc)FunctionConstructor;
5565 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005566#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005567 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005568#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005569 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005570#endif
5571
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005572 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5573 OptionsType.tp_name = "vim.options";
5574 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005575 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005576 OptionsType.tp_doc = "object for manipulating options";
5577 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005578 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5579 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5580 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005581
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005582 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5583 LoaderType.tp_name = "vim.Loader";
5584 LoaderType.tp_basicsize = sizeof(LoaderObject);
5585 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5586 LoaderType.tp_doc = "vim message object";
5587 LoaderType.tp_methods = LoaderMethods;
5588 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5589
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005590#if PY_MAJOR_VERSION >= 3
5591 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5592 vimmodule.m_name = "vim";
5593 vimmodule.m_doc = "Vim Python interface\n";
5594 vimmodule.m_size = -1;
5595 vimmodule.m_methods = VimMethods;
5596#endif
5597}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005598
5599#define PYTYPE_READY(type) \
5600 if (PyType_Ready(&type)) \
5601 return -1;
5602
5603 static int
5604init_types()
5605{
5606 PYTYPE_READY(IterType);
5607 PYTYPE_READY(BufferType);
5608 PYTYPE_READY(RangeType);
5609 PYTYPE_READY(WindowType);
5610 PYTYPE_READY(TabPageType);
5611 PYTYPE_READY(BufMapType);
5612 PYTYPE_READY(WinListType);
5613 PYTYPE_READY(TabListType);
5614 PYTYPE_READY(CurrentType);
5615 PYTYPE_READY(DictionaryType);
5616 PYTYPE_READY(ListType);
5617 PYTYPE_READY(FunctionType);
5618 PYTYPE_READY(OptionsType);
5619 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005620 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005621 return 0;
5622}
5623
5624 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005625init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005626{
5627 PyObject *path;
5628 PyObject *path_hook;
5629 PyObject *path_hooks;
5630
5631 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5632 return -1;
5633
5634 if (!(path_hooks = PySys_GetObject("path_hooks")))
5635 {
5636 PyErr_Clear();
5637 path_hooks = PyList_New(1);
5638 PyList_SET_ITEM(path_hooks, 0, path_hook);
5639 if (PySys_SetObject("path_hooks", path_hooks))
5640 {
5641 Py_DECREF(path_hooks);
5642 return -1;
5643 }
5644 Py_DECREF(path_hooks);
5645 }
5646 else if (PyList_Check(path_hooks))
5647 {
5648 if (PyList_Append(path_hooks, path_hook))
5649 {
5650 Py_DECREF(path_hook);
5651 return -1;
5652 }
5653 Py_DECREF(path_hook);
5654 }
5655 else
5656 {
5657 VimTryStart();
5658 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5659 "You should now do the following:\n"
5660 "- append vim.path_hook to sys.path_hooks\n"
5661 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5662 VimTryEnd(); /* Discard the error */
5663 Py_DECREF(path_hook);
5664 return 0;
5665 }
5666
5667 if (!(path = PySys_GetObject("path")))
5668 {
5669 PyErr_Clear();
5670 path = PyList_New(1);
5671 Py_INCREF(vim_special_path_object);
5672 PyList_SET_ITEM(path, 0, vim_special_path_object);
5673 if (PySys_SetObject("path", path))
5674 {
5675 Py_DECREF(path);
5676 return -1;
5677 }
5678 Py_DECREF(path);
5679 }
5680 else if (PyList_Check(path))
5681 {
5682 if (PyList_Append(path, vim_special_path_object))
5683 return -1;
5684 }
5685 else
5686 {
5687 VimTryStart();
5688 EMSG(_("Failed to set path: sys.path is not a list\n"
5689 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5690 VimTryEnd(); /* Discard the error */
5691 }
5692
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005693 return 0;
5694}
5695
5696static BufMapObject TheBufferMap =
5697{
5698 PyObject_HEAD_INIT(&BufMapType)
5699};
5700
5701static WinListObject TheWindowList =
5702{
5703 PyObject_HEAD_INIT(&WinListType)
5704 NULL
5705};
5706
5707static CurrentObject TheCurrent =
5708{
5709 PyObject_HEAD_INIT(&CurrentType)
5710};
5711
5712static TabListObject TheTabPageList =
5713{
5714 PyObject_HEAD_INIT(&TabListType)
5715};
5716
5717static struct numeric_constant {
5718 char *name;
5719 int value;
5720} numeric_constants[] = {
5721 {"VAR_LOCKED", VAR_LOCKED},
5722 {"VAR_FIXED", VAR_FIXED},
5723 {"VAR_SCOPE", VAR_SCOPE},
5724 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5725};
5726
5727static struct object_constant {
5728 char *name;
5729 PyObject *value;
5730} object_constants[] = {
5731 {"buffers", (PyObject *)(void *)&TheBufferMap},
5732 {"windows", (PyObject *)(void *)&TheWindowList},
5733 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5734 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005735
5736 {"Buffer", (PyObject *)&BufferType},
5737 {"Range", (PyObject *)&RangeType},
5738 {"Window", (PyObject *)&WindowType},
5739 {"TabPage", (PyObject *)&TabPageType},
5740 {"Dictionary", (PyObject *)&DictionaryType},
5741 {"List", (PyObject *)&ListType},
5742 {"Function", (PyObject *)&FunctionType},
5743 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005744 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005745};
5746
5747typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005748typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005749
5750#define ADD_OBJECT(m, name, obj) \
5751 if (add_object(m, name, obj)) \
5752 return -1;
5753
5754#define ADD_CHECKED_OBJECT(m, name, obj) \
5755 { \
5756 PyObject *value = obj; \
5757 if (!value) \
5758 return -1; \
5759 ADD_OBJECT(m, name, value); \
5760 }
5761
5762 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005763populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005764{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005765 int i;
5766 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005767 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005768 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005769
5770 for (i = 0; i < (int)(sizeof(numeric_constants)
5771 / sizeof(struct numeric_constant));
5772 ++i)
5773 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5774 PyInt_FromLong(numeric_constants[i].value));
5775
5776 for (i = 0; i < (int)(sizeof(object_constants)
5777 / sizeof(struct object_constant));
5778 ++i)
5779 {
5780 PyObject *value;
5781
5782 value = object_constants[i].value;
5783 Py_INCREF(value);
5784 ADD_OBJECT(m, object_constants[i].name, value);
5785 }
5786
5787 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5788 return -1;
5789 ADD_OBJECT(m, "error", VimError);
5790
Bram Moolenaara9922d62013-05-30 13:01:18 +02005791 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5792 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005793 ADD_CHECKED_OBJECT(m, "options",
5794 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005795
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005796 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005797 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005798 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005799
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005800 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005801 return -1;
5802 ADD_OBJECT(m, "_getcwd", py_getcwd)
5803
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005804 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005805 return -1;
5806 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005807 if (!(attr = get_attr(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005808 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005809 if (PyObject_SetAttrString(other_module, "chdir", attr))
5810 {
5811 Py_DECREF(attr);
5812 return -1;
5813 }
5814 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005815
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005816 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005817 {
5818 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005819 if (!(attr = get_attr(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005820 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005821 if (PyObject_SetAttrString(other_module, "fchdir", attr))
5822 {
5823 Py_DECREF(attr);
5824 return -1;
5825 }
5826 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005827 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02005828 else
5829 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02005830
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005831 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
5832 return -1;
5833
5834 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
5835
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005836 if (!(imp = PyImport_ImportModule("imp")))
5837 return -1;
5838
5839 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
5840 {
5841 Py_DECREF(imp);
5842 return -1;
5843 }
5844
5845 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
5846 {
5847 Py_DECREF(py_find_module);
5848 Py_DECREF(imp);
5849 return -1;
5850 }
5851
5852 Py_DECREF(imp);
5853
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005854 ADD_OBJECT(m, "_find_module", py_find_module);
5855 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005856
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005857 return 0;
5858}