blob: 59bb0f56d35c07ab8f4980208e06aba7f5366c0e [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020016#if PY_VERSION_HEX < 0x02050000
17typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
18#endif
19
Bram Moolenaar91805fc2011-06-26 04:01:44 +020020#ifdef FEAT_MBYTE
21# define ENC_OPT p_enc
22#else
23# define ENC_OPT "latin1"
24#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020025#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020026
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020027#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
28
29#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
30#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020031#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020032
Bram Moolenaare9ba5162013-05-29 22:02:22 +020033#define DICTKEY_DECL \
Bram Moolenaara9922d62013-05-30 13:01:18 +020034 PyObject *dictkey_todecref = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +020035#define DICTKEY_CHECK_EMPTY(err) \
36 if (*key == NUL) \
37 { \
38 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
39 return err; \
40 }
41#define DICTKEY_SET_KEY (key = StringToChars(keyObject, &dictkey_todecref))
Bram Moolenaara03e6312013-05-29 22:49:26 +020042#define DICTKEY_GET(err, decref) \
Bram Moolenaar9bb77d62013-05-30 12:14:49 +020043 if (!DICTKEY_SET_KEY) \
Bram Moolenaara03e6312013-05-29 22:49:26 +020044 { \
45 if (decref) \
46 { \
47 Py_DECREF(keyObject); \
48 } \
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +020049 return err; \
Bram Moolenaara03e6312013-05-29 22:49:26 +020050 } \
51 if (decref && !dictkey_todecref) \
52 dictkey_todecref = keyObject; \
Bram Moolenaar9bb77d62013-05-30 12:14:49 +020053 DICTKEY_CHECK_EMPTY(err)
Bram Moolenaare9ba5162013-05-29 22:02:22 +020054#define DICTKEY_UNREF \
55 Py_XDECREF(dictkey_todecref);
56
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020057typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020058typedef void (*runner)(const char *, void *
59#ifdef PY_CAN_RECURSE
60 , PyGILState_STATE *
61#endif
62 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020063
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064static int ConvertFromPyObject(PyObject *, typval_T *);
65static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020066static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020067static PyObject *WindowNew(win_T *, tabpage_T *);
68static PyObject *BufferNew (buf_T *);
69static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020070
71static PyInt RangeStart;
72static PyInt RangeEnd;
73
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020074static PyObject *globals;
75
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020076/*
77 * obtain a lock on the Vim data structures
78 */
79 static void
80Python_Lock_Vim(void)
81{
82}
83
84/*
85 * release a lock on the Vim data structures
86 */
87 static void
88Python_Release_Vim(void)
89{
90}
91
Bram Moolenaare9ba5162013-05-29 22:02:22 +020092/*
93 * The "todecref" argument holds a pointer to PyObject * that must be
94 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
95 * was needed to generate returned value is object.
96 *
97 * Use Py_XDECREF to decrement reference count.
98 */
99 static char_u *
100StringToChars(PyObject *object, PyObject **todecref)
101{
102 char_u *p;
103 PyObject *bytes = NULL;
104
105 if (PyBytes_Check(object))
106 {
107
108 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
109 return NULL;
110 if (p == NULL)
111 return NULL;
112
113 *todecref = NULL;
114 }
115 else if (PyUnicode_Check(object))
116 {
117 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
118 if (bytes == NULL)
119 return NULL;
120
121 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
122 return NULL;
123 if (p == NULL)
124 return NULL;
125
126 *todecref = bytes;
127 }
128 else
129 {
130 PyErr_SetString(PyExc_TypeError, _("object must be string"));
131 return NULL;
132 }
133
134 return (char_u *) p;
135}
136
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200137 static int
138add_string(PyObject *list, char *s)
139{
140 PyObject *string;
141
142 if (!(string = PyString_FromString(s)))
143 return -1;
144 if (PyList_Append(list, string))
145 {
146 Py_DECREF(string);
147 return -1;
148 }
149
150 Py_DECREF(string);
151 return 0;
152}
153
154 static PyObject *
155ObjectDir(PyObject *self, char **attributes)
156{
157 PyMethodDef *method;
158 char **attr;
159 PyObject *r;
160
161 if (!(r = PyList_New(0)))
162 return NULL;
163
164 if (self)
165 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
166 if (add_string(r, (char *) method->ml_name))
167 {
168 Py_DECREF(r);
169 return NULL;
170 }
171
172 for (attr = attributes ; *attr ; ++attr)
173 if (add_string(r, *attr))
174 {
175 Py_DECREF(r);
176 return NULL;
177 }
178
179#if PY_MAJOR_VERSION < 3
180 if (add_string(r, "__members__"))
181 {
182 Py_DECREF(r);
183 return NULL;
184 }
185#endif
186
187 return r;
188}
189
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200190/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200191 */
192
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200193/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200194typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200195
196static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200197
198typedef struct
199{
200 PyObject_HEAD
201 long softspace;
202 long error;
203} OutputObject;
204
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200205static char *OutputAttrs[] = {
206 "softspace",
207 NULL
208};
209
210 static PyObject *
211OutputDir(PyObject *self)
212{
213 return ObjectDir(self, OutputAttrs);
214}
215
Bram Moolenaar77045652012-09-21 13:46:06 +0200216 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200217OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200218{
219 if (val == NULL)
220 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200221 PyErr_SetString(PyExc_AttributeError,
222 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200223 return -1;
224 }
225
226 if (strcmp(name, "softspace") == 0)
227 {
228 if (!PyInt_Check(val))
229 {
230 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
231 return -1;
232 }
233
Bram Moolenaard6e39182013-05-21 18:30:34 +0200234 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200235 return 0;
236 }
237
238 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
239 return -1;
240}
241
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200242/* Buffer IO, we write one whole line at a time. */
243static garray_T io_ga = {0, 0, 1, 80, NULL};
244static writefn old_fn = NULL;
245
246 static void
247PythonIO_Flush(void)
248{
249 if (old_fn != NULL && io_ga.ga_len > 0)
250 {
251 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
252 old_fn((char_u *)io_ga.ga_data);
253 }
254 io_ga.ga_len = 0;
255}
256
257 static void
258writer(writefn fn, char_u *str, PyInt n)
259{
260 char_u *ptr;
261
262 /* Flush when switching output function. */
263 if (fn != old_fn)
264 PythonIO_Flush();
265 old_fn = fn;
266
267 /* Write each NL separated line. Text after the last NL is kept for
268 * writing later. */
269 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
270 {
271 PyInt len = ptr - str;
272
273 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
274 break;
275
276 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
277 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
278 fn((char_u *)io_ga.ga_data);
279 str = ptr + 1;
280 n -= len + 1;
281 io_ga.ga_len = 0;
282 }
283
284 /* Put the remaining text into io_ga for later printing. */
285 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
286 {
287 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
288 io_ga.ga_len += (int)n;
289 }
290}
291
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200292 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200293OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200294{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200295 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200296 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200297 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200298
Bram Moolenaar27564802011-09-07 19:30:21 +0200299 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200300 return NULL;
301
302 Py_BEGIN_ALLOW_THREADS
303 Python_Lock_Vim();
304 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
305 Python_Release_Vim();
306 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200307 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200308
309 Py_INCREF(Py_None);
310 return Py_None;
311}
312
313 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200314OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200316 PyObject *seq;
317 PyObject *iterator;
318 PyObject *item;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200319 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200320
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200321 if (!PyArg_ParseTuple(args, "O", &seq))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200322 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200323
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200324 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200325 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200326
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200327 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200328 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200329 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200330 PyInt len;
331
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200332 if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
Bram Moolenaardb913952012-06-29 12:54:53 +0200333 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200334 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200335 Py_DECREF(iterator);
336 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200337 return NULL;
338 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200339 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200340
341 Py_BEGIN_ALLOW_THREADS
342 Python_Lock_Vim();
343 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
344 Python_Release_Vim();
345 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200346 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200347 }
348
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200349 Py_DECREF(iterator);
350
351 /* Iterator may have finished due to an exception */
352 if (PyErr_Occurred())
353 return NULL;
354
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200355 Py_INCREF(Py_None);
356 return Py_None;
357}
358
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100359 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200360OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100361{
362 /* do nothing */
363 Py_INCREF(Py_None);
364 return Py_None;
365}
366
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200367/***************/
368
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200369static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200370 /* name, function, calling, doc */
371 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
372 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
373 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200374 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200375 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200376};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200377
378static OutputObject Output =
379{
380 PyObject_HEAD_INIT(&OutputType)
381 0,
382 0
383};
384
385static OutputObject Error =
386{
387 PyObject_HEAD_INIT(&OutputType)
388 0,
389 1
390};
391
392 static int
393PythonIO_Init_io(void)
394{
395 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
396 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
397
398 if (PyErr_Occurred())
399 {
400 EMSG(_("E264: Python: Error initialising I/O objects"));
401 return -1;
402 }
403
404 return 0;
405}
406
407
408static PyObject *VimError;
409
410/* Check to see whether a Vim error has been reported, or a keyboard
411 * interrupt has been detected.
412 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200413
414 static void
415VimTryStart(void)
416{
417 ++trylevel;
418}
419
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200420 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200421VimTryEnd(void)
422{
423 --trylevel;
424 if (got_int)
425 {
426 PyErr_SetNone(PyExc_KeyboardInterrupt);
427 return 1;
428 }
429 else if (!did_throw)
430 return 0;
431 else if (PyErr_Occurred())
432 return 1;
433 else
434 {
435 PyErr_SetVim((char *) current_exception->value);
436 discard_current_exception();
437 return 1;
438 }
439}
440
441 static int
442VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443{
444 if (got_int)
445 {
446 PyErr_SetNone(PyExc_KeyboardInterrupt);
447 return 1;
448 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200449 return 0;
450}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200451
452/* Vim module - Implementation
453 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200454
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200455 static PyObject *
456VimCommand(PyObject *self UNUSED, PyObject *args)
457{
458 char *cmd;
459 PyObject *result;
460
461 if (!PyArg_ParseTuple(args, "s", &cmd))
462 return NULL;
463
464 PyErr_Clear();
465
466 Py_BEGIN_ALLOW_THREADS
467 Python_Lock_Vim();
468
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200469 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200470 do_cmdline_cmd((char_u *)cmd);
471 update_screen(VALID);
472
473 Python_Release_Vim();
474 Py_END_ALLOW_THREADS
475
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200476 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200477 result = NULL;
478 else
479 result = Py_None;
480
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200481
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200482 Py_XINCREF(result);
483 return result;
484}
485
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200486/*
487 * Function to translate a typval_T into a PyObject; this will recursively
488 * translate lists/dictionaries into their Python equivalents.
489 *
490 * The depth parameter is to avoid infinite recursion, set it to 1 when
491 * you call VimToPython.
492 */
493 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200494VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200495{
496 PyObject *result;
497 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200498 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200499
500 /* Avoid infinite recursion */
501 if (depth > 100)
502 {
503 Py_INCREF(Py_None);
504 result = Py_None;
505 return result;
506 }
507
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200508 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200509 * then and we can use it again. */
510 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
511 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
512 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200513 sprintf(ptrBuf, "%p",
514 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
515 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200516
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200517 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200518 {
519 Py_INCREF(result);
520 return result;
521 }
522 }
523
524 if (our_tv->v_type == VAR_STRING)
525 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200526 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200527 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200528 }
529 else if (our_tv->v_type == VAR_NUMBER)
530 {
531 char buf[NUMBUFLEN];
532
533 /* For backwards compatibility numbers are stored as strings. */
534 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200535 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200536 }
537# ifdef FEAT_FLOAT
538 else if (our_tv->v_type == VAR_FLOAT)
539 {
540 char buf[NUMBUFLEN];
541
542 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200543 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200544 }
545# endif
546 else if (our_tv->v_type == VAR_LIST)
547 {
548 list_T *list = our_tv->vval.v_list;
549 listitem_T *curr;
550
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200551 if (list == NULL)
552 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200553
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200554 if (!(result = PyList_New(0)))
555 return NULL;
556
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200557 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200558 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200559 Py_DECREF(result);
560 return NULL;
561 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200562
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200563 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
564 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200565 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200566 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200567 Py_DECREF(result);
568 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200569 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200570 if (PyList_Append(result, newObj))
571 {
572 Py_DECREF(newObj);
573 Py_DECREF(result);
574 return NULL;
575 }
576 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200577 }
578 }
579 else if (our_tv->v_type == VAR_DICT)
580 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200581
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200582 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
583 long_u todo = ht->ht_used;
584 hashitem_T *hi;
585 dictitem_T *di;
586 if (our_tv->vval.v_dict == NULL)
587 return NULL;
588
589 if (!(result = PyDict_New()))
590 return NULL;
591
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200592 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200593 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200594 Py_DECREF(result);
595 return NULL;
596 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200597
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200598 for (hi = ht->ht_array; todo > 0; ++hi)
599 {
600 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200601 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200602 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200603
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200604 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200605 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200606 {
607 Py_DECREF(result);
608 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200609 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200610 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
611 {
612 Py_DECREF(result);
613 Py_DECREF(newObj);
614 return NULL;
615 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200616 }
617 }
618 }
619 else
620 {
621 Py_INCREF(Py_None);
622 result = Py_None;
623 }
624
625 return result;
626}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200627
628 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200629VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200630{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200631 char *expr;
632 typval_T *our_tv;
633 PyObject *result;
634 PyObject *lookup_dict;
635
636 if (!PyArg_ParseTuple(args, "s", &expr))
637 return NULL;
638
639 Py_BEGIN_ALLOW_THREADS
640 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200641 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200642 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200643 Python_Release_Vim();
644 Py_END_ALLOW_THREADS
645
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200646 if (VimTryEnd())
647 return NULL;
648
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200649 if (our_tv == NULL)
650 {
651 PyErr_SetVim(_("invalid expression"));
652 return NULL;
653 }
654
655 /* Convert the Vim type into a Python type. Create a dictionary that's
656 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200657 if (!(lookup_dict = PyDict_New()))
658 result = NULL;
659 else
660 {
661 result = VimToPython(our_tv, 1, lookup_dict);
662 Py_DECREF(lookup_dict);
663 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200664
665
666 Py_BEGIN_ALLOW_THREADS
667 Python_Lock_Vim();
668 free_tv(our_tv);
669 Python_Release_Vim();
670 Py_END_ALLOW_THREADS
671
672 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200673}
674
Bram Moolenaardb913952012-06-29 12:54:53 +0200675static PyObject *ConvertToPyObject(typval_T *);
676
677 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200678VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200679{
Bram Moolenaardb913952012-06-29 12:54:53 +0200680 char *expr;
681 typval_T *our_tv;
682 PyObject *result;
683
684 if (!PyArg_ParseTuple(args, "s", &expr))
685 return NULL;
686
687 Py_BEGIN_ALLOW_THREADS
688 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200689 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200690 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200691 Python_Release_Vim();
692 Py_END_ALLOW_THREADS
693
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200694 if (VimTryEnd())
695 return NULL;
696
Bram Moolenaardb913952012-06-29 12:54:53 +0200697 if (our_tv == NULL)
698 {
699 PyErr_SetVim(_("invalid expression"));
700 return NULL;
701 }
702
703 result = ConvertToPyObject(our_tv);
704 Py_BEGIN_ALLOW_THREADS
705 Python_Lock_Vim();
706 free_tv(our_tv);
707 Python_Release_Vim();
708 Py_END_ALLOW_THREADS
709
710 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200711}
712
713 static PyObject *
714VimStrwidth(PyObject *self UNUSED, PyObject *args)
715{
716 char *expr;
717
718 if (!PyArg_ParseTuple(args, "s", &expr))
719 return NULL;
720
Bram Moolenaara54bf402012-12-05 16:30:07 +0100721 return PyLong_FromLong(
722#ifdef FEAT_MBYTE
723 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
724#else
725 STRLEN(expr)
726#endif
727 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200728}
729
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730/*
731 * Vim module - Definitions
732 */
733
734static struct PyMethodDef VimMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200735 /* name, function, calling, documentation */
736 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
737 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
738 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
739 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
740 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200741};
742
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200743/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200744 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745 */
746
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200747static PyTypeObject IterType;
748
749typedef PyObject *(*nextfun)(void **);
750typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200751typedef int (*traversefun)(void *, visitproc, void *);
752typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200753
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200754/* Main purpose of this object is removing the need for do python
755 * initialization (i.e. PyType_Ready and setting type attributes) for a big
756 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200757
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200758typedef struct
759{
760 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200761 void *cur;
762 nextfun next;
763 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200764 traversefun traverse;
765 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200766} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200767
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200768 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200769IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
770 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200772 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773
Bram Moolenaar774267b2013-05-21 20:51:59 +0200774 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200775 self->cur = start;
776 self->next = next;
777 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200778 self->traverse = traverse;
779 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200780
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200781 return (PyObject *)(self);
782}
783
784 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200785IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200786{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200787 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200788 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200789 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200790}
791
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200792 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200793IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200794{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200795 if (self->traverse != NULL)
796 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200797 else
798 return 0;
799}
800
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200801/* Mac OSX defines clear() somewhere. */
802#ifdef clear
803# undef clear
804#endif
805
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200806 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200807IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200808{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200809 if (self->clear != NULL)
810 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200811 else
812 return 0;
813}
814
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200815 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200816IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200817{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200818 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200819}
820
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200821 static PyObject *
822IterIter(PyObject *self)
823{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +0200824 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200825 return self;
826}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200827
Bram Moolenaardb913952012-06-29 12:54:53 +0200828typedef struct pylinkedlist_S {
829 struct pylinkedlist_S *pll_next;
830 struct pylinkedlist_S *pll_prev;
831 PyObject *pll_obj;
832} pylinkedlist_T;
833
834static pylinkedlist_T *lastdict = NULL;
835static pylinkedlist_T *lastlist = NULL;
836
837 static void
838pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
839{
840 if (ref->pll_prev == NULL)
841 {
842 if (ref->pll_next == NULL)
843 {
844 *last = NULL;
845 return;
846 }
847 }
848 else
849 ref->pll_prev->pll_next = ref->pll_next;
850
851 if (ref->pll_next == NULL)
852 *last = ref->pll_prev;
853 else
854 ref->pll_next->pll_prev = ref->pll_prev;
855}
856
857 static void
858pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
859{
860 if (*last == NULL)
861 ref->pll_prev = NULL;
862 else
863 {
864 (*last)->pll_next = ref;
865 ref->pll_prev = *last;
866 }
867 ref->pll_next = NULL;
868 ref->pll_obj = self;
869 *last = ref;
870}
871
872static PyTypeObject DictionaryType;
873
874typedef struct
875{
876 PyObject_HEAD
877 dict_T *dict;
878 pylinkedlist_T ref;
879} DictionaryObject;
880
Bram Moolenaara9922d62013-05-30 13:01:18 +0200881static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
882
883#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
884
Bram Moolenaardb913952012-06-29 12:54:53 +0200885 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +0200886DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +0200887{
888 DictionaryObject *self;
889
Bram Moolenaara9922d62013-05-30 13:01:18 +0200890 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200891 if (self == NULL)
892 return NULL;
893 self->dict = dict;
894 ++dict->dv_refcount;
895
896 pyll_add((PyObject *)(self), &self->ref, &lastdict);
897
898 return (PyObject *)(self);
899}
900
Bram Moolenaara9922d62013-05-30 13:01:18 +0200901 static dict_T *
902py_dict_alloc()
903{
904 dict_T *r;
905
906 if (!(r = dict_alloc()))
907 {
908 PyErr_NoMemory();
909 return NULL;
910 }
911 ++r->dv_refcount;
912
913 return r;
914}
915
916 static PyObject *
917DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
918{
919 DictionaryObject *self;
920 dict_T *dict;
921
922 if (!(dict = py_dict_alloc()))
923 return NULL;
924
925 self = (DictionaryObject *) DictionaryNew(subtype, dict);
926
927 --dict->dv_refcount;
928
929 if (kwargs || PyTuple_Size(args))
930 {
931 PyObject *tmp;
932 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
933 {
934 Py_DECREF(self);
935 return NULL;
936 }
937
938 Py_DECREF(tmp);
939 }
940
941 return (PyObject *)(self);
942}
943
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200944 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200945DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200946{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200947 pyll_remove(&self->ref, &lastdict);
948 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200949
950 DESTRUCTOR_FINISH(self);
951}
952
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200953static char *DictionaryAttrs[] = {
954 "locked", "scope",
955 NULL
956};
957
958 static PyObject *
959DictionaryDir(PyObject *self)
960{
961 return ObjectDir(self, DictionaryAttrs);
962}
963
Bram Moolenaardb913952012-06-29 12:54:53 +0200964 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200965DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200966{
967 if (val == NULL)
968 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200969 PyErr_SetString(PyExc_AttributeError,
970 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200971 return -1;
972 }
973
974 if (strcmp(name, "locked") == 0)
975 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200976 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200977 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200978 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200979 return -1;
980 }
981 else
982 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200983 int istrue = PyObject_IsTrue(val);
984 if (istrue == -1)
985 return -1;
986 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200987 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200988 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200989 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200990 }
991 return 0;
992 }
993 else
994 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200995 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200996 return -1;
997 }
998}
999
1000 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001001DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001002{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001003 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001004}
1005
Bram Moolenaara9922d62013-05-30 13:01:18 +02001006#define DICT_FLAG_HAS_DEFAULT 0x01
1007#define DICT_FLAG_POP 0x02
1008#define DICT_FLAG_NONE_DEFAULT 0x04
1009#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1010#define DICT_FLAG_RETURN_PAIR 0x10
1011
Bram Moolenaardb913952012-06-29 12:54:53 +02001012 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001013_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001014{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001015 PyObject *keyObject;
1016 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1017 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001018 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001019 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001020 dict_T *dict = self->dict;
1021 hashitem_T *hi;
1022
Bram Moolenaardb913952012-06-29 12:54:53 +02001023 DICTKEY_DECL
1024
Bram Moolenaara9922d62013-05-30 13:01:18 +02001025 if (flags & DICT_FLAG_HAS_DEFAULT)
1026 {
1027 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1028 return NULL;
1029 }
1030 else
1031 keyObject = args;
1032
1033 if (flags & DICT_FLAG_RETURN_BOOL)
1034 defObject = Py_False;
1035
Bram Moolenaara03e6312013-05-29 22:49:26 +02001036 DICTKEY_GET(NULL, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +02001037
Bram Moolenaara9922d62013-05-30 13:01:18 +02001038 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001039
Bram Moolenaar696c2112012-09-21 13:43:14 +02001040 DICTKEY_UNREF
1041
Bram Moolenaara9922d62013-05-30 13:01:18 +02001042 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001043 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001044 if (defObject)
1045 {
1046 Py_INCREF(defObject);
1047 return defObject;
1048 }
1049 else
1050 {
1051 PyErr_SetObject(PyExc_KeyError, keyObject);
1052 return NULL;
1053 }
1054 }
1055 else if (flags & DICT_FLAG_RETURN_BOOL)
1056 {
1057 Py_INCREF(Py_True);
1058 return Py_True;
1059 }
1060
1061 di = dict_lookup(hi);
1062
1063 if (!(r = ConvertToPyObject(&di->di_tv)))
1064 return NULL;
1065
1066 if (flags & DICT_FLAG_POP)
1067 {
1068 if (dict->dv_lock)
1069 {
1070 PyErr_SetVim(_("dict is locked"));
1071 Py_DECREF(r);
1072 return NULL;
1073 }
1074
1075 hash_remove(&dict->dv_hashtab, hi);
1076 dictitem_free(di);
1077 }
1078
1079 if (flags & DICT_FLAG_RETURN_PAIR)
1080 {
1081 PyObject *tmp = r;
1082
1083 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, tmp)))
1084 {
1085 Py_DECREF(tmp);
1086 return NULL;
1087 }
1088 }
1089
1090 return r;
1091}
1092
1093 static PyObject *
1094DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1095{
1096 return _DictionaryItem(self, keyObject, 0);
1097}
1098
1099 static int
1100DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1101{
1102 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1103 int r;
1104
1105 r = (rObj == Py_True);
1106
1107 Py_DECREF(Py_True);
1108
1109 return r;
1110}
1111
1112typedef struct
1113{
1114 hashitem_T *ht_array;
1115 long_u ht_used;
1116 hashtab_T *ht;
1117 hashitem_T *hi;
1118 int todo;
1119} dictiterinfo_T;
1120
1121 static PyObject *
1122DictionaryIterNext(dictiterinfo_T **dii)
1123{
1124 PyObject *r;
1125
1126 if (!(*dii)->todo)
1127 return NULL;
1128
1129 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1130 (*dii)->ht->ht_used != (*dii)->ht_used)
1131 {
1132 PyErr_SetString(PyExc_RuntimeError,
1133 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001134 return NULL;
1135 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001136
Bram Moolenaara9922d62013-05-30 13:01:18 +02001137 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1138 ++((*dii)->hi);
1139
1140 --((*dii)->todo);
1141
1142 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1143 return NULL;
1144
1145 return r;
1146}
1147
1148 static PyObject *
1149DictionaryIter(DictionaryObject *self)
1150{
1151 dictiterinfo_T *dii;
1152 hashtab_T *ht;
1153
1154 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1155 {
1156 PyErr_NoMemory();
1157 return NULL;
1158 }
1159
1160 ht = &self->dict->dv_hashtab;
1161 dii->ht_array = ht->ht_array;
1162 dii->ht_used = ht->ht_used;
1163 dii->ht = ht;
1164 dii->hi = dii->ht_array;
1165 dii->todo = dii->ht_used;
1166
1167 return IterNew(dii,
1168 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1169 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001170}
1171
1172 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001173DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001174{
1175 char_u *key;
1176 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001177 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001178 dictitem_T *di;
1179 DICTKEY_DECL
1180
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001181 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001182 {
1183 PyErr_SetVim(_("dict is locked"));
1184 return -1;
1185 }
1186
Bram Moolenaara03e6312013-05-29 22:49:26 +02001187 DICTKEY_GET(-1, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +02001188
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001189 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001190
1191 if (valObject == NULL)
1192 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001193 hashitem_T *hi;
1194
Bram Moolenaardb913952012-06-29 12:54:53 +02001195 if (di == NULL)
1196 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001197 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001198 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001199 return -1;
1200 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001201 hi = hash_find(&dict->dv_hashtab, di->di_key);
1202 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001203 dictitem_free(di);
1204 return 0;
1205 }
1206
1207 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001208 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001209
1210 if (di == NULL)
1211 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001212 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001213 {
1214 PyErr_NoMemory();
1215 return -1;
1216 }
1217 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001218 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001219
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001220 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001221 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001222 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +02001223 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001224 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001225 PyErr_SetVim(_("failed to add key to dictionary"));
1226 return -1;
1227 }
1228 }
1229 else
1230 clear_tv(&di->di_tv);
1231
1232 DICTKEY_UNREF
1233
1234 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001235 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001236 return 0;
1237}
1238
Bram Moolenaara9922d62013-05-30 13:01:18 +02001239typedef PyObject *(*hi_to_py)(hashitem_T *);
1240
Bram Moolenaardb913952012-06-29 12:54:53 +02001241 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001242DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001243{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001244 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001245 long_u todo = dict->dv_hashtab.ht_used;
1246 Py_ssize_t i = 0;
1247 PyObject *r;
1248 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001249 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001250
1251 r = PyList_New(todo);
1252 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1253 {
1254 if (!HASHITEM_EMPTY(hi))
1255 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001256 if (!(newObj = hiconvert(hi)))
1257 {
1258 Py_DECREF(r);
1259 return NULL;
1260 }
1261 if (PyList_SetItem(r, i, newObj))
1262 {
1263 Py_DECREF(r);
1264 Py_DECREF(newObj);
1265 return NULL;
1266 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001267 --todo;
1268 ++i;
1269 }
1270 }
1271 return r;
1272}
1273
Bram Moolenaara9922d62013-05-30 13:01:18 +02001274 static PyObject *
1275dict_key(hashitem_T *hi)
1276{
1277 return PyBytes_FromString((char *)(hi->hi_key));
1278}
1279
1280 static PyObject *
1281DictionaryListKeys(DictionaryObject *self)
1282{
1283 return DictionaryListObjects(self, dict_key);
1284}
1285
1286 static PyObject *
1287dict_val(hashitem_T *hi)
1288{
1289 dictitem_T *di;
1290
1291 di = dict_lookup(hi);
1292 return ConvertToPyObject(&di->di_tv);
1293}
1294
1295 static PyObject *
1296DictionaryListValues(DictionaryObject *self)
1297{
1298 return DictionaryListObjects(self, dict_val);
1299}
1300
1301 static PyObject *
1302dict_item(hashitem_T *hi)
1303{
1304 PyObject *keyObject;
1305 PyObject *valObject;
1306 PyObject *r;
1307
1308 if (!(keyObject = dict_key(hi)))
1309 return NULL;
1310
1311 if (!(valObject = dict_val(hi)))
1312 {
1313 Py_DECREF(keyObject);
1314 return NULL;
1315 }
1316
1317 r = Py_BuildValue("(OO)", keyObject, valObject);
1318
1319 Py_DECREF(keyObject);
1320 Py_DECREF(valObject);
1321
1322 return r;
1323}
1324
1325 static PyObject *
1326DictionaryListItems(DictionaryObject *self)
1327{
1328 return DictionaryListObjects(self, dict_item);
1329}
1330
1331 static PyObject *
1332DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1333{
1334 dict_T *dict = self->dict;
1335
1336 if (dict->dv_lock)
1337 {
1338 PyErr_SetVim(_("dict is locked"));
1339 return NULL;
1340 }
1341
1342 if (kwargs)
1343 {
1344 typval_T tv;
1345
1346 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1347 return NULL;
1348
1349 VimTryStart();
1350 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1351 clear_tv(&tv);
1352 if (VimTryEnd())
1353 return NULL;
1354 }
1355 else
1356 {
1357 PyObject *object;
1358
1359 if (!PyArg_Parse(args, "(O)", &object))
1360 return NULL;
1361
1362 if (PyObject_HasAttrString(object, "keys"))
1363 return DictionaryUpdate(self, NULL, object);
1364 else
1365 {
1366 PyObject *iterator;
1367 PyObject *item;
1368
1369 if (!(iterator = PyObject_GetIter(object)))
1370 return NULL;
1371
1372 while ((item = PyIter_Next(iterator)))
1373 {
1374 PyObject *fast;
1375 PyObject *keyObject;
1376 PyObject *valObject;
1377 PyObject *todecref;
1378 char_u *key;
1379 dictitem_T *di;
1380
1381 if (!(fast = PySequence_Fast(item, "")))
1382 {
1383 Py_DECREF(iterator);
1384 Py_DECREF(item);
1385 return NULL;
1386 }
1387
1388 Py_DECREF(item);
1389
1390 if (PySequence_Fast_GET_SIZE(fast) != 2)
1391 {
1392 Py_DECREF(iterator);
1393 Py_DECREF(fast);
1394 PyErr_SetString(PyExc_ValueError,
1395 _("expected sequence element of size 2"));
1396 return NULL;
1397 }
1398
1399 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1400
1401 if (!(key = StringToChars(keyObject, &todecref)))
1402 {
1403 Py_DECREF(iterator);
1404 Py_DECREF(fast);
1405 return NULL;
1406 }
1407
1408 di = dictitem_alloc(key);
1409
1410 Py_XDECREF(todecref);
1411
1412 if (di == NULL)
1413 {
1414 Py_DECREF(fast);
1415 Py_DECREF(iterator);
1416 PyErr_NoMemory();
1417 return NULL;
1418 }
1419 di->di_tv.v_lock = 0;
1420 di->di_tv.v_type = VAR_UNKNOWN;
1421
1422 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1423
1424 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1425 {
1426 Py_DECREF(iterator);
1427 Py_DECREF(fast);
1428 dictitem_free(di);
1429 return NULL;
1430 }
1431
1432 Py_DECREF(fast);
1433
1434 if (dict_add(dict, di) == FAIL)
1435 {
1436 Py_DECREF(iterator);
1437 dictitem_free(di);
1438 PyErr_SetVim(_("failed to add key to dictionary"));
1439 return NULL;
1440 }
1441 }
1442
1443 Py_DECREF(iterator);
1444
1445 /* Iterator may have finished due to an exception */
1446 if (PyErr_Occurred())
1447 return NULL;
1448 }
1449 }
1450 Py_INCREF(Py_None);
1451 return Py_None;
1452}
1453
1454 static PyObject *
1455DictionaryGet(DictionaryObject *self, PyObject *args)
1456{
1457 return _DictionaryItem(self, args,
1458 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1459}
1460
1461 static PyObject *
1462DictionaryPop(DictionaryObject *self, PyObject *args)
1463{
1464 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1465}
1466
1467 static PyObject *
1468DictionaryPopItem(DictionaryObject *self, PyObject *args)
1469{
1470 PyObject *keyObject;
1471
1472 if (!PyArg_ParseTuple(args, "O", &keyObject))
1473 return NULL;
1474
1475 return _DictionaryItem(self, keyObject,
1476 DICT_FLAG_POP|DICT_FLAG_RETURN_PAIR);
1477}
1478
1479 static PyObject *
1480DictionaryHasKey(DictionaryObject *self, PyObject *args)
1481{
1482 PyObject *keyObject;
1483
1484 if (!PyArg_ParseTuple(args, "O", &keyObject))
1485 return NULL;
1486
1487 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1488}
1489
1490static PySequenceMethods DictionaryAsSeq = {
1491 0, /* sq_length */
1492 0, /* sq_concat */
1493 0, /* sq_repeat */
1494 0, /* sq_item */
1495 0, /* sq_slice */
1496 0, /* sq_ass_item */
1497 0, /* sq_ass_slice */
1498 (objobjproc) DictionaryContains, /* sq_contains */
1499 0, /* sq_inplace_concat */
1500 0, /* sq_inplace_repeat */
1501};
1502
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001503static PyMappingMethods DictionaryAsMapping = {
1504 (lenfunc) DictionaryLength,
1505 (binaryfunc) DictionaryItem,
1506 (objobjargproc) DictionaryAssItem,
1507};
1508
Bram Moolenaardb913952012-06-29 12:54:53 +02001509static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001510 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001511 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1512 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1513 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1514 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1515 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
1516 {"popitem", (PyCFunction)DictionaryPopItem, METH_VARARGS, ""},
1517 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001518 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1519 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001520};
1521
1522static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001523static PySequenceMethods ListAsSeq;
1524static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001525
1526typedef struct
1527{
1528 PyObject_HEAD
1529 list_T *list;
1530 pylinkedlist_T ref;
1531} ListObject;
1532
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001533#define NEW_LIST(list) ListNew(&ListType, list)
1534
Bram Moolenaardb913952012-06-29 12:54:53 +02001535 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001536ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001537{
1538 ListObject *self;
1539
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001540 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001541 if (self == NULL)
1542 return NULL;
1543 self->list = list;
1544 ++list->lv_refcount;
1545
1546 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1547
1548 return (PyObject *)(self);
1549}
1550
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001551 static list_T *
1552py_list_alloc()
1553{
1554 list_T *r;
1555
1556 if (!(r = list_alloc()))
1557 {
1558 PyErr_NoMemory();
1559 return NULL;
1560 }
1561 ++r->lv_refcount;
1562
1563 return r;
1564}
1565
1566 static int
1567list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1568{
1569 PyObject *iterator;
1570 PyObject *item;
1571 listitem_T *li;
1572
1573 if (!(iterator = PyObject_GetIter(obj)))
1574 return -1;
1575
1576 while ((item = PyIter_Next(iterator)))
1577 {
1578 if (!(li = listitem_alloc()))
1579 {
1580 PyErr_NoMemory();
1581 Py_DECREF(item);
1582 Py_DECREF(iterator);
1583 return -1;
1584 }
1585 li->li_tv.v_lock = 0;
1586 li->li_tv.v_type = VAR_UNKNOWN;
1587
1588 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1589 {
1590 Py_DECREF(item);
1591 Py_DECREF(iterator);
1592 listitem_free(li);
1593 return -1;
1594 }
1595
1596 Py_DECREF(item);
1597
1598 list_append(l, li);
1599 }
1600
1601 Py_DECREF(iterator);
1602
1603 /* Iterator may have finished due to an exception */
1604 if (PyErr_Occurred())
1605 return -1;
1606
1607 return 0;
1608}
1609
1610 static PyObject *
1611ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1612{
1613 list_T *list;
1614 PyObject *obj = NULL;
1615
1616 if (kwargs)
1617 {
1618 PyErr_SetString(PyExc_TypeError,
1619 _("list constructor does not accept keyword arguments"));
1620 return NULL;
1621 }
1622
1623 if (!PyArg_ParseTuple(args, "|O", &obj))
1624 return NULL;
1625
1626 if (!(list = py_list_alloc()))
1627 return NULL;
1628
1629 if (obj)
1630 {
1631 PyObject *lookup_dict;
1632
1633 if (!(lookup_dict = PyDict_New()))
1634 {
1635 list_unref(list);
1636 return NULL;
1637 }
1638
1639 if (list_py_concat(list, obj, lookup_dict) == -1)
1640 {
1641 Py_DECREF(lookup_dict);
1642 list_unref(list);
1643 return NULL;
1644 }
1645
1646 Py_DECREF(lookup_dict);
1647 }
1648
1649 return ListNew(subtype, list);
1650}
1651
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001652 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001653ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001654{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001655 pyll_remove(&self->ref, &lastlist);
1656 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001657
1658 DESTRUCTOR_FINISH(self);
1659}
1660
Bram Moolenaardb913952012-06-29 12:54:53 +02001661 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001662ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001663{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001664 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001665}
1666
1667 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001668ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001669{
1670 listitem_T *li;
1671
Bram Moolenaard6e39182013-05-21 18:30:34 +02001672 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001673 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001674 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001675 return NULL;
1676 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001677 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001678 if (li == NULL)
1679 {
1680 PyErr_SetVim(_("internal error: failed to get vim list item"));
1681 return NULL;
1682 }
1683 return ConvertToPyObject(&li->li_tv);
1684}
1685
1686#define PROC_RANGE \
1687 if (last < 0) {\
1688 if (last < -size) \
1689 last = 0; \
1690 else \
1691 last += size; \
1692 } \
1693 if (first < 0) \
1694 first = 0; \
1695 if (first > size) \
1696 first = size; \
1697 if (last > size) \
1698 last = size;
1699
1700 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001701ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001702{
1703 PyInt i;
1704 PyInt size = ListLength(self);
1705 PyInt n;
1706 PyObject *list;
1707 int reversed = 0;
1708
1709 PROC_RANGE
1710 if (first >= last)
1711 first = last;
1712
1713 n = last-first;
1714 list = PyList_New(n);
1715 if (list == NULL)
1716 return NULL;
1717
1718 for (i = 0; i < n; ++i)
1719 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001720 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001721 if (item == NULL)
1722 {
1723 Py_DECREF(list);
1724 return NULL;
1725 }
1726
1727 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1728 {
1729 Py_DECREF(item);
1730 Py_DECREF(list);
1731 return NULL;
1732 }
1733 }
1734
1735 return list;
1736}
1737
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001738typedef struct
1739{
1740 listwatch_T lw;
1741 list_T *list;
1742} listiterinfo_T;
1743
1744 static void
1745ListIterDestruct(listiterinfo_T *lii)
1746{
1747 list_rem_watch(lii->list, &lii->lw);
1748 PyMem_Free(lii);
1749}
1750
1751 static PyObject *
1752ListIterNext(listiterinfo_T **lii)
1753{
1754 PyObject *r;
1755
1756 if (!((*lii)->lw.lw_item))
1757 return NULL;
1758
1759 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1760 return NULL;
1761
1762 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1763
1764 return r;
1765}
1766
1767 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001768ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001769{
1770 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001771 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001772
1773 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1774 {
1775 PyErr_NoMemory();
1776 return NULL;
1777 }
1778
1779 list_add_watch(l, &lii->lw);
1780 lii->lw.lw_item = l->lv_first;
1781 lii->list = l;
1782
1783 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001784 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1785 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001786}
1787
Bram Moolenaardb913952012-06-29 12:54:53 +02001788 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001789ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001790{
1791 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001792 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001793 listitem_T *li;
1794 Py_ssize_t length = ListLength(self);
1795
1796 if (l->lv_lock)
1797 {
1798 PyErr_SetVim(_("list is locked"));
1799 return -1;
1800 }
1801 if (index>length || (index==length && obj==NULL))
1802 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001803 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001804 return -1;
1805 }
1806
1807 if (obj == NULL)
1808 {
1809 li = list_find(l, (long) index);
1810 list_remove(l, li, li);
1811 clear_tv(&li->li_tv);
1812 vim_free(li);
1813 return 0;
1814 }
1815
1816 if (ConvertFromPyObject(obj, &tv) == -1)
1817 return -1;
1818
1819 if (index == length)
1820 {
1821 if (list_append_tv(l, &tv) == FAIL)
1822 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001823 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001824 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001825 return -1;
1826 }
1827 }
1828 else
1829 {
1830 li = list_find(l, (long) index);
1831 clear_tv(&li->li_tv);
1832 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001833 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001834 }
1835 return 0;
1836}
1837
1838 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001839ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001840{
1841 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001842 PyObject *iterator;
1843 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02001844 listitem_T *li;
1845 listitem_T *next;
1846 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001847 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001848 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02001849
1850 if (l->lv_lock)
1851 {
1852 PyErr_SetVim(_("list is locked"));
1853 return -1;
1854 }
1855
1856 PROC_RANGE
1857
1858 if (first == size)
1859 li = NULL;
1860 else
1861 {
1862 li = list_find(l, (long) first);
1863 if (li == NULL)
1864 {
1865 PyErr_SetVim(_("internal error: no vim list item"));
1866 return -1;
1867 }
1868 if (last > first)
1869 {
1870 i = last - first;
1871 while (i-- && li != NULL)
1872 {
1873 next = li->li_next;
1874 listitem_remove(l, li);
1875 li = next;
1876 }
1877 }
1878 }
1879
1880 if (obj == NULL)
1881 return 0;
1882
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001883 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001884 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001885
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001886 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001887 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001888 if (ConvertFromPyObject(item, &v) == -1)
1889 {
1890 Py_DECREF(iterator);
1891 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001892 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001893 }
1894 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001895 if (list_insert_tv(l, &v, li) == FAIL)
1896 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001897 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001898 PyErr_SetVim(_("internal error: failed to add item to list"));
1899 return -1;
1900 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001901 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001902 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001903 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02001904 return 0;
1905}
1906
1907 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001908ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001909{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001910 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001911 PyObject *lookup_dict;
1912
1913 if (l->lv_lock)
1914 {
1915 PyErr_SetVim(_("list is locked"));
1916 return NULL;
1917 }
1918
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001919 if (!(lookup_dict = PyDict_New()))
1920 return NULL;
1921
Bram Moolenaardb913952012-06-29 12:54:53 +02001922 if (list_py_concat(l, obj, lookup_dict) == -1)
1923 {
1924 Py_DECREF(lookup_dict);
1925 return NULL;
1926 }
1927 Py_DECREF(lookup_dict);
1928
1929 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001930 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001931}
1932
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001933static char *ListAttrs[] = {
1934 "locked",
1935 NULL
1936};
1937
1938 static PyObject *
1939ListDir(PyObject *self)
1940{
1941 return ObjectDir(self, ListAttrs);
1942}
1943
Bram Moolenaar66b79852012-09-21 14:00:35 +02001944 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001945ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001946{
1947 if (val == NULL)
1948 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001949 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001950 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001951 return -1;
1952 }
1953
1954 if (strcmp(name, "locked") == 0)
1955 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001956 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001957 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001958 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001959 return -1;
1960 }
1961 else
1962 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001963 int istrue = PyObject_IsTrue(val);
1964 if (istrue == -1)
1965 return -1;
1966 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001967 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001968 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001969 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001970 }
1971 return 0;
1972 }
1973 else
1974 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001975 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001976 return -1;
1977 }
1978}
1979
Bram Moolenaardb913952012-06-29 12:54:53 +02001980static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001981 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1982 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
1983 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001984};
1985
1986typedef struct
1987{
1988 PyObject_HEAD
1989 char_u *name;
1990} FunctionObject;
1991
1992static PyTypeObject FunctionType;
1993
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02001994#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
1995
Bram Moolenaardb913952012-06-29 12:54:53 +02001996 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02001997FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02001998{
1999 FunctionObject *self;
2000
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002001 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2002
Bram Moolenaardb913952012-06-29 12:54:53 +02002003 if (self == NULL)
2004 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002005
2006 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002007 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002008 if (!translated_function_exists(name))
2009 {
2010 PyErr_SetString(PyExc_ValueError,
2011 _("unnamed function does not exist"));
2012 return NULL;
2013 }
2014 self->name = vim_strsave(name);
2015 func_ref(self->name);
2016 }
2017 else
2018 {
2019 self->name = get_expanded_name(name, TRUE);
2020 if (self->name == NULL)
2021 {
2022 if (script_autoload(name, TRUE) && !aborting())
2023 self->name = get_expanded_name(name, TRUE);
2024 if (self->name == NULL)
2025 {
2026 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2027 return NULL;
2028 }
2029 }
2030 }
2031
2032 return (PyObject *)(self);
2033}
2034
2035 static PyObject *
2036FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2037{
2038 PyObject *self;
2039 char_u *name;
2040
2041 if (kwargs)
2042 {
2043 PyErr_SetString(PyExc_TypeError,
2044 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002045 return NULL;
2046 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002047
2048 if (!PyArg_ParseTuple(args, "s", &name))
2049 return NULL;
2050
2051 self = FunctionNew(subtype, name);
2052
2053 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002054}
2055
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002056 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002057FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002058{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002059 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002060 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002061
2062 DESTRUCTOR_FINISH(self);
2063}
2064
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002065static char *FunctionAttrs[] = {
2066 "softspace",
2067 NULL
2068};
2069
2070 static PyObject *
2071FunctionDir(PyObject *self)
2072{
2073 return ObjectDir(self, FunctionAttrs);
2074}
2075
Bram Moolenaardb913952012-06-29 12:54:53 +02002076 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002077FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002078{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002079 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002080 typval_T args;
2081 typval_T selfdicttv;
2082 typval_T rettv;
2083 dict_T *selfdict = NULL;
2084 PyObject *selfdictObject;
2085 PyObject *result;
2086 int error;
2087
2088 if (ConvertFromPyObject(argsObject, &args) == -1)
2089 return NULL;
2090
2091 if (kwargs != NULL)
2092 {
2093 selfdictObject = PyDict_GetItemString(kwargs, "self");
2094 if (selfdictObject != NULL)
2095 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002096 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002097 {
2098 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002099 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002100 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002101 selfdict = selfdicttv.vval.v_dict;
2102 }
2103 }
2104
Bram Moolenaar71700b82013-05-15 17:49:05 +02002105 Py_BEGIN_ALLOW_THREADS
2106 Python_Lock_Vim();
2107
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002108 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002109 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002110
2111 Python_Release_Vim();
2112 Py_END_ALLOW_THREADS
2113
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002114 if (VimTryEnd())
2115 result = NULL;
2116 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002117 {
2118 result = NULL;
2119 PyErr_SetVim(_("failed to run function"));
2120 }
2121 else
2122 result = ConvertToPyObject(&rettv);
2123
Bram Moolenaardb913952012-06-29 12:54:53 +02002124 clear_tv(&args);
2125 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002126 if (selfdict != NULL)
2127 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002128
2129 return result;
2130}
2131
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002132 static PyObject *
2133FunctionRepr(FunctionObject *self)
2134{
2135 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2136}
2137
Bram Moolenaardb913952012-06-29 12:54:53 +02002138static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002139 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2140 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002141};
2142
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002143/*
2144 * Options object
2145 */
2146
2147static PyTypeObject OptionsType;
2148
2149typedef int (*checkfun)(void *);
2150
2151typedef struct
2152{
2153 PyObject_HEAD
2154 int opt_type;
2155 void *from;
2156 checkfun Check;
2157 PyObject *fromObj;
2158} OptionsObject;
2159
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002160 static int
2161dummy_check(void *arg UNUSED)
2162{
2163 return 0;
2164}
2165
2166 static PyObject *
2167OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2168{
2169 OptionsObject *self;
2170
Bram Moolenaar774267b2013-05-21 20:51:59 +02002171 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002172 if (self == NULL)
2173 return NULL;
2174
2175 self->opt_type = opt_type;
2176 self->from = from;
2177 self->Check = Check;
2178 self->fromObj = fromObj;
2179 if (fromObj)
2180 Py_INCREF(fromObj);
2181
2182 return (PyObject *)(self);
2183}
2184
2185 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002186OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002187{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002188 PyObject_GC_UnTrack((void *)(self));
2189 Py_XDECREF(self->fromObj);
2190 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002191}
2192
2193 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002194OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002195{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002196 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002197 return 0;
2198}
2199
2200 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002201OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002202{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002203 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002204 return 0;
2205}
2206
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002207 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002208OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002209{
2210 char_u *key;
2211 int flags;
2212 long numval;
2213 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02002214 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002215
Bram Moolenaard6e39182013-05-21 18:30:34 +02002216 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002217 return NULL;
2218
Bram Moolenaara03e6312013-05-29 22:49:26 +02002219 DICTKEY_GET(NULL, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002220
2221 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002222 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002223
2224 DICTKEY_UNREF
2225
2226 if (flags == 0)
2227 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002228 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002229 return NULL;
2230 }
2231
2232 if (flags & SOPT_UNSET)
2233 {
2234 Py_INCREF(Py_None);
2235 return Py_None;
2236 }
2237 else if (flags & SOPT_BOOL)
2238 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002239 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002240 r = numval ? Py_True : Py_False;
2241 Py_INCREF(r);
2242 return r;
2243 }
2244 else if (flags & SOPT_NUM)
2245 return PyInt_FromLong(numval);
2246 else if (flags & SOPT_STRING)
2247 {
2248 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002249 {
2250 PyObject *r = PyBytes_FromString((char *) stringval);
2251 vim_free(stringval);
2252 return r;
2253 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002254 else
2255 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002256 PyErr_SetString(PyExc_RuntimeError,
2257 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002258 return NULL;
2259 }
2260 }
2261 else
2262 {
2263 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2264 return NULL;
2265 }
2266}
2267
2268 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002269set_option_value_err(key, numval, stringval, opt_flags)
2270 char_u *key;
2271 int numval;
2272 char_u *stringval;
2273 int opt_flags;
2274{
2275 char_u *errmsg;
2276
2277 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2278 {
2279 if (VimTryEnd())
2280 return FAIL;
2281 PyErr_SetVim((char *)errmsg);
2282 return FAIL;
2283 }
2284 return OK;
2285}
2286
2287 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002288set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2289 char_u *key;
2290 int numval;
2291 char_u *stringval;
2292 int opt_flags;
2293 int opt_type;
2294 void *from;
2295{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002296 win_T *save_curwin = NULL;
2297 tabpage_T *save_curtab = NULL;
2298 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002299 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002300
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002301 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002302 switch (opt_type)
2303 {
2304 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002305 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2306 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002307 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002308 if (VimTryEnd())
2309 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002310 PyErr_SetVim("Problem while switching windows.");
2311 return -1;
2312 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002313 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002314 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002315 if (r == FAIL)
2316 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002317 break;
2318 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002319 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002320 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002321 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002322 if (r == FAIL)
2323 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002324 break;
2325 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002326 r = set_option_value_err(key, numval, stringval, opt_flags);
2327 if (r == FAIL)
2328 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002329 break;
2330 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002331 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002332}
2333
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002334 static void *
2335py_memsave(void *p, size_t len)
2336{
2337 void *r;
2338 if (!(r = PyMem_Malloc(len)))
2339 return NULL;
2340 mch_memmove(r, p, len);
2341 return r;
2342}
2343
2344#define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1))
2345
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002346 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002347OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002348{
2349 char_u *key;
2350 int flags;
2351 int opt_flags;
2352 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02002353 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002354
Bram Moolenaard6e39182013-05-21 18:30:34 +02002355 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002356 return -1;
2357
Bram Moolenaara03e6312013-05-29 22:49:26 +02002358 DICTKEY_GET(-1, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002359
2360 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002361 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002362
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002363 if (flags == 0)
2364 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002365 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002366 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002367 return -1;
2368 }
2369
2370 if (valObject == NULL)
2371 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002372 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002373 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002374 PyErr_SetString(PyExc_ValueError,
2375 _("unable to unset global option"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002376 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002377 return -1;
2378 }
2379 else if (!(flags & SOPT_GLOBAL))
2380 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002381 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2382 "without global value"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002383 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002384 return -1;
2385 }
2386 else
2387 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002388 unset_global_local_option(key, self->from);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002389 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002390 return 0;
2391 }
2392 }
2393
Bram Moolenaard6e39182013-05-21 18:30:34 +02002394 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002395
2396 if (flags & SOPT_BOOL)
2397 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002398 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002399
Bram Moolenaarb983f752013-05-15 16:11:50 +02002400 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002401 r = -1;
2402 else
2403 r = set_option_value_for(key, istrue, NULL,
2404 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002405 }
2406 else if (flags & SOPT_NUM)
2407 {
2408 int val;
2409
2410#if PY_MAJOR_VERSION < 3
2411 if (PyInt_Check(valObject))
2412 val = PyInt_AsLong(valObject);
2413 else
2414#endif
2415 if (PyLong_Check(valObject))
2416 val = PyLong_AsLong(valObject);
2417 else
2418 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002419 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002420 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002421 return -1;
2422 }
2423
2424 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002425 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002426 }
2427 else
2428 {
2429 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002430 PyObject *todecref;
2431
2432 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002433 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002434 r = set_option_value_for(key, 0, val, opt_flags,
2435 self->opt_type, self->from);
2436 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002437 }
2438 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002439 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002440 }
2441
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002442 DICTKEY_UNREF
2443
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002444 return r;
2445}
2446
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002447static PyMappingMethods OptionsAsMapping = {
2448 (lenfunc) NULL,
2449 (binaryfunc) OptionsItem,
2450 (objobjargproc) OptionsAssItem,
2451};
2452
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002453/* Tabpage object
2454 */
2455
2456typedef struct
2457{
2458 PyObject_HEAD
2459 tabpage_T *tab;
2460} TabPageObject;
2461
2462static PyObject *WinListNew(TabPageObject *tabObject);
2463
2464static PyTypeObject TabPageType;
2465
2466 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002467CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002468{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002469 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002470 {
2471 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2472 return -1;
2473 }
2474
2475 return 0;
2476}
2477
2478 static PyObject *
2479TabPageNew(tabpage_T *tab)
2480{
2481 TabPageObject *self;
2482
2483 if (TAB_PYTHON_REF(tab))
2484 {
2485 self = TAB_PYTHON_REF(tab);
2486 Py_INCREF(self);
2487 }
2488 else
2489 {
2490 self = PyObject_NEW(TabPageObject, &TabPageType);
2491 if (self == NULL)
2492 return NULL;
2493 self->tab = tab;
2494 TAB_PYTHON_REF(tab) = self;
2495 }
2496
2497 return (PyObject *)(self);
2498}
2499
2500 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002501TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002502{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002503 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2504 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002505
2506 DESTRUCTOR_FINISH(self);
2507}
2508
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002509static char *TabPageAttrs[] = {
2510 "windows", "number", "vars", "window", "valid",
2511 NULL
2512};
2513
2514 static PyObject *
2515TabPageDir(PyObject *self)
2516{
2517 return ObjectDir(self, TabPageAttrs);
2518}
2519
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002520 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002521TabPageAttrValid(TabPageObject *self, char *name)
2522{
2523 PyObject *r;
2524
2525 if (strcmp(name, "valid") != 0)
2526 return NULL;
2527
2528 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2529 Py_INCREF(r);
2530 return r;
2531}
2532
2533 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002534TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002535{
2536 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002537 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002538 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002539 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002540 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002541 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002542 else if (strcmp(name, "window") == 0)
2543 {
2544 /* For current tab window.c does not bother to set or update tp_curwin
2545 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002546 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002547 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002548 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002549 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002550 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002551 else if (strcmp(name, "__members__") == 0)
2552 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002553 return NULL;
2554}
2555
2556 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002557TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002558{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002559 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002560 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002561 else
2562 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002563 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002564
2565 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002566 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2567 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002568 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002569 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002570 }
2571}
2572
2573static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002574 /* name, function, calling, documentation */
2575 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2576 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002577};
2578
2579/*
2580 * Window list object
2581 */
2582
2583static PyTypeObject TabListType;
2584static PySequenceMethods TabListAsSeq;
2585
2586typedef struct
2587{
2588 PyObject_HEAD
2589} TabListObject;
2590
2591 static PyInt
2592TabListLength(PyObject *self UNUSED)
2593{
2594 tabpage_T *tp = first_tabpage;
2595 PyInt n = 0;
2596
2597 while (tp != NULL)
2598 {
2599 ++n;
2600 tp = tp->tp_next;
2601 }
2602
2603 return n;
2604}
2605
2606 static PyObject *
2607TabListItem(PyObject *self UNUSED, PyInt n)
2608{
2609 tabpage_T *tp;
2610
2611 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2612 if (n == 0)
2613 return TabPageNew(tp);
2614
2615 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2616 return NULL;
2617}
2618
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002619/* Window object
2620 */
2621
2622typedef struct
2623{
2624 PyObject_HEAD
2625 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002626 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002627} WindowObject;
2628
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002629static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002630
2631 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002632CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002633{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002634 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002635 {
2636 PyErr_SetVim(_("attempt to refer to deleted window"));
2637 return -1;
2638 }
2639
2640 return 0;
2641}
2642
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002643 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002644WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002645{
2646 /* We need to handle deletion of windows underneath us.
2647 * If we add a "w_python*_ref" field to the win_T structure,
2648 * then we can get at it in win_free() in vim. We then
2649 * need to create only ONE Python object per window - if
2650 * we try to create a second, just INCREF the existing one
2651 * and return it. The (single) Python object referring to
2652 * the window is stored in "w_python*_ref".
2653 * On a win_free() we set the Python object's win_T* field
2654 * to an invalid value. We trap all uses of a window
2655 * object, and reject them if the win_T* field is invalid.
2656 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002657 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002658 * w_python_ref and w_python3_ref fields respectively.
2659 */
2660
2661 WindowObject *self;
2662
2663 if (WIN_PYTHON_REF(win))
2664 {
2665 self = WIN_PYTHON_REF(win);
2666 Py_INCREF(self);
2667 }
2668 else
2669 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002670 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002671 if (self == NULL)
2672 return NULL;
2673 self->win = win;
2674 WIN_PYTHON_REF(win) = self;
2675 }
2676
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002677 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2678
Bram Moolenaar971db462013-05-12 18:44:48 +02002679 return (PyObject *)(self);
2680}
2681
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002682 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002683WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002684{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002685 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002686 if (self->win && self->win != INVALID_WINDOW_VALUE)
2687 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002688 Py_XDECREF(((PyObject *)(self->tabObject)));
2689 PyObject_GC_Del((void *)(self));
2690}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002691
Bram Moolenaar774267b2013-05-21 20:51:59 +02002692 static int
2693WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2694{
2695 Py_VISIT(((PyObject *)(self->tabObject)));
2696 return 0;
2697}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002698
Bram Moolenaar774267b2013-05-21 20:51:59 +02002699 static int
2700WindowClear(WindowObject *self)
2701{
2702 Py_CLEAR(self->tabObject);
2703 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002704}
2705
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002706 static win_T *
2707get_firstwin(TabPageObject *tabObject)
2708{
2709 if (tabObject)
2710 {
2711 if (CheckTabPage(tabObject))
2712 return NULL;
2713 /* For current tab window.c does not bother to set or update tp_firstwin
2714 */
2715 else if (tabObject->tab == curtab)
2716 return firstwin;
2717 else
2718 return tabObject->tab->tp_firstwin;
2719 }
2720 else
2721 return firstwin;
2722}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002723static char *WindowAttrs[] = {
2724 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2725 "tabpage", "valid",
2726 NULL
2727};
2728
2729 static PyObject *
2730WindowDir(PyObject *self)
2731{
2732 return ObjectDir(self, WindowAttrs);
2733}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002734
Bram Moolenaar971db462013-05-12 18:44:48 +02002735 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002736WindowAttrValid(WindowObject *self, char *name)
2737{
2738 PyObject *r;
2739
2740 if (strcmp(name, "valid") != 0)
2741 return NULL;
2742
2743 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2744 Py_INCREF(r);
2745 return r;
2746}
2747
2748 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002749WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002750{
2751 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002752 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002753 else if (strcmp(name, "cursor") == 0)
2754 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002755 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002756
2757 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2758 }
2759 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002760 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002761#ifdef FEAT_WINDOWS
2762 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002763 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002764#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002765#ifdef FEAT_VERTSPLIT
2766 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002767 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002768 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002769 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002770#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002771 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002772 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002773 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002774 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2775 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002776 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002777 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002778 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002779 return NULL;
2780 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002781 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002782 }
2783 else if (strcmp(name, "tabpage") == 0)
2784 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002785 Py_INCREF(self->tabObject);
2786 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002787 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002788 else if (strcmp(name, "__members__") == 0)
2789 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002790 else
2791 return NULL;
2792}
2793
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002794 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002795WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002796{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002797 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002798 return -1;
2799
2800 if (strcmp(name, "buffer") == 0)
2801 {
2802 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2803 return -1;
2804 }
2805 else if (strcmp(name, "cursor") == 0)
2806 {
2807 long lnum;
2808 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002809
2810 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2811 return -1;
2812
Bram Moolenaard6e39182013-05-21 18:30:34 +02002813 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002814 {
2815 PyErr_SetVim(_("cursor position outside buffer"));
2816 return -1;
2817 }
2818
2819 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002820 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002821 return -1;
2822
Bram Moolenaard6e39182013-05-21 18:30:34 +02002823 self->win->w_cursor.lnum = lnum;
2824 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002825#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002826 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002827#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002828 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002829 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002830
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002831 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002832 return 0;
2833 }
2834 else if (strcmp(name, "height") == 0)
2835 {
2836 int height;
2837 win_T *savewin;
2838
2839 if (!PyArg_Parse(val, "i", &height))
2840 return -1;
2841
2842#ifdef FEAT_GUI
2843 need_mouse_correct = TRUE;
2844#endif
2845 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002846 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002847
2848 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002849 win_setheight(height);
2850 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002851 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002852 return -1;
2853
2854 return 0;
2855 }
2856#ifdef FEAT_VERTSPLIT
2857 else if (strcmp(name, "width") == 0)
2858 {
2859 int width;
2860 win_T *savewin;
2861
2862 if (!PyArg_Parse(val, "i", &width))
2863 return -1;
2864
2865#ifdef FEAT_GUI
2866 need_mouse_correct = TRUE;
2867#endif
2868 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002869 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002870
2871 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002872 win_setwidth(width);
2873 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002874 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002875 return -1;
2876
2877 return 0;
2878 }
2879#endif
2880 else
2881 {
2882 PyErr_SetString(PyExc_AttributeError, name);
2883 return -1;
2884 }
2885}
2886
2887 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002888WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002889{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002890 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002891 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002892 else
2893 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002894 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002895
Bram Moolenaar6d216452013-05-12 19:00:41 +02002896 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002897 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002898 (self));
2899 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002900 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002901 }
2902}
2903
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002904static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002905 /* name, function, calling, documentation */
2906 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2907 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002908};
2909
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002910/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002911 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002912 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002913
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002914static PyTypeObject WinListType;
2915static PySequenceMethods WinListAsSeq;
2916
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002917typedef struct
2918{
2919 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002920 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002921} WinListObject;
2922
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002923 static PyObject *
2924WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002925{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002926 WinListObject *self;
2927
2928 self = PyObject_NEW(WinListObject, &WinListType);
2929 self->tabObject = tabObject;
2930 Py_INCREF(tabObject);
2931
2932 return (PyObject *)(self);
2933}
2934
2935 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002936WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002937{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002938 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002939
2940 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02002941 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002942 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02002943 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002944
2945 DESTRUCTOR_FINISH(self);
2946}
2947
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002948 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002949WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002950{
2951 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002952 PyInt n = 0;
2953
Bram Moolenaard6e39182013-05-21 18:30:34 +02002954 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002955 return -1;
2956
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002957 while (w != NULL)
2958 {
2959 ++n;
2960 w = W_NEXT(w);
2961 }
2962
2963 return n;
2964}
2965
2966 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002967WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002968{
2969 win_T *w;
2970
Bram Moolenaard6e39182013-05-21 18:30:34 +02002971 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002972 return NULL;
2973
2974 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002975 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002976 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002977
2978 PyErr_SetString(PyExc_IndexError, _("no such window"));
2979 return NULL;
2980}
2981
2982/* Convert a Python string into a Vim line.
2983 *
2984 * The result is in allocated memory. All internal nulls are replaced by
2985 * newline characters. It is an error for the string to contain newline
2986 * characters.
2987 *
2988 * On errors, the Python exception data is set, and NULL is returned.
2989 */
2990 static char *
2991StringToLine(PyObject *obj)
2992{
2993 const char *str;
2994 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002995 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002996 PyInt len;
2997 PyInt i;
2998 char *p;
2999
3000 if (obj == NULL || !PyString_Check(obj))
3001 {
3002 PyErr_BadArgument();
3003 return NULL;
3004 }
3005
Bram Moolenaar19e60942011-06-19 00:27:51 +02003006 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
3007 str = PyString_AsString(bytes);
3008 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003009
3010 /*
3011 * Error checking: String must not contain newlines, as we
3012 * are replacing a single line, and we must replace it with
3013 * a single line.
3014 * A trailing newline is removed, so that append(f.readlines()) works.
3015 */
3016 p = memchr(str, '\n', len);
3017 if (p != NULL)
3018 {
3019 if (p == str + len - 1)
3020 --len;
3021 else
3022 {
3023 PyErr_SetVim(_("string cannot contain newlines"));
3024 return NULL;
3025 }
3026 }
3027
3028 /* Create a copy of the string, with internal nulls replaced by
3029 * newline characters, as is the vim convention.
3030 */
3031 save = (char *)alloc((unsigned)(len+1));
3032 if (save == NULL)
3033 {
3034 PyErr_NoMemory();
3035 return NULL;
3036 }
3037
3038 for (i = 0; i < len; ++i)
3039 {
3040 if (str[i] == '\0')
3041 save[i] = '\n';
3042 else
3043 save[i] = str[i];
3044 }
3045
3046 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003047 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003048
3049 return save;
3050}
3051
3052/* Get a line from the specified buffer. The line number is
3053 * in Vim format (1-based). The line is returned as a Python
3054 * string object.
3055 */
3056 static PyObject *
3057GetBufferLine(buf_T *buf, PyInt n)
3058{
3059 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3060}
3061
3062
3063/* Get a list of lines from the specified buffer. The line numbers
3064 * are in Vim format (1-based). The range is from lo up to, but not
3065 * including, hi. The list is returned as a Python list of string objects.
3066 */
3067 static PyObject *
3068GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3069{
3070 PyInt i;
3071 PyInt n = hi - lo;
3072 PyObject *list = PyList_New(n);
3073
3074 if (list == NULL)
3075 return NULL;
3076
3077 for (i = 0; i < n; ++i)
3078 {
3079 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3080
3081 /* Error check - was the Python string creation OK? */
3082 if (str == NULL)
3083 {
3084 Py_DECREF(list);
3085 return NULL;
3086 }
3087
3088 /* Set the list item */
3089 if (PyList_SetItem(list, i, str))
3090 {
3091 Py_DECREF(str);
3092 Py_DECREF(list);
3093 return NULL;
3094 }
3095 }
3096
3097 /* The ownership of the Python list is passed to the caller (ie,
3098 * the caller should Py_DECREF() the object when it is finished
3099 * with it).
3100 */
3101
3102 return list;
3103}
3104
3105/*
3106 * Check if deleting lines made the cursor position invalid.
3107 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3108 * deleted).
3109 */
3110 static void
3111py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3112{
3113 if (curwin->w_cursor.lnum >= lo)
3114 {
3115 /* Adjust the cursor position if it's in/after the changed
3116 * lines. */
3117 if (curwin->w_cursor.lnum >= hi)
3118 {
3119 curwin->w_cursor.lnum += extra;
3120 check_cursor_col();
3121 }
3122 else if (extra < 0)
3123 {
3124 curwin->w_cursor.lnum = lo;
3125 check_cursor();
3126 }
3127 else
3128 check_cursor_col();
3129 changed_cline_bef_curs();
3130 }
3131 invalidate_botline();
3132}
3133
Bram Moolenaar19e60942011-06-19 00:27:51 +02003134/*
3135 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003136 * in Vim format (1-based). The replacement line is given as
3137 * a Python string object. The object is checked for validity
3138 * and correct format. Errors are returned as a value of FAIL.
3139 * The return value is OK on success.
3140 * If OK is returned and len_change is not NULL, *len_change
3141 * is set to the change in the buffer length.
3142 */
3143 static int
3144SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3145{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003146 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003147 * There are three cases:
3148 * 1. NULL, or None - this is a deletion.
3149 * 2. A string - this is a replacement.
3150 * 3. Anything else - this is an error.
3151 */
3152 if (line == Py_None || line == NULL)
3153 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003154 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003155
3156 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003157 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003158
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003159 VimTryStart();
3160
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003161 if (u_savedel((linenr_T)n, 1L) == FAIL)
3162 PyErr_SetVim(_("cannot save undo information"));
3163 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3164 PyErr_SetVim(_("cannot delete line"));
3165 else
3166 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003167 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003168 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3169 deleted_lines_mark((linenr_T)n, 1L);
3170 }
3171
Bram Moolenaar105bc352013-05-17 16:03:57 +02003172 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003173
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003174 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003175 return FAIL;
3176
3177 if (len_change)
3178 *len_change = -1;
3179
3180 return OK;
3181 }
3182 else if (PyString_Check(line))
3183 {
3184 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003185 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003186
3187 if (save == NULL)
3188 return FAIL;
3189
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003190 VimTryStart();
3191
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003192 /* We do not need to free "save" if ml_replace() consumes it. */
3193 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003194 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003195
3196 if (u_savesub((linenr_T)n) == FAIL)
3197 {
3198 PyErr_SetVim(_("cannot save undo information"));
3199 vim_free(save);
3200 }
3201 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3202 {
3203 PyErr_SetVim(_("cannot replace line"));
3204 vim_free(save);
3205 }
3206 else
3207 changed_bytes((linenr_T)n, 0);
3208
Bram Moolenaar105bc352013-05-17 16:03:57 +02003209 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003210
3211 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003212 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003213 check_cursor_col();
3214
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003215 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003216 return FAIL;
3217
3218 if (len_change)
3219 *len_change = 0;
3220
3221 return OK;
3222 }
3223 else
3224 {
3225 PyErr_BadArgument();
3226 return FAIL;
3227 }
3228}
3229
Bram Moolenaar19e60942011-06-19 00:27:51 +02003230/* Replace a range of lines in the specified buffer. The line numbers are in
3231 * Vim format (1-based). The range is from lo up to, but not including, hi.
3232 * The replacement lines are given as a Python list of string objects. The
3233 * list is checked for validity and correct format. Errors are returned as a
3234 * value of FAIL. The return value is OK on success.
3235 * If OK is returned and len_change is not NULL, *len_change
3236 * is set to the change in the buffer length.
3237 */
3238 static int
3239SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3240{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003241 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003242 * There are three cases:
3243 * 1. NULL, or None - this is a deletion.
3244 * 2. A list - this is a replacement.
3245 * 3. Anything else - this is an error.
3246 */
3247 if (list == Py_None || list == NULL)
3248 {
3249 PyInt i;
3250 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003251 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003252
3253 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003254 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003255 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003256
3257 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3258 PyErr_SetVim(_("cannot save undo information"));
3259 else
3260 {
3261 for (i = 0; i < n; ++i)
3262 {
3263 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3264 {
3265 PyErr_SetVim(_("cannot delete line"));
3266 break;
3267 }
3268 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003269 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003270 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3271 deleted_lines_mark((linenr_T)lo, (long)i);
3272 }
3273
Bram Moolenaar105bc352013-05-17 16:03:57 +02003274 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003275
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003276 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003277 return FAIL;
3278
3279 if (len_change)
3280 *len_change = -n;
3281
3282 return OK;
3283 }
3284 else if (PyList_Check(list))
3285 {
3286 PyInt i;
3287 PyInt new_len = PyList_Size(list);
3288 PyInt old_len = hi - lo;
3289 PyInt extra = 0; /* lines added to text, can be negative */
3290 char **array;
3291 buf_T *savebuf;
3292
3293 if (new_len == 0) /* avoid allocating zero bytes */
3294 array = NULL;
3295 else
3296 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003297 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003298 if (array == NULL)
3299 {
3300 PyErr_NoMemory();
3301 return FAIL;
3302 }
3303 }
3304
3305 for (i = 0; i < new_len; ++i)
3306 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003307 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003308
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003309 if (!(line = PyList_GetItem(list, i)) ||
3310 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003311 {
3312 while (i)
3313 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003314 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003315 return FAIL;
3316 }
3317 }
3318
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003319 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003320 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003321
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003322 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003323 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003324
3325 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3326 PyErr_SetVim(_("cannot save undo information"));
3327
3328 /* If the size of the range is reducing (ie, new_len < old_len) we
3329 * need to delete some old_len. We do this at the start, by
3330 * repeatedly deleting line "lo".
3331 */
3332 if (!PyErr_Occurred())
3333 {
3334 for (i = 0; i < old_len - new_len; ++i)
3335 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3336 {
3337 PyErr_SetVim(_("cannot delete line"));
3338 break;
3339 }
3340 extra -= i;
3341 }
3342
3343 /* For as long as possible, replace the existing old_len with the
3344 * new old_len. This is a more efficient operation, as it requires
3345 * less memory allocation and freeing.
3346 */
3347 if (!PyErr_Occurred())
3348 {
3349 for (i = 0; i < old_len && i < new_len; ++i)
3350 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3351 == FAIL)
3352 {
3353 PyErr_SetVim(_("cannot replace line"));
3354 break;
3355 }
3356 }
3357 else
3358 i = 0;
3359
3360 /* Now we may need to insert the remaining new old_len. If we do, we
3361 * must free the strings as we finish with them (we can't pass the
3362 * responsibility to vim in this case).
3363 */
3364 if (!PyErr_Occurred())
3365 {
3366 while (i < new_len)
3367 {
3368 if (ml_append((linenr_T)(lo + i - 1),
3369 (char_u *)array[i], 0, FALSE) == FAIL)
3370 {
3371 PyErr_SetVim(_("cannot insert line"));
3372 break;
3373 }
3374 vim_free(array[i]);
3375 ++i;
3376 ++extra;
3377 }
3378 }
3379
3380 /* Free any left-over old_len, as a result of an error */
3381 while (i < new_len)
3382 {
3383 vim_free(array[i]);
3384 ++i;
3385 }
3386
3387 /* Free the array of old_len. All of its contents have now
3388 * been dealt with (either freed, or the responsibility passed
3389 * to vim.
3390 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003391 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003392
3393 /* Adjust marks. Invalidate any which lie in the
3394 * changed range, and move any in the remainder of the buffer.
3395 */
3396 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3397 (long)MAXLNUM, (long)extra);
3398 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3399
Bram Moolenaar105bc352013-05-17 16:03:57 +02003400 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003401 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3402
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003403 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003404 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003405
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003406 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003407 return FAIL;
3408
3409 if (len_change)
3410 *len_change = new_len - old_len;
3411
3412 return OK;
3413 }
3414 else
3415 {
3416 PyErr_BadArgument();
3417 return FAIL;
3418 }
3419}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003420
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003421/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003422 * The line number is in Vim format (1-based). The lines to be inserted are
3423 * given as a Python list of string objects or as a single string. The lines
3424 * to be added are checked for validity and correct format. Errors are
3425 * returned as a value of FAIL. The return value is OK on success.
3426 * If OK is returned and len_change is not NULL, *len_change
3427 * is set to the change in the buffer length.
3428 */
3429 static int
3430InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3431{
3432 /* First of all, we check the type of the supplied Python object.
3433 * It must be a string or a list, or the call is in error.
3434 */
3435 if (PyString_Check(lines))
3436 {
3437 char *str = StringToLine(lines);
3438 buf_T *savebuf;
3439
3440 if (str == NULL)
3441 return FAIL;
3442
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003443 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003444 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003445 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003446
3447 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3448 PyErr_SetVim(_("cannot save undo information"));
3449 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3450 PyErr_SetVim(_("cannot insert line"));
3451 else
3452 appended_lines_mark((linenr_T)n, 1L);
3453
3454 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003455 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003456 update_screen(VALID);
3457
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003458 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003459 return FAIL;
3460
3461 if (len_change)
3462 *len_change = 1;
3463
3464 return OK;
3465 }
3466 else if (PyList_Check(lines))
3467 {
3468 PyInt i;
3469 PyInt size = PyList_Size(lines);
3470 char **array;
3471 buf_T *savebuf;
3472
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003473 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003474 if (array == NULL)
3475 {
3476 PyErr_NoMemory();
3477 return FAIL;
3478 }
3479
3480 for (i = 0; i < size; ++i)
3481 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003482 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003483
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003484 if (!(line = PyList_GetItem(lines, i)) ||
3485 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003486 {
3487 while (i)
3488 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003489 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003490 return FAIL;
3491 }
3492 }
3493
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003494 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003495 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003496 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003497
3498 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3499 PyErr_SetVim(_("cannot save undo information"));
3500 else
3501 {
3502 for (i = 0; i < size; ++i)
3503 {
3504 if (ml_append((linenr_T)(n + i),
3505 (char_u *)array[i], 0, FALSE) == FAIL)
3506 {
3507 PyErr_SetVim(_("cannot insert line"));
3508
3509 /* Free the rest of the lines */
3510 while (i < size)
3511 vim_free(array[i++]);
3512
3513 break;
3514 }
3515 vim_free(array[i]);
3516 }
3517 if (i > 0)
3518 appended_lines_mark((linenr_T)n, (long)i);
3519 }
3520
3521 /* Free the array of lines. All of its contents have now
3522 * been freed.
3523 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003524 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003525
Bram Moolenaar105bc352013-05-17 16:03:57 +02003526 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003527 update_screen(VALID);
3528
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003529 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003530 return FAIL;
3531
3532 if (len_change)
3533 *len_change = size;
3534
3535 return OK;
3536 }
3537 else
3538 {
3539 PyErr_BadArgument();
3540 return FAIL;
3541 }
3542}
3543
3544/*
3545 * Common routines for buffers and line ranges
3546 * -------------------------------------------
3547 */
3548
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003549typedef struct
3550{
3551 PyObject_HEAD
3552 buf_T *buf;
3553} BufferObject;
3554
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003555 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003556CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003557{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003558 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003559 {
3560 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3561 return -1;
3562 }
3563
3564 return 0;
3565}
3566
3567 static PyObject *
3568RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3569{
3570 if (CheckBuffer(self))
3571 return NULL;
3572
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003573 if (end == -1)
3574 end = self->buf->b_ml.ml_line_count;
3575
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003576 if (n < 0)
3577 n += end - start + 1;
3578
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003579 if (n < 0 || n > end - start)
3580 {
3581 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3582 return NULL;
3583 }
3584
3585 return GetBufferLine(self->buf, n+start);
3586}
3587
3588 static PyObject *
3589RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3590{
3591 PyInt size;
3592
3593 if (CheckBuffer(self))
3594 return NULL;
3595
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003596 if (end == -1)
3597 end = self->buf->b_ml.ml_line_count;
3598
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003599 size = end - start + 1;
3600
3601 if (lo < 0)
3602 lo = 0;
3603 else if (lo > size)
3604 lo = size;
3605 if (hi < 0)
3606 hi = 0;
3607 if (hi < lo)
3608 hi = lo;
3609 else if (hi > size)
3610 hi = size;
3611
3612 return GetBufferLineList(self->buf, lo+start, hi+start);
3613}
3614
3615 static PyInt
3616RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3617{
3618 PyInt len_change;
3619
3620 if (CheckBuffer(self))
3621 return -1;
3622
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003623 if (end == -1)
3624 end = self->buf->b_ml.ml_line_count;
3625
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003626 if (n < 0)
3627 n += end - start + 1;
3628
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003629 if (n < 0 || n > end - start)
3630 {
3631 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3632 return -1;
3633 }
3634
3635 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3636 return -1;
3637
3638 if (new_end)
3639 *new_end = end + len_change;
3640
3641 return 0;
3642}
3643
Bram Moolenaar19e60942011-06-19 00:27:51 +02003644 static PyInt
3645RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3646{
3647 PyInt size;
3648 PyInt len_change;
3649
3650 /* Self must be a valid buffer */
3651 if (CheckBuffer(self))
3652 return -1;
3653
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003654 if (end == -1)
3655 end = self->buf->b_ml.ml_line_count;
3656
Bram Moolenaar19e60942011-06-19 00:27:51 +02003657 /* Sort out the slice range */
3658 size = end - start + 1;
3659
3660 if (lo < 0)
3661 lo = 0;
3662 else if (lo > size)
3663 lo = size;
3664 if (hi < 0)
3665 hi = 0;
3666 if (hi < lo)
3667 hi = lo;
3668 else if (hi > size)
3669 hi = size;
3670
3671 if (SetBufferLineList(self->buf, lo + start, hi + start,
3672 val, &len_change) == FAIL)
3673 return -1;
3674
3675 if (new_end)
3676 *new_end = end + len_change;
3677
3678 return 0;
3679}
3680
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003681
3682 static PyObject *
3683RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3684{
3685 PyObject *lines;
3686 PyInt len_change;
3687 PyInt max;
3688 PyInt n;
3689
3690 if (CheckBuffer(self))
3691 return NULL;
3692
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003693 if (end == -1)
3694 end = self->buf->b_ml.ml_line_count;
3695
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003696 max = n = end - start + 1;
3697
3698 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3699 return NULL;
3700
3701 if (n < 0 || n > max)
3702 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003703 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003704 return NULL;
3705 }
3706
3707 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3708 return NULL;
3709
3710 if (new_end)
3711 *new_end = end + len_change;
3712
3713 Py_INCREF(Py_None);
3714 return Py_None;
3715}
3716
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003717/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003718 */
3719
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003720static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003721static PySequenceMethods RangeAsSeq;
3722static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003723
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003724typedef struct
3725{
3726 PyObject_HEAD
3727 BufferObject *buf;
3728 PyInt start;
3729 PyInt end;
3730} RangeObject;
3731
3732 static PyObject *
3733RangeNew(buf_T *buf, PyInt start, PyInt end)
3734{
3735 BufferObject *bufr;
3736 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003737 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003738 if (self == NULL)
3739 return NULL;
3740
3741 bufr = (BufferObject *)BufferNew(buf);
3742 if (bufr == NULL)
3743 {
3744 Py_DECREF(self);
3745 return NULL;
3746 }
3747 Py_INCREF(bufr);
3748
3749 self->buf = bufr;
3750 self->start = start;
3751 self->end = end;
3752
3753 return (PyObject *)(self);
3754}
3755
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003756 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003757RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003758{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003759 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003760 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003761 PyObject_GC_Del((void *)(self));
3762}
3763
3764 static int
3765RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3766{
3767 Py_VISIT(((PyObject *)(self->buf)));
3768 return 0;
3769}
3770
3771 static int
3772RangeClear(RangeObject *self)
3773{
3774 Py_CLEAR(self->buf);
3775 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003776}
3777
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003778 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003779RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003780{
3781 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003782 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003783 return -1; /* ??? */
3784
Bram Moolenaard6e39182013-05-21 18:30:34 +02003785 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003786}
3787
3788 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003789RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003790{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003791 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003792}
3793
3794 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003795RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003796{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003797 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003798}
3799
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003800static char *RangeAttrs[] = {
3801 "start", "end",
3802 NULL
3803};
3804
3805 static PyObject *
3806RangeDir(PyObject *self)
3807{
3808 return ObjectDir(self, RangeAttrs);
3809}
3810
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003811 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003812RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003813{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003814 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003815}
3816
3817 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003818RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003819{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003820 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003821 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
3822 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003823 else
3824 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003825 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003826
3827 if (name == NULL)
3828 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003829
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003830 return PyString_FromFormat("<range %s (%d:%d)>",
3831 name, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003832 }
3833}
3834
3835static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003836 /* name, function, calling, documentation */
3837 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003838 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3839 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003840};
3841
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003842static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003843static PySequenceMethods BufferAsSeq;
3844static PyMappingMethods BufferAsMapping;
3845
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003846 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003847BufferNew(buf_T *buf)
3848{
3849 /* We need to handle deletion of buffers underneath us.
3850 * If we add a "b_python*_ref" field to the buf_T structure,
3851 * then we can get at it in buf_freeall() in vim. We then
3852 * need to create only ONE Python object per buffer - if
3853 * we try to create a second, just INCREF the existing one
3854 * and return it. The (single) Python object referring to
3855 * the buffer is stored in "b_python*_ref".
3856 * Question: what to do on a buf_freeall(). We'll probably
3857 * have to either delete the Python object (DECREF it to
3858 * zero - a bad idea, as it leaves dangling refs!) or
3859 * set the buf_T * value to an invalid value (-1?), which
3860 * means we need checks in all access functions... Bah.
3861 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003862 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003863 * b_python_ref and b_python3_ref fields respectively.
3864 */
3865
3866 BufferObject *self;
3867
3868 if (BUF_PYTHON_REF(buf) != NULL)
3869 {
3870 self = BUF_PYTHON_REF(buf);
3871 Py_INCREF(self);
3872 }
3873 else
3874 {
3875 self = PyObject_NEW(BufferObject, &BufferType);
3876 if (self == NULL)
3877 return NULL;
3878 self->buf = buf;
3879 BUF_PYTHON_REF(buf) = self;
3880 }
3881
3882 return (PyObject *)(self);
3883}
3884
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003885 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003886BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003887{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003888 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3889 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003890
3891 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003892}
3893
Bram Moolenaar971db462013-05-12 18:44:48 +02003894 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003895BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003896{
3897 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003898 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003899 return -1; /* ??? */
3900
Bram Moolenaard6e39182013-05-21 18:30:34 +02003901 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003902}
3903
3904 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003905BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003906{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003907 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003908}
3909
3910 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003911BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003912{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003913 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003914}
3915
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003916static char *BufferAttrs[] = {
3917 "name", "number", "vars", "options", "valid",
3918 NULL
3919};
3920
3921 static PyObject *
3922BufferDir(PyObject *self)
3923{
3924 return ObjectDir(self, BufferAttrs);
3925}
3926
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003927 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003928BufferAttrValid(BufferObject *self, char *name)
3929{
3930 PyObject *r;
3931
3932 if (strcmp(name, "valid") != 0)
3933 return NULL;
3934
3935 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
3936 Py_INCREF(r);
3937 return r;
3938}
3939
3940 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003941BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003942{
3943 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02003944 return PyString_FromString((self->buf->b_ffname == NULL
3945 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003946 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003947 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003948 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003949 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003950 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003951 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3952 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003953 else if (strcmp(name, "__members__") == 0)
3954 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003955 else
3956 return NULL;
3957}
3958
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003959 static int
3960BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3961{
3962 if (CheckBuffer(self))
3963 return -1;
3964
3965 if (strcmp(name, "name") == 0)
3966 {
3967 char_u *val;
3968 aco_save_T aco;
3969 int r;
3970 PyObject *todecref;
3971
3972 if (!(val = StringToChars(valObject, &todecref)))
3973 return -1;
3974
3975 VimTryStart();
3976 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3977 aucmd_prepbuf(&aco, self->buf);
3978 r = rename_buffer(val);
3979 aucmd_restbuf(&aco);
3980 Py_XDECREF(todecref);
3981 if (VimTryEnd())
3982 return -1;
3983
3984 if (r == FAIL)
3985 {
3986 PyErr_SetVim(_("failed to rename buffer"));
3987 return -1;
3988 }
3989 return 0;
3990 }
3991 else
3992 {
3993 PyErr_SetString(PyExc_AttributeError, name);
3994 return -1;
3995 }
3996}
3997
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003998 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003999BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004000{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004001 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004002}
4003
4004 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004005BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004006{
4007 pos_T *posp;
4008 char *pmark;
4009 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004010 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004011
Bram Moolenaard6e39182013-05-21 18:30:34 +02004012 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004013 return NULL;
4014
4015 if (!PyArg_ParseTuple(args, "s", &pmark))
4016 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004017
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004018 if (STRLEN(pmark) != 1)
4019 {
4020 PyErr_SetString(PyExc_ValueError,
4021 _("mark name must be a single character"));
4022 return NULL;
4023 }
4024
4025 mark = *pmark;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004026 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004027 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004028 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004029 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004030 if (VimTryEnd())
4031 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004032
4033 if (posp == NULL)
4034 {
4035 PyErr_SetVim(_("invalid mark name"));
4036 return NULL;
4037 }
4038
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004039 if (posp->lnum <= 0)
4040 {
4041 /* Or raise an error? */
4042 Py_INCREF(Py_None);
4043 return Py_None;
4044 }
4045
4046 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4047}
4048
4049 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004050BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051{
4052 PyInt start;
4053 PyInt end;
4054
Bram Moolenaard6e39182013-05-21 18:30:34 +02004055 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004056 return NULL;
4057
4058 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4059 return NULL;
4060
Bram Moolenaard6e39182013-05-21 18:30:34 +02004061 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062}
4063
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004064 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004065BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004066{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004067 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004068 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004069 else
4070 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004071 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004072
4073 if (name == NULL)
4074 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004075
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004076 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004077 }
4078}
4079
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004080static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004081 /* name, function, calling, documentation */
4082 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4083 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4084 {"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 +02004085 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4086 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087};
4088
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004089/*
4090 * Buffer list object - Implementation
4091 */
4092
4093static PyTypeObject BufMapType;
4094
4095typedef struct
4096{
4097 PyObject_HEAD
4098} BufMapObject;
4099
4100 static PyInt
4101BufMapLength(PyObject *self UNUSED)
4102{
4103 buf_T *b = firstbuf;
4104 PyInt n = 0;
4105
4106 while (b)
4107 {
4108 ++n;
4109 b = b->b_next;
4110 }
4111
4112 return n;
4113}
4114
4115 static PyObject *
4116BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4117{
4118 buf_T *b;
4119 int bnr;
4120
4121#if PY_MAJOR_VERSION < 3
4122 if (PyInt_Check(keyObject))
4123 bnr = PyInt_AsLong(keyObject);
4124 else
4125#endif
4126 if (PyLong_Check(keyObject))
4127 bnr = PyLong_AsLong(keyObject);
4128 else
4129 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004130 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004131 return NULL;
4132 }
4133
4134 b = buflist_findnr(bnr);
4135
4136 if (b)
4137 return BufferNew(b);
4138 else
4139 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004140 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004141 return NULL;
4142 }
4143}
4144
4145 static void
4146BufMapIterDestruct(PyObject *buffer)
4147{
4148 /* Iteration was stopped before all buffers were processed */
4149 if (buffer)
4150 {
4151 Py_DECREF(buffer);
4152 }
4153}
4154
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004155 static int
4156BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4157{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004158 if (buffer)
4159 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004160 return 0;
4161}
4162
4163 static int
4164BufMapIterClear(PyObject **buffer)
4165{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004166 if (*buffer)
4167 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004168 return 0;
4169}
4170
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004171 static PyObject *
4172BufMapIterNext(PyObject **buffer)
4173{
4174 PyObject *next;
4175 PyObject *r;
4176
4177 if (!*buffer)
4178 return NULL;
4179
4180 r = *buffer;
4181
4182 if (CheckBuffer((BufferObject *)(r)))
4183 {
4184 *buffer = NULL;
4185 return NULL;
4186 }
4187
4188 if (!((BufferObject *)(r))->buf->b_next)
4189 next = NULL;
4190 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4191 return NULL;
4192 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004193 /* Do not increment reference: we no longer hold it (decref), but whoever
4194 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004195 return r;
4196}
4197
4198 static PyObject *
4199BufMapIter(PyObject *self UNUSED)
4200{
4201 PyObject *buffer;
4202
4203 buffer = BufferNew(firstbuf);
4204 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004205 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4206 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004207}
4208
4209static PyMappingMethods BufMapAsMapping = {
4210 (lenfunc) BufMapLength,
4211 (binaryfunc) BufMapItem,
4212 (objobjargproc) 0,
4213};
4214
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004215/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004216 */
4217
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004218static char *CurrentAttrs[] = {
4219 "buffer", "window", "line", "range", "tabpage",
4220 NULL
4221};
4222
4223 static PyObject *
4224CurrentDir(PyObject *self)
4225{
4226 return ObjectDir(self, CurrentAttrs);
4227}
4228
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004229 static PyObject *
4230CurrentGetattr(PyObject *self UNUSED, char *name)
4231{
4232 if (strcmp(name, "buffer") == 0)
4233 return (PyObject *)BufferNew(curbuf);
4234 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004235 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004236 else if (strcmp(name, "tabpage") == 0)
4237 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004238 else if (strcmp(name, "line") == 0)
4239 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4240 else if (strcmp(name, "range") == 0)
4241 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004242 else if (strcmp(name, "__members__") == 0)
4243 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004244 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004245#if PY_MAJOR_VERSION < 3
4246 return Py_FindMethod(WindowMethods, self, name);
4247#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004248 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004249#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004250}
4251
4252 static int
4253CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4254{
4255 if (strcmp(name, "line") == 0)
4256 {
4257 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4258 return -1;
4259
4260 return 0;
4261 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004262 else if (strcmp(name, "buffer") == 0)
4263 {
4264 int count;
4265
4266 if (value->ob_type != &BufferType)
4267 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004268 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004269 return -1;
4270 }
4271
4272 if (CheckBuffer((BufferObject *)(value)))
4273 return -1;
4274 count = ((BufferObject *)(value))->buf->b_fnum;
4275
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004276 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004277 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4278 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004279 if (VimTryEnd())
4280 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004281 PyErr_SetVim(_("failed to switch to given buffer"));
4282 return -1;
4283 }
4284
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004285 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004286 }
4287 else if (strcmp(name, "window") == 0)
4288 {
4289 int count;
4290
4291 if (value->ob_type != &WindowType)
4292 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004293 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004294 return -1;
4295 }
4296
4297 if (CheckWindow((WindowObject *)(value)))
4298 return -1;
4299 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4300
4301 if (!count)
4302 {
4303 PyErr_SetString(PyExc_ValueError,
4304 _("failed to find window in the current tab page"));
4305 return -1;
4306 }
4307
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004308 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004309 win_goto(((WindowObject *)(value))->win);
4310 if (((WindowObject *)(value))->win != curwin)
4311 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004312 if (VimTryEnd())
4313 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004314 PyErr_SetString(PyExc_RuntimeError,
4315 _("did not switch to the specified window"));
4316 return -1;
4317 }
4318
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004319 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004320 }
4321 else if (strcmp(name, "tabpage") == 0)
4322 {
4323 if (value->ob_type != &TabPageType)
4324 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004325 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004326 return -1;
4327 }
4328
4329 if (CheckTabPage((TabPageObject *)(value)))
4330 return -1;
4331
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004332 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004333 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4334 if (((TabPageObject *)(value))->tab != curtab)
4335 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004336 if (VimTryEnd())
4337 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004338 PyErr_SetString(PyExc_RuntimeError,
4339 _("did not switch to the specified tab page"));
4340 return -1;
4341 }
4342
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004343 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004344 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004345 else
4346 {
4347 PyErr_SetString(PyExc_AttributeError, name);
4348 return -1;
4349 }
4350}
4351
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004352static struct PyMethodDef CurrentMethods[] = {
4353 /* name, function, calling, documentation */
4354 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4355 { NULL, NULL, 0, NULL}
4356};
4357
Bram Moolenaardb913952012-06-29 12:54:53 +02004358 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004359init_range_cmd(exarg_T *eap)
4360{
4361 RangeStart = eap->line1;
4362 RangeEnd = eap->line2;
4363}
4364
4365 static void
4366init_range_eval(typval_T *rettv UNUSED)
4367{
4368 RangeStart = (PyInt) curwin->w_cursor.lnum;
4369 RangeEnd = RangeStart;
4370}
4371
4372 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004373run_cmd(const char *cmd, void *arg UNUSED
4374#ifdef PY_CAN_RECURSE
4375 , PyGILState_STATE *pygilstate UNUSED
4376#endif
4377 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004378{
4379 PyRun_SimpleString((char *) cmd);
4380}
4381
4382static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4383static int code_hdr_len = 30;
4384
4385 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004386run_do(const char *cmd, void *arg UNUSED
4387#ifdef PY_CAN_RECURSE
4388 , PyGILState_STATE *pygilstate
4389#endif
4390 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004391{
4392 PyInt lnum;
4393 size_t len;
4394 char *code;
4395 int status;
4396 PyObject *pyfunc, *pymain;
4397
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004398 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004399 {
4400 EMSG(_("cannot save undo information"));
4401 return;
4402 }
4403
4404 len = code_hdr_len + STRLEN(cmd);
4405 code = PyMem_New(char, len + 1);
4406 memcpy(code, code_hdr, code_hdr_len);
4407 STRCPY(code + code_hdr_len, cmd);
4408 status = PyRun_SimpleString(code);
4409 PyMem_Free(code);
4410
4411 if (status)
4412 {
4413 EMSG(_("failed to run the code"));
4414 return;
4415 }
4416
4417 status = 0;
4418 pymain = PyImport_AddModule("__main__");
4419 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004420#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004421 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004422#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004423
4424 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4425 {
4426 PyObject *line, *linenr, *ret;
4427
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004428#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004429 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004430#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004431 if (!(line = GetBufferLine(curbuf, lnum)))
4432 goto err;
4433 if (!(linenr = PyInt_FromLong((long) lnum)))
4434 {
4435 Py_DECREF(line);
4436 goto err;
4437 }
4438 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4439 Py_DECREF(line);
4440 Py_DECREF(linenr);
4441 if (!ret)
4442 goto err;
4443
4444 if (ret != Py_None)
4445 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4446 goto err;
4447
4448 Py_XDECREF(ret);
4449 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004450#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004451 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004452#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004453 }
4454 goto out;
4455err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004456#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004457 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004458#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004459 PyErr_PrintEx(0);
4460 PythonIO_Flush();
4461 status = 1;
4462out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004463#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004464 if (!status)
4465 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004466#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004467 Py_DECREF(pyfunc);
4468 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4469 if (status)
4470 return;
4471 check_cursor();
4472 update_curbuf(NOT_VALID);
4473}
4474
4475 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004476run_eval(const char *cmd, typval_T *rettv
4477#ifdef PY_CAN_RECURSE
4478 , PyGILState_STATE *pygilstate UNUSED
4479#endif
4480 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004481{
4482 PyObject *r;
4483
4484 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4485 if (r == NULL)
4486 {
4487 if (PyErr_Occurred() && !msg_silent)
4488 PyErr_PrintEx(0);
4489 EMSG(_("E858: Eval did not return a valid python object"));
4490 }
4491 else
4492 {
4493 if (ConvertFromPyObject(r, rettv) == -1)
4494 EMSG(_("E859: Failed to convert returned python object to vim value"));
4495 Py_DECREF(r);
4496 }
4497 PyErr_Clear();
4498}
4499
4500 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004501set_ref_in_py(const int copyID)
4502{
4503 pylinkedlist_T *cur;
4504 dict_T *dd;
4505 list_T *ll;
4506
4507 if (lastdict != NULL)
4508 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4509 {
4510 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4511 if (dd->dv_copyID != copyID)
4512 {
4513 dd->dv_copyID = copyID;
4514 set_ref_in_ht(&dd->dv_hashtab, copyID);
4515 }
4516 }
4517
4518 if (lastlist != NULL)
4519 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4520 {
4521 ll = ((ListObject *) (cur->pll_obj))->list;
4522 if (ll->lv_copyID != copyID)
4523 {
4524 ll->lv_copyID = copyID;
4525 set_ref_in_list(ll, copyID);
4526 }
4527 }
4528}
4529
4530 static int
4531set_string_copy(char_u *str, typval_T *tv)
4532{
4533 tv->vval.v_string = vim_strsave(str);
4534 if (tv->vval.v_string == NULL)
4535 {
4536 PyErr_NoMemory();
4537 return -1;
4538 }
4539 return 0;
4540}
4541
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004542 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004543pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004544{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004545 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004546 char_u *key;
4547 dictitem_T *di;
4548 PyObject *keyObject;
4549 PyObject *valObject;
4550 Py_ssize_t iter = 0;
4551
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004552 if (!(dict = dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004553 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004554
4555 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004556 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004557
4558 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4559 {
4560 DICTKEY_DECL
4561
Bram Moolenaara03e6312013-05-29 22:49:26 +02004562 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004563 {
4564 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004565 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004566 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004567
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004568 if (!DICTKEY_SET_KEY)
4569 {
4570 dict_unref(dict);
4571 return -1;
4572 }
4573 DICTKEY_CHECK_EMPTY(-1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004574
4575 di = dictitem_alloc(key);
4576
4577 DICTKEY_UNREF
4578
4579 if (di == NULL)
4580 {
4581 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004582 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004583 return -1;
4584 }
4585 di->di_tv.v_lock = 0;
4586
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004587 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004588 {
4589 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004590 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004591 return -1;
4592 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004593
4594 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004595 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004596 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004597 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004598 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004599 PyErr_SetVim(_("failed to add key to dictionary"));
4600 return -1;
4601 }
4602 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004603
4604 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004605 return 0;
4606}
4607
4608 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004609pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004610{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004611 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004612 char_u *key;
4613 dictitem_T *di;
4614 PyObject *list;
4615 PyObject *litem;
4616 PyObject *keyObject;
4617 PyObject *valObject;
4618 Py_ssize_t lsize;
4619
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004620 if (!(dict = dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004621 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004622
4623 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004624 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004625
4626 list = PyMapping_Items(obj);
4627 if (list == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004628 {
4629 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004630 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004631 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004632 lsize = PyList_Size(list);
4633 while (lsize--)
4634 {
4635 DICTKEY_DECL
4636
4637 litem = PyList_GetItem(list, lsize);
4638 if (litem == NULL)
4639 {
4640 Py_DECREF(list);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004641 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004642 return -1;
4643 }
4644
Bram Moolenaara03e6312013-05-29 22:49:26 +02004645 if (!(keyObject = PyTuple_GetItem(litem, 0)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004646 {
4647 Py_DECREF(list);
4648 Py_DECREF(litem);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004649 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004650 return -1;
4651 }
4652
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004653 if (!DICTKEY_SET_KEY)
4654 {
4655 dict_unref(dict);
4656 Py_DECREF(list);
4657 Py_DECREF(litem);
4658 DICTKEY_UNREF
4659 return -1;
4660 }
4661 DICTKEY_CHECK_EMPTY(-1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004662
Bram Moolenaara03e6312013-05-29 22:49:26 +02004663 if (!(valObject = PyTuple_GetItem(litem, 1)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004664 {
4665 Py_DECREF(list);
4666 Py_DECREF(litem);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004667 dict_unref(dict);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004668 DICTKEY_UNREF
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004669 return -1;
4670 }
4671
Bram Moolenaara03e6312013-05-29 22:49:26 +02004672 Py_DECREF(litem);
4673
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004674 di = dictitem_alloc(key);
4675
4676 DICTKEY_UNREF
4677
4678 if (di == NULL)
4679 {
4680 Py_DECREF(list);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004681 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004682 PyErr_NoMemory();
4683 return -1;
4684 }
4685 di->di_tv.v_lock = 0;
4686
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004687 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004688 {
4689 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004690 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004691 Py_DECREF(list);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004692 return -1;
4693 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004694
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004695 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004696 {
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004697 dictitem_free(di);
4698 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004699 Py_DECREF(list);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004700 PyErr_SetVim(_("failed to add key to dictionary"));
4701 return -1;
4702 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004703 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004704 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004705 Py_DECREF(list);
4706 return 0;
4707}
4708
4709 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004710pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004711{
4712 list_T *l;
4713
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004714 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004715 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004716
4717 tv->v_type = VAR_LIST;
4718 tv->vval.v_list = l;
4719
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004720 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004721 {
4722 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004723 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004724 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004725
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004726 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004727 return 0;
4728}
4729
Bram Moolenaardb913952012-06-29 12:54:53 +02004730typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4731
4732 static int
4733convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004734 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004735{
4736 PyObject *capsule;
4737 char hexBuf[sizeof(void *) * 2 + 3];
4738
4739 sprintf(hexBuf, "%p", obj);
4740
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004741# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004742 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004743# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004744 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004745# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004746 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004747 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004748# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004749 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004750# else
4751 capsule = PyCObject_FromVoidPtr(tv, NULL);
4752# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004753 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4754 {
4755 Py_DECREF(capsule);
4756 tv->v_type = VAR_UNKNOWN;
4757 return -1;
4758 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004759 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004760 {
4761 tv->v_type = VAR_UNKNOWN;
4762 return -1;
4763 }
4764 /* As we are not using copy_tv which increments reference count we must
4765 * do it ourself. */
4766 switch(tv->v_type)
4767 {
4768 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4769 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4770 }
4771 }
4772 else
4773 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004774 typval_T *v;
4775
4776# ifdef PY_USE_CAPSULE
4777 v = PyCapsule_GetPointer(capsule, NULL);
4778# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004779 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004780# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004781 copy_tv(v, tv);
4782 }
4783 return 0;
4784}
4785
4786 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02004787ConvertFromPyMapping(PyObject *obj, typval_T *tv)
4788{
4789 PyObject *lookup_dict;
4790 int r;
4791
4792 if (!(lookup_dict = PyDict_New()))
4793 return -1;
4794
4795 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
4796 {
4797 tv->v_type = VAR_DICT;
4798 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4799 ++tv->vval.v_dict->dv_refcount;
4800 r = 0;
4801 }
4802 else if (PyDict_Check(obj))
4803 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
4804 else if (PyMapping_Check(obj))
4805 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
4806 else
4807 {
4808 PyErr_SetString(PyExc_TypeError,
4809 _("unable to convert object to vim dictionary"));
4810 r = -1;
4811 }
4812 Py_DECREF(lookup_dict);
4813 return r;
4814}
4815
4816 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02004817ConvertFromPyObject(PyObject *obj, typval_T *tv)
4818{
4819 PyObject *lookup_dict;
4820 int r;
4821
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004822 if (!(lookup_dict = PyDict_New()))
4823 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004824 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4825 Py_DECREF(lookup_dict);
4826 return r;
4827}
4828
4829 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004830_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004831{
Bram Moolenaara9922d62013-05-30 13:01:18 +02004832 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02004833 {
4834 tv->v_type = VAR_DICT;
4835 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4836 ++tv->vval.v_dict->dv_refcount;
4837 }
4838 else if (obj->ob_type == &ListType)
4839 {
4840 tv->v_type = VAR_LIST;
4841 tv->vval.v_list = (((ListObject *)(obj))->list);
4842 ++tv->vval.v_list->lv_refcount;
4843 }
4844 else if (obj->ob_type == &FunctionType)
4845 {
4846 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4847 return -1;
4848
4849 tv->v_type = VAR_FUNC;
4850 func_ref(tv->vval.v_string);
4851 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004852 else if (PyBytes_Check(obj))
4853 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004854 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004855
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004856 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4857 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004858 if (result == NULL)
4859 return -1;
4860
4861 if (set_string_copy(result, tv) == -1)
4862 return -1;
4863
4864 tv->v_type = VAR_STRING;
4865 }
4866 else if (PyUnicode_Check(obj))
4867 {
4868 PyObject *bytes;
4869 char_u *result;
4870
Bram Moolenaardb913952012-06-29 12:54:53 +02004871 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4872 if (bytes == NULL)
4873 return -1;
4874
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004875 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4876 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004877 if (result == NULL)
4878 return -1;
4879
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004880 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004881 {
4882 Py_XDECREF(bytes);
4883 return -1;
4884 }
4885 Py_XDECREF(bytes);
4886
4887 tv->v_type = VAR_STRING;
4888 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004889#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004890 else if (PyInt_Check(obj))
4891 {
4892 tv->v_type = VAR_NUMBER;
4893 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4894 }
4895#endif
4896 else if (PyLong_Check(obj))
4897 {
4898 tv->v_type = VAR_NUMBER;
4899 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4900 }
4901 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004902 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004903#ifdef FEAT_FLOAT
4904 else if (PyFloat_Check(obj))
4905 {
4906 tv->v_type = VAR_FLOAT;
4907 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4908 }
4909#endif
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004910 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004911 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004912 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004913 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004914 else
4915 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004916 PyErr_SetString(PyExc_TypeError,
4917 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004918 return -1;
4919 }
4920 return 0;
4921}
4922
4923 static PyObject *
4924ConvertToPyObject(typval_T *tv)
4925{
4926 if (tv == NULL)
4927 {
4928 PyErr_SetVim(_("NULL reference passed"));
4929 return NULL;
4930 }
4931 switch (tv->v_type)
4932 {
4933 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004934 return PyBytes_FromString(tv->vval.v_string == NULL
4935 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004936 case VAR_NUMBER:
4937 return PyLong_FromLong((long) tv->vval.v_number);
4938#ifdef FEAT_FLOAT
4939 case VAR_FLOAT:
4940 return PyFloat_FromDouble((double) tv->vval.v_float);
4941#endif
4942 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004943 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02004944 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02004945 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004946 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02004947 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004948 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004949 case VAR_UNKNOWN:
4950 Py_INCREF(Py_None);
4951 return Py_None;
4952 default:
4953 PyErr_SetVim(_("internal error: invalid value type"));
4954 return NULL;
4955 }
4956}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004957
4958typedef struct
4959{
4960 PyObject_HEAD
4961} CurrentObject;
4962static PyTypeObject CurrentType;
4963
4964 static void
4965init_structs(void)
4966{
4967 vim_memset(&OutputType, 0, sizeof(OutputType));
4968 OutputType.tp_name = "vim.message";
4969 OutputType.tp_basicsize = sizeof(OutputObject);
4970 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4971 OutputType.tp_doc = "vim message object";
4972 OutputType.tp_methods = OutputMethods;
4973#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004974 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4975 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004976 OutputType.tp_alloc = call_PyType_GenericAlloc;
4977 OutputType.tp_new = call_PyType_GenericNew;
4978 OutputType.tp_free = call_PyObject_Free;
4979#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004980 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4981 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004982#endif
4983
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004984 vim_memset(&IterType, 0, sizeof(IterType));
4985 IterType.tp_name = "vim.iter";
4986 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02004987 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004988 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004989 IterType.tp_iter = (getiterfunc)IterIter;
4990 IterType.tp_iternext = (iternextfunc)IterNext;
4991 IterType.tp_dealloc = (destructor)IterDestructor;
4992 IterType.tp_traverse = (traverseproc)IterTraverse;
4993 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004994
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004995 vim_memset(&BufferType, 0, sizeof(BufferType));
4996 BufferType.tp_name = "vim.buffer";
4997 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004998 BufferType.tp_dealloc = (destructor)BufferDestructor;
4999 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005000 BufferType.tp_as_sequence = &BufferAsSeq;
5001 BufferType.tp_as_mapping = &BufferAsMapping;
5002 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5003 BufferType.tp_doc = "vim buffer object";
5004 BufferType.tp_methods = BufferMethods;
5005#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005006 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005007 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005008 BufferType.tp_alloc = call_PyType_GenericAlloc;
5009 BufferType.tp_new = call_PyType_GenericNew;
5010 BufferType.tp_free = call_PyObject_Free;
5011#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005012 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005013 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005014#endif
5015
5016 vim_memset(&WindowType, 0, sizeof(WindowType));
5017 WindowType.tp_name = "vim.window";
5018 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005019 WindowType.tp_dealloc = (destructor)WindowDestructor;
5020 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005021 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005022 WindowType.tp_doc = "vim Window object";
5023 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005024 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5025 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005026#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005027 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5028 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005029 WindowType.tp_alloc = call_PyType_GenericAlloc;
5030 WindowType.tp_new = call_PyType_GenericNew;
5031 WindowType.tp_free = call_PyObject_Free;
5032#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005033 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5034 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005035#endif
5036
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005037 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5038 TabPageType.tp_name = "vim.tabpage";
5039 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005040 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5041 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005042 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5043 TabPageType.tp_doc = "vim tab page object";
5044 TabPageType.tp_methods = TabPageMethods;
5045#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005046 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005047 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5048 TabPageType.tp_new = call_PyType_GenericNew;
5049 TabPageType.tp_free = call_PyObject_Free;
5050#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005051 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005052#endif
5053
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005054 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5055 BufMapType.tp_name = "vim.bufferlist";
5056 BufMapType.tp_basicsize = sizeof(BufMapObject);
5057 BufMapType.tp_as_mapping = &BufMapAsMapping;
5058 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005059 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005060 BufferType.tp_doc = "vim buffer list";
5061
5062 vim_memset(&WinListType, 0, sizeof(WinListType));
5063 WinListType.tp_name = "vim.windowlist";
5064 WinListType.tp_basicsize = sizeof(WinListType);
5065 WinListType.tp_as_sequence = &WinListAsSeq;
5066 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5067 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005068 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005069
5070 vim_memset(&TabListType, 0, sizeof(TabListType));
5071 TabListType.tp_name = "vim.tabpagelist";
5072 TabListType.tp_basicsize = sizeof(TabListType);
5073 TabListType.tp_as_sequence = &TabListAsSeq;
5074 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5075 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005076
5077 vim_memset(&RangeType, 0, sizeof(RangeType));
5078 RangeType.tp_name = "vim.range";
5079 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005080 RangeType.tp_dealloc = (destructor)RangeDestructor;
5081 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005082 RangeType.tp_as_sequence = &RangeAsSeq;
5083 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005084 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005085 RangeType.tp_doc = "vim Range object";
5086 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005087 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5088 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005089#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005090 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005091 RangeType.tp_alloc = call_PyType_GenericAlloc;
5092 RangeType.tp_new = call_PyType_GenericNew;
5093 RangeType.tp_free = call_PyObject_Free;
5094#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005095 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005096#endif
5097
5098 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5099 CurrentType.tp_name = "vim.currentdata";
5100 CurrentType.tp_basicsize = sizeof(CurrentObject);
5101 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5102 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005103 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005104#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005105 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5106 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005107#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005108 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5109 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005110#endif
5111
5112 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5113 DictionaryType.tp_name = "vim.dictionary";
5114 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005115 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005116 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005117 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005118 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005119 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5120 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005121 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5122 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5123 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005124#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005125 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5126 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005127#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005128 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5129 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005130#endif
5131
5132 vim_memset(&ListType, 0, sizeof(ListType));
5133 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005134 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005135 ListType.tp_basicsize = sizeof(ListObject);
5136 ListType.tp_as_sequence = &ListAsSeq;
5137 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005138 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005139 ListType.tp_doc = "list pushing modifications to vim structure";
5140 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005141 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005142 ListType.tp_new = (newfunc)ListConstructor;
5143 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005144#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005145 ListType.tp_getattro = (getattrofunc)ListGetattro;
5146 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005147#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005148 ListType.tp_getattr = (getattrfunc)ListGetattr;
5149 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005150#endif
5151
5152 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005153 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005154 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005155 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5156 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005157 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005158 FunctionType.tp_doc = "object that calls vim function";
5159 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005160 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005161 FunctionType.tp_new = (newfunc)FunctionConstructor;
5162 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005163#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005164 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005165#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005166 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005167#endif
5168
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005169 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5170 OptionsType.tp_name = "vim.options";
5171 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005172 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005173 OptionsType.tp_doc = "object for manipulating options";
5174 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005175 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5176 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5177 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005178
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005179#if PY_MAJOR_VERSION >= 3
5180 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5181 vimmodule.m_name = "vim";
5182 vimmodule.m_doc = "Vim Python interface\n";
5183 vimmodule.m_size = -1;
5184 vimmodule.m_methods = VimMethods;
5185#endif
5186}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005187
5188#define PYTYPE_READY(type) \
5189 if (PyType_Ready(&type)) \
5190 return -1;
5191
5192 static int
5193init_types()
5194{
5195 PYTYPE_READY(IterType);
5196 PYTYPE_READY(BufferType);
5197 PYTYPE_READY(RangeType);
5198 PYTYPE_READY(WindowType);
5199 PYTYPE_READY(TabPageType);
5200 PYTYPE_READY(BufMapType);
5201 PYTYPE_READY(WinListType);
5202 PYTYPE_READY(TabListType);
5203 PYTYPE_READY(CurrentType);
5204 PYTYPE_READY(DictionaryType);
5205 PYTYPE_READY(ListType);
5206 PYTYPE_READY(FunctionType);
5207 PYTYPE_READY(OptionsType);
5208 PYTYPE_READY(OutputType);
5209 return 0;
5210}
5211
5212static BufMapObject TheBufferMap =
5213{
5214 PyObject_HEAD_INIT(&BufMapType)
5215};
5216
5217static WinListObject TheWindowList =
5218{
5219 PyObject_HEAD_INIT(&WinListType)
5220 NULL
5221};
5222
5223static CurrentObject TheCurrent =
5224{
5225 PyObject_HEAD_INIT(&CurrentType)
5226};
5227
5228static TabListObject TheTabPageList =
5229{
5230 PyObject_HEAD_INIT(&TabListType)
5231};
5232
5233static struct numeric_constant {
5234 char *name;
5235 int value;
5236} numeric_constants[] = {
5237 {"VAR_LOCKED", VAR_LOCKED},
5238 {"VAR_FIXED", VAR_FIXED},
5239 {"VAR_SCOPE", VAR_SCOPE},
5240 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5241};
5242
5243static struct object_constant {
5244 char *name;
5245 PyObject *value;
5246} object_constants[] = {
5247 {"buffers", (PyObject *)(void *)&TheBufferMap},
5248 {"windows", (PyObject *)(void *)&TheWindowList},
5249 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5250 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005251
5252 {"Buffer", (PyObject *)&BufferType},
5253 {"Range", (PyObject *)&RangeType},
5254 {"Window", (PyObject *)&WindowType},
5255 {"TabPage", (PyObject *)&TabPageType},
5256 {"Dictionary", (PyObject *)&DictionaryType},
5257 {"List", (PyObject *)&ListType},
5258 {"Function", (PyObject *)&FunctionType},
5259 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005260};
5261
5262typedef int (*object_adder)(PyObject *, const char *, PyObject *);
5263
5264#define ADD_OBJECT(m, name, obj) \
5265 if (add_object(m, name, obj)) \
5266 return -1;
5267
5268#define ADD_CHECKED_OBJECT(m, name, obj) \
5269 { \
5270 PyObject *value = obj; \
5271 if (!value) \
5272 return -1; \
5273 ADD_OBJECT(m, name, value); \
5274 }
5275
5276 static int
5277populate_module(PyObject *m, object_adder add_object)
5278{
5279 int i;
5280
5281 for (i = 0; i < (int)(sizeof(numeric_constants)
5282 / sizeof(struct numeric_constant));
5283 ++i)
5284 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5285 PyInt_FromLong(numeric_constants[i].value));
5286
5287 for (i = 0; i < (int)(sizeof(object_constants)
5288 / sizeof(struct object_constant));
5289 ++i)
5290 {
5291 PyObject *value;
5292
5293 value = object_constants[i].value;
5294 Py_INCREF(value);
5295 ADD_OBJECT(m, object_constants[i].name, value);
5296 }
5297
5298 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5299 return -1;
5300 ADD_OBJECT(m, "error", VimError);
5301
Bram Moolenaara9922d62013-05-30 13:01:18 +02005302 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5303 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005304 ADD_CHECKED_OBJECT(m, "options",
5305 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
5306 return 0;
5307}