blob: 2ad19c4062255b502b1610ac5bd3f045b6f3a1d2 [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 \
34 PyObject *dictkey_todecref;
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 Moolenaarcabf80f2013-05-17 16:18:33 +020066static PyObject *WindowNew(win_T *, tabpage_T *);
67static PyObject *BufferNew (buf_T *);
68static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020069
70static PyInt RangeStart;
71static PyInt RangeEnd;
72
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020073static PyObject *globals;
74
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020075/*
76 * obtain a lock on the Vim data structures
77 */
78 static void
79Python_Lock_Vim(void)
80{
81}
82
83/*
84 * release a lock on the Vim data structures
85 */
86 static void
87Python_Release_Vim(void)
88{
89}
90
Bram Moolenaare9ba5162013-05-29 22:02:22 +020091/*
92 * The "todecref" argument holds a pointer to PyObject * that must be
93 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
94 * was needed to generate returned value is object.
95 *
96 * Use Py_XDECREF to decrement reference count.
97 */
98 static char_u *
99StringToChars(PyObject *object, PyObject **todecref)
100{
101 char_u *p;
102 PyObject *bytes = NULL;
103
104 if (PyBytes_Check(object))
105 {
106
107 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
108 return NULL;
109 if (p == NULL)
110 return NULL;
111
112 *todecref = NULL;
113 }
114 else if (PyUnicode_Check(object))
115 {
116 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
117 if (bytes == NULL)
118 return NULL;
119
120 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
121 return NULL;
122 if (p == NULL)
123 return NULL;
124
125 *todecref = bytes;
126 }
127 else
128 {
129 PyErr_SetString(PyExc_TypeError, _("object must be string"));
130 return NULL;
131 }
132
133 return (char_u *) p;
134}
135
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200136 static int
137add_string(PyObject *list, char *s)
138{
139 PyObject *string;
140
141 if (!(string = PyString_FromString(s)))
142 return -1;
143 if (PyList_Append(list, string))
144 {
145 Py_DECREF(string);
146 return -1;
147 }
148
149 Py_DECREF(string);
150 return 0;
151}
152
153 static PyObject *
154ObjectDir(PyObject *self, char **attributes)
155{
156 PyMethodDef *method;
157 char **attr;
158 PyObject *r;
159
160 if (!(r = PyList_New(0)))
161 return NULL;
162
163 if (self)
164 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
165 if (add_string(r, (char *) method->ml_name))
166 {
167 Py_DECREF(r);
168 return NULL;
169 }
170
171 for (attr = attributes ; *attr ; ++attr)
172 if (add_string(r, *attr))
173 {
174 Py_DECREF(r);
175 return NULL;
176 }
177
178#if PY_MAJOR_VERSION < 3
179 if (add_string(r, "__members__"))
180 {
181 Py_DECREF(r);
182 return NULL;
183 }
184#endif
185
186 return r;
187}
188
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200189/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200190 */
191
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200192/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200193typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200194
195static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200196
197typedef struct
198{
199 PyObject_HEAD
200 long softspace;
201 long error;
202} OutputObject;
203
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200204static char *OutputAttrs[] = {
205 "softspace",
206 NULL
207};
208
209 static PyObject *
210OutputDir(PyObject *self)
211{
212 return ObjectDir(self, OutputAttrs);
213}
214
Bram Moolenaar77045652012-09-21 13:46:06 +0200215 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200216OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200217{
218 if (val == NULL)
219 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200220 PyErr_SetString(PyExc_AttributeError,
221 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200222 return -1;
223 }
224
225 if (strcmp(name, "softspace") == 0)
226 {
227 if (!PyInt_Check(val))
228 {
229 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
230 return -1;
231 }
232
Bram Moolenaard6e39182013-05-21 18:30:34 +0200233 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200234 return 0;
235 }
236
237 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
238 return -1;
239}
240
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200241/* Buffer IO, we write one whole line at a time. */
242static garray_T io_ga = {0, 0, 1, 80, NULL};
243static writefn old_fn = NULL;
244
245 static void
246PythonIO_Flush(void)
247{
248 if (old_fn != NULL && io_ga.ga_len > 0)
249 {
250 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
251 old_fn((char_u *)io_ga.ga_data);
252 }
253 io_ga.ga_len = 0;
254}
255
256 static void
257writer(writefn fn, char_u *str, PyInt n)
258{
259 char_u *ptr;
260
261 /* Flush when switching output function. */
262 if (fn != old_fn)
263 PythonIO_Flush();
264 old_fn = fn;
265
266 /* Write each NL separated line. Text after the last NL is kept for
267 * writing later. */
268 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
269 {
270 PyInt len = ptr - str;
271
272 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
273 break;
274
275 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
276 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
277 fn((char_u *)io_ga.ga_data);
278 str = ptr + 1;
279 n -= len + 1;
280 io_ga.ga_len = 0;
281 }
282
283 /* Put the remaining text into io_ga for later printing. */
284 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
285 {
286 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
287 io_ga.ga_len += (int)n;
288 }
289}
290
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200291 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200292OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200293{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200294 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200295 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200296 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200297
Bram Moolenaar27564802011-09-07 19:30:21 +0200298 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200299 return NULL;
300
301 Py_BEGIN_ALLOW_THREADS
302 Python_Lock_Vim();
303 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
304 Python_Release_Vim();
305 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200306 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200307
308 Py_INCREF(Py_None);
309 return Py_None;
310}
311
312 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200313OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200314{
315 PyInt n;
316 PyInt i;
317 PyObject *list;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200318 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200319
320 if (!PyArg_ParseTuple(args, "O", &list))
321 return NULL;
322 Py_INCREF(list);
323
Bram Moolenaardb913952012-06-29 12:54:53 +0200324 if (!PyList_Check(list))
325 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200326 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
327 Py_DECREF(list);
328 return NULL;
329 }
330
331 n = PyList_Size(list);
332
333 for (i = 0; i < n; ++i)
334 {
335 PyObject *line = PyList_GetItem(list, i);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200336 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200337 PyInt len;
338
Bram Moolenaardb913952012-06-29 12:54:53 +0200339 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
340 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200341 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
342 Py_DECREF(list);
343 return NULL;
344 }
345
346 Py_BEGIN_ALLOW_THREADS
347 Python_Lock_Vim();
348 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
349 Python_Release_Vim();
350 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200351 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200352 }
353
354 Py_DECREF(list);
355 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
881 static PyObject *
882DictionaryNew(dict_T *dict)
883{
884 DictionaryObject *self;
885
886 self = PyObject_NEW(DictionaryObject, &DictionaryType);
887 if (self == NULL)
888 return NULL;
889 self->dict = dict;
890 ++dict->dv_refcount;
891
892 pyll_add((PyObject *)(self), &self->ref, &lastdict);
893
894 return (PyObject *)(self);
895}
896
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200897 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200898DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200899{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200900 pyll_remove(&self->ref, &lastdict);
901 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200902
903 DESTRUCTOR_FINISH(self);
904}
905
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200906static char *DictionaryAttrs[] = {
907 "locked", "scope",
908 NULL
909};
910
911 static PyObject *
912DictionaryDir(PyObject *self)
913{
914 return ObjectDir(self, DictionaryAttrs);
915}
916
Bram Moolenaardb913952012-06-29 12:54:53 +0200917 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200918DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200919{
920 if (val == NULL)
921 {
922 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
923 return -1;
924 }
925
926 if (strcmp(name, "locked") == 0)
927 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200928 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200929 {
930 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
931 return -1;
932 }
933 else
934 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200935 int istrue = PyObject_IsTrue(val);
936 if (istrue == -1)
937 return -1;
938 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200939 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200940 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200941 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200942 }
943 return 0;
944 }
945 else
946 {
947 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
948 return -1;
949 }
950}
951
952 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200953DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200954{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200955 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +0200956}
957
958 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200959DictionaryItem(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200960{
961 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200962 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200963 DICTKEY_DECL
964
Bram Moolenaara03e6312013-05-29 22:49:26 +0200965 DICTKEY_GET(NULL, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +0200966
Bram Moolenaard6e39182013-05-21 18:30:34 +0200967 di = dict_find(self->dict, key, -1);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200968
Bram Moolenaar696c2112012-09-21 13:43:14 +0200969 DICTKEY_UNREF
970
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200971 if (di == NULL)
972 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200973 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200974 return NULL;
975 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200976
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200977 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200978}
979
980 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200981DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200982{
983 char_u *key;
984 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200985 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +0200986 dictitem_T *di;
987 DICTKEY_DECL
988
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200989 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +0200990 {
991 PyErr_SetVim(_("dict is locked"));
992 return -1;
993 }
994
Bram Moolenaara03e6312013-05-29 22:49:26 +0200995 DICTKEY_GET(-1, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +0200996
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200997 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +0200998
999 if (valObject == NULL)
1000 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001001 hashitem_T *hi;
1002
Bram Moolenaardb913952012-06-29 12:54:53 +02001003 if (di == NULL)
1004 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001005 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001006 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001007 return -1;
1008 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001009 hi = hash_find(&dict->dv_hashtab, di->di_key);
1010 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001011 dictitem_free(di);
1012 return 0;
1013 }
1014
1015 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001016 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001017
1018 if (di == NULL)
1019 {
1020 di = dictitem_alloc(key);
1021 if (di == NULL)
1022 {
1023 PyErr_NoMemory();
1024 return -1;
1025 }
1026 di->di_tv.v_lock = 0;
1027
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001028 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001029 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001030 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +02001031 vim_free(di);
1032 PyErr_SetVim(_("failed to add key to dictionary"));
1033 return -1;
1034 }
1035 }
1036 else
1037 clear_tv(&di->di_tv);
1038
1039 DICTKEY_UNREF
1040
1041 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001042 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001043 return 0;
1044}
1045
1046 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001047DictionaryListKeys(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001048{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001049 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001050 long_u todo = dict->dv_hashtab.ht_used;
1051 Py_ssize_t i = 0;
1052 PyObject *r;
1053 hashitem_T *hi;
1054
1055 r = PyList_New(todo);
1056 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1057 {
1058 if (!HASHITEM_EMPTY(hi))
1059 {
1060 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
1061 --todo;
1062 ++i;
1063 }
1064 }
1065 return r;
1066}
1067
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001068static PyMappingMethods DictionaryAsMapping = {
1069 (lenfunc) DictionaryLength,
1070 (binaryfunc) DictionaryItem,
1071 (objobjargproc) DictionaryAssItem,
1072};
1073
Bram Moolenaardb913952012-06-29 12:54:53 +02001074static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001075 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001076 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1077 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001078};
1079
1080static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001081static PySequenceMethods ListAsSeq;
1082static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001083
1084typedef struct
1085{
1086 PyObject_HEAD
1087 list_T *list;
1088 pylinkedlist_T ref;
1089} ListObject;
1090
1091 static PyObject *
1092ListNew(list_T *list)
1093{
1094 ListObject *self;
1095
1096 self = PyObject_NEW(ListObject, &ListType);
1097 if (self == NULL)
1098 return NULL;
1099 self->list = list;
1100 ++list->lv_refcount;
1101
1102 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1103
1104 return (PyObject *)(self);
1105}
1106
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001107 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001108ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001109{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001110 pyll_remove(&self->ref, &lastlist);
1111 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001112
1113 DESTRUCTOR_FINISH(self);
1114}
1115
Bram Moolenaardb913952012-06-29 12:54:53 +02001116 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001117list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001118{
1119 Py_ssize_t i;
1120 Py_ssize_t lsize = PySequence_Size(obj);
1121 PyObject *litem;
1122 listitem_T *li;
1123
1124 for(i=0; i<lsize; i++)
1125 {
1126 li = listitem_alloc();
1127 if (li == NULL)
1128 {
1129 PyErr_NoMemory();
1130 return -1;
1131 }
1132 li->li_tv.v_lock = 0;
1133
1134 litem = PySequence_GetItem(obj, i);
1135 if (litem == NULL)
1136 return -1;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001137 if (_ConvertFromPyObject(litem, &li->li_tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001138 return -1;
1139
1140 list_append(l, li);
1141 }
1142 return 0;
1143}
1144
Bram Moolenaardb913952012-06-29 12:54:53 +02001145 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001146ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001147{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001148 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001149}
1150
1151 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001152ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001153{
1154 listitem_T *li;
1155
Bram Moolenaard6e39182013-05-21 18:30:34 +02001156 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001157 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001158 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001159 return NULL;
1160 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001161 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001162 if (li == NULL)
1163 {
1164 PyErr_SetVim(_("internal error: failed to get vim list item"));
1165 return NULL;
1166 }
1167 return ConvertToPyObject(&li->li_tv);
1168}
1169
1170#define PROC_RANGE \
1171 if (last < 0) {\
1172 if (last < -size) \
1173 last = 0; \
1174 else \
1175 last += size; \
1176 } \
1177 if (first < 0) \
1178 first = 0; \
1179 if (first > size) \
1180 first = size; \
1181 if (last > size) \
1182 last = size;
1183
1184 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001185ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001186{
1187 PyInt i;
1188 PyInt size = ListLength(self);
1189 PyInt n;
1190 PyObject *list;
1191 int reversed = 0;
1192
1193 PROC_RANGE
1194 if (first >= last)
1195 first = last;
1196
1197 n = last-first;
1198 list = PyList_New(n);
1199 if (list == NULL)
1200 return NULL;
1201
1202 for (i = 0; i < n; ++i)
1203 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001204 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001205 if (item == NULL)
1206 {
1207 Py_DECREF(list);
1208 return NULL;
1209 }
1210
1211 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1212 {
1213 Py_DECREF(item);
1214 Py_DECREF(list);
1215 return NULL;
1216 }
1217 }
1218
1219 return list;
1220}
1221
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001222typedef struct
1223{
1224 listwatch_T lw;
1225 list_T *list;
1226} listiterinfo_T;
1227
1228 static void
1229ListIterDestruct(listiterinfo_T *lii)
1230{
1231 list_rem_watch(lii->list, &lii->lw);
1232 PyMem_Free(lii);
1233}
1234
1235 static PyObject *
1236ListIterNext(listiterinfo_T **lii)
1237{
1238 PyObject *r;
1239
1240 if (!((*lii)->lw.lw_item))
1241 return NULL;
1242
1243 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1244 return NULL;
1245
1246 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1247
1248 return r;
1249}
1250
1251 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001252ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001253{
1254 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001255 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001256
1257 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1258 {
1259 PyErr_NoMemory();
1260 return NULL;
1261 }
1262
1263 list_add_watch(l, &lii->lw);
1264 lii->lw.lw_item = l->lv_first;
1265 lii->list = l;
1266
1267 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001268 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1269 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001270}
1271
Bram Moolenaardb913952012-06-29 12:54:53 +02001272 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001273ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001274{
1275 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001276 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001277 listitem_T *li;
1278 Py_ssize_t length = ListLength(self);
1279
1280 if (l->lv_lock)
1281 {
1282 PyErr_SetVim(_("list is locked"));
1283 return -1;
1284 }
1285 if (index>length || (index==length && obj==NULL))
1286 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001287 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001288 return -1;
1289 }
1290
1291 if (obj == NULL)
1292 {
1293 li = list_find(l, (long) index);
1294 list_remove(l, li, li);
1295 clear_tv(&li->li_tv);
1296 vim_free(li);
1297 return 0;
1298 }
1299
1300 if (ConvertFromPyObject(obj, &tv) == -1)
1301 return -1;
1302
1303 if (index == length)
1304 {
1305 if (list_append_tv(l, &tv) == FAIL)
1306 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001307 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001308 PyErr_SetVim(_("Failed to add item to list"));
1309 return -1;
1310 }
1311 }
1312 else
1313 {
1314 li = list_find(l, (long) index);
1315 clear_tv(&li->li_tv);
1316 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001317 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001318 }
1319 return 0;
1320}
1321
1322 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001323ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001324{
1325 PyInt size = ListLength(self);
1326 Py_ssize_t i;
1327 Py_ssize_t lsize;
1328 PyObject *litem;
1329 listitem_T *li;
1330 listitem_T *next;
1331 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001332 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001333
1334 if (l->lv_lock)
1335 {
1336 PyErr_SetVim(_("list is locked"));
1337 return -1;
1338 }
1339
1340 PROC_RANGE
1341
1342 if (first == size)
1343 li = NULL;
1344 else
1345 {
1346 li = list_find(l, (long) first);
1347 if (li == NULL)
1348 {
1349 PyErr_SetVim(_("internal error: no vim list item"));
1350 return -1;
1351 }
1352 if (last > first)
1353 {
1354 i = last - first;
1355 while (i-- && li != NULL)
1356 {
1357 next = li->li_next;
1358 listitem_remove(l, li);
1359 li = next;
1360 }
1361 }
1362 }
1363
1364 if (obj == NULL)
1365 return 0;
1366
1367 if (!PyList_Check(obj))
1368 {
1369 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1370 return -1;
1371 }
1372
1373 lsize = PyList_Size(obj);
1374
1375 for(i=0; i<lsize; i++)
1376 {
1377 litem = PyList_GetItem(obj, i);
1378 if (litem == NULL)
1379 return -1;
1380 if (ConvertFromPyObject(litem, &v) == -1)
1381 return -1;
1382 if (list_insert_tv(l, &v, li) == FAIL)
1383 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001384 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001385 PyErr_SetVim(_("internal error: failed to add item to list"));
1386 return -1;
1387 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001388 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001389 }
1390 return 0;
1391}
1392
1393 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001394ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001395{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001396 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001397 PyObject *lookup_dict;
1398
1399 if (l->lv_lock)
1400 {
1401 PyErr_SetVim(_("list is locked"));
1402 return NULL;
1403 }
1404
1405 if (!PySequence_Check(obj))
1406 {
1407 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1408 return NULL;
1409 }
1410
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001411 if (!(lookup_dict = PyDict_New()))
1412 return NULL;
1413
Bram Moolenaardb913952012-06-29 12:54:53 +02001414 if (list_py_concat(l, obj, lookup_dict) == -1)
1415 {
1416 Py_DECREF(lookup_dict);
1417 return NULL;
1418 }
1419 Py_DECREF(lookup_dict);
1420
1421 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001422 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001423}
1424
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001425static char *ListAttrs[] = {
1426 "locked",
1427 NULL
1428};
1429
1430 static PyObject *
1431ListDir(PyObject *self)
1432{
1433 return ObjectDir(self, ListAttrs);
1434}
1435
Bram Moolenaar66b79852012-09-21 14:00:35 +02001436 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001437ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001438{
1439 if (val == NULL)
1440 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001441 PyErr_SetString(PyExc_AttributeError,
1442 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001443 return -1;
1444 }
1445
1446 if (strcmp(name, "locked") == 0)
1447 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001448 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001449 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001450 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001451 return -1;
1452 }
1453 else
1454 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001455 int istrue = PyObject_IsTrue(val);
1456 if (istrue == -1)
1457 return -1;
1458 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001459 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001460 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001461 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001462 }
1463 return 0;
1464 }
1465 else
1466 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001467 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001468 return -1;
1469 }
1470}
1471
Bram Moolenaardb913952012-06-29 12:54:53 +02001472static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001473 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1474 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
1475 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001476};
1477
1478typedef struct
1479{
1480 PyObject_HEAD
1481 char_u *name;
1482} FunctionObject;
1483
1484static PyTypeObject FunctionType;
1485
1486 static PyObject *
1487FunctionNew(char_u *name)
1488{
1489 FunctionObject *self;
1490
1491 self = PyObject_NEW(FunctionObject, &FunctionType);
1492 if (self == NULL)
1493 return NULL;
1494 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1495 if (self->name == NULL)
1496 {
1497 PyErr_NoMemory();
1498 return NULL;
1499 }
1500 STRCPY(self->name, name);
1501 func_ref(name);
1502 return (PyObject *)(self);
1503}
1504
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001505 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001506FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001507{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001508 func_unref(self->name);
1509 PyMem_Free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001510
1511 DESTRUCTOR_FINISH(self);
1512}
1513
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001514static char *FunctionAttrs[] = {
1515 "softspace",
1516 NULL
1517};
1518
1519 static PyObject *
1520FunctionDir(PyObject *self)
1521{
1522 return ObjectDir(self, FunctionAttrs);
1523}
1524
Bram Moolenaardb913952012-06-29 12:54:53 +02001525 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001526FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02001527{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001528 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02001529 typval_T args;
1530 typval_T selfdicttv;
1531 typval_T rettv;
1532 dict_T *selfdict = NULL;
1533 PyObject *selfdictObject;
1534 PyObject *result;
1535 int error;
1536
1537 if (ConvertFromPyObject(argsObject, &args) == -1)
1538 return NULL;
1539
1540 if (kwargs != NULL)
1541 {
1542 selfdictObject = PyDict_GetItemString(kwargs, "self");
1543 if (selfdictObject != NULL)
1544 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001545 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001546 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001547 PyErr_SetString(PyExc_TypeError,
1548 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001549 clear_tv(&args);
1550 return NULL;
1551 }
1552 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001553 {
1554 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02001555 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001556 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001557 selfdict = selfdicttv.vval.v_dict;
1558 }
1559 }
1560
Bram Moolenaar71700b82013-05-15 17:49:05 +02001561 Py_BEGIN_ALLOW_THREADS
1562 Python_Lock_Vim();
1563
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001564 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02001565 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001566
1567 Python_Release_Vim();
1568 Py_END_ALLOW_THREADS
1569
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001570 if (VimTryEnd())
1571 result = NULL;
1572 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02001573 {
1574 result = NULL;
1575 PyErr_SetVim(_("failed to run function"));
1576 }
1577 else
1578 result = ConvertToPyObject(&rettv);
1579
Bram Moolenaardb913952012-06-29 12:54:53 +02001580 clear_tv(&args);
1581 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001582 if (selfdict != NULL)
1583 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001584
1585 return result;
1586}
1587
1588static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001589 {"__call__",(PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1590 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
1591 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001592};
1593
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001594/*
1595 * Options object
1596 */
1597
1598static PyTypeObject OptionsType;
1599
1600typedef int (*checkfun)(void *);
1601
1602typedef struct
1603{
1604 PyObject_HEAD
1605 int opt_type;
1606 void *from;
1607 checkfun Check;
1608 PyObject *fromObj;
1609} OptionsObject;
1610
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001611 static int
1612dummy_check(void *arg UNUSED)
1613{
1614 return 0;
1615}
1616
1617 static PyObject *
1618OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1619{
1620 OptionsObject *self;
1621
Bram Moolenaar774267b2013-05-21 20:51:59 +02001622 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001623 if (self == NULL)
1624 return NULL;
1625
1626 self->opt_type = opt_type;
1627 self->from = from;
1628 self->Check = Check;
1629 self->fromObj = fromObj;
1630 if (fromObj)
1631 Py_INCREF(fromObj);
1632
1633 return (PyObject *)(self);
1634}
1635
1636 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001637OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001638{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001639 PyObject_GC_UnTrack((void *)(self));
1640 Py_XDECREF(self->fromObj);
1641 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001642}
1643
1644 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001645OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001646{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001647 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001648 return 0;
1649}
1650
1651 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001652OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001653{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001654 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001655 return 0;
1656}
1657
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001658 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001659OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001660{
1661 char_u *key;
1662 int flags;
1663 long numval;
1664 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001665 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001666
Bram Moolenaard6e39182013-05-21 18:30:34 +02001667 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001668 return NULL;
1669
Bram Moolenaara03e6312013-05-29 22:49:26 +02001670 DICTKEY_GET(NULL, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001671
1672 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001673 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001674
1675 DICTKEY_UNREF
1676
1677 if (flags == 0)
1678 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001679 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001680 return NULL;
1681 }
1682
1683 if (flags & SOPT_UNSET)
1684 {
1685 Py_INCREF(Py_None);
1686 return Py_None;
1687 }
1688 else if (flags & SOPT_BOOL)
1689 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001690 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001691 r = numval ? Py_True : Py_False;
1692 Py_INCREF(r);
1693 return r;
1694 }
1695 else if (flags & SOPT_NUM)
1696 return PyInt_FromLong(numval);
1697 else if (flags & SOPT_STRING)
1698 {
1699 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001700 {
1701 PyObject *r = PyBytes_FromString((char *) stringval);
1702 vim_free(stringval);
1703 return r;
1704 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001705 else
1706 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001707 PyErr_SetString(PyExc_RuntimeError,
1708 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001709 return NULL;
1710 }
1711 }
1712 else
1713 {
1714 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1715 return NULL;
1716 }
1717}
1718
1719 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001720set_option_value_err(key, numval, stringval, opt_flags)
1721 char_u *key;
1722 int numval;
1723 char_u *stringval;
1724 int opt_flags;
1725{
1726 char_u *errmsg;
1727
1728 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
1729 {
1730 if (VimTryEnd())
1731 return FAIL;
1732 PyErr_SetVim((char *)errmsg);
1733 return FAIL;
1734 }
1735 return OK;
1736}
1737
1738 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001739set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1740 char_u *key;
1741 int numval;
1742 char_u *stringval;
1743 int opt_flags;
1744 int opt_type;
1745 void *from;
1746{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001747 win_T *save_curwin = NULL;
1748 tabpage_T *save_curtab = NULL;
1749 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001750 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001751
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001752 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001753 switch (opt_type)
1754 {
1755 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001756 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1757 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001758 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001759 if (VimTryEnd())
1760 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001761 PyErr_SetVim("Problem while switching windows.");
1762 return -1;
1763 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001764 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001765 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001766 if (r == FAIL)
1767 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001768 break;
1769 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001770 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001771 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001772 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001773 if (r == FAIL)
1774 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001775 break;
1776 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001777 r = set_option_value_err(key, numval, stringval, opt_flags);
1778 if (r == FAIL)
1779 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001780 break;
1781 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001782 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001783}
1784
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001785 static void *
1786py_memsave(void *p, size_t len)
1787{
1788 void *r;
1789 if (!(r = PyMem_Malloc(len)))
1790 return NULL;
1791 mch_memmove(r, p, len);
1792 return r;
1793}
1794
1795#define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1))
1796
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001797 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001798OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001799{
1800 char_u *key;
1801 int flags;
1802 int opt_flags;
1803 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001804 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001805
Bram Moolenaard6e39182013-05-21 18:30:34 +02001806 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001807 return -1;
1808
Bram Moolenaara03e6312013-05-29 22:49:26 +02001809 DICTKEY_GET(-1, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001810
1811 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001812 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001813
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001814 if (flags == 0)
1815 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001816 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001817 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001818 return -1;
1819 }
1820
1821 if (valObject == NULL)
1822 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001823 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001824 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001825 PyErr_SetString(PyExc_ValueError,
1826 _("unable to unset global option"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001827 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001828 return -1;
1829 }
1830 else if (!(flags & SOPT_GLOBAL))
1831 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001832 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1833 "without global value"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001834 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001835 return -1;
1836 }
1837 else
1838 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001839 unset_global_local_option(key, self->from);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001840 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001841 return 0;
1842 }
1843 }
1844
Bram Moolenaard6e39182013-05-21 18:30:34 +02001845 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001846
1847 if (flags & SOPT_BOOL)
1848 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001849 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001850
Bram Moolenaarb983f752013-05-15 16:11:50 +02001851 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001852 r = -1;
1853 else
1854 r = set_option_value_for(key, istrue, NULL,
1855 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001856 }
1857 else if (flags & SOPT_NUM)
1858 {
1859 int val;
1860
1861#if PY_MAJOR_VERSION < 3
1862 if (PyInt_Check(valObject))
1863 val = PyInt_AsLong(valObject);
1864 else
1865#endif
1866 if (PyLong_Check(valObject))
1867 val = PyLong_AsLong(valObject);
1868 else
1869 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001870 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001871 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001872 return -1;
1873 }
1874
1875 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001876 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001877 }
1878 else
1879 {
1880 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001881 PyObject *todecref;
1882
1883 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001884 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001885 r = set_option_value_for(key, 0, val, opt_flags,
1886 self->opt_type, self->from);
1887 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001888 }
1889 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001890 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001891 }
1892
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001893 DICTKEY_UNREF
1894
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001895 return r;
1896}
1897
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001898static PyMappingMethods OptionsAsMapping = {
1899 (lenfunc) NULL,
1900 (binaryfunc) OptionsItem,
1901 (objobjargproc) OptionsAssItem,
1902};
1903
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001904/* Tabpage object
1905 */
1906
1907typedef struct
1908{
1909 PyObject_HEAD
1910 tabpage_T *tab;
1911} TabPageObject;
1912
1913static PyObject *WinListNew(TabPageObject *tabObject);
1914
1915static PyTypeObject TabPageType;
1916
1917 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001918CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001919{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001920 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001921 {
1922 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1923 return -1;
1924 }
1925
1926 return 0;
1927}
1928
1929 static PyObject *
1930TabPageNew(tabpage_T *tab)
1931{
1932 TabPageObject *self;
1933
1934 if (TAB_PYTHON_REF(tab))
1935 {
1936 self = TAB_PYTHON_REF(tab);
1937 Py_INCREF(self);
1938 }
1939 else
1940 {
1941 self = PyObject_NEW(TabPageObject, &TabPageType);
1942 if (self == NULL)
1943 return NULL;
1944 self->tab = tab;
1945 TAB_PYTHON_REF(tab) = self;
1946 }
1947
1948 return (PyObject *)(self);
1949}
1950
1951 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001952TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001953{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001954 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
1955 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001956
1957 DESTRUCTOR_FINISH(self);
1958}
1959
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001960static char *TabPageAttrs[] = {
1961 "windows", "number", "vars", "window", "valid",
1962 NULL
1963};
1964
1965 static PyObject *
1966TabPageDir(PyObject *self)
1967{
1968 return ObjectDir(self, TabPageAttrs);
1969}
1970
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001971 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001972TabPageAttrValid(TabPageObject *self, char *name)
1973{
1974 PyObject *r;
1975
1976 if (strcmp(name, "valid") != 0)
1977 return NULL;
1978
1979 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
1980 Py_INCREF(r);
1981 return r;
1982}
1983
1984 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001985TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001986{
1987 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001988 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001989 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001990 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001991 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001992 return DictionaryNew(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001993 else if (strcmp(name, "window") == 0)
1994 {
1995 /* For current tab window.c does not bother to set or update tp_curwin
1996 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02001997 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001998 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001999 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002000 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002001 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002002 else if (strcmp(name, "__members__") == 0)
2003 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002004 return NULL;
2005}
2006
2007 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002008TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002009{
2010 static char repr[100];
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002011
Bram Moolenaard6e39182013-05-21 18:30:34 +02002012 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002013 {
2014 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
2015 return PyString_FromString(repr);
2016 }
2017 else
2018 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002019 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002020
2021 if (t == 0)
2022 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
2023 (self));
2024 else
2025 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
2026
2027 return PyString_FromString(repr);
2028 }
2029}
2030
2031static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002032 /* name, function, calling, documentation */
2033 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2034 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002035};
2036
2037/*
2038 * Window list object
2039 */
2040
2041static PyTypeObject TabListType;
2042static PySequenceMethods TabListAsSeq;
2043
2044typedef struct
2045{
2046 PyObject_HEAD
2047} TabListObject;
2048
2049 static PyInt
2050TabListLength(PyObject *self UNUSED)
2051{
2052 tabpage_T *tp = first_tabpage;
2053 PyInt n = 0;
2054
2055 while (tp != NULL)
2056 {
2057 ++n;
2058 tp = tp->tp_next;
2059 }
2060
2061 return n;
2062}
2063
2064 static PyObject *
2065TabListItem(PyObject *self UNUSED, PyInt n)
2066{
2067 tabpage_T *tp;
2068
2069 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2070 if (n == 0)
2071 return TabPageNew(tp);
2072
2073 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2074 return NULL;
2075}
2076
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002077/* Window object
2078 */
2079
2080typedef struct
2081{
2082 PyObject_HEAD
2083 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002084 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002085} WindowObject;
2086
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002087static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002088
2089 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002090CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002091{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002092 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002093 {
2094 PyErr_SetVim(_("attempt to refer to deleted window"));
2095 return -1;
2096 }
2097
2098 return 0;
2099}
2100
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002101 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002102WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002103{
2104 /* We need to handle deletion of windows underneath us.
2105 * If we add a "w_python*_ref" field to the win_T structure,
2106 * then we can get at it in win_free() in vim. We then
2107 * need to create only ONE Python object per window - if
2108 * we try to create a second, just INCREF the existing one
2109 * and return it. The (single) Python object referring to
2110 * the window is stored in "w_python*_ref".
2111 * On a win_free() we set the Python object's win_T* field
2112 * to an invalid value. We trap all uses of a window
2113 * object, and reject them if the win_T* field is invalid.
2114 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002115 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002116 * w_python_ref and w_python3_ref fields respectively.
2117 */
2118
2119 WindowObject *self;
2120
2121 if (WIN_PYTHON_REF(win))
2122 {
2123 self = WIN_PYTHON_REF(win);
2124 Py_INCREF(self);
2125 }
2126 else
2127 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002128 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002129 if (self == NULL)
2130 return NULL;
2131 self->win = win;
2132 WIN_PYTHON_REF(win) = self;
2133 }
2134
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002135 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2136
Bram Moolenaar971db462013-05-12 18:44:48 +02002137 return (PyObject *)(self);
2138}
2139
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002140 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002141WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002142{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002143 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002144 if (self->win && self->win != INVALID_WINDOW_VALUE)
2145 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002146 Py_XDECREF(((PyObject *)(self->tabObject)));
2147 PyObject_GC_Del((void *)(self));
2148}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002149
Bram Moolenaar774267b2013-05-21 20:51:59 +02002150 static int
2151WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2152{
2153 Py_VISIT(((PyObject *)(self->tabObject)));
2154 return 0;
2155}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002156
Bram Moolenaar774267b2013-05-21 20:51:59 +02002157 static int
2158WindowClear(WindowObject *self)
2159{
2160 Py_CLEAR(self->tabObject);
2161 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002162}
2163
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002164 static win_T *
2165get_firstwin(TabPageObject *tabObject)
2166{
2167 if (tabObject)
2168 {
2169 if (CheckTabPage(tabObject))
2170 return NULL;
2171 /* For current tab window.c does not bother to set or update tp_firstwin
2172 */
2173 else if (tabObject->tab == curtab)
2174 return firstwin;
2175 else
2176 return tabObject->tab->tp_firstwin;
2177 }
2178 else
2179 return firstwin;
2180}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002181static char *WindowAttrs[] = {
2182 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2183 "tabpage", "valid",
2184 NULL
2185};
2186
2187 static PyObject *
2188WindowDir(PyObject *self)
2189{
2190 return ObjectDir(self, WindowAttrs);
2191}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002192
Bram Moolenaar971db462013-05-12 18:44:48 +02002193 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002194WindowAttrValid(WindowObject *self, char *name)
2195{
2196 PyObject *r;
2197
2198 if (strcmp(name, "valid") != 0)
2199 return NULL;
2200
2201 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2202 Py_INCREF(r);
2203 return r;
2204}
2205
2206 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002207WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002208{
2209 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002210 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002211 else if (strcmp(name, "cursor") == 0)
2212 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002213 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002214
2215 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2216 }
2217 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002218 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002219#ifdef FEAT_WINDOWS
2220 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002221 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002222#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002223#ifdef FEAT_VERTSPLIT
2224 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002225 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002226 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002227 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002228#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002229 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002230 return DictionaryNew(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002231 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002232 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2233 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002234 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002235 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002236 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002237 return NULL;
2238 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002239 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002240 }
2241 else if (strcmp(name, "tabpage") == 0)
2242 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002243 Py_INCREF(self->tabObject);
2244 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002245 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002246 else if (strcmp(name, "__members__") == 0)
2247 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002248 else
2249 return NULL;
2250}
2251
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002252 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002253WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002254{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002255 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002256 return -1;
2257
2258 if (strcmp(name, "buffer") == 0)
2259 {
2260 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2261 return -1;
2262 }
2263 else if (strcmp(name, "cursor") == 0)
2264 {
2265 long lnum;
2266 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002267
2268 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2269 return -1;
2270
Bram Moolenaard6e39182013-05-21 18:30:34 +02002271 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002272 {
2273 PyErr_SetVim(_("cursor position outside buffer"));
2274 return -1;
2275 }
2276
2277 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002278 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002279 return -1;
2280
Bram Moolenaard6e39182013-05-21 18:30:34 +02002281 self->win->w_cursor.lnum = lnum;
2282 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002283#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002284 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002285#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002286 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002287 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002288
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002289 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002290 return 0;
2291 }
2292 else if (strcmp(name, "height") == 0)
2293 {
2294 int height;
2295 win_T *savewin;
2296
2297 if (!PyArg_Parse(val, "i", &height))
2298 return -1;
2299
2300#ifdef FEAT_GUI
2301 need_mouse_correct = TRUE;
2302#endif
2303 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002304 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002305
2306 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002307 win_setheight(height);
2308 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002309 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002310 return -1;
2311
2312 return 0;
2313 }
2314#ifdef FEAT_VERTSPLIT
2315 else if (strcmp(name, "width") == 0)
2316 {
2317 int width;
2318 win_T *savewin;
2319
2320 if (!PyArg_Parse(val, "i", &width))
2321 return -1;
2322
2323#ifdef FEAT_GUI
2324 need_mouse_correct = TRUE;
2325#endif
2326 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002327 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002328
2329 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002330 win_setwidth(width);
2331 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002332 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002333 return -1;
2334
2335 return 0;
2336 }
2337#endif
2338 else
2339 {
2340 PyErr_SetString(PyExc_AttributeError, name);
2341 return -1;
2342 }
2343}
2344
2345 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002346WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002347{
2348 static char repr[100];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002349
Bram Moolenaard6e39182013-05-21 18:30:34 +02002350 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002351 {
2352 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2353 return PyString_FromString(repr);
2354 }
2355 else
2356 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002357 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002358
Bram Moolenaar6d216452013-05-12 19:00:41 +02002359 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002360 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2361 (self));
2362 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002363 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002364
2365 return PyString_FromString(repr);
2366 }
2367}
2368
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002369static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002370 /* name, function, calling, documentation */
2371 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2372 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002373};
2374
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002375/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002376 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002377 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002378
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002379static PyTypeObject WinListType;
2380static PySequenceMethods WinListAsSeq;
2381
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002382typedef struct
2383{
2384 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002385 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002386} WinListObject;
2387
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002388 static PyObject *
2389WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002390{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002391 WinListObject *self;
2392
2393 self = PyObject_NEW(WinListObject, &WinListType);
2394 self->tabObject = tabObject;
2395 Py_INCREF(tabObject);
2396
2397 return (PyObject *)(self);
2398}
2399
2400 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002401WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002402{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002403 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002404
2405 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02002406 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002407 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02002408 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002409
2410 DESTRUCTOR_FINISH(self);
2411}
2412
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002413 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002414WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002415{
2416 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002417 PyInt n = 0;
2418
Bram Moolenaard6e39182013-05-21 18:30:34 +02002419 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002420 return -1;
2421
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002422 while (w != NULL)
2423 {
2424 ++n;
2425 w = W_NEXT(w);
2426 }
2427
2428 return n;
2429}
2430
2431 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002432WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002433{
2434 win_T *w;
2435
Bram Moolenaard6e39182013-05-21 18:30:34 +02002436 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002437 return NULL;
2438
2439 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002440 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002441 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002442
2443 PyErr_SetString(PyExc_IndexError, _("no such window"));
2444 return NULL;
2445}
2446
2447/* Convert a Python string into a Vim line.
2448 *
2449 * The result is in allocated memory. All internal nulls are replaced by
2450 * newline characters. It is an error for the string to contain newline
2451 * characters.
2452 *
2453 * On errors, the Python exception data is set, and NULL is returned.
2454 */
2455 static char *
2456StringToLine(PyObject *obj)
2457{
2458 const char *str;
2459 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002460 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002461 PyInt len;
2462 PyInt i;
2463 char *p;
2464
2465 if (obj == NULL || !PyString_Check(obj))
2466 {
2467 PyErr_BadArgument();
2468 return NULL;
2469 }
2470
Bram Moolenaar19e60942011-06-19 00:27:51 +02002471 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2472 str = PyString_AsString(bytes);
2473 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002474
2475 /*
2476 * Error checking: String must not contain newlines, as we
2477 * are replacing a single line, and we must replace it with
2478 * a single line.
2479 * A trailing newline is removed, so that append(f.readlines()) works.
2480 */
2481 p = memchr(str, '\n', len);
2482 if (p != NULL)
2483 {
2484 if (p == str + len - 1)
2485 --len;
2486 else
2487 {
2488 PyErr_SetVim(_("string cannot contain newlines"));
2489 return NULL;
2490 }
2491 }
2492
2493 /* Create a copy of the string, with internal nulls replaced by
2494 * newline characters, as is the vim convention.
2495 */
2496 save = (char *)alloc((unsigned)(len+1));
2497 if (save == NULL)
2498 {
2499 PyErr_NoMemory();
2500 return NULL;
2501 }
2502
2503 for (i = 0; i < len; ++i)
2504 {
2505 if (str[i] == '\0')
2506 save[i] = '\n';
2507 else
2508 save[i] = str[i];
2509 }
2510
2511 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002512 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002513
2514 return save;
2515}
2516
2517/* Get a line from the specified buffer. The line number is
2518 * in Vim format (1-based). The line is returned as a Python
2519 * string object.
2520 */
2521 static PyObject *
2522GetBufferLine(buf_T *buf, PyInt n)
2523{
2524 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2525}
2526
2527
2528/* Get a list of lines from the specified buffer. The line numbers
2529 * are in Vim format (1-based). The range is from lo up to, but not
2530 * including, hi. The list is returned as a Python list of string objects.
2531 */
2532 static PyObject *
2533GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2534{
2535 PyInt i;
2536 PyInt n = hi - lo;
2537 PyObject *list = PyList_New(n);
2538
2539 if (list == NULL)
2540 return NULL;
2541
2542 for (i = 0; i < n; ++i)
2543 {
2544 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2545
2546 /* Error check - was the Python string creation OK? */
2547 if (str == NULL)
2548 {
2549 Py_DECREF(list);
2550 return NULL;
2551 }
2552
2553 /* Set the list item */
2554 if (PyList_SetItem(list, i, str))
2555 {
2556 Py_DECREF(str);
2557 Py_DECREF(list);
2558 return NULL;
2559 }
2560 }
2561
2562 /* The ownership of the Python list is passed to the caller (ie,
2563 * the caller should Py_DECREF() the object when it is finished
2564 * with it).
2565 */
2566
2567 return list;
2568}
2569
2570/*
2571 * Check if deleting lines made the cursor position invalid.
2572 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2573 * deleted).
2574 */
2575 static void
2576py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2577{
2578 if (curwin->w_cursor.lnum >= lo)
2579 {
2580 /* Adjust the cursor position if it's in/after the changed
2581 * lines. */
2582 if (curwin->w_cursor.lnum >= hi)
2583 {
2584 curwin->w_cursor.lnum += extra;
2585 check_cursor_col();
2586 }
2587 else if (extra < 0)
2588 {
2589 curwin->w_cursor.lnum = lo;
2590 check_cursor();
2591 }
2592 else
2593 check_cursor_col();
2594 changed_cline_bef_curs();
2595 }
2596 invalidate_botline();
2597}
2598
Bram Moolenaar19e60942011-06-19 00:27:51 +02002599/*
2600 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002601 * in Vim format (1-based). The replacement line is given as
2602 * a Python string object. The object is checked for validity
2603 * and correct format. Errors are returned as a value of FAIL.
2604 * The return value is OK on success.
2605 * If OK is returned and len_change is not NULL, *len_change
2606 * is set to the change in the buffer length.
2607 */
2608 static int
2609SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2610{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002611 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002612 * There are three cases:
2613 * 1. NULL, or None - this is a deletion.
2614 * 2. A string - this is a replacement.
2615 * 3. Anything else - this is an error.
2616 */
2617 if (line == Py_None || line == NULL)
2618 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002619 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002620
2621 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002622 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002623
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002624 VimTryStart();
2625
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002626 if (u_savedel((linenr_T)n, 1L) == FAIL)
2627 PyErr_SetVim(_("cannot save undo information"));
2628 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2629 PyErr_SetVim(_("cannot delete line"));
2630 else
2631 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002632 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002633 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2634 deleted_lines_mark((linenr_T)n, 1L);
2635 }
2636
Bram Moolenaar105bc352013-05-17 16:03:57 +02002637 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002638
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002639 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002640 return FAIL;
2641
2642 if (len_change)
2643 *len_change = -1;
2644
2645 return OK;
2646 }
2647 else if (PyString_Check(line))
2648 {
2649 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002650 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002651
2652 if (save == NULL)
2653 return FAIL;
2654
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002655 VimTryStart();
2656
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002657 /* We do not need to free "save" if ml_replace() consumes it. */
2658 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002659 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002660
2661 if (u_savesub((linenr_T)n) == FAIL)
2662 {
2663 PyErr_SetVim(_("cannot save undo information"));
2664 vim_free(save);
2665 }
2666 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2667 {
2668 PyErr_SetVim(_("cannot replace line"));
2669 vim_free(save);
2670 }
2671 else
2672 changed_bytes((linenr_T)n, 0);
2673
Bram Moolenaar105bc352013-05-17 16:03:57 +02002674 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002675
2676 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002677 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002678 check_cursor_col();
2679
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002680 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002681 return FAIL;
2682
2683 if (len_change)
2684 *len_change = 0;
2685
2686 return OK;
2687 }
2688 else
2689 {
2690 PyErr_BadArgument();
2691 return FAIL;
2692 }
2693}
2694
Bram Moolenaar19e60942011-06-19 00:27:51 +02002695/* Replace a range of lines in the specified buffer. The line numbers are in
2696 * Vim format (1-based). The range is from lo up to, but not including, hi.
2697 * The replacement lines are given as a Python list of string objects. The
2698 * list is checked for validity and correct format. Errors are returned as a
2699 * value of FAIL. The return value is OK on success.
2700 * If OK is returned and len_change is not NULL, *len_change
2701 * is set to the change in the buffer length.
2702 */
2703 static int
2704SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2705{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002706 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002707 * There are three cases:
2708 * 1. NULL, or None - this is a deletion.
2709 * 2. A list - this is a replacement.
2710 * 3. Anything else - this is an error.
2711 */
2712 if (list == Py_None || list == NULL)
2713 {
2714 PyInt i;
2715 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002716 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002717
2718 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002719 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002720 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002721
2722 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2723 PyErr_SetVim(_("cannot save undo information"));
2724 else
2725 {
2726 for (i = 0; i < n; ++i)
2727 {
2728 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2729 {
2730 PyErr_SetVim(_("cannot delete line"));
2731 break;
2732 }
2733 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002734 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002735 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2736 deleted_lines_mark((linenr_T)lo, (long)i);
2737 }
2738
Bram Moolenaar105bc352013-05-17 16:03:57 +02002739 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002740
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002741 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002742 return FAIL;
2743
2744 if (len_change)
2745 *len_change = -n;
2746
2747 return OK;
2748 }
2749 else if (PyList_Check(list))
2750 {
2751 PyInt i;
2752 PyInt new_len = PyList_Size(list);
2753 PyInt old_len = hi - lo;
2754 PyInt extra = 0; /* lines added to text, can be negative */
2755 char **array;
2756 buf_T *savebuf;
2757
2758 if (new_len == 0) /* avoid allocating zero bytes */
2759 array = NULL;
2760 else
2761 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002762 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002763 if (array == NULL)
2764 {
2765 PyErr_NoMemory();
2766 return FAIL;
2767 }
2768 }
2769
2770 for (i = 0; i < new_len; ++i)
2771 {
2772 PyObject *line = PyList_GetItem(list, i);
2773
2774 array[i] = StringToLine(line);
2775 if (array[i] == NULL)
2776 {
2777 while (i)
2778 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002779 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002780 return FAIL;
2781 }
2782 }
2783
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002784 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02002785 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002786
2787 // START of region without "return". Must call restore_buffer()!
2788 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002789
2790 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2791 PyErr_SetVim(_("cannot save undo information"));
2792
2793 /* If the size of the range is reducing (ie, new_len < old_len) we
2794 * need to delete some old_len. We do this at the start, by
2795 * repeatedly deleting line "lo".
2796 */
2797 if (!PyErr_Occurred())
2798 {
2799 for (i = 0; i < old_len - new_len; ++i)
2800 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2801 {
2802 PyErr_SetVim(_("cannot delete line"));
2803 break;
2804 }
2805 extra -= i;
2806 }
2807
2808 /* For as long as possible, replace the existing old_len with the
2809 * new old_len. This is a more efficient operation, as it requires
2810 * less memory allocation and freeing.
2811 */
2812 if (!PyErr_Occurred())
2813 {
2814 for (i = 0; i < old_len && i < new_len; ++i)
2815 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2816 == FAIL)
2817 {
2818 PyErr_SetVim(_("cannot replace line"));
2819 break;
2820 }
2821 }
2822 else
2823 i = 0;
2824
2825 /* Now we may need to insert the remaining new old_len. If we do, we
2826 * must free the strings as we finish with them (we can't pass the
2827 * responsibility to vim in this case).
2828 */
2829 if (!PyErr_Occurred())
2830 {
2831 while (i < new_len)
2832 {
2833 if (ml_append((linenr_T)(lo + i - 1),
2834 (char_u *)array[i], 0, FALSE) == FAIL)
2835 {
2836 PyErr_SetVim(_("cannot insert line"));
2837 break;
2838 }
2839 vim_free(array[i]);
2840 ++i;
2841 ++extra;
2842 }
2843 }
2844
2845 /* Free any left-over old_len, as a result of an error */
2846 while (i < new_len)
2847 {
2848 vim_free(array[i]);
2849 ++i;
2850 }
2851
2852 /* Free the array of old_len. All of its contents have now
2853 * been dealt with (either freed, or the responsibility passed
2854 * to vim.
2855 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002856 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002857
2858 /* Adjust marks. Invalidate any which lie in the
2859 * changed range, and move any in the remainder of the buffer.
2860 */
2861 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2862 (long)MAXLNUM, (long)extra);
2863 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2864
Bram Moolenaar105bc352013-05-17 16:03:57 +02002865 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002866 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2867
Bram Moolenaar105bc352013-05-17 16:03:57 +02002868 // END of region without "return".
2869 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002870
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002871 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002872 return FAIL;
2873
2874 if (len_change)
2875 *len_change = new_len - old_len;
2876
2877 return OK;
2878 }
2879 else
2880 {
2881 PyErr_BadArgument();
2882 return FAIL;
2883 }
2884}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002885
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002886/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002887 * The line number is in Vim format (1-based). The lines to be inserted are
2888 * given as a Python list of string objects or as a single string. The lines
2889 * to be added are checked for validity and correct format. Errors are
2890 * returned as a value of FAIL. The return value is OK on success.
2891 * If OK is returned and len_change is not NULL, *len_change
2892 * is set to the change in the buffer length.
2893 */
2894 static int
2895InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2896{
2897 /* First of all, we check the type of the supplied Python object.
2898 * It must be a string or a list, or the call is in error.
2899 */
2900 if (PyString_Check(lines))
2901 {
2902 char *str = StringToLine(lines);
2903 buf_T *savebuf;
2904
2905 if (str == NULL)
2906 return FAIL;
2907
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002908 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002909 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002910 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002911
2912 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2913 PyErr_SetVim(_("cannot save undo information"));
2914 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2915 PyErr_SetVim(_("cannot insert line"));
2916 else
2917 appended_lines_mark((linenr_T)n, 1L);
2918
2919 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002920 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002921 update_screen(VALID);
2922
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002923 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002924 return FAIL;
2925
2926 if (len_change)
2927 *len_change = 1;
2928
2929 return OK;
2930 }
2931 else if (PyList_Check(lines))
2932 {
2933 PyInt i;
2934 PyInt size = PyList_Size(lines);
2935 char **array;
2936 buf_T *savebuf;
2937
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002938 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002939 if (array == NULL)
2940 {
2941 PyErr_NoMemory();
2942 return FAIL;
2943 }
2944
2945 for (i = 0; i < size; ++i)
2946 {
2947 PyObject *line = PyList_GetItem(lines, i);
2948 array[i] = StringToLine(line);
2949
2950 if (array[i] == NULL)
2951 {
2952 while (i)
2953 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002954 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002955 return FAIL;
2956 }
2957 }
2958
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002959 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002960 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002961 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002962
2963 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2964 PyErr_SetVim(_("cannot save undo information"));
2965 else
2966 {
2967 for (i = 0; i < size; ++i)
2968 {
2969 if (ml_append((linenr_T)(n + i),
2970 (char_u *)array[i], 0, FALSE) == FAIL)
2971 {
2972 PyErr_SetVim(_("cannot insert line"));
2973
2974 /* Free the rest of the lines */
2975 while (i < size)
2976 vim_free(array[i++]);
2977
2978 break;
2979 }
2980 vim_free(array[i]);
2981 }
2982 if (i > 0)
2983 appended_lines_mark((linenr_T)n, (long)i);
2984 }
2985
2986 /* Free the array of lines. All of its contents have now
2987 * been freed.
2988 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002989 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002990
Bram Moolenaar105bc352013-05-17 16:03:57 +02002991 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002992 update_screen(VALID);
2993
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002994 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002995 return FAIL;
2996
2997 if (len_change)
2998 *len_change = size;
2999
3000 return OK;
3001 }
3002 else
3003 {
3004 PyErr_BadArgument();
3005 return FAIL;
3006 }
3007}
3008
3009/*
3010 * Common routines for buffers and line ranges
3011 * -------------------------------------------
3012 */
3013
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003014typedef struct
3015{
3016 PyObject_HEAD
3017 buf_T *buf;
3018} BufferObject;
3019
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003020 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003021CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003022{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003023 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003024 {
3025 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3026 return -1;
3027 }
3028
3029 return 0;
3030}
3031
3032 static PyObject *
3033RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3034{
3035 if (CheckBuffer(self))
3036 return NULL;
3037
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003038 if (end == -1)
3039 end = self->buf->b_ml.ml_line_count;
3040
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003041 if (n < 0)
3042 n += end - start + 1;
3043
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003044 if (n < 0 || n > end - start)
3045 {
3046 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3047 return NULL;
3048 }
3049
3050 return GetBufferLine(self->buf, n+start);
3051}
3052
3053 static PyObject *
3054RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3055{
3056 PyInt size;
3057
3058 if (CheckBuffer(self))
3059 return NULL;
3060
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003061 if (end == -1)
3062 end = self->buf->b_ml.ml_line_count;
3063
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003064 size = end - start + 1;
3065
3066 if (lo < 0)
3067 lo = 0;
3068 else if (lo > size)
3069 lo = size;
3070 if (hi < 0)
3071 hi = 0;
3072 if (hi < lo)
3073 hi = lo;
3074 else if (hi > size)
3075 hi = size;
3076
3077 return GetBufferLineList(self->buf, lo+start, hi+start);
3078}
3079
3080 static PyInt
3081RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3082{
3083 PyInt len_change;
3084
3085 if (CheckBuffer(self))
3086 return -1;
3087
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003088 if (end == -1)
3089 end = self->buf->b_ml.ml_line_count;
3090
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003091 if (n < 0)
3092 n += end - start + 1;
3093
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003094 if (n < 0 || n > end - start)
3095 {
3096 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3097 return -1;
3098 }
3099
3100 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3101 return -1;
3102
3103 if (new_end)
3104 *new_end = end + len_change;
3105
3106 return 0;
3107}
3108
Bram Moolenaar19e60942011-06-19 00:27:51 +02003109 static PyInt
3110RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3111{
3112 PyInt size;
3113 PyInt len_change;
3114
3115 /* Self must be a valid buffer */
3116 if (CheckBuffer(self))
3117 return -1;
3118
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003119 if (end == -1)
3120 end = self->buf->b_ml.ml_line_count;
3121
Bram Moolenaar19e60942011-06-19 00:27:51 +02003122 /* Sort out the slice range */
3123 size = end - start + 1;
3124
3125 if (lo < 0)
3126 lo = 0;
3127 else if (lo > size)
3128 lo = size;
3129 if (hi < 0)
3130 hi = 0;
3131 if (hi < lo)
3132 hi = lo;
3133 else if (hi > size)
3134 hi = size;
3135
3136 if (SetBufferLineList(self->buf, lo + start, hi + start,
3137 val, &len_change) == FAIL)
3138 return -1;
3139
3140 if (new_end)
3141 *new_end = end + len_change;
3142
3143 return 0;
3144}
3145
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003146
3147 static PyObject *
3148RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3149{
3150 PyObject *lines;
3151 PyInt len_change;
3152 PyInt max;
3153 PyInt n;
3154
3155 if (CheckBuffer(self))
3156 return NULL;
3157
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003158 if (end == -1)
3159 end = self->buf->b_ml.ml_line_count;
3160
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003161 max = n = end - start + 1;
3162
3163 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3164 return NULL;
3165
3166 if (n < 0 || n > max)
3167 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003168 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003169 return NULL;
3170 }
3171
3172 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3173 return NULL;
3174
3175 if (new_end)
3176 *new_end = end + len_change;
3177
3178 Py_INCREF(Py_None);
3179 return Py_None;
3180}
3181
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003182/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003183 */
3184
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003185static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003186static PySequenceMethods RangeAsSeq;
3187static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003188
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003189typedef struct
3190{
3191 PyObject_HEAD
3192 BufferObject *buf;
3193 PyInt start;
3194 PyInt end;
3195} RangeObject;
3196
3197 static PyObject *
3198RangeNew(buf_T *buf, PyInt start, PyInt end)
3199{
3200 BufferObject *bufr;
3201 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003202 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003203 if (self == NULL)
3204 return NULL;
3205
3206 bufr = (BufferObject *)BufferNew(buf);
3207 if (bufr == NULL)
3208 {
3209 Py_DECREF(self);
3210 return NULL;
3211 }
3212 Py_INCREF(bufr);
3213
3214 self->buf = bufr;
3215 self->start = start;
3216 self->end = end;
3217
3218 return (PyObject *)(self);
3219}
3220
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003221 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003222RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003223{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003224 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003225 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003226 PyObject_GC_Del((void *)(self));
3227}
3228
3229 static int
3230RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3231{
3232 Py_VISIT(((PyObject *)(self->buf)));
3233 return 0;
3234}
3235
3236 static int
3237RangeClear(RangeObject *self)
3238{
3239 Py_CLEAR(self->buf);
3240 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003241}
3242
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003243 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003244RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003245{
3246 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003247 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003248 return -1; /* ??? */
3249
Bram Moolenaard6e39182013-05-21 18:30:34 +02003250 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003251}
3252
3253 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003254RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003255{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003256 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003257}
3258
3259 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003260RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003261{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003262 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003263}
3264
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003265static char *RangeAttrs[] = {
3266 "start", "end",
3267 NULL
3268};
3269
3270 static PyObject *
3271RangeDir(PyObject *self)
3272{
3273 return ObjectDir(self, RangeAttrs);
3274}
3275
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003276 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003277RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003278{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003279 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003280}
3281
3282 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003283RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003284{
3285 static char repr[100];
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003286
Bram Moolenaard6e39182013-05-21 18:30:34 +02003287 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003288 {
3289 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
3290 (self));
3291 return PyString_FromString(repr);
3292 }
3293 else
3294 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003295 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003296 int len;
3297
3298 if (name == NULL)
3299 name = "";
3300 len = (int)strlen(name);
3301
3302 if (len > 45)
3303 name = name + (45 - len);
3304
3305 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
3306 len > 45 ? "..." : "", name,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003307 self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003308
3309 return PyString_FromString(repr);
3310 }
3311}
3312
3313static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003314 /* name, function, calling, documentation */
3315 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003316 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3317 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003318};
3319
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003320static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003321static PySequenceMethods BufferAsSeq;
3322static PyMappingMethods BufferAsMapping;
3323
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003324 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003325BufferNew(buf_T *buf)
3326{
3327 /* We need to handle deletion of buffers underneath us.
3328 * If we add a "b_python*_ref" field to the buf_T structure,
3329 * then we can get at it in buf_freeall() in vim. We then
3330 * need to create only ONE Python object per buffer - if
3331 * we try to create a second, just INCREF the existing one
3332 * and return it. The (single) Python object referring to
3333 * the buffer is stored in "b_python*_ref".
3334 * Question: what to do on a buf_freeall(). We'll probably
3335 * have to either delete the Python object (DECREF it to
3336 * zero - a bad idea, as it leaves dangling refs!) or
3337 * set the buf_T * value to an invalid value (-1?), which
3338 * means we need checks in all access functions... Bah.
3339 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003340 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003341 * b_python_ref and b_python3_ref fields respectively.
3342 */
3343
3344 BufferObject *self;
3345
3346 if (BUF_PYTHON_REF(buf) != NULL)
3347 {
3348 self = BUF_PYTHON_REF(buf);
3349 Py_INCREF(self);
3350 }
3351 else
3352 {
3353 self = PyObject_NEW(BufferObject, &BufferType);
3354 if (self == NULL)
3355 return NULL;
3356 self->buf = buf;
3357 BUF_PYTHON_REF(buf) = self;
3358 }
3359
3360 return (PyObject *)(self);
3361}
3362
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003363 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003364BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003365{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3367 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003368
3369 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003370}
3371
Bram Moolenaar971db462013-05-12 18:44:48 +02003372 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003373BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003374{
3375 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003376 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003377 return -1; /* ??? */
3378
Bram Moolenaard6e39182013-05-21 18:30:34 +02003379 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003380}
3381
3382 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003383BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003384{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003385 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003386}
3387
3388 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003389BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003390{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003391 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003392}
3393
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003394static char *BufferAttrs[] = {
3395 "name", "number", "vars", "options", "valid",
3396 NULL
3397};
3398
3399 static PyObject *
3400BufferDir(PyObject *self)
3401{
3402 return ObjectDir(self, BufferAttrs);
3403}
3404
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003405 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003406BufferAttrValid(BufferObject *self, char *name)
3407{
3408 PyObject *r;
3409
3410 if (strcmp(name, "valid") != 0)
3411 return NULL;
3412
3413 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
3414 Py_INCREF(r);
3415 return r;
3416}
3417
3418 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003419BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003420{
3421 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02003422 return PyString_FromString((self->buf->b_ffname == NULL
3423 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003424 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003425 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003426 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003427 return DictionaryNew(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003428 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003429 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3430 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003431 else if (strcmp(name, "__members__") == 0)
3432 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003433 else
3434 return NULL;
3435}
3436
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003437 static int
3438BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3439{
3440 if (CheckBuffer(self))
3441 return -1;
3442
3443 if (strcmp(name, "name") == 0)
3444 {
3445 char_u *val;
3446 aco_save_T aco;
3447 int r;
3448 PyObject *todecref;
3449
3450 if (!(val = StringToChars(valObject, &todecref)))
3451 return -1;
3452
3453 VimTryStart();
3454 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3455 aucmd_prepbuf(&aco, self->buf);
3456 r = rename_buffer(val);
3457 aucmd_restbuf(&aco);
3458 Py_XDECREF(todecref);
3459 if (VimTryEnd())
3460 return -1;
3461
3462 if (r == FAIL)
3463 {
3464 PyErr_SetVim(_("failed to rename buffer"));
3465 return -1;
3466 }
3467 return 0;
3468 }
3469 else
3470 {
3471 PyErr_SetString(PyExc_AttributeError, name);
3472 return -1;
3473 }
3474}
3475
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003476 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003477BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003478{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003479 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003480}
3481
3482 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003483BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003484{
3485 pos_T *posp;
3486 char *pmark;
3487 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003488 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003489
Bram Moolenaard6e39182013-05-21 18:30:34 +02003490 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003491 return NULL;
3492
3493 if (!PyArg_ParseTuple(args, "s", &pmark))
3494 return NULL;
3495 mark = *pmark;
3496
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003497 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02003498 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003499 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003500 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003501 if (VimTryEnd())
3502 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003503
3504 if (posp == NULL)
3505 {
3506 PyErr_SetVim(_("invalid mark name"));
3507 return NULL;
3508 }
3509
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003510 if (posp->lnum <= 0)
3511 {
3512 /* Or raise an error? */
3513 Py_INCREF(Py_None);
3514 return Py_None;
3515 }
3516
3517 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3518}
3519
3520 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003521BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003522{
3523 PyInt start;
3524 PyInt end;
3525
Bram Moolenaard6e39182013-05-21 18:30:34 +02003526 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003527 return NULL;
3528
3529 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3530 return NULL;
3531
Bram Moolenaard6e39182013-05-21 18:30:34 +02003532 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003533}
3534
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003535 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003536BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003537{
3538 static char repr[100];
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003539
Bram Moolenaard6e39182013-05-21 18:30:34 +02003540 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003541 {
3542 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3543 return PyString_FromString(repr);
3544 }
3545 else
3546 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003547 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003548 PyInt len;
3549
3550 if (name == NULL)
3551 name = "";
3552 len = strlen(name);
3553
3554 if (len > 35)
3555 name = name + (35 - len);
3556
3557 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3558
3559 return PyString_FromString(repr);
3560 }
3561}
3562
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003563static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003564 /* name, function, calling, documentation */
3565 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
3566 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
3567 {"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 +02003568 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
3569 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003570};
3571
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003572/*
3573 * Buffer list object - Implementation
3574 */
3575
3576static PyTypeObject BufMapType;
3577
3578typedef struct
3579{
3580 PyObject_HEAD
3581} BufMapObject;
3582
3583 static PyInt
3584BufMapLength(PyObject *self UNUSED)
3585{
3586 buf_T *b = firstbuf;
3587 PyInt n = 0;
3588
3589 while (b)
3590 {
3591 ++n;
3592 b = b->b_next;
3593 }
3594
3595 return n;
3596}
3597
3598 static PyObject *
3599BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3600{
3601 buf_T *b;
3602 int bnr;
3603
3604#if PY_MAJOR_VERSION < 3
3605 if (PyInt_Check(keyObject))
3606 bnr = PyInt_AsLong(keyObject);
3607 else
3608#endif
3609 if (PyLong_Check(keyObject))
3610 bnr = PyLong_AsLong(keyObject);
3611 else
3612 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003613 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003614 return NULL;
3615 }
3616
3617 b = buflist_findnr(bnr);
3618
3619 if (b)
3620 return BufferNew(b);
3621 else
3622 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003623 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003624 return NULL;
3625 }
3626}
3627
3628 static void
3629BufMapIterDestruct(PyObject *buffer)
3630{
3631 /* Iteration was stopped before all buffers were processed */
3632 if (buffer)
3633 {
3634 Py_DECREF(buffer);
3635 }
3636}
3637
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003638 static int
3639BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3640{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003641 if (buffer)
3642 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003643 return 0;
3644}
3645
3646 static int
3647BufMapIterClear(PyObject **buffer)
3648{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003649 if (*buffer)
3650 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003651 return 0;
3652}
3653
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003654 static PyObject *
3655BufMapIterNext(PyObject **buffer)
3656{
3657 PyObject *next;
3658 PyObject *r;
3659
3660 if (!*buffer)
3661 return NULL;
3662
3663 r = *buffer;
3664
3665 if (CheckBuffer((BufferObject *)(r)))
3666 {
3667 *buffer = NULL;
3668 return NULL;
3669 }
3670
3671 if (!((BufferObject *)(r))->buf->b_next)
3672 next = NULL;
3673 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3674 return NULL;
3675 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02003676 /* Do not increment reference: we no longer hold it (decref), but whoever
3677 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003678 return r;
3679}
3680
3681 static PyObject *
3682BufMapIter(PyObject *self UNUSED)
3683{
3684 PyObject *buffer;
3685
3686 buffer = BufferNew(firstbuf);
3687 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003688 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3689 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003690}
3691
3692static PyMappingMethods BufMapAsMapping = {
3693 (lenfunc) BufMapLength,
3694 (binaryfunc) BufMapItem,
3695 (objobjargproc) 0,
3696};
3697
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003698/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003699 */
3700
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003701static char *CurrentAttrs[] = {
3702 "buffer", "window", "line", "range", "tabpage",
3703 NULL
3704};
3705
3706 static PyObject *
3707CurrentDir(PyObject *self)
3708{
3709 return ObjectDir(self, CurrentAttrs);
3710}
3711
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003712 static PyObject *
3713CurrentGetattr(PyObject *self UNUSED, char *name)
3714{
3715 if (strcmp(name, "buffer") == 0)
3716 return (PyObject *)BufferNew(curbuf);
3717 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003718 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003719 else if (strcmp(name, "tabpage") == 0)
3720 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003721 else if (strcmp(name, "line") == 0)
3722 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3723 else if (strcmp(name, "range") == 0)
3724 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003725 else if (strcmp(name, "__members__") == 0)
3726 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003727 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003728#if PY_MAJOR_VERSION < 3
3729 return Py_FindMethod(WindowMethods, self, name);
3730#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003731 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003732#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003733}
3734
3735 static int
3736CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3737{
3738 if (strcmp(name, "line") == 0)
3739 {
3740 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3741 return -1;
3742
3743 return 0;
3744 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003745 else if (strcmp(name, "buffer") == 0)
3746 {
3747 int count;
3748
3749 if (value->ob_type != &BufferType)
3750 {
3751 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3752 return -1;
3753 }
3754
3755 if (CheckBuffer((BufferObject *)(value)))
3756 return -1;
3757 count = ((BufferObject *)(value))->buf->b_fnum;
3758
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003759 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003760 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3761 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003762 if (VimTryEnd())
3763 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003764 PyErr_SetVim(_("failed to switch to given buffer"));
3765 return -1;
3766 }
3767
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003768 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003769 }
3770 else if (strcmp(name, "window") == 0)
3771 {
3772 int count;
3773
3774 if (value->ob_type != &WindowType)
3775 {
3776 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3777 return -1;
3778 }
3779
3780 if (CheckWindow((WindowObject *)(value)))
3781 return -1;
3782 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3783
3784 if (!count)
3785 {
3786 PyErr_SetString(PyExc_ValueError,
3787 _("failed to find window in the current tab page"));
3788 return -1;
3789 }
3790
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003791 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003792 win_goto(((WindowObject *)(value))->win);
3793 if (((WindowObject *)(value))->win != curwin)
3794 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003795 if (VimTryEnd())
3796 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003797 PyErr_SetString(PyExc_RuntimeError,
3798 _("did not switch to the specified window"));
3799 return -1;
3800 }
3801
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003802 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003803 }
3804 else if (strcmp(name, "tabpage") == 0)
3805 {
3806 if (value->ob_type != &TabPageType)
3807 {
3808 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3809 return -1;
3810 }
3811
3812 if (CheckTabPage((TabPageObject *)(value)))
3813 return -1;
3814
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003815 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003816 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3817 if (((TabPageObject *)(value))->tab != curtab)
3818 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003819 if (VimTryEnd())
3820 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003821 PyErr_SetString(PyExc_RuntimeError,
3822 _("did not switch to the specified tab page"));
3823 return -1;
3824 }
3825
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003826 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003827 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003828 else
3829 {
3830 PyErr_SetString(PyExc_AttributeError, name);
3831 return -1;
3832 }
3833}
3834
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003835static struct PyMethodDef CurrentMethods[] = {
3836 /* name, function, calling, documentation */
3837 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
3838 { NULL, NULL, 0, NULL}
3839};
3840
Bram Moolenaardb913952012-06-29 12:54:53 +02003841 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003842init_range_cmd(exarg_T *eap)
3843{
3844 RangeStart = eap->line1;
3845 RangeEnd = eap->line2;
3846}
3847
3848 static void
3849init_range_eval(typval_T *rettv UNUSED)
3850{
3851 RangeStart = (PyInt) curwin->w_cursor.lnum;
3852 RangeEnd = RangeStart;
3853}
3854
3855 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003856run_cmd(const char *cmd, void *arg UNUSED
3857#ifdef PY_CAN_RECURSE
3858 , PyGILState_STATE *pygilstate UNUSED
3859#endif
3860 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003861{
3862 PyRun_SimpleString((char *) cmd);
3863}
3864
3865static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
3866static int code_hdr_len = 30;
3867
3868 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003869run_do(const char *cmd, void *arg UNUSED
3870#ifdef PY_CAN_RECURSE
3871 , PyGILState_STATE *pygilstate
3872#endif
3873 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003874{
3875 PyInt lnum;
3876 size_t len;
3877 char *code;
3878 int status;
3879 PyObject *pyfunc, *pymain;
3880
Bram Moolenaar4ac66762013-05-28 22:31:46 +02003881 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003882 {
3883 EMSG(_("cannot save undo information"));
3884 return;
3885 }
3886
3887 len = code_hdr_len + STRLEN(cmd);
3888 code = PyMem_New(char, len + 1);
3889 memcpy(code, code_hdr, code_hdr_len);
3890 STRCPY(code + code_hdr_len, cmd);
3891 status = PyRun_SimpleString(code);
3892 PyMem_Free(code);
3893
3894 if (status)
3895 {
3896 EMSG(_("failed to run the code"));
3897 return;
3898 }
3899
3900 status = 0;
3901 pymain = PyImport_AddModule("__main__");
3902 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003903#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003904 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003905#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003906
3907 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
3908 {
3909 PyObject *line, *linenr, *ret;
3910
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003911#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003912 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003913#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003914 if (!(line = GetBufferLine(curbuf, lnum)))
3915 goto err;
3916 if (!(linenr = PyInt_FromLong((long) lnum)))
3917 {
3918 Py_DECREF(line);
3919 goto err;
3920 }
3921 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
3922 Py_DECREF(line);
3923 Py_DECREF(linenr);
3924 if (!ret)
3925 goto err;
3926
3927 if (ret != Py_None)
3928 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
3929 goto err;
3930
3931 Py_XDECREF(ret);
3932 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003933#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003934 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003935#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003936 }
3937 goto out;
3938err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003939#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003940 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003941#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003942 PyErr_PrintEx(0);
3943 PythonIO_Flush();
3944 status = 1;
3945out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003946#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003947 if (!status)
3948 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003949#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003950 Py_DECREF(pyfunc);
3951 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
3952 if (status)
3953 return;
3954 check_cursor();
3955 update_curbuf(NOT_VALID);
3956}
3957
3958 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003959run_eval(const char *cmd, typval_T *rettv
3960#ifdef PY_CAN_RECURSE
3961 , PyGILState_STATE *pygilstate UNUSED
3962#endif
3963 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003964{
3965 PyObject *r;
3966
3967 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
3968 if (r == NULL)
3969 {
3970 if (PyErr_Occurred() && !msg_silent)
3971 PyErr_PrintEx(0);
3972 EMSG(_("E858: Eval did not return a valid python object"));
3973 }
3974 else
3975 {
3976 if (ConvertFromPyObject(r, rettv) == -1)
3977 EMSG(_("E859: Failed to convert returned python object to vim value"));
3978 Py_DECREF(r);
3979 }
3980 PyErr_Clear();
3981}
3982
3983 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02003984set_ref_in_py(const int copyID)
3985{
3986 pylinkedlist_T *cur;
3987 dict_T *dd;
3988 list_T *ll;
3989
3990 if (lastdict != NULL)
3991 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3992 {
3993 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3994 if (dd->dv_copyID != copyID)
3995 {
3996 dd->dv_copyID = copyID;
3997 set_ref_in_ht(&dd->dv_hashtab, copyID);
3998 }
3999 }
4000
4001 if (lastlist != NULL)
4002 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4003 {
4004 ll = ((ListObject *) (cur->pll_obj))->list;
4005 if (ll->lv_copyID != copyID)
4006 {
4007 ll->lv_copyID = copyID;
4008 set_ref_in_list(ll, copyID);
4009 }
4010 }
4011}
4012
4013 static int
4014set_string_copy(char_u *str, typval_T *tv)
4015{
4016 tv->vval.v_string = vim_strsave(str);
4017 if (tv->vval.v_string == NULL)
4018 {
4019 PyErr_NoMemory();
4020 return -1;
4021 }
4022 return 0;
4023}
4024
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004025 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004026pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004027{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004028 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004029 char_u *key;
4030 dictitem_T *di;
4031 PyObject *keyObject;
4032 PyObject *valObject;
4033 Py_ssize_t iter = 0;
4034
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004035 if (!(dict = dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004036 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004037
4038 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004039 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004040
4041 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4042 {
4043 DICTKEY_DECL
4044
Bram Moolenaara03e6312013-05-29 22:49:26 +02004045 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004046 {
4047 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004048 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004049 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004050
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004051 if (!DICTKEY_SET_KEY)
4052 {
4053 dict_unref(dict);
4054 return -1;
4055 }
4056 DICTKEY_CHECK_EMPTY(-1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004057
4058 di = dictitem_alloc(key);
4059
4060 DICTKEY_UNREF
4061
4062 if (di == NULL)
4063 {
4064 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004065 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004066 return -1;
4067 }
4068 di->di_tv.v_lock = 0;
4069
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004070 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004071 {
4072 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004073 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004074 return -1;
4075 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004076
4077 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004078 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004079 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004080 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004081 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004082 PyErr_SetVim(_("failed to add key to dictionary"));
4083 return -1;
4084 }
4085 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004086
4087 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004088 return 0;
4089}
4090
4091 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004092pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004093{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004094 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004095 char_u *key;
4096 dictitem_T *di;
4097 PyObject *list;
4098 PyObject *litem;
4099 PyObject *keyObject;
4100 PyObject *valObject;
4101 Py_ssize_t lsize;
4102
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004103 if (!(dict = dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004104 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004105
4106 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004107 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004108
4109 list = PyMapping_Items(obj);
4110 if (list == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004111 {
4112 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004113 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004114 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004115 lsize = PyList_Size(list);
4116 while (lsize--)
4117 {
4118 DICTKEY_DECL
4119
4120 litem = PyList_GetItem(list, lsize);
4121 if (litem == NULL)
4122 {
4123 Py_DECREF(list);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004124 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004125 return -1;
4126 }
4127
Bram Moolenaara03e6312013-05-29 22:49:26 +02004128 if (!(keyObject = PyTuple_GetItem(litem, 0)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004129 {
4130 Py_DECREF(list);
4131 Py_DECREF(litem);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004132 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004133 return -1;
4134 }
4135
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004136 if (!DICTKEY_SET_KEY)
4137 {
4138 dict_unref(dict);
4139 Py_DECREF(list);
4140 Py_DECREF(litem);
4141 DICTKEY_UNREF
4142 return -1;
4143 }
4144 DICTKEY_CHECK_EMPTY(-1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004145
Bram Moolenaara03e6312013-05-29 22:49:26 +02004146 if (!(valObject = PyTuple_GetItem(litem, 1)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004147 {
4148 Py_DECREF(list);
4149 Py_DECREF(litem);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004150 dict_unref(dict);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004151 DICTKEY_UNREF
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004152 return -1;
4153 }
4154
Bram Moolenaara03e6312013-05-29 22:49:26 +02004155 Py_DECREF(litem);
4156
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004157 di = dictitem_alloc(key);
4158
4159 DICTKEY_UNREF
4160
4161 if (di == NULL)
4162 {
4163 Py_DECREF(list);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004164 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004165 PyErr_NoMemory();
4166 return -1;
4167 }
4168 di->di_tv.v_lock = 0;
4169
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004170 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004171 {
4172 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004173 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004174 Py_DECREF(list);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004175 return -1;
4176 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004177
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004178 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004179 {
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004180 dictitem_free(di);
4181 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004182 Py_DECREF(list);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004183 PyErr_SetVim(_("failed to add key to dictionary"));
4184 return -1;
4185 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004186 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004187 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004188 Py_DECREF(list);
4189 return 0;
4190}
4191
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004192 static list_T *
4193py_list_alloc()
4194{
4195 list_T *r;
4196
4197 if (!(r = list_alloc()))
4198 {
4199 PyErr_NoMemory();
4200 return NULL;
4201 }
4202 ++r->lv_refcount;
4203
4204 return r;
4205}
4206
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004207 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004208pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004209{
4210 list_T *l;
4211
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004212 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004213 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004214
4215 tv->v_type = VAR_LIST;
4216 tv->vval.v_list = l;
4217
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004218 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004219 {
4220 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004221 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004222 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004223
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004224 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004225 return 0;
4226}
4227
4228 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004229pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004230{
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004231 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004232 PyObject *item;
4233 list_T *l;
4234 listitem_T *li;
4235
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004236 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004237 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004238
4239 tv->vval.v_list = l;
4240 tv->v_type = VAR_LIST;
4241
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004242 if (!(iterator = PyObject_GetIter(obj)))
4243 {
4244 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004245 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004246 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004247
Bram Moolenaarc8366792013-05-29 22:46:26 +02004248 while ((item = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004249 {
4250 li = listitem_alloc();
4251 if (li == NULL)
4252 {
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004253 list_unref(l);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004254 Py_DECREF(iterator);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004255 PyErr_NoMemory();
4256 return -1;
4257 }
4258 li->li_tv.v_lock = 0;
4259
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004260 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
Bram Moolenaara03e6312013-05-29 22:49:26 +02004261 {
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004262 list_unref(l);
4263 listitem_free(li);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004264 Py_DECREF(item);
4265 Py_DECREF(iterator);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004266 return -1;
Bram Moolenaara03e6312013-05-29 22:49:26 +02004267 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004268
4269 list_append(l, li);
4270
4271 Py_DECREF(item);
4272 }
4273
4274 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004275
4276 /* Iterator may have finished due to an exception */
4277 if (PyErr_Occurred())
4278 {
4279 list_unref(l);
4280 return -1;
4281 }
4282
4283 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004284 return 0;
4285}
4286
Bram Moolenaardb913952012-06-29 12:54:53 +02004287typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4288
4289 static int
4290convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004291 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004292{
4293 PyObject *capsule;
4294 char hexBuf[sizeof(void *) * 2 + 3];
4295
4296 sprintf(hexBuf, "%p", obj);
4297
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004298# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004299 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004300# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004301 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004302# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004303 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004304 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004305# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004306 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004307# else
4308 capsule = PyCObject_FromVoidPtr(tv, NULL);
4309# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004310 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4311 {
4312 Py_DECREF(capsule);
4313 tv->v_type = VAR_UNKNOWN;
4314 return -1;
4315 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004316 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004317 {
4318 tv->v_type = VAR_UNKNOWN;
4319 return -1;
4320 }
4321 /* As we are not using copy_tv which increments reference count we must
4322 * do it ourself. */
4323 switch(tv->v_type)
4324 {
4325 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4326 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4327 }
4328 }
4329 else
4330 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004331 typval_T *v;
4332
4333# ifdef PY_USE_CAPSULE
4334 v = PyCapsule_GetPointer(capsule, NULL);
4335# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004336 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004337# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004338 copy_tv(v, tv);
4339 }
4340 return 0;
4341}
4342
4343 static int
4344ConvertFromPyObject(PyObject *obj, typval_T *tv)
4345{
4346 PyObject *lookup_dict;
4347 int r;
4348
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004349 if (!(lookup_dict = PyDict_New()))
4350 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004351 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4352 Py_DECREF(lookup_dict);
4353 return r;
4354}
4355
4356 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004357_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004358{
4359 if (obj->ob_type == &DictionaryType)
4360 {
4361 tv->v_type = VAR_DICT;
4362 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4363 ++tv->vval.v_dict->dv_refcount;
4364 }
4365 else if (obj->ob_type == &ListType)
4366 {
4367 tv->v_type = VAR_LIST;
4368 tv->vval.v_list = (((ListObject *)(obj))->list);
4369 ++tv->vval.v_list->lv_refcount;
4370 }
4371 else if (obj->ob_type == &FunctionType)
4372 {
4373 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4374 return -1;
4375
4376 tv->v_type = VAR_FUNC;
4377 func_ref(tv->vval.v_string);
4378 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004379 else if (PyBytes_Check(obj))
4380 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004381 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004382
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004383 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4384 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004385 if (result == NULL)
4386 return -1;
4387
4388 if (set_string_copy(result, tv) == -1)
4389 return -1;
4390
4391 tv->v_type = VAR_STRING;
4392 }
4393 else if (PyUnicode_Check(obj))
4394 {
4395 PyObject *bytes;
4396 char_u *result;
4397
Bram Moolenaardb913952012-06-29 12:54:53 +02004398 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4399 if (bytes == NULL)
4400 return -1;
4401
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004402 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4403 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004404 if (result == NULL)
4405 return -1;
4406
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004407 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004408 {
4409 Py_XDECREF(bytes);
4410 return -1;
4411 }
4412 Py_XDECREF(bytes);
4413
4414 tv->v_type = VAR_STRING;
4415 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004416#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004417 else if (PyInt_Check(obj))
4418 {
4419 tv->v_type = VAR_NUMBER;
4420 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4421 }
4422#endif
4423 else if (PyLong_Check(obj))
4424 {
4425 tv->v_type = VAR_NUMBER;
4426 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4427 }
4428 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004429 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004430#ifdef FEAT_FLOAT
4431 else if (PyFloat_Check(obj))
4432 {
4433 tv->v_type = VAR_FLOAT;
4434 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4435 }
4436#endif
4437 else if (PyIter_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004438 return convert_dl(obj, tv, pyiter_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004439 else if (PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004440 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004441 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004442 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004443 else
4444 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004445 PyErr_SetString(PyExc_TypeError,
4446 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004447 return -1;
4448 }
4449 return 0;
4450}
4451
4452 static PyObject *
4453ConvertToPyObject(typval_T *tv)
4454{
4455 if (tv == NULL)
4456 {
4457 PyErr_SetVim(_("NULL reference passed"));
4458 return NULL;
4459 }
4460 switch (tv->v_type)
4461 {
4462 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004463 return PyBytes_FromString(tv->vval.v_string == NULL
4464 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004465 case VAR_NUMBER:
4466 return PyLong_FromLong((long) tv->vval.v_number);
4467#ifdef FEAT_FLOAT
4468 case VAR_FLOAT:
4469 return PyFloat_FromDouble((double) tv->vval.v_float);
4470#endif
4471 case VAR_LIST:
4472 return ListNew(tv->vval.v_list);
4473 case VAR_DICT:
4474 return DictionaryNew(tv->vval.v_dict);
4475 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004476 return FunctionNew(tv->vval.v_string == NULL
4477 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004478 case VAR_UNKNOWN:
4479 Py_INCREF(Py_None);
4480 return Py_None;
4481 default:
4482 PyErr_SetVim(_("internal error: invalid value type"));
4483 return NULL;
4484 }
4485}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004486
4487typedef struct
4488{
4489 PyObject_HEAD
4490} CurrentObject;
4491static PyTypeObject CurrentType;
4492
4493 static void
4494init_structs(void)
4495{
4496 vim_memset(&OutputType, 0, sizeof(OutputType));
4497 OutputType.tp_name = "vim.message";
4498 OutputType.tp_basicsize = sizeof(OutputObject);
4499 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4500 OutputType.tp_doc = "vim message object";
4501 OutputType.tp_methods = OutputMethods;
4502#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004503 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4504 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004505 OutputType.tp_alloc = call_PyType_GenericAlloc;
4506 OutputType.tp_new = call_PyType_GenericNew;
4507 OutputType.tp_free = call_PyObject_Free;
4508#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004509 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4510 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004511#endif
4512
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004513 vim_memset(&IterType, 0, sizeof(IterType));
4514 IterType.tp_name = "vim.iter";
4515 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02004516 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004517 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004518 IterType.tp_iter = (getiterfunc)IterIter;
4519 IterType.tp_iternext = (iternextfunc)IterNext;
4520 IterType.tp_dealloc = (destructor)IterDestructor;
4521 IterType.tp_traverse = (traverseproc)IterTraverse;
4522 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004523
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004524 vim_memset(&BufferType, 0, sizeof(BufferType));
4525 BufferType.tp_name = "vim.buffer";
4526 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004527 BufferType.tp_dealloc = (destructor)BufferDestructor;
4528 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004529 BufferType.tp_as_sequence = &BufferAsSeq;
4530 BufferType.tp_as_mapping = &BufferAsMapping;
4531 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4532 BufferType.tp_doc = "vim buffer object";
4533 BufferType.tp_methods = BufferMethods;
4534#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004535 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004536 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004537 BufferType.tp_alloc = call_PyType_GenericAlloc;
4538 BufferType.tp_new = call_PyType_GenericNew;
4539 BufferType.tp_free = call_PyObject_Free;
4540#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004541 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004542 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004543#endif
4544
4545 vim_memset(&WindowType, 0, sizeof(WindowType));
4546 WindowType.tp_name = "vim.window";
4547 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004548 WindowType.tp_dealloc = (destructor)WindowDestructor;
4549 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02004550 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004551 WindowType.tp_doc = "vim Window object";
4552 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004553 WindowType.tp_traverse = (traverseproc)WindowTraverse;
4554 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004555#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004556 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
4557 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004558 WindowType.tp_alloc = call_PyType_GenericAlloc;
4559 WindowType.tp_new = call_PyType_GenericNew;
4560 WindowType.tp_free = call_PyObject_Free;
4561#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004562 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
4563 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004564#endif
4565
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004566 vim_memset(&TabPageType, 0, sizeof(TabPageType));
4567 TabPageType.tp_name = "vim.tabpage";
4568 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004569 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
4570 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004571 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
4572 TabPageType.tp_doc = "vim tab page object";
4573 TabPageType.tp_methods = TabPageMethods;
4574#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004575 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004576 TabPageType.tp_alloc = call_PyType_GenericAlloc;
4577 TabPageType.tp_new = call_PyType_GenericNew;
4578 TabPageType.tp_free = call_PyObject_Free;
4579#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004580 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004581#endif
4582
Bram Moolenaardfa38d42013-05-15 13:38:47 +02004583 vim_memset(&BufMapType, 0, sizeof(BufMapType));
4584 BufMapType.tp_name = "vim.bufferlist";
4585 BufMapType.tp_basicsize = sizeof(BufMapObject);
4586 BufMapType.tp_as_mapping = &BufMapAsMapping;
4587 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004588 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004589 BufferType.tp_doc = "vim buffer list";
4590
4591 vim_memset(&WinListType, 0, sizeof(WinListType));
4592 WinListType.tp_name = "vim.windowlist";
4593 WinListType.tp_basicsize = sizeof(WinListType);
4594 WinListType.tp_as_sequence = &WinListAsSeq;
4595 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
4596 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004597 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004598
4599 vim_memset(&TabListType, 0, sizeof(TabListType));
4600 TabListType.tp_name = "vim.tabpagelist";
4601 TabListType.tp_basicsize = sizeof(TabListType);
4602 TabListType.tp_as_sequence = &TabListAsSeq;
4603 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
4604 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004605
4606 vim_memset(&RangeType, 0, sizeof(RangeType));
4607 RangeType.tp_name = "vim.range";
4608 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004609 RangeType.tp_dealloc = (destructor)RangeDestructor;
4610 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004611 RangeType.tp_as_sequence = &RangeAsSeq;
4612 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02004613 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004614 RangeType.tp_doc = "vim Range object";
4615 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004616 RangeType.tp_traverse = (traverseproc)RangeTraverse;
4617 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004618#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004619 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004620 RangeType.tp_alloc = call_PyType_GenericAlloc;
4621 RangeType.tp_new = call_PyType_GenericNew;
4622 RangeType.tp_free = call_PyObject_Free;
4623#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004624 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004625#endif
4626
4627 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4628 CurrentType.tp_name = "vim.currentdata";
4629 CurrentType.tp_basicsize = sizeof(CurrentObject);
4630 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4631 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004632 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004633#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004634 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
4635 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004636#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004637 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
4638 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004639#endif
4640
4641 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4642 DictionaryType.tp_name = "vim.dictionary";
4643 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004644 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004645 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4646 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4647 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4648 DictionaryType.tp_methods = DictionaryMethods;
4649#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004650 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
4651 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004652#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004653 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
4654 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004655#endif
4656
4657 vim_memset(&ListType, 0, sizeof(ListType));
4658 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004659 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004660 ListType.tp_basicsize = sizeof(ListObject);
4661 ListType.tp_as_sequence = &ListAsSeq;
4662 ListType.tp_as_mapping = &ListAsMapping;
4663 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4664 ListType.tp_doc = "list pushing modifications to vim structure";
4665 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004666 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004667#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004668 ListType.tp_getattro = (getattrofunc)ListGetattro;
4669 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004670#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004671 ListType.tp_getattr = (getattrfunc)ListGetattr;
4672 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004673#endif
4674
4675 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004676 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004677 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004678 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
4679 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004680 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4681 FunctionType.tp_doc = "object that calls vim function";
4682 FunctionType.tp_methods = FunctionMethods;
4683#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004684 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004685#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004686 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004687#endif
4688
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004689 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4690 OptionsType.tp_name = "vim.options";
4691 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02004692 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004693 OptionsType.tp_doc = "object for manipulating options";
4694 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004695 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
4696 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
4697 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004698
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004699#if PY_MAJOR_VERSION >= 3
4700 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4701 vimmodule.m_name = "vim";
4702 vimmodule.m_doc = "Vim Python interface\n";
4703 vimmodule.m_size = -1;
4704 vimmodule.m_methods = VimMethods;
4705#endif
4706}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004707
4708#define PYTYPE_READY(type) \
4709 if (PyType_Ready(&type)) \
4710 return -1;
4711
4712 static int
4713init_types()
4714{
4715 PYTYPE_READY(IterType);
4716 PYTYPE_READY(BufferType);
4717 PYTYPE_READY(RangeType);
4718 PYTYPE_READY(WindowType);
4719 PYTYPE_READY(TabPageType);
4720 PYTYPE_READY(BufMapType);
4721 PYTYPE_READY(WinListType);
4722 PYTYPE_READY(TabListType);
4723 PYTYPE_READY(CurrentType);
4724 PYTYPE_READY(DictionaryType);
4725 PYTYPE_READY(ListType);
4726 PYTYPE_READY(FunctionType);
4727 PYTYPE_READY(OptionsType);
4728 PYTYPE_READY(OutputType);
4729 return 0;
4730}
4731
4732static BufMapObject TheBufferMap =
4733{
4734 PyObject_HEAD_INIT(&BufMapType)
4735};
4736
4737static WinListObject TheWindowList =
4738{
4739 PyObject_HEAD_INIT(&WinListType)
4740 NULL
4741};
4742
4743static CurrentObject TheCurrent =
4744{
4745 PyObject_HEAD_INIT(&CurrentType)
4746};
4747
4748static TabListObject TheTabPageList =
4749{
4750 PyObject_HEAD_INIT(&TabListType)
4751};
4752
4753static struct numeric_constant {
4754 char *name;
4755 int value;
4756} numeric_constants[] = {
4757 {"VAR_LOCKED", VAR_LOCKED},
4758 {"VAR_FIXED", VAR_FIXED},
4759 {"VAR_SCOPE", VAR_SCOPE},
4760 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
4761};
4762
4763static struct object_constant {
4764 char *name;
4765 PyObject *value;
4766} object_constants[] = {
4767 {"buffers", (PyObject *)(void *)&TheBufferMap},
4768 {"windows", (PyObject *)(void *)&TheWindowList},
4769 {"tabpages", (PyObject *)(void *)&TheTabPageList},
4770 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02004771
4772 {"Buffer", (PyObject *)&BufferType},
4773 {"Range", (PyObject *)&RangeType},
4774 {"Window", (PyObject *)&WindowType},
4775 {"TabPage", (PyObject *)&TabPageType},
4776 {"Dictionary", (PyObject *)&DictionaryType},
4777 {"List", (PyObject *)&ListType},
4778 {"Function", (PyObject *)&FunctionType},
4779 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004780};
4781
4782typedef int (*object_adder)(PyObject *, const char *, PyObject *);
4783
4784#define ADD_OBJECT(m, name, obj) \
4785 if (add_object(m, name, obj)) \
4786 return -1;
4787
4788#define ADD_CHECKED_OBJECT(m, name, obj) \
4789 { \
4790 PyObject *value = obj; \
4791 if (!value) \
4792 return -1; \
4793 ADD_OBJECT(m, name, value); \
4794 }
4795
4796 static int
4797populate_module(PyObject *m, object_adder add_object)
4798{
4799 int i;
4800
4801 for (i = 0; i < (int)(sizeof(numeric_constants)
4802 / sizeof(struct numeric_constant));
4803 ++i)
4804 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
4805 PyInt_FromLong(numeric_constants[i].value));
4806
4807 for (i = 0; i < (int)(sizeof(object_constants)
4808 / sizeof(struct object_constant));
4809 ++i)
4810 {
4811 PyObject *value;
4812
4813 value = object_constants[i].value;
4814 Py_INCREF(value);
4815 ADD_OBJECT(m, object_constants[i].name, value);
4816 }
4817
4818 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
4819 return -1;
4820 ADD_OBJECT(m, "error", VimError);
4821
4822 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict));
4823 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
4824 ADD_CHECKED_OBJECT(m, "options",
4825 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
4826 return 0;
4827}