blob: eefd6ed82681b4cc2e42807cbca8fae268f857c6 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020016#if PY_VERSION_HEX < 0x02050000
17typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
18#endif
19
Bram Moolenaar91805fc2011-06-26 04:01:44 +020020#ifdef FEAT_MBYTE
21# define ENC_OPT p_enc
22#else
23# define ENC_OPT "latin1"
24#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020025#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020026
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020027#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
28
29#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
30#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020031#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020032
Bram Moolenaare9ba5162013-05-29 22:02:22 +020033#define DICTKEY_DECL \
Bram Moolenaara9922d62013-05-30 13:01:18 +020034 PyObject *dictkey_todecref = NULL;
Bram Moolenaara03e6312013-05-29 22:49:26 +020035#define DICTKEY_GET(err, decref) \
Bram Moolenaarfc714b32013-05-30 14:52:37 +020036 if (!(key = StringToChars(keyObject, &dictkey_todecref))) \
Bram Moolenaara03e6312013-05-29 22:49:26 +020037 { \
38 if (decref) \
39 { \
40 Py_DECREF(keyObject); \
41 } \
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +020042 return err; \
Bram Moolenaara03e6312013-05-29 22:49:26 +020043 } \
44 if (decref && !dictkey_todecref) \
45 dictkey_todecref = keyObject; \
Bram Moolenaarfc714b32013-05-30 14:52:37 +020046 if (*key == NUL) \
47 { \
48 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
49 return err; \
50 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +020051#define DICTKEY_UNREF \
52 Py_XDECREF(dictkey_todecref);
53
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020054typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020055typedef void (*runner)(const char *, void *
56#ifdef PY_CAN_RECURSE
57 , PyGILState_STATE *
58#endif
59 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061static int ConvertFromPyObject(PyObject *, typval_T *);
62static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020063static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020064static PyObject *WindowNew(win_T *, tabpage_T *);
65static PyObject *BufferNew (buf_T *);
66static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020067
68static PyInt RangeStart;
69static PyInt RangeEnd;
70
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020071static PyObject *globals;
72
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020073/*
74 * obtain a lock on the Vim data structures
75 */
76 static void
77Python_Lock_Vim(void)
78{
79}
80
81/*
82 * release a lock on the Vim data structures
83 */
84 static void
85Python_Release_Vim(void)
86{
87}
88
Bram Moolenaare9ba5162013-05-29 22:02:22 +020089/*
90 * The "todecref" argument holds a pointer to PyObject * that must be
91 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
92 * was needed to generate returned value is object.
93 *
94 * Use Py_XDECREF to decrement reference count.
95 */
96 static char_u *
97StringToChars(PyObject *object, PyObject **todecref)
98{
99 char_u *p;
100 PyObject *bytes = NULL;
101
102 if (PyBytes_Check(object))
103 {
104
105 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
106 return NULL;
107 if (p == NULL)
108 return NULL;
109
110 *todecref = NULL;
111 }
112 else if (PyUnicode_Check(object))
113 {
114 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
115 if (bytes == NULL)
116 return NULL;
117
118 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
119 return NULL;
120 if (p == NULL)
121 return NULL;
122
123 *todecref = bytes;
124 }
125 else
126 {
127 PyErr_SetString(PyExc_TypeError, _("object must be string"));
128 return NULL;
129 }
130
131 return (char_u *) p;
132}
133
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200134 static int
135add_string(PyObject *list, char *s)
136{
137 PyObject *string;
138
139 if (!(string = PyString_FromString(s)))
140 return -1;
141 if (PyList_Append(list, string))
142 {
143 Py_DECREF(string);
144 return -1;
145 }
146
147 Py_DECREF(string);
148 return 0;
149}
150
151 static PyObject *
152ObjectDir(PyObject *self, char **attributes)
153{
154 PyMethodDef *method;
155 char **attr;
156 PyObject *r;
157
158 if (!(r = PyList_New(0)))
159 return NULL;
160
161 if (self)
162 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
163 if (add_string(r, (char *) method->ml_name))
164 {
165 Py_DECREF(r);
166 return NULL;
167 }
168
169 for (attr = attributes ; *attr ; ++attr)
170 if (add_string(r, *attr))
171 {
172 Py_DECREF(r);
173 return NULL;
174 }
175
176#if PY_MAJOR_VERSION < 3
177 if (add_string(r, "__members__"))
178 {
179 Py_DECREF(r);
180 return NULL;
181 }
182#endif
183
184 return r;
185}
186
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200187/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200188 */
189
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200190/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200191typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200192
193static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200194
195typedef struct
196{
197 PyObject_HEAD
198 long softspace;
199 long error;
200} OutputObject;
201
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200202static char *OutputAttrs[] = {
203 "softspace",
204 NULL
205};
206
207 static PyObject *
208OutputDir(PyObject *self)
209{
210 return ObjectDir(self, OutputAttrs);
211}
212
Bram Moolenaar77045652012-09-21 13:46:06 +0200213 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200214OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200215{
216 if (val == NULL)
217 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200218 PyErr_SetString(PyExc_AttributeError,
219 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200220 return -1;
221 }
222
223 if (strcmp(name, "softspace") == 0)
224 {
225 if (!PyInt_Check(val))
226 {
227 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
228 return -1;
229 }
230
Bram Moolenaard6e39182013-05-21 18:30:34 +0200231 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200232 return 0;
233 }
234
235 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
236 return -1;
237}
238
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200239/* Buffer IO, we write one whole line at a time. */
240static garray_T io_ga = {0, 0, 1, 80, NULL};
241static writefn old_fn = NULL;
242
243 static void
244PythonIO_Flush(void)
245{
246 if (old_fn != NULL && io_ga.ga_len > 0)
247 {
248 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
249 old_fn((char_u *)io_ga.ga_data);
250 }
251 io_ga.ga_len = 0;
252}
253
254 static void
255writer(writefn fn, char_u *str, PyInt n)
256{
257 char_u *ptr;
258
259 /* Flush when switching output function. */
260 if (fn != old_fn)
261 PythonIO_Flush();
262 old_fn = fn;
263
264 /* Write each NL separated line. Text after the last NL is kept for
265 * writing later. */
266 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
267 {
268 PyInt len = ptr - str;
269
270 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
271 break;
272
273 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
274 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
275 fn((char_u *)io_ga.ga_data);
276 str = ptr + 1;
277 n -= len + 1;
278 io_ga.ga_len = 0;
279 }
280
281 /* Put the remaining text into io_ga for later printing. */
282 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
283 {
284 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
285 io_ga.ga_len += (int)n;
286 }
287}
288
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200289 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200290OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200291{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200292 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200293 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200294 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200295
Bram Moolenaar27564802011-09-07 19:30:21 +0200296 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200297 return NULL;
298
299 Py_BEGIN_ALLOW_THREADS
300 Python_Lock_Vim();
301 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
302 Python_Release_Vim();
303 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200304 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200305
306 Py_INCREF(Py_None);
307 return Py_None;
308}
309
310 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200311OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200312{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200313 PyObject *seq;
314 PyObject *iterator;
315 PyObject *item;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200316 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200317
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200318 if (!PyArg_ParseTuple(args, "O", &seq))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200319 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200320
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200321 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200322 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200323
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200324 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200325 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200326 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200327 PyInt len;
328
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200329 if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
Bram Moolenaardb913952012-06-29 12:54:53 +0200330 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200331 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200332 Py_DECREF(iterator);
333 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200334 return NULL;
335 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200336 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200337
338 Py_BEGIN_ALLOW_THREADS
339 Python_Lock_Vim();
340 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
341 Python_Release_Vim();
342 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200343 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200344 }
345
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200346 Py_DECREF(iterator);
347
348 /* Iterator may have finished due to an exception */
349 if (PyErr_Occurred())
350 return NULL;
351
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200352 Py_INCREF(Py_None);
353 return Py_None;
354}
355
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100356 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200357OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100358{
359 /* do nothing */
360 Py_INCREF(Py_None);
361 return Py_None;
362}
363
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200364/***************/
365
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200366static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200367 /* name, function, calling, doc */
368 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
369 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
370 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200371 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200372 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200373};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200374
375static OutputObject Output =
376{
377 PyObject_HEAD_INIT(&OutputType)
378 0,
379 0
380};
381
382static OutputObject Error =
383{
384 PyObject_HEAD_INIT(&OutputType)
385 0,
386 1
387};
388
389 static int
390PythonIO_Init_io(void)
391{
392 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
393 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
394
395 if (PyErr_Occurred())
396 {
397 EMSG(_("E264: Python: Error initialising I/O objects"));
398 return -1;
399 }
400
401 return 0;
402}
403
404
405static PyObject *VimError;
406
407/* Check to see whether a Vim error has been reported, or a keyboard
408 * interrupt has been detected.
409 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200410
411 static void
412VimTryStart(void)
413{
414 ++trylevel;
415}
416
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200418VimTryEnd(void)
419{
420 --trylevel;
421 if (got_int)
422 {
423 PyErr_SetNone(PyExc_KeyboardInterrupt);
424 return 1;
425 }
426 else if (!did_throw)
427 return 0;
428 else if (PyErr_Occurred())
429 return 1;
430 else
431 {
432 PyErr_SetVim((char *) current_exception->value);
433 discard_current_exception();
434 return 1;
435 }
436}
437
438 static int
439VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440{
441 if (got_int)
442 {
443 PyErr_SetNone(PyExc_KeyboardInterrupt);
444 return 1;
445 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446 return 0;
447}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200448
449/* Vim module - Implementation
450 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200451
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200452 static PyObject *
453VimCommand(PyObject *self UNUSED, PyObject *args)
454{
455 char *cmd;
456 PyObject *result;
457
458 if (!PyArg_ParseTuple(args, "s", &cmd))
459 return NULL;
460
461 PyErr_Clear();
462
463 Py_BEGIN_ALLOW_THREADS
464 Python_Lock_Vim();
465
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200466 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200467 do_cmdline_cmd((char_u *)cmd);
468 update_screen(VALID);
469
470 Python_Release_Vim();
471 Py_END_ALLOW_THREADS
472
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200473 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200474 result = NULL;
475 else
476 result = Py_None;
477
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200478
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200479 Py_XINCREF(result);
480 return result;
481}
482
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200483/*
484 * Function to translate a typval_T into a PyObject; this will recursively
485 * translate lists/dictionaries into their Python equivalents.
486 *
487 * The depth parameter is to avoid infinite recursion, set it to 1 when
488 * you call VimToPython.
489 */
490 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200491VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200492{
493 PyObject *result;
494 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200495 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200496
497 /* Avoid infinite recursion */
498 if (depth > 100)
499 {
500 Py_INCREF(Py_None);
501 result = Py_None;
502 return result;
503 }
504
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200505 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200506 * then and we can use it again. */
507 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
508 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
509 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200510 sprintf(ptrBuf, "%p",
511 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
512 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200513
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200514 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200515 {
516 Py_INCREF(result);
517 return result;
518 }
519 }
520
521 if (our_tv->v_type == VAR_STRING)
522 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200523 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200524 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200525 }
526 else if (our_tv->v_type == VAR_NUMBER)
527 {
528 char buf[NUMBUFLEN];
529
530 /* For backwards compatibility numbers are stored as strings. */
531 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200532 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200533 }
534# ifdef FEAT_FLOAT
535 else if (our_tv->v_type == VAR_FLOAT)
536 {
537 char buf[NUMBUFLEN];
538
539 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200540 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200541 }
542# endif
543 else if (our_tv->v_type == VAR_LIST)
544 {
545 list_T *list = our_tv->vval.v_list;
546 listitem_T *curr;
547
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200548 if (list == NULL)
549 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200550
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200551 if (!(result = PyList_New(0)))
552 return NULL;
553
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200554 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200555 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200556 Py_DECREF(result);
557 return NULL;
558 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200559
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200560 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
561 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200562 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200563 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200564 Py_DECREF(result);
565 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200566 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200567 if (PyList_Append(result, newObj))
568 {
569 Py_DECREF(newObj);
570 Py_DECREF(result);
571 return NULL;
572 }
573 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200574 }
575 }
576 else if (our_tv->v_type == VAR_DICT)
577 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200578
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200579 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
580 long_u todo = ht->ht_used;
581 hashitem_T *hi;
582 dictitem_T *di;
583 if (our_tv->vval.v_dict == NULL)
584 return NULL;
585
586 if (!(result = PyDict_New()))
587 return NULL;
588
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200589 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200590 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200591 Py_DECREF(result);
592 return NULL;
593 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200594
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200595 for (hi = ht->ht_array; todo > 0; ++hi)
596 {
597 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200598 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200599 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200600
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200601 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200602 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200603 {
604 Py_DECREF(result);
605 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200606 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200607 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
608 {
609 Py_DECREF(result);
610 Py_DECREF(newObj);
611 return NULL;
612 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613 }
614 }
615 }
616 else
617 {
618 Py_INCREF(Py_None);
619 result = Py_None;
620 }
621
622 return result;
623}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200624
625 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200626VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200627{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628 char *expr;
629 typval_T *our_tv;
630 PyObject *result;
631 PyObject *lookup_dict;
632
633 if (!PyArg_ParseTuple(args, "s", &expr))
634 return NULL;
635
636 Py_BEGIN_ALLOW_THREADS
637 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200638 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200639 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200640 Python_Release_Vim();
641 Py_END_ALLOW_THREADS
642
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200643 if (VimTryEnd())
644 return NULL;
645
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646 if (our_tv == NULL)
647 {
648 PyErr_SetVim(_("invalid expression"));
649 return NULL;
650 }
651
652 /* Convert the Vim type into a Python type. Create a dictionary that's
653 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200654 if (!(lookup_dict = PyDict_New()))
655 result = NULL;
656 else
657 {
658 result = VimToPython(our_tv, 1, lookup_dict);
659 Py_DECREF(lookup_dict);
660 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200661
662
663 Py_BEGIN_ALLOW_THREADS
664 Python_Lock_Vim();
665 free_tv(our_tv);
666 Python_Release_Vim();
667 Py_END_ALLOW_THREADS
668
669 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200670}
671
Bram Moolenaardb913952012-06-29 12:54:53 +0200672static PyObject *ConvertToPyObject(typval_T *);
673
674 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200675VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200676{
Bram Moolenaardb913952012-06-29 12:54:53 +0200677 char *expr;
678 typval_T *our_tv;
679 PyObject *result;
680
681 if (!PyArg_ParseTuple(args, "s", &expr))
682 return NULL;
683
684 Py_BEGIN_ALLOW_THREADS
685 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200686 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200687 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200688 Python_Release_Vim();
689 Py_END_ALLOW_THREADS
690
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200691 if (VimTryEnd())
692 return NULL;
693
Bram Moolenaardb913952012-06-29 12:54:53 +0200694 if (our_tv == NULL)
695 {
696 PyErr_SetVim(_("invalid expression"));
697 return NULL;
698 }
699
700 result = ConvertToPyObject(our_tv);
701 Py_BEGIN_ALLOW_THREADS
702 Python_Lock_Vim();
703 free_tv(our_tv);
704 Python_Release_Vim();
705 Py_END_ALLOW_THREADS
706
707 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200708}
709
710 static PyObject *
711VimStrwidth(PyObject *self UNUSED, PyObject *args)
712{
713 char *expr;
714
715 if (!PyArg_ParseTuple(args, "s", &expr))
716 return NULL;
717
Bram Moolenaara54bf402012-12-05 16:30:07 +0100718 return PyLong_FromLong(
719#ifdef FEAT_MBYTE
720 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
721#else
722 STRLEN(expr)
723#endif
724 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200725}
726
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200727/*
728 * Vim module - Definitions
729 */
730
731static struct PyMethodDef VimMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200732 /* name, function, calling, documentation */
733 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
734 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
735 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
736 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
737 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200738};
739
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200740/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200741 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200742 */
743
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200744static PyTypeObject IterType;
745
746typedef PyObject *(*nextfun)(void **);
747typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200748typedef int (*traversefun)(void *, visitproc, void *);
749typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200750
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200751/* Main purpose of this object is removing the need for do python
752 * initialization (i.e. PyType_Ready and setting type attributes) for a big
753 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200754
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200755typedef struct
756{
757 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200758 void *cur;
759 nextfun next;
760 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200761 traversefun traverse;
762 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200763} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200764
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200765 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200766IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
767 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200768{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200769 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200770
Bram Moolenaar774267b2013-05-21 20:51:59 +0200771 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200772 self->cur = start;
773 self->next = next;
774 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200775 self->traverse = traverse;
776 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200777
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200778 return (PyObject *)(self);
779}
780
781 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200782IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200783{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200784 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200785 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200786 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787}
788
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200789 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200790IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200791{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200792 if (self->traverse != NULL)
793 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200794 else
795 return 0;
796}
797
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200798/* Mac OSX defines clear() somewhere. */
799#ifdef clear
800# undef clear
801#endif
802
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200803 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200804IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200805{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200806 if (self->clear != NULL)
807 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200808 else
809 return 0;
810}
811
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200812 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200813IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200814{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200815 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200816}
817
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200818 static PyObject *
819IterIter(PyObject *self)
820{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +0200821 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200822 return self;
823}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200824
Bram Moolenaardb913952012-06-29 12:54:53 +0200825typedef struct pylinkedlist_S {
826 struct pylinkedlist_S *pll_next;
827 struct pylinkedlist_S *pll_prev;
828 PyObject *pll_obj;
829} pylinkedlist_T;
830
831static pylinkedlist_T *lastdict = NULL;
832static pylinkedlist_T *lastlist = NULL;
833
834 static void
835pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
836{
837 if (ref->pll_prev == NULL)
838 {
839 if (ref->pll_next == NULL)
840 {
841 *last = NULL;
842 return;
843 }
844 }
845 else
846 ref->pll_prev->pll_next = ref->pll_next;
847
848 if (ref->pll_next == NULL)
849 *last = ref->pll_prev;
850 else
851 ref->pll_next->pll_prev = ref->pll_prev;
852}
853
854 static void
855pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
856{
857 if (*last == NULL)
858 ref->pll_prev = NULL;
859 else
860 {
861 (*last)->pll_next = ref;
862 ref->pll_prev = *last;
863 }
864 ref->pll_next = NULL;
865 ref->pll_obj = self;
866 *last = ref;
867}
868
869static PyTypeObject DictionaryType;
870
871typedef struct
872{
873 PyObject_HEAD
874 dict_T *dict;
875 pylinkedlist_T ref;
876} DictionaryObject;
877
Bram Moolenaara9922d62013-05-30 13:01:18 +0200878static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
879
880#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
881
Bram Moolenaardb913952012-06-29 12:54:53 +0200882 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +0200883DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +0200884{
885 DictionaryObject *self;
886
Bram Moolenaara9922d62013-05-30 13:01:18 +0200887 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200888 if (self == NULL)
889 return NULL;
890 self->dict = dict;
891 ++dict->dv_refcount;
892
893 pyll_add((PyObject *)(self), &self->ref, &lastdict);
894
895 return (PyObject *)(self);
896}
897
Bram Moolenaara9922d62013-05-30 13:01:18 +0200898 static dict_T *
899py_dict_alloc()
900{
901 dict_T *r;
902
903 if (!(r = dict_alloc()))
904 {
905 PyErr_NoMemory();
906 return NULL;
907 }
908 ++r->dv_refcount;
909
910 return r;
911}
912
913 static PyObject *
914DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
915{
916 DictionaryObject *self;
917 dict_T *dict;
918
919 if (!(dict = py_dict_alloc()))
920 return NULL;
921
922 self = (DictionaryObject *) DictionaryNew(subtype, dict);
923
924 --dict->dv_refcount;
925
926 if (kwargs || PyTuple_Size(args))
927 {
928 PyObject *tmp;
929 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
930 {
931 Py_DECREF(self);
932 return NULL;
933 }
934
935 Py_DECREF(tmp);
936 }
937
938 return (PyObject *)(self);
939}
940
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200941 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200942DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200943{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200944 pyll_remove(&self->ref, &lastdict);
945 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200946
947 DESTRUCTOR_FINISH(self);
948}
949
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200950static char *DictionaryAttrs[] = {
951 "locked", "scope",
952 NULL
953};
954
955 static PyObject *
956DictionaryDir(PyObject *self)
957{
958 return ObjectDir(self, DictionaryAttrs);
959}
960
Bram Moolenaardb913952012-06-29 12:54:53 +0200961 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200962DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200963{
964 if (val == NULL)
965 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200966 PyErr_SetString(PyExc_AttributeError,
967 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200968 return -1;
969 }
970
971 if (strcmp(name, "locked") == 0)
972 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200973 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200974 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200975 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200976 return -1;
977 }
978 else
979 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200980 int istrue = PyObject_IsTrue(val);
981 if (istrue == -1)
982 return -1;
983 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200984 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200985 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200986 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200987 }
988 return 0;
989 }
990 else
991 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200992 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200993 return -1;
994 }
995}
996
997 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200998DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200999{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001000 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001001}
1002
Bram Moolenaara9922d62013-05-30 13:01:18 +02001003#define DICT_FLAG_HAS_DEFAULT 0x01
1004#define DICT_FLAG_POP 0x02
1005#define DICT_FLAG_NONE_DEFAULT 0x04
1006#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1007#define DICT_FLAG_RETURN_PAIR 0x10
1008
Bram Moolenaardb913952012-06-29 12:54:53 +02001009 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001010_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001011{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001012 PyObject *keyObject;
1013 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1014 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001015 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001016 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001017 dict_T *dict = self->dict;
1018 hashitem_T *hi;
1019
Bram Moolenaardb913952012-06-29 12:54:53 +02001020 DICTKEY_DECL
1021
Bram Moolenaara9922d62013-05-30 13:01:18 +02001022 if (flags & DICT_FLAG_HAS_DEFAULT)
1023 {
1024 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1025 return NULL;
1026 }
1027 else
1028 keyObject = args;
1029
1030 if (flags & DICT_FLAG_RETURN_BOOL)
1031 defObject = Py_False;
1032
Bram Moolenaara03e6312013-05-29 22:49:26 +02001033 DICTKEY_GET(NULL, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +02001034
Bram Moolenaara9922d62013-05-30 13:01:18 +02001035 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001036
Bram Moolenaar696c2112012-09-21 13:43:14 +02001037 DICTKEY_UNREF
1038
Bram Moolenaara9922d62013-05-30 13:01:18 +02001039 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001040 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001041 if (defObject)
1042 {
1043 Py_INCREF(defObject);
1044 return defObject;
1045 }
1046 else
1047 {
1048 PyErr_SetObject(PyExc_KeyError, keyObject);
1049 return NULL;
1050 }
1051 }
1052 else if (flags & DICT_FLAG_RETURN_BOOL)
1053 {
1054 Py_INCREF(Py_True);
1055 return Py_True;
1056 }
1057
1058 di = dict_lookup(hi);
1059
1060 if (!(r = ConvertToPyObject(&di->di_tv)))
1061 return NULL;
1062
1063 if (flags & DICT_FLAG_POP)
1064 {
1065 if (dict->dv_lock)
1066 {
1067 PyErr_SetVim(_("dict is locked"));
1068 Py_DECREF(r);
1069 return NULL;
1070 }
1071
1072 hash_remove(&dict->dv_hashtab, hi);
1073 dictitem_free(di);
1074 }
1075
1076 if (flags & DICT_FLAG_RETURN_PAIR)
1077 {
1078 PyObject *tmp = r;
1079
1080 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, tmp)))
1081 {
1082 Py_DECREF(tmp);
1083 return NULL;
1084 }
1085 }
1086
1087 return r;
1088}
1089
1090 static PyObject *
1091DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1092{
1093 return _DictionaryItem(self, keyObject, 0);
1094}
1095
1096 static int
1097DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1098{
1099 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1100 int r;
1101
1102 r = (rObj == Py_True);
1103
1104 Py_DECREF(Py_True);
1105
1106 return r;
1107}
1108
1109typedef struct
1110{
1111 hashitem_T *ht_array;
1112 long_u ht_used;
1113 hashtab_T *ht;
1114 hashitem_T *hi;
1115 int todo;
1116} dictiterinfo_T;
1117
1118 static PyObject *
1119DictionaryIterNext(dictiterinfo_T **dii)
1120{
1121 PyObject *r;
1122
1123 if (!(*dii)->todo)
1124 return NULL;
1125
1126 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1127 (*dii)->ht->ht_used != (*dii)->ht_used)
1128 {
1129 PyErr_SetString(PyExc_RuntimeError,
1130 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001131 return NULL;
1132 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001133
Bram Moolenaara9922d62013-05-30 13:01:18 +02001134 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1135 ++((*dii)->hi);
1136
1137 --((*dii)->todo);
1138
1139 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1140 return NULL;
1141
1142 return r;
1143}
1144
1145 static PyObject *
1146DictionaryIter(DictionaryObject *self)
1147{
1148 dictiterinfo_T *dii;
1149 hashtab_T *ht;
1150
1151 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1152 {
1153 PyErr_NoMemory();
1154 return NULL;
1155 }
1156
1157 ht = &self->dict->dv_hashtab;
1158 dii->ht_array = ht->ht_array;
1159 dii->ht_used = ht->ht_used;
1160 dii->ht = ht;
1161 dii->hi = dii->ht_array;
1162 dii->todo = dii->ht_used;
1163
1164 return IterNew(dii,
1165 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1166 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001167}
1168
1169 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001170DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001171{
1172 char_u *key;
1173 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001174 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001175 dictitem_T *di;
1176 DICTKEY_DECL
1177
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001178 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001179 {
1180 PyErr_SetVim(_("dict is locked"));
1181 return -1;
1182 }
1183
Bram Moolenaara03e6312013-05-29 22:49:26 +02001184 DICTKEY_GET(-1, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +02001185
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001186 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001187
1188 if (valObject == NULL)
1189 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001190 hashitem_T *hi;
1191
Bram Moolenaardb913952012-06-29 12:54:53 +02001192 if (di == NULL)
1193 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001194 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001195 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001196 return -1;
1197 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001198 hi = hash_find(&dict->dv_hashtab, di->di_key);
1199 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001200 dictitem_free(di);
1201 return 0;
1202 }
1203
1204 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001205 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001206
1207 if (di == NULL)
1208 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001209 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001210 {
1211 PyErr_NoMemory();
1212 return -1;
1213 }
1214 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001215 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001216
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001217 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001218 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001219 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +02001220 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001221 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001222 PyErr_SetVim(_("failed to add key to dictionary"));
1223 return -1;
1224 }
1225 }
1226 else
1227 clear_tv(&di->di_tv);
1228
1229 DICTKEY_UNREF
1230
1231 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001232 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001233 return 0;
1234}
1235
Bram Moolenaara9922d62013-05-30 13:01:18 +02001236typedef PyObject *(*hi_to_py)(hashitem_T *);
1237
Bram Moolenaardb913952012-06-29 12:54:53 +02001238 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001239DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001240{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001241 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001242 long_u todo = dict->dv_hashtab.ht_used;
1243 Py_ssize_t i = 0;
1244 PyObject *r;
1245 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001246 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001247
1248 r = PyList_New(todo);
1249 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1250 {
1251 if (!HASHITEM_EMPTY(hi))
1252 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001253 if (!(newObj = hiconvert(hi)))
1254 {
1255 Py_DECREF(r);
1256 return NULL;
1257 }
1258 if (PyList_SetItem(r, i, newObj))
1259 {
1260 Py_DECREF(r);
1261 Py_DECREF(newObj);
1262 return NULL;
1263 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001264 --todo;
1265 ++i;
1266 }
1267 }
1268 return r;
1269}
1270
Bram Moolenaara9922d62013-05-30 13:01:18 +02001271 static PyObject *
1272dict_key(hashitem_T *hi)
1273{
1274 return PyBytes_FromString((char *)(hi->hi_key));
1275}
1276
1277 static PyObject *
1278DictionaryListKeys(DictionaryObject *self)
1279{
1280 return DictionaryListObjects(self, dict_key);
1281}
1282
1283 static PyObject *
1284dict_val(hashitem_T *hi)
1285{
1286 dictitem_T *di;
1287
1288 di = dict_lookup(hi);
1289 return ConvertToPyObject(&di->di_tv);
1290}
1291
1292 static PyObject *
1293DictionaryListValues(DictionaryObject *self)
1294{
1295 return DictionaryListObjects(self, dict_val);
1296}
1297
1298 static PyObject *
1299dict_item(hashitem_T *hi)
1300{
1301 PyObject *keyObject;
1302 PyObject *valObject;
1303 PyObject *r;
1304
1305 if (!(keyObject = dict_key(hi)))
1306 return NULL;
1307
1308 if (!(valObject = dict_val(hi)))
1309 {
1310 Py_DECREF(keyObject);
1311 return NULL;
1312 }
1313
1314 r = Py_BuildValue("(OO)", keyObject, valObject);
1315
1316 Py_DECREF(keyObject);
1317 Py_DECREF(valObject);
1318
1319 return r;
1320}
1321
1322 static PyObject *
1323DictionaryListItems(DictionaryObject *self)
1324{
1325 return DictionaryListObjects(self, dict_item);
1326}
1327
1328 static PyObject *
1329DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1330{
1331 dict_T *dict = self->dict;
1332
1333 if (dict->dv_lock)
1334 {
1335 PyErr_SetVim(_("dict is locked"));
1336 return NULL;
1337 }
1338
1339 if (kwargs)
1340 {
1341 typval_T tv;
1342
1343 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1344 return NULL;
1345
1346 VimTryStart();
1347 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1348 clear_tv(&tv);
1349 if (VimTryEnd())
1350 return NULL;
1351 }
1352 else
1353 {
1354 PyObject *object;
1355
1356 if (!PyArg_Parse(args, "(O)", &object))
1357 return NULL;
1358
1359 if (PyObject_HasAttrString(object, "keys"))
1360 return DictionaryUpdate(self, NULL, object);
1361 else
1362 {
1363 PyObject *iterator;
1364 PyObject *item;
1365
1366 if (!(iterator = PyObject_GetIter(object)))
1367 return NULL;
1368
1369 while ((item = PyIter_Next(iterator)))
1370 {
1371 PyObject *fast;
1372 PyObject *keyObject;
1373 PyObject *valObject;
1374 PyObject *todecref;
1375 char_u *key;
1376 dictitem_T *di;
1377
1378 if (!(fast = PySequence_Fast(item, "")))
1379 {
1380 Py_DECREF(iterator);
1381 Py_DECREF(item);
1382 return NULL;
1383 }
1384
1385 Py_DECREF(item);
1386
1387 if (PySequence_Fast_GET_SIZE(fast) != 2)
1388 {
1389 Py_DECREF(iterator);
1390 Py_DECREF(fast);
1391 PyErr_SetString(PyExc_ValueError,
1392 _("expected sequence element of size 2"));
1393 return NULL;
1394 }
1395
1396 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1397
1398 if (!(key = StringToChars(keyObject, &todecref)))
1399 {
1400 Py_DECREF(iterator);
1401 Py_DECREF(fast);
1402 return NULL;
1403 }
1404
1405 di = dictitem_alloc(key);
1406
1407 Py_XDECREF(todecref);
1408
1409 if (di == NULL)
1410 {
1411 Py_DECREF(fast);
1412 Py_DECREF(iterator);
1413 PyErr_NoMemory();
1414 return NULL;
1415 }
1416 di->di_tv.v_lock = 0;
1417 di->di_tv.v_type = VAR_UNKNOWN;
1418
1419 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1420
1421 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1422 {
1423 Py_DECREF(iterator);
1424 Py_DECREF(fast);
1425 dictitem_free(di);
1426 return NULL;
1427 }
1428
1429 Py_DECREF(fast);
1430
1431 if (dict_add(dict, di) == FAIL)
1432 {
1433 Py_DECREF(iterator);
1434 dictitem_free(di);
1435 PyErr_SetVim(_("failed to add key to dictionary"));
1436 return NULL;
1437 }
1438 }
1439
1440 Py_DECREF(iterator);
1441
1442 /* Iterator may have finished due to an exception */
1443 if (PyErr_Occurred())
1444 return NULL;
1445 }
1446 }
1447 Py_INCREF(Py_None);
1448 return Py_None;
1449}
1450
1451 static PyObject *
1452DictionaryGet(DictionaryObject *self, PyObject *args)
1453{
1454 return _DictionaryItem(self, args,
1455 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1456}
1457
1458 static PyObject *
1459DictionaryPop(DictionaryObject *self, PyObject *args)
1460{
1461 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1462}
1463
1464 static PyObject *
1465DictionaryPopItem(DictionaryObject *self, PyObject *args)
1466{
1467 PyObject *keyObject;
1468
1469 if (!PyArg_ParseTuple(args, "O", &keyObject))
1470 return NULL;
1471
1472 return _DictionaryItem(self, keyObject,
1473 DICT_FLAG_POP|DICT_FLAG_RETURN_PAIR);
1474}
1475
1476 static PyObject *
1477DictionaryHasKey(DictionaryObject *self, PyObject *args)
1478{
1479 PyObject *keyObject;
1480
1481 if (!PyArg_ParseTuple(args, "O", &keyObject))
1482 return NULL;
1483
1484 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1485}
1486
1487static PySequenceMethods DictionaryAsSeq = {
1488 0, /* sq_length */
1489 0, /* sq_concat */
1490 0, /* sq_repeat */
1491 0, /* sq_item */
1492 0, /* sq_slice */
1493 0, /* sq_ass_item */
1494 0, /* sq_ass_slice */
1495 (objobjproc) DictionaryContains, /* sq_contains */
1496 0, /* sq_inplace_concat */
1497 0, /* sq_inplace_repeat */
1498};
1499
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001500static PyMappingMethods DictionaryAsMapping = {
1501 (lenfunc) DictionaryLength,
1502 (binaryfunc) DictionaryItem,
1503 (objobjargproc) DictionaryAssItem,
1504};
1505
Bram Moolenaardb913952012-06-29 12:54:53 +02001506static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001507 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001508 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1509 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1510 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1511 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1512 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
1513 {"popitem", (PyCFunction)DictionaryPopItem, METH_VARARGS, ""},
1514 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001515 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1516 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001517};
1518
1519static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001520static PySequenceMethods ListAsSeq;
1521static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001522
1523typedef struct
1524{
1525 PyObject_HEAD
1526 list_T *list;
1527 pylinkedlist_T ref;
1528} ListObject;
1529
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001530#define NEW_LIST(list) ListNew(&ListType, list)
1531
Bram Moolenaardb913952012-06-29 12:54:53 +02001532 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001533ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001534{
1535 ListObject *self;
1536
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001537 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001538 if (self == NULL)
1539 return NULL;
1540 self->list = list;
1541 ++list->lv_refcount;
1542
1543 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1544
1545 return (PyObject *)(self);
1546}
1547
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001548 static list_T *
1549py_list_alloc()
1550{
1551 list_T *r;
1552
1553 if (!(r = list_alloc()))
1554 {
1555 PyErr_NoMemory();
1556 return NULL;
1557 }
1558 ++r->lv_refcount;
1559
1560 return r;
1561}
1562
1563 static int
1564list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1565{
1566 PyObject *iterator;
1567 PyObject *item;
1568 listitem_T *li;
1569
1570 if (!(iterator = PyObject_GetIter(obj)))
1571 return -1;
1572
1573 while ((item = PyIter_Next(iterator)))
1574 {
1575 if (!(li = listitem_alloc()))
1576 {
1577 PyErr_NoMemory();
1578 Py_DECREF(item);
1579 Py_DECREF(iterator);
1580 return -1;
1581 }
1582 li->li_tv.v_lock = 0;
1583 li->li_tv.v_type = VAR_UNKNOWN;
1584
1585 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1586 {
1587 Py_DECREF(item);
1588 Py_DECREF(iterator);
1589 listitem_free(li);
1590 return -1;
1591 }
1592
1593 Py_DECREF(item);
1594
1595 list_append(l, li);
1596 }
1597
1598 Py_DECREF(iterator);
1599
1600 /* Iterator may have finished due to an exception */
1601 if (PyErr_Occurred())
1602 return -1;
1603
1604 return 0;
1605}
1606
1607 static PyObject *
1608ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1609{
1610 list_T *list;
1611 PyObject *obj = NULL;
1612
1613 if (kwargs)
1614 {
1615 PyErr_SetString(PyExc_TypeError,
1616 _("list constructor does not accept keyword arguments"));
1617 return NULL;
1618 }
1619
1620 if (!PyArg_ParseTuple(args, "|O", &obj))
1621 return NULL;
1622
1623 if (!(list = py_list_alloc()))
1624 return NULL;
1625
1626 if (obj)
1627 {
1628 PyObject *lookup_dict;
1629
1630 if (!(lookup_dict = PyDict_New()))
1631 {
1632 list_unref(list);
1633 return NULL;
1634 }
1635
1636 if (list_py_concat(list, obj, lookup_dict) == -1)
1637 {
1638 Py_DECREF(lookup_dict);
1639 list_unref(list);
1640 return NULL;
1641 }
1642
1643 Py_DECREF(lookup_dict);
1644 }
1645
1646 return ListNew(subtype, list);
1647}
1648
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001649 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001650ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001651{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001652 pyll_remove(&self->ref, &lastlist);
1653 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001654
1655 DESTRUCTOR_FINISH(self);
1656}
1657
Bram Moolenaardb913952012-06-29 12:54:53 +02001658 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001659ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001660{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001661 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001662}
1663
1664 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001665ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001666{
1667 listitem_T *li;
1668
Bram Moolenaard6e39182013-05-21 18:30:34 +02001669 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001670 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001671 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001672 return NULL;
1673 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001674 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001675 if (li == NULL)
1676 {
1677 PyErr_SetVim(_("internal error: failed to get vim list item"));
1678 return NULL;
1679 }
1680 return ConvertToPyObject(&li->li_tv);
1681}
1682
1683#define PROC_RANGE \
1684 if (last < 0) {\
1685 if (last < -size) \
1686 last = 0; \
1687 else \
1688 last += size; \
1689 } \
1690 if (first < 0) \
1691 first = 0; \
1692 if (first > size) \
1693 first = size; \
1694 if (last > size) \
1695 last = size;
1696
1697 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001698ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001699{
1700 PyInt i;
1701 PyInt size = ListLength(self);
1702 PyInt n;
1703 PyObject *list;
1704 int reversed = 0;
1705
1706 PROC_RANGE
1707 if (first >= last)
1708 first = last;
1709
1710 n = last-first;
1711 list = PyList_New(n);
1712 if (list == NULL)
1713 return NULL;
1714
1715 for (i = 0; i < n; ++i)
1716 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001717 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001718 if (item == NULL)
1719 {
1720 Py_DECREF(list);
1721 return NULL;
1722 }
1723
1724 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1725 {
1726 Py_DECREF(item);
1727 Py_DECREF(list);
1728 return NULL;
1729 }
1730 }
1731
1732 return list;
1733}
1734
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001735typedef struct
1736{
1737 listwatch_T lw;
1738 list_T *list;
1739} listiterinfo_T;
1740
1741 static void
1742ListIterDestruct(listiterinfo_T *lii)
1743{
1744 list_rem_watch(lii->list, &lii->lw);
1745 PyMem_Free(lii);
1746}
1747
1748 static PyObject *
1749ListIterNext(listiterinfo_T **lii)
1750{
1751 PyObject *r;
1752
1753 if (!((*lii)->lw.lw_item))
1754 return NULL;
1755
1756 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1757 return NULL;
1758
1759 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1760
1761 return r;
1762}
1763
1764 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001765ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001766{
1767 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001768 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001769
1770 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1771 {
1772 PyErr_NoMemory();
1773 return NULL;
1774 }
1775
1776 list_add_watch(l, &lii->lw);
1777 lii->lw.lw_item = l->lv_first;
1778 lii->list = l;
1779
1780 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001781 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1782 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001783}
1784
Bram Moolenaardb913952012-06-29 12:54:53 +02001785 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001786ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001787{
1788 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001789 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001790 listitem_T *li;
1791 Py_ssize_t length = ListLength(self);
1792
1793 if (l->lv_lock)
1794 {
1795 PyErr_SetVim(_("list is locked"));
1796 return -1;
1797 }
1798 if (index>length || (index==length && obj==NULL))
1799 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001800 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001801 return -1;
1802 }
1803
1804 if (obj == NULL)
1805 {
1806 li = list_find(l, (long) index);
1807 list_remove(l, li, li);
1808 clear_tv(&li->li_tv);
1809 vim_free(li);
1810 return 0;
1811 }
1812
1813 if (ConvertFromPyObject(obj, &tv) == -1)
1814 return -1;
1815
1816 if (index == length)
1817 {
1818 if (list_append_tv(l, &tv) == FAIL)
1819 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001820 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001821 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001822 return -1;
1823 }
1824 }
1825 else
1826 {
1827 li = list_find(l, (long) index);
1828 clear_tv(&li->li_tv);
1829 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001830 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001831 }
1832 return 0;
1833}
1834
1835 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001836ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001837{
1838 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001839 PyObject *iterator;
1840 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02001841 listitem_T *li;
1842 listitem_T *next;
1843 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001844 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001845 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02001846
1847 if (l->lv_lock)
1848 {
1849 PyErr_SetVim(_("list is locked"));
1850 return -1;
1851 }
1852
1853 PROC_RANGE
1854
1855 if (first == size)
1856 li = NULL;
1857 else
1858 {
1859 li = list_find(l, (long) first);
1860 if (li == NULL)
1861 {
1862 PyErr_SetVim(_("internal error: no vim list item"));
1863 return -1;
1864 }
1865 if (last > first)
1866 {
1867 i = last - first;
1868 while (i-- && li != NULL)
1869 {
1870 next = li->li_next;
1871 listitem_remove(l, li);
1872 li = next;
1873 }
1874 }
1875 }
1876
1877 if (obj == NULL)
1878 return 0;
1879
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001880 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001881 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001882
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001883 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001884 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001885 if (ConvertFromPyObject(item, &v) == -1)
1886 {
1887 Py_DECREF(iterator);
1888 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001889 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001890 }
1891 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001892 if (list_insert_tv(l, &v, li) == FAIL)
1893 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001894 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001895 PyErr_SetVim(_("internal error: failed to add item to list"));
1896 return -1;
1897 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001898 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001899 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001900 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02001901 return 0;
1902}
1903
1904 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001905ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001906{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001907 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001908 PyObject *lookup_dict;
1909
1910 if (l->lv_lock)
1911 {
1912 PyErr_SetVim(_("list is locked"));
1913 return NULL;
1914 }
1915
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001916 if (!(lookup_dict = PyDict_New()))
1917 return NULL;
1918
Bram Moolenaardb913952012-06-29 12:54:53 +02001919 if (list_py_concat(l, obj, lookup_dict) == -1)
1920 {
1921 Py_DECREF(lookup_dict);
1922 return NULL;
1923 }
1924 Py_DECREF(lookup_dict);
1925
1926 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001927 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001928}
1929
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001930static char *ListAttrs[] = {
1931 "locked",
1932 NULL
1933};
1934
1935 static PyObject *
1936ListDir(PyObject *self)
1937{
1938 return ObjectDir(self, ListAttrs);
1939}
1940
Bram Moolenaar66b79852012-09-21 14:00:35 +02001941 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001942ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001943{
1944 if (val == NULL)
1945 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001946 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001947 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001948 return -1;
1949 }
1950
1951 if (strcmp(name, "locked") == 0)
1952 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001953 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001954 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001955 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001956 return -1;
1957 }
1958 else
1959 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001960 int istrue = PyObject_IsTrue(val);
1961 if (istrue == -1)
1962 return -1;
1963 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001964 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001965 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001966 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001967 }
1968 return 0;
1969 }
1970 else
1971 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001972 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001973 return -1;
1974 }
1975}
1976
Bram Moolenaardb913952012-06-29 12:54:53 +02001977static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001978 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1979 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
1980 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001981};
1982
1983typedef struct
1984{
1985 PyObject_HEAD
1986 char_u *name;
1987} FunctionObject;
1988
1989static PyTypeObject FunctionType;
1990
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02001991#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
1992
Bram Moolenaardb913952012-06-29 12:54:53 +02001993 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02001994FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02001995{
1996 FunctionObject *self;
1997
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02001998 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
1999
Bram Moolenaardb913952012-06-29 12:54:53 +02002000 if (self == NULL)
2001 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002002
2003 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002004 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002005 if (!translated_function_exists(name))
2006 {
2007 PyErr_SetString(PyExc_ValueError,
2008 _("unnamed function does not exist"));
2009 return NULL;
2010 }
2011 self->name = vim_strsave(name);
2012 func_ref(self->name);
2013 }
2014 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002015 if ((self->name = get_expanded_name(name,
2016 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2017 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002018 {
Bram Moolenaar018acca2013-05-30 13:37:28 +02002019 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2020 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002021 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002022
2023 return (PyObject *)(self);
2024}
2025
2026 static PyObject *
2027FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2028{
2029 PyObject *self;
2030 char_u *name;
2031
2032 if (kwargs)
2033 {
2034 PyErr_SetString(PyExc_TypeError,
2035 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002036 return NULL;
2037 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002038
2039 if (!PyArg_ParseTuple(args, "s", &name))
2040 return NULL;
2041
2042 self = FunctionNew(subtype, name);
2043
2044 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002045}
2046
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002047 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002048FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002049{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002050 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002051 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002052
2053 DESTRUCTOR_FINISH(self);
2054}
2055
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002056static char *FunctionAttrs[] = {
2057 "softspace",
2058 NULL
2059};
2060
2061 static PyObject *
2062FunctionDir(PyObject *self)
2063{
2064 return ObjectDir(self, FunctionAttrs);
2065}
2066
Bram Moolenaardb913952012-06-29 12:54:53 +02002067 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002068FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002069{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002070 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002071 typval_T args;
2072 typval_T selfdicttv;
2073 typval_T rettv;
2074 dict_T *selfdict = NULL;
2075 PyObject *selfdictObject;
2076 PyObject *result;
2077 int error;
2078
2079 if (ConvertFromPyObject(argsObject, &args) == -1)
2080 return NULL;
2081
2082 if (kwargs != NULL)
2083 {
2084 selfdictObject = PyDict_GetItemString(kwargs, "self");
2085 if (selfdictObject != NULL)
2086 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002087 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002088 {
2089 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002090 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002091 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002092 selfdict = selfdicttv.vval.v_dict;
2093 }
2094 }
2095
Bram Moolenaar71700b82013-05-15 17:49:05 +02002096 Py_BEGIN_ALLOW_THREADS
2097 Python_Lock_Vim();
2098
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002099 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002100 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002101
2102 Python_Release_Vim();
2103 Py_END_ALLOW_THREADS
2104
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002105 if (VimTryEnd())
2106 result = NULL;
2107 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002108 {
2109 result = NULL;
2110 PyErr_SetVim(_("failed to run function"));
2111 }
2112 else
2113 result = ConvertToPyObject(&rettv);
2114
Bram Moolenaardb913952012-06-29 12:54:53 +02002115 clear_tv(&args);
2116 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002117 if (selfdict != NULL)
2118 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002119
2120 return result;
2121}
2122
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002123 static PyObject *
2124FunctionRepr(FunctionObject *self)
2125{
2126 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2127}
2128
Bram Moolenaardb913952012-06-29 12:54:53 +02002129static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002130 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2131 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002132};
2133
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002134/*
2135 * Options object
2136 */
2137
2138static PyTypeObject OptionsType;
2139
2140typedef int (*checkfun)(void *);
2141
2142typedef struct
2143{
2144 PyObject_HEAD
2145 int opt_type;
2146 void *from;
2147 checkfun Check;
2148 PyObject *fromObj;
2149} OptionsObject;
2150
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002151 static int
2152dummy_check(void *arg UNUSED)
2153{
2154 return 0;
2155}
2156
2157 static PyObject *
2158OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2159{
2160 OptionsObject *self;
2161
Bram Moolenaar774267b2013-05-21 20:51:59 +02002162 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002163 if (self == NULL)
2164 return NULL;
2165
2166 self->opt_type = opt_type;
2167 self->from = from;
2168 self->Check = Check;
2169 self->fromObj = fromObj;
2170 if (fromObj)
2171 Py_INCREF(fromObj);
2172
2173 return (PyObject *)(self);
2174}
2175
2176 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002177OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002178{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002179 PyObject_GC_UnTrack((void *)(self));
2180 Py_XDECREF(self->fromObj);
2181 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002182}
2183
2184 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002185OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002186{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002187 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002188 return 0;
2189}
2190
2191 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002192OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002193{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002194 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002195 return 0;
2196}
2197
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002198 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002199OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002200{
2201 char_u *key;
2202 int flags;
2203 long numval;
2204 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02002205 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002206
Bram Moolenaard6e39182013-05-21 18:30:34 +02002207 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002208 return NULL;
2209
Bram Moolenaara03e6312013-05-29 22:49:26 +02002210 DICTKEY_GET(NULL, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002211
2212 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002213 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002214
2215 DICTKEY_UNREF
2216
2217 if (flags == 0)
2218 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002219 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002220 return NULL;
2221 }
2222
2223 if (flags & SOPT_UNSET)
2224 {
2225 Py_INCREF(Py_None);
2226 return Py_None;
2227 }
2228 else if (flags & SOPT_BOOL)
2229 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002230 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002231 r = numval ? Py_True : Py_False;
2232 Py_INCREF(r);
2233 return r;
2234 }
2235 else if (flags & SOPT_NUM)
2236 return PyInt_FromLong(numval);
2237 else if (flags & SOPT_STRING)
2238 {
2239 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002240 {
2241 PyObject *r = PyBytes_FromString((char *) stringval);
2242 vim_free(stringval);
2243 return r;
2244 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002245 else
2246 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002247 PyErr_SetString(PyExc_RuntimeError,
2248 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002249 return NULL;
2250 }
2251 }
2252 else
2253 {
2254 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2255 return NULL;
2256 }
2257}
2258
2259 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002260set_option_value_err(key, numval, stringval, opt_flags)
2261 char_u *key;
2262 int numval;
2263 char_u *stringval;
2264 int opt_flags;
2265{
2266 char_u *errmsg;
2267
2268 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2269 {
2270 if (VimTryEnd())
2271 return FAIL;
2272 PyErr_SetVim((char *)errmsg);
2273 return FAIL;
2274 }
2275 return OK;
2276}
2277
2278 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002279set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2280 char_u *key;
2281 int numval;
2282 char_u *stringval;
2283 int opt_flags;
2284 int opt_type;
2285 void *from;
2286{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002287 win_T *save_curwin = NULL;
2288 tabpage_T *save_curtab = NULL;
2289 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002290 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002291
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002292 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002293 switch (opt_type)
2294 {
2295 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002296 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2297 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002298 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002299 if (VimTryEnd())
2300 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002301 PyErr_SetVim("Problem while switching windows.");
2302 return -1;
2303 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002304 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002305 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002306 if (r == FAIL)
2307 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002308 break;
2309 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002310 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002311 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002312 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002313 if (r == FAIL)
2314 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002315 break;
2316 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002317 r = set_option_value_err(key, numval, stringval, opt_flags);
2318 if (r == FAIL)
2319 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002320 break;
2321 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002322 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002323}
2324
2325 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002326OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002327{
2328 char_u *key;
2329 int flags;
2330 int opt_flags;
2331 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02002332 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002333
Bram Moolenaard6e39182013-05-21 18:30:34 +02002334 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002335 return -1;
2336
Bram Moolenaara03e6312013-05-29 22:49:26 +02002337 DICTKEY_GET(-1, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002338
2339 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002340 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002341
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002342 if (flags == 0)
2343 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002344 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002345 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002346 return -1;
2347 }
2348
2349 if (valObject == NULL)
2350 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002351 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002352 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002353 PyErr_SetString(PyExc_ValueError,
2354 _("unable to unset global option"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002355 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002356 return -1;
2357 }
2358 else if (!(flags & SOPT_GLOBAL))
2359 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002360 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2361 "without global value"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002362 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002363 return -1;
2364 }
2365 else
2366 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002367 unset_global_local_option(key, self->from);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002368 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002369 return 0;
2370 }
2371 }
2372
Bram Moolenaard6e39182013-05-21 18:30:34 +02002373 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002374
2375 if (flags & SOPT_BOOL)
2376 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002377 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002378
Bram Moolenaarb983f752013-05-15 16:11:50 +02002379 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002380 r = -1;
2381 else
2382 r = set_option_value_for(key, istrue, NULL,
2383 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002384 }
2385 else if (flags & SOPT_NUM)
2386 {
2387 int val;
2388
2389#if PY_MAJOR_VERSION < 3
2390 if (PyInt_Check(valObject))
2391 val = PyInt_AsLong(valObject);
2392 else
2393#endif
2394 if (PyLong_Check(valObject))
2395 val = PyLong_AsLong(valObject);
2396 else
2397 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002398 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002399 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002400 return -1;
2401 }
2402
2403 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002404 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002405 }
2406 else
2407 {
2408 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002409 PyObject *todecref;
2410
2411 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002412 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002413 r = set_option_value_for(key, 0, val, opt_flags,
2414 self->opt_type, self->from);
2415 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002416 }
2417 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002418 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002419 }
2420
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002421 DICTKEY_UNREF
2422
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002423 return r;
2424}
2425
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002426static PyMappingMethods OptionsAsMapping = {
2427 (lenfunc) NULL,
2428 (binaryfunc) OptionsItem,
2429 (objobjargproc) OptionsAssItem,
2430};
2431
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002432/* Tabpage object
2433 */
2434
2435typedef struct
2436{
2437 PyObject_HEAD
2438 tabpage_T *tab;
2439} TabPageObject;
2440
2441static PyObject *WinListNew(TabPageObject *tabObject);
2442
2443static PyTypeObject TabPageType;
2444
2445 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002446CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002447{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002448 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002449 {
2450 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2451 return -1;
2452 }
2453
2454 return 0;
2455}
2456
2457 static PyObject *
2458TabPageNew(tabpage_T *tab)
2459{
2460 TabPageObject *self;
2461
2462 if (TAB_PYTHON_REF(tab))
2463 {
2464 self = TAB_PYTHON_REF(tab);
2465 Py_INCREF(self);
2466 }
2467 else
2468 {
2469 self = PyObject_NEW(TabPageObject, &TabPageType);
2470 if (self == NULL)
2471 return NULL;
2472 self->tab = tab;
2473 TAB_PYTHON_REF(tab) = self;
2474 }
2475
2476 return (PyObject *)(self);
2477}
2478
2479 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002480TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002481{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002482 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2483 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002484
2485 DESTRUCTOR_FINISH(self);
2486}
2487
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002488static char *TabPageAttrs[] = {
2489 "windows", "number", "vars", "window", "valid",
2490 NULL
2491};
2492
2493 static PyObject *
2494TabPageDir(PyObject *self)
2495{
2496 return ObjectDir(self, TabPageAttrs);
2497}
2498
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002499 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002500TabPageAttrValid(TabPageObject *self, char *name)
2501{
2502 PyObject *r;
2503
2504 if (strcmp(name, "valid") != 0)
2505 return NULL;
2506
2507 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2508 Py_INCREF(r);
2509 return r;
2510}
2511
2512 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002513TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002514{
2515 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002516 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002517 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002518 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002519 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002520 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002521 else if (strcmp(name, "window") == 0)
2522 {
2523 /* For current tab window.c does not bother to set or update tp_curwin
2524 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002525 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002526 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002527 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002528 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002529 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002530 else if (strcmp(name, "__members__") == 0)
2531 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002532 return NULL;
2533}
2534
2535 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002536TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002537{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002538 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002539 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002540 else
2541 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002542 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002543
2544 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002545 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2546 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002547 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002548 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002549 }
2550}
2551
2552static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002553 /* name, function, calling, documentation */
2554 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2555 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002556};
2557
2558/*
2559 * Window list object
2560 */
2561
2562static PyTypeObject TabListType;
2563static PySequenceMethods TabListAsSeq;
2564
2565typedef struct
2566{
2567 PyObject_HEAD
2568} TabListObject;
2569
2570 static PyInt
2571TabListLength(PyObject *self UNUSED)
2572{
2573 tabpage_T *tp = first_tabpage;
2574 PyInt n = 0;
2575
2576 while (tp != NULL)
2577 {
2578 ++n;
2579 tp = tp->tp_next;
2580 }
2581
2582 return n;
2583}
2584
2585 static PyObject *
2586TabListItem(PyObject *self UNUSED, PyInt n)
2587{
2588 tabpage_T *tp;
2589
2590 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2591 if (n == 0)
2592 return TabPageNew(tp);
2593
2594 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2595 return NULL;
2596}
2597
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002598/* Window object
2599 */
2600
2601typedef struct
2602{
2603 PyObject_HEAD
2604 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002605 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002606} WindowObject;
2607
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002608static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002609
2610 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002611CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002612{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002613 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002614 {
2615 PyErr_SetVim(_("attempt to refer to deleted window"));
2616 return -1;
2617 }
2618
2619 return 0;
2620}
2621
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002622 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002623WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002624{
2625 /* We need to handle deletion of windows underneath us.
2626 * If we add a "w_python*_ref" field to the win_T structure,
2627 * then we can get at it in win_free() in vim. We then
2628 * need to create only ONE Python object per window - if
2629 * we try to create a second, just INCREF the existing one
2630 * and return it. The (single) Python object referring to
2631 * the window is stored in "w_python*_ref".
2632 * On a win_free() we set the Python object's win_T* field
2633 * to an invalid value. We trap all uses of a window
2634 * object, and reject them if the win_T* field is invalid.
2635 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002636 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002637 * w_python_ref and w_python3_ref fields respectively.
2638 */
2639
2640 WindowObject *self;
2641
2642 if (WIN_PYTHON_REF(win))
2643 {
2644 self = WIN_PYTHON_REF(win);
2645 Py_INCREF(self);
2646 }
2647 else
2648 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002649 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002650 if (self == NULL)
2651 return NULL;
2652 self->win = win;
2653 WIN_PYTHON_REF(win) = self;
2654 }
2655
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002656 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2657
Bram Moolenaar971db462013-05-12 18:44:48 +02002658 return (PyObject *)(self);
2659}
2660
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002661 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002662WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002663{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002664 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002665 if (self->win && self->win != INVALID_WINDOW_VALUE)
2666 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002667 Py_XDECREF(((PyObject *)(self->tabObject)));
2668 PyObject_GC_Del((void *)(self));
2669}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002670
Bram Moolenaar774267b2013-05-21 20:51:59 +02002671 static int
2672WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2673{
2674 Py_VISIT(((PyObject *)(self->tabObject)));
2675 return 0;
2676}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002677
Bram Moolenaar774267b2013-05-21 20:51:59 +02002678 static int
2679WindowClear(WindowObject *self)
2680{
2681 Py_CLEAR(self->tabObject);
2682 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002683}
2684
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002685 static win_T *
2686get_firstwin(TabPageObject *tabObject)
2687{
2688 if (tabObject)
2689 {
2690 if (CheckTabPage(tabObject))
2691 return NULL;
2692 /* For current tab window.c does not bother to set or update tp_firstwin
2693 */
2694 else if (tabObject->tab == curtab)
2695 return firstwin;
2696 else
2697 return tabObject->tab->tp_firstwin;
2698 }
2699 else
2700 return firstwin;
2701}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002702static char *WindowAttrs[] = {
2703 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2704 "tabpage", "valid",
2705 NULL
2706};
2707
2708 static PyObject *
2709WindowDir(PyObject *self)
2710{
2711 return ObjectDir(self, WindowAttrs);
2712}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002713
Bram Moolenaar971db462013-05-12 18:44:48 +02002714 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002715WindowAttrValid(WindowObject *self, char *name)
2716{
2717 PyObject *r;
2718
2719 if (strcmp(name, "valid") != 0)
2720 return NULL;
2721
2722 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2723 Py_INCREF(r);
2724 return r;
2725}
2726
2727 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002728WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002729{
2730 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002731 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002732 else if (strcmp(name, "cursor") == 0)
2733 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002734 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002735
2736 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2737 }
2738 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002739 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002740#ifdef FEAT_WINDOWS
2741 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002742 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002743#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002744#ifdef FEAT_VERTSPLIT
2745 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002746 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002747 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002748 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002749#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002750 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002751 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002752 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002753 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2754 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002755 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002756 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002757 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002758 return NULL;
2759 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002760 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002761 }
2762 else if (strcmp(name, "tabpage") == 0)
2763 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002764 Py_INCREF(self->tabObject);
2765 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002766 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002767 else if (strcmp(name, "__members__") == 0)
2768 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002769 else
2770 return NULL;
2771}
2772
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002773 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002774WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002775{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002776 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002777 return -1;
2778
2779 if (strcmp(name, "buffer") == 0)
2780 {
2781 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2782 return -1;
2783 }
2784 else if (strcmp(name, "cursor") == 0)
2785 {
2786 long lnum;
2787 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002788
2789 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2790 return -1;
2791
Bram Moolenaard6e39182013-05-21 18:30:34 +02002792 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002793 {
2794 PyErr_SetVim(_("cursor position outside buffer"));
2795 return -1;
2796 }
2797
2798 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002799 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002800 return -1;
2801
Bram Moolenaard6e39182013-05-21 18:30:34 +02002802 self->win->w_cursor.lnum = lnum;
2803 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002804#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002805 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002806#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002807 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002808 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002809
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002810 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002811 return 0;
2812 }
2813 else if (strcmp(name, "height") == 0)
2814 {
2815 int height;
2816 win_T *savewin;
2817
2818 if (!PyArg_Parse(val, "i", &height))
2819 return -1;
2820
2821#ifdef FEAT_GUI
2822 need_mouse_correct = TRUE;
2823#endif
2824 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002825 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002826
2827 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002828 win_setheight(height);
2829 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002830 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002831 return -1;
2832
2833 return 0;
2834 }
2835#ifdef FEAT_VERTSPLIT
2836 else if (strcmp(name, "width") == 0)
2837 {
2838 int width;
2839 win_T *savewin;
2840
2841 if (!PyArg_Parse(val, "i", &width))
2842 return -1;
2843
2844#ifdef FEAT_GUI
2845 need_mouse_correct = TRUE;
2846#endif
2847 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002848 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002849
2850 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002851 win_setwidth(width);
2852 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002853 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002854 return -1;
2855
2856 return 0;
2857 }
2858#endif
2859 else
2860 {
2861 PyErr_SetString(PyExc_AttributeError, name);
2862 return -1;
2863 }
2864}
2865
2866 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002867WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002868{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002869 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002870 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002871 else
2872 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002873 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002874
Bram Moolenaar6d216452013-05-12 19:00:41 +02002875 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002876 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002877 (self));
2878 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002879 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002880 }
2881}
2882
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002883static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002884 /* name, function, calling, documentation */
2885 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2886 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002887};
2888
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002889/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002890 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002891 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002892
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002893static PyTypeObject WinListType;
2894static PySequenceMethods WinListAsSeq;
2895
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002896typedef struct
2897{
2898 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002899 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002900} WinListObject;
2901
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002902 static PyObject *
2903WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002904{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002905 WinListObject *self;
2906
2907 self = PyObject_NEW(WinListObject, &WinListType);
2908 self->tabObject = tabObject;
2909 Py_INCREF(tabObject);
2910
2911 return (PyObject *)(self);
2912}
2913
2914 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002915WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002916{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002917 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002918
2919 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02002920 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002921 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02002922 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002923
2924 DESTRUCTOR_FINISH(self);
2925}
2926
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002927 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002928WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002929{
2930 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002931 PyInt n = 0;
2932
Bram Moolenaard6e39182013-05-21 18:30:34 +02002933 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002934 return -1;
2935
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002936 while (w != NULL)
2937 {
2938 ++n;
2939 w = W_NEXT(w);
2940 }
2941
2942 return n;
2943}
2944
2945 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002946WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002947{
2948 win_T *w;
2949
Bram Moolenaard6e39182013-05-21 18:30:34 +02002950 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002951 return NULL;
2952
2953 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002954 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002955 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002956
2957 PyErr_SetString(PyExc_IndexError, _("no such window"));
2958 return NULL;
2959}
2960
2961/* Convert a Python string into a Vim line.
2962 *
2963 * The result is in allocated memory. All internal nulls are replaced by
2964 * newline characters. It is an error for the string to contain newline
2965 * characters.
2966 *
2967 * On errors, the Python exception data is set, and NULL is returned.
2968 */
2969 static char *
2970StringToLine(PyObject *obj)
2971{
2972 const char *str;
2973 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002974 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002975 PyInt len;
2976 PyInt i;
2977 char *p;
2978
2979 if (obj == NULL || !PyString_Check(obj))
2980 {
2981 PyErr_BadArgument();
2982 return NULL;
2983 }
2984
Bram Moolenaar19e60942011-06-19 00:27:51 +02002985 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2986 str = PyString_AsString(bytes);
2987 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002988
2989 /*
2990 * Error checking: String must not contain newlines, as we
2991 * are replacing a single line, and we must replace it with
2992 * a single line.
2993 * A trailing newline is removed, so that append(f.readlines()) works.
2994 */
2995 p = memchr(str, '\n', len);
2996 if (p != NULL)
2997 {
2998 if (p == str + len - 1)
2999 --len;
3000 else
3001 {
3002 PyErr_SetVim(_("string cannot contain newlines"));
3003 return NULL;
3004 }
3005 }
3006
3007 /* Create a copy of the string, with internal nulls replaced by
3008 * newline characters, as is the vim convention.
3009 */
3010 save = (char *)alloc((unsigned)(len+1));
3011 if (save == NULL)
3012 {
3013 PyErr_NoMemory();
3014 return NULL;
3015 }
3016
3017 for (i = 0; i < len; ++i)
3018 {
3019 if (str[i] == '\0')
3020 save[i] = '\n';
3021 else
3022 save[i] = str[i];
3023 }
3024
3025 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003026 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003027
3028 return save;
3029}
3030
3031/* Get a line from the specified buffer. The line number is
3032 * in Vim format (1-based). The line is returned as a Python
3033 * string object.
3034 */
3035 static PyObject *
3036GetBufferLine(buf_T *buf, PyInt n)
3037{
3038 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3039}
3040
3041
3042/* Get a list of lines from the specified buffer. The line numbers
3043 * are in Vim format (1-based). The range is from lo up to, but not
3044 * including, hi. The list is returned as a Python list of string objects.
3045 */
3046 static PyObject *
3047GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3048{
3049 PyInt i;
3050 PyInt n = hi - lo;
3051 PyObject *list = PyList_New(n);
3052
3053 if (list == NULL)
3054 return NULL;
3055
3056 for (i = 0; i < n; ++i)
3057 {
3058 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3059
3060 /* Error check - was the Python string creation OK? */
3061 if (str == NULL)
3062 {
3063 Py_DECREF(list);
3064 return NULL;
3065 }
3066
3067 /* Set the list item */
3068 if (PyList_SetItem(list, i, str))
3069 {
3070 Py_DECREF(str);
3071 Py_DECREF(list);
3072 return NULL;
3073 }
3074 }
3075
3076 /* The ownership of the Python list is passed to the caller (ie,
3077 * the caller should Py_DECREF() the object when it is finished
3078 * with it).
3079 */
3080
3081 return list;
3082}
3083
3084/*
3085 * Check if deleting lines made the cursor position invalid.
3086 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3087 * deleted).
3088 */
3089 static void
3090py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3091{
3092 if (curwin->w_cursor.lnum >= lo)
3093 {
3094 /* Adjust the cursor position if it's in/after the changed
3095 * lines. */
3096 if (curwin->w_cursor.lnum >= hi)
3097 {
3098 curwin->w_cursor.lnum += extra;
3099 check_cursor_col();
3100 }
3101 else if (extra < 0)
3102 {
3103 curwin->w_cursor.lnum = lo;
3104 check_cursor();
3105 }
3106 else
3107 check_cursor_col();
3108 changed_cline_bef_curs();
3109 }
3110 invalidate_botline();
3111}
3112
Bram Moolenaar19e60942011-06-19 00:27:51 +02003113/*
3114 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003115 * in Vim format (1-based). The replacement line is given as
3116 * a Python string object. The object is checked for validity
3117 * and correct format. Errors are returned as a value of FAIL.
3118 * The return value is OK on success.
3119 * If OK is returned and len_change is not NULL, *len_change
3120 * is set to the change in the buffer length.
3121 */
3122 static int
3123SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3124{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003125 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003126 * There are three cases:
3127 * 1. NULL, or None - this is a deletion.
3128 * 2. A string - this is a replacement.
3129 * 3. Anything else - this is an error.
3130 */
3131 if (line == Py_None || line == NULL)
3132 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003133 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003134
3135 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003136 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003137
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003138 VimTryStart();
3139
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003140 if (u_savedel((linenr_T)n, 1L) == FAIL)
3141 PyErr_SetVim(_("cannot save undo information"));
3142 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3143 PyErr_SetVim(_("cannot delete line"));
3144 else
3145 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003146 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003147 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3148 deleted_lines_mark((linenr_T)n, 1L);
3149 }
3150
Bram Moolenaar105bc352013-05-17 16:03:57 +02003151 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003152
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003153 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003154 return FAIL;
3155
3156 if (len_change)
3157 *len_change = -1;
3158
3159 return OK;
3160 }
3161 else if (PyString_Check(line))
3162 {
3163 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003164 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003165
3166 if (save == NULL)
3167 return FAIL;
3168
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003169 VimTryStart();
3170
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003171 /* We do not need to free "save" if ml_replace() consumes it. */
3172 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003173 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003174
3175 if (u_savesub((linenr_T)n) == FAIL)
3176 {
3177 PyErr_SetVim(_("cannot save undo information"));
3178 vim_free(save);
3179 }
3180 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3181 {
3182 PyErr_SetVim(_("cannot replace line"));
3183 vim_free(save);
3184 }
3185 else
3186 changed_bytes((linenr_T)n, 0);
3187
Bram Moolenaar105bc352013-05-17 16:03:57 +02003188 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003189
3190 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003191 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003192 check_cursor_col();
3193
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003194 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003195 return FAIL;
3196
3197 if (len_change)
3198 *len_change = 0;
3199
3200 return OK;
3201 }
3202 else
3203 {
3204 PyErr_BadArgument();
3205 return FAIL;
3206 }
3207}
3208
Bram Moolenaar19e60942011-06-19 00:27:51 +02003209/* Replace a range of lines in the specified buffer. The line numbers are in
3210 * Vim format (1-based). The range is from lo up to, but not including, hi.
3211 * The replacement lines are given as a Python list of string objects. The
3212 * list is checked for validity and correct format. Errors are returned as a
3213 * value of FAIL. The return value is OK on success.
3214 * If OK is returned and len_change is not NULL, *len_change
3215 * is set to the change in the buffer length.
3216 */
3217 static int
3218SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3219{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003220 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003221 * There are three cases:
3222 * 1. NULL, or None - this is a deletion.
3223 * 2. A list - this is a replacement.
3224 * 3. Anything else - this is an error.
3225 */
3226 if (list == Py_None || list == NULL)
3227 {
3228 PyInt i;
3229 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003230 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003231
3232 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003233 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003234 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003235
3236 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3237 PyErr_SetVim(_("cannot save undo information"));
3238 else
3239 {
3240 for (i = 0; i < n; ++i)
3241 {
3242 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3243 {
3244 PyErr_SetVim(_("cannot delete line"));
3245 break;
3246 }
3247 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003248 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003249 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3250 deleted_lines_mark((linenr_T)lo, (long)i);
3251 }
3252
Bram Moolenaar105bc352013-05-17 16:03:57 +02003253 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003254
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003255 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003256 return FAIL;
3257
3258 if (len_change)
3259 *len_change = -n;
3260
3261 return OK;
3262 }
3263 else if (PyList_Check(list))
3264 {
3265 PyInt i;
3266 PyInt new_len = PyList_Size(list);
3267 PyInt old_len = hi - lo;
3268 PyInt extra = 0; /* lines added to text, can be negative */
3269 char **array;
3270 buf_T *savebuf;
3271
3272 if (new_len == 0) /* avoid allocating zero bytes */
3273 array = NULL;
3274 else
3275 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003276 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003277 if (array == NULL)
3278 {
3279 PyErr_NoMemory();
3280 return FAIL;
3281 }
3282 }
3283
3284 for (i = 0; i < new_len; ++i)
3285 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003286 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003287
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003288 if (!(line = PyList_GetItem(list, i)) ||
3289 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003290 {
3291 while (i)
3292 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003293 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003294 return FAIL;
3295 }
3296 }
3297
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003298 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003299 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003300
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003301 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003302 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003303
3304 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3305 PyErr_SetVim(_("cannot save undo information"));
3306
3307 /* If the size of the range is reducing (ie, new_len < old_len) we
3308 * need to delete some old_len. We do this at the start, by
3309 * repeatedly deleting line "lo".
3310 */
3311 if (!PyErr_Occurred())
3312 {
3313 for (i = 0; i < old_len - new_len; ++i)
3314 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3315 {
3316 PyErr_SetVim(_("cannot delete line"));
3317 break;
3318 }
3319 extra -= i;
3320 }
3321
3322 /* For as long as possible, replace the existing old_len with the
3323 * new old_len. This is a more efficient operation, as it requires
3324 * less memory allocation and freeing.
3325 */
3326 if (!PyErr_Occurred())
3327 {
3328 for (i = 0; i < old_len && i < new_len; ++i)
3329 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3330 == FAIL)
3331 {
3332 PyErr_SetVim(_("cannot replace line"));
3333 break;
3334 }
3335 }
3336 else
3337 i = 0;
3338
3339 /* Now we may need to insert the remaining new old_len. If we do, we
3340 * must free the strings as we finish with them (we can't pass the
3341 * responsibility to vim in this case).
3342 */
3343 if (!PyErr_Occurred())
3344 {
3345 while (i < new_len)
3346 {
3347 if (ml_append((linenr_T)(lo + i - 1),
3348 (char_u *)array[i], 0, FALSE) == FAIL)
3349 {
3350 PyErr_SetVim(_("cannot insert line"));
3351 break;
3352 }
3353 vim_free(array[i]);
3354 ++i;
3355 ++extra;
3356 }
3357 }
3358
3359 /* Free any left-over old_len, as a result of an error */
3360 while (i < new_len)
3361 {
3362 vim_free(array[i]);
3363 ++i;
3364 }
3365
3366 /* Free the array of old_len. All of its contents have now
3367 * been dealt with (either freed, or the responsibility passed
3368 * to vim.
3369 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003370 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003371
3372 /* Adjust marks. Invalidate any which lie in the
3373 * changed range, and move any in the remainder of the buffer.
3374 */
3375 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3376 (long)MAXLNUM, (long)extra);
3377 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3378
Bram Moolenaar105bc352013-05-17 16:03:57 +02003379 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003380 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3381
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003382 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003383 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003384
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003385 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003386 return FAIL;
3387
3388 if (len_change)
3389 *len_change = new_len - old_len;
3390
3391 return OK;
3392 }
3393 else
3394 {
3395 PyErr_BadArgument();
3396 return FAIL;
3397 }
3398}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003399
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003400/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003401 * The line number is in Vim format (1-based). The lines to be inserted are
3402 * given as a Python list of string objects or as a single string. The lines
3403 * to be added are checked for validity and correct format. Errors are
3404 * returned as a value of FAIL. The return value is OK on success.
3405 * If OK is returned and len_change is not NULL, *len_change
3406 * is set to the change in the buffer length.
3407 */
3408 static int
3409InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3410{
3411 /* First of all, we check the type of the supplied Python object.
3412 * It must be a string or a list, or the call is in error.
3413 */
3414 if (PyString_Check(lines))
3415 {
3416 char *str = StringToLine(lines);
3417 buf_T *savebuf;
3418
3419 if (str == NULL)
3420 return FAIL;
3421
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003422 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003423 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003424 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003425
3426 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3427 PyErr_SetVim(_("cannot save undo information"));
3428 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3429 PyErr_SetVim(_("cannot insert line"));
3430 else
3431 appended_lines_mark((linenr_T)n, 1L);
3432
3433 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003434 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003435 update_screen(VALID);
3436
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003437 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003438 return FAIL;
3439
3440 if (len_change)
3441 *len_change = 1;
3442
3443 return OK;
3444 }
3445 else if (PyList_Check(lines))
3446 {
3447 PyInt i;
3448 PyInt size = PyList_Size(lines);
3449 char **array;
3450 buf_T *savebuf;
3451
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003452 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003453 if (array == NULL)
3454 {
3455 PyErr_NoMemory();
3456 return FAIL;
3457 }
3458
3459 for (i = 0; i < size; ++i)
3460 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003461 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003462
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003463 if (!(line = PyList_GetItem(lines, i)) ||
3464 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003465 {
3466 while (i)
3467 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003468 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003469 return FAIL;
3470 }
3471 }
3472
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003473 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003474 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003475 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003476
3477 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3478 PyErr_SetVim(_("cannot save undo information"));
3479 else
3480 {
3481 for (i = 0; i < size; ++i)
3482 {
3483 if (ml_append((linenr_T)(n + i),
3484 (char_u *)array[i], 0, FALSE) == FAIL)
3485 {
3486 PyErr_SetVim(_("cannot insert line"));
3487
3488 /* Free the rest of the lines */
3489 while (i < size)
3490 vim_free(array[i++]);
3491
3492 break;
3493 }
3494 vim_free(array[i]);
3495 }
3496 if (i > 0)
3497 appended_lines_mark((linenr_T)n, (long)i);
3498 }
3499
3500 /* Free the array of lines. All of its contents have now
3501 * been freed.
3502 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003503 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003504
Bram Moolenaar105bc352013-05-17 16:03:57 +02003505 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003506 update_screen(VALID);
3507
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003508 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003509 return FAIL;
3510
3511 if (len_change)
3512 *len_change = size;
3513
3514 return OK;
3515 }
3516 else
3517 {
3518 PyErr_BadArgument();
3519 return FAIL;
3520 }
3521}
3522
3523/*
3524 * Common routines for buffers and line ranges
3525 * -------------------------------------------
3526 */
3527
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003528typedef struct
3529{
3530 PyObject_HEAD
3531 buf_T *buf;
3532} BufferObject;
3533
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003534 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003535CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003536{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003537 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003538 {
3539 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3540 return -1;
3541 }
3542
3543 return 0;
3544}
3545
3546 static PyObject *
3547RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3548{
3549 if (CheckBuffer(self))
3550 return NULL;
3551
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003552 if (end == -1)
3553 end = self->buf->b_ml.ml_line_count;
3554
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003555 if (n < 0)
3556 n += end - start + 1;
3557
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003558 if (n < 0 || n > end - start)
3559 {
3560 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3561 return NULL;
3562 }
3563
3564 return GetBufferLine(self->buf, n+start);
3565}
3566
3567 static PyObject *
3568RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3569{
3570 PyInt size;
3571
3572 if (CheckBuffer(self))
3573 return NULL;
3574
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003575 if (end == -1)
3576 end = self->buf->b_ml.ml_line_count;
3577
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003578 size = end - start + 1;
3579
3580 if (lo < 0)
3581 lo = 0;
3582 else if (lo > size)
3583 lo = size;
3584 if (hi < 0)
3585 hi = 0;
3586 if (hi < lo)
3587 hi = lo;
3588 else if (hi > size)
3589 hi = size;
3590
3591 return GetBufferLineList(self->buf, lo+start, hi+start);
3592}
3593
3594 static PyInt
3595RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3596{
3597 PyInt len_change;
3598
3599 if (CheckBuffer(self))
3600 return -1;
3601
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003602 if (end == -1)
3603 end = self->buf->b_ml.ml_line_count;
3604
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003605 if (n < 0)
3606 n += end - start + 1;
3607
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003608 if (n < 0 || n > end - start)
3609 {
3610 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3611 return -1;
3612 }
3613
3614 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3615 return -1;
3616
3617 if (new_end)
3618 *new_end = end + len_change;
3619
3620 return 0;
3621}
3622
Bram Moolenaar19e60942011-06-19 00:27:51 +02003623 static PyInt
3624RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3625{
3626 PyInt size;
3627 PyInt len_change;
3628
3629 /* Self must be a valid buffer */
3630 if (CheckBuffer(self))
3631 return -1;
3632
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003633 if (end == -1)
3634 end = self->buf->b_ml.ml_line_count;
3635
Bram Moolenaar19e60942011-06-19 00:27:51 +02003636 /* Sort out the slice range */
3637 size = end - start + 1;
3638
3639 if (lo < 0)
3640 lo = 0;
3641 else if (lo > size)
3642 lo = size;
3643 if (hi < 0)
3644 hi = 0;
3645 if (hi < lo)
3646 hi = lo;
3647 else if (hi > size)
3648 hi = size;
3649
3650 if (SetBufferLineList(self->buf, lo + start, hi + start,
3651 val, &len_change) == FAIL)
3652 return -1;
3653
3654 if (new_end)
3655 *new_end = end + len_change;
3656
3657 return 0;
3658}
3659
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003660
3661 static PyObject *
3662RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3663{
3664 PyObject *lines;
3665 PyInt len_change;
3666 PyInt max;
3667 PyInt n;
3668
3669 if (CheckBuffer(self))
3670 return NULL;
3671
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003672 if (end == -1)
3673 end = self->buf->b_ml.ml_line_count;
3674
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003675 max = n = end - start + 1;
3676
3677 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3678 return NULL;
3679
3680 if (n < 0 || n > max)
3681 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003682 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003683 return NULL;
3684 }
3685
3686 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3687 return NULL;
3688
3689 if (new_end)
3690 *new_end = end + len_change;
3691
3692 Py_INCREF(Py_None);
3693 return Py_None;
3694}
3695
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003696/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003697 */
3698
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003699static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003700static PySequenceMethods RangeAsSeq;
3701static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003702
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003703typedef struct
3704{
3705 PyObject_HEAD
3706 BufferObject *buf;
3707 PyInt start;
3708 PyInt end;
3709} RangeObject;
3710
3711 static PyObject *
3712RangeNew(buf_T *buf, PyInt start, PyInt end)
3713{
3714 BufferObject *bufr;
3715 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003716 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003717 if (self == NULL)
3718 return NULL;
3719
3720 bufr = (BufferObject *)BufferNew(buf);
3721 if (bufr == NULL)
3722 {
3723 Py_DECREF(self);
3724 return NULL;
3725 }
3726 Py_INCREF(bufr);
3727
3728 self->buf = bufr;
3729 self->start = start;
3730 self->end = end;
3731
3732 return (PyObject *)(self);
3733}
3734
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003735 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003736RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003737{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003738 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003739 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003740 PyObject_GC_Del((void *)(self));
3741}
3742
3743 static int
3744RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3745{
3746 Py_VISIT(((PyObject *)(self->buf)));
3747 return 0;
3748}
3749
3750 static int
3751RangeClear(RangeObject *self)
3752{
3753 Py_CLEAR(self->buf);
3754 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003755}
3756
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003757 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003758RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003759{
3760 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003761 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003762 return -1; /* ??? */
3763
Bram Moolenaard6e39182013-05-21 18:30:34 +02003764 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003765}
3766
3767 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003768RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003769{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003770 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003771}
3772
3773 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003774RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003775{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003776 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003777}
3778
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003779static char *RangeAttrs[] = {
3780 "start", "end",
3781 NULL
3782};
3783
3784 static PyObject *
3785RangeDir(PyObject *self)
3786{
3787 return ObjectDir(self, RangeAttrs);
3788}
3789
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003790 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003791RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003792{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003793 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003794}
3795
3796 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003797RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003798{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003799 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003800 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
3801 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003802 else
3803 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003804 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003805
3806 if (name == NULL)
3807 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003808
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003809 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02003810 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003811 }
3812}
3813
3814static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003815 /* name, function, calling, documentation */
3816 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003817 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3818 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003819};
3820
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003821static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003822static PySequenceMethods BufferAsSeq;
3823static PyMappingMethods BufferAsMapping;
3824
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003825 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003826BufferNew(buf_T *buf)
3827{
3828 /* We need to handle deletion of buffers underneath us.
3829 * If we add a "b_python*_ref" field to the buf_T structure,
3830 * then we can get at it in buf_freeall() in vim. We then
3831 * need to create only ONE Python object per buffer - if
3832 * we try to create a second, just INCREF the existing one
3833 * and return it. The (single) Python object referring to
3834 * the buffer is stored in "b_python*_ref".
3835 * Question: what to do on a buf_freeall(). We'll probably
3836 * have to either delete the Python object (DECREF it to
3837 * zero - a bad idea, as it leaves dangling refs!) or
3838 * set the buf_T * value to an invalid value (-1?), which
3839 * means we need checks in all access functions... Bah.
3840 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003841 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003842 * b_python_ref and b_python3_ref fields respectively.
3843 */
3844
3845 BufferObject *self;
3846
3847 if (BUF_PYTHON_REF(buf) != NULL)
3848 {
3849 self = BUF_PYTHON_REF(buf);
3850 Py_INCREF(self);
3851 }
3852 else
3853 {
3854 self = PyObject_NEW(BufferObject, &BufferType);
3855 if (self == NULL)
3856 return NULL;
3857 self->buf = buf;
3858 BUF_PYTHON_REF(buf) = self;
3859 }
3860
3861 return (PyObject *)(self);
3862}
3863
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003864 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003865BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003866{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003867 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3868 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003869
3870 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003871}
3872
Bram Moolenaar971db462013-05-12 18:44:48 +02003873 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003874BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003875{
3876 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003877 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003878 return -1; /* ??? */
3879
Bram Moolenaard6e39182013-05-21 18:30:34 +02003880 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003881}
3882
3883 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003884BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003885{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003886 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003887}
3888
3889 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003890BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003891{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003892 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003893}
3894
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003895static char *BufferAttrs[] = {
3896 "name", "number", "vars", "options", "valid",
3897 NULL
3898};
3899
3900 static PyObject *
3901BufferDir(PyObject *self)
3902{
3903 return ObjectDir(self, BufferAttrs);
3904}
3905
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003906 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003907BufferAttrValid(BufferObject *self, char *name)
3908{
3909 PyObject *r;
3910
3911 if (strcmp(name, "valid") != 0)
3912 return NULL;
3913
3914 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
3915 Py_INCREF(r);
3916 return r;
3917}
3918
3919 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003920BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003921{
3922 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02003923 return PyString_FromString((self->buf->b_ffname == NULL
3924 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003925 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003926 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003927 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003928 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003929 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003930 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3931 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003932 else if (strcmp(name, "__members__") == 0)
3933 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003934 else
3935 return NULL;
3936}
3937
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003938 static int
3939BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3940{
3941 if (CheckBuffer(self))
3942 return -1;
3943
3944 if (strcmp(name, "name") == 0)
3945 {
3946 char_u *val;
3947 aco_save_T aco;
3948 int r;
3949 PyObject *todecref;
3950
3951 if (!(val = StringToChars(valObject, &todecref)))
3952 return -1;
3953
3954 VimTryStart();
3955 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3956 aucmd_prepbuf(&aco, self->buf);
3957 r = rename_buffer(val);
3958 aucmd_restbuf(&aco);
3959 Py_XDECREF(todecref);
3960 if (VimTryEnd())
3961 return -1;
3962
3963 if (r == FAIL)
3964 {
3965 PyErr_SetVim(_("failed to rename buffer"));
3966 return -1;
3967 }
3968 return 0;
3969 }
3970 else
3971 {
3972 PyErr_SetString(PyExc_AttributeError, name);
3973 return -1;
3974 }
3975}
3976
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003977 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003978BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003979{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003980 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003981}
3982
3983 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003984BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003985{
3986 pos_T *posp;
3987 char *pmark;
3988 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003989 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003990
Bram Moolenaard6e39182013-05-21 18:30:34 +02003991 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003992 return NULL;
3993
3994 if (!PyArg_ParseTuple(args, "s", &pmark))
3995 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003996
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003997 if (STRLEN(pmark) != 1)
3998 {
3999 PyErr_SetString(PyExc_ValueError,
4000 _("mark name must be a single character"));
4001 return NULL;
4002 }
4003
4004 mark = *pmark;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004005 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004006 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004007 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004008 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004009 if (VimTryEnd())
4010 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004011
4012 if (posp == NULL)
4013 {
4014 PyErr_SetVim(_("invalid mark name"));
4015 return NULL;
4016 }
4017
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004018 if (posp->lnum <= 0)
4019 {
4020 /* Or raise an error? */
4021 Py_INCREF(Py_None);
4022 return Py_None;
4023 }
4024
4025 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4026}
4027
4028 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004029BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004030{
4031 PyInt start;
4032 PyInt end;
4033
Bram Moolenaard6e39182013-05-21 18:30:34 +02004034 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 return NULL;
4036
4037 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4038 return NULL;
4039
Bram Moolenaard6e39182013-05-21 18:30:34 +02004040 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004041}
4042
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004043 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004044BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004045{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004046 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004047 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004048 else
4049 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004050 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004051
4052 if (name == NULL)
4053 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004054
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004055 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004056 }
4057}
4058
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004059static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004060 /* name, function, calling, documentation */
4061 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4062 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4063 {"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 +02004064 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4065 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004066};
4067
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004068/*
4069 * Buffer list object - Implementation
4070 */
4071
4072static PyTypeObject BufMapType;
4073
4074typedef struct
4075{
4076 PyObject_HEAD
4077} BufMapObject;
4078
4079 static PyInt
4080BufMapLength(PyObject *self UNUSED)
4081{
4082 buf_T *b = firstbuf;
4083 PyInt n = 0;
4084
4085 while (b)
4086 {
4087 ++n;
4088 b = b->b_next;
4089 }
4090
4091 return n;
4092}
4093
4094 static PyObject *
4095BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4096{
4097 buf_T *b;
4098 int bnr;
4099
4100#if PY_MAJOR_VERSION < 3
4101 if (PyInt_Check(keyObject))
4102 bnr = PyInt_AsLong(keyObject);
4103 else
4104#endif
4105 if (PyLong_Check(keyObject))
4106 bnr = PyLong_AsLong(keyObject);
4107 else
4108 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004109 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004110 return NULL;
4111 }
4112
4113 b = buflist_findnr(bnr);
4114
4115 if (b)
4116 return BufferNew(b);
4117 else
4118 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004119 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004120 return NULL;
4121 }
4122}
4123
4124 static void
4125BufMapIterDestruct(PyObject *buffer)
4126{
4127 /* Iteration was stopped before all buffers were processed */
4128 if (buffer)
4129 {
4130 Py_DECREF(buffer);
4131 }
4132}
4133
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004134 static int
4135BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4136{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004137 if (buffer)
4138 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004139 return 0;
4140}
4141
4142 static int
4143BufMapIterClear(PyObject **buffer)
4144{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004145 if (*buffer)
4146 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004147 return 0;
4148}
4149
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004150 static PyObject *
4151BufMapIterNext(PyObject **buffer)
4152{
4153 PyObject *next;
4154 PyObject *r;
4155
4156 if (!*buffer)
4157 return NULL;
4158
4159 r = *buffer;
4160
4161 if (CheckBuffer((BufferObject *)(r)))
4162 {
4163 *buffer = NULL;
4164 return NULL;
4165 }
4166
4167 if (!((BufferObject *)(r))->buf->b_next)
4168 next = NULL;
4169 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4170 return NULL;
4171 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004172 /* Do not increment reference: we no longer hold it (decref), but whoever
4173 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004174 return r;
4175}
4176
4177 static PyObject *
4178BufMapIter(PyObject *self UNUSED)
4179{
4180 PyObject *buffer;
4181
4182 buffer = BufferNew(firstbuf);
4183 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004184 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4185 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004186}
4187
4188static PyMappingMethods BufMapAsMapping = {
4189 (lenfunc) BufMapLength,
4190 (binaryfunc) BufMapItem,
4191 (objobjargproc) 0,
4192};
4193
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004194/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004195 */
4196
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004197static char *CurrentAttrs[] = {
4198 "buffer", "window", "line", "range", "tabpage",
4199 NULL
4200};
4201
4202 static PyObject *
4203CurrentDir(PyObject *self)
4204{
4205 return ObjectDir(self, CurrentAttrs);
4206}
4207
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004208 static PyObject *
4209CurrentGetattr(PyObject *self UNUSED, char *name)
4210{
4211 if (strcmp(name, "buffer") == 0)
4212 return (PyObject *)BufferNew(curbuf);
4213 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004214 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004215 else if (strcmp(name, "tabpage") == 0)
4216 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004217 else if (strcmp(name, "line") == 0)
4218 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4219 else if (strcmp(name, "range") == 0)
4220 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004221 else if (strcmp(name, "__members__") == 0)
4222 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004223 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004224#if PY_MAJOR_VERSION < 3
4225 return Py_FindMethod(WindowMethods, self, name);
4226#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004227 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004228#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004229}
4230
4231 static int
4232CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4233{
4234 if (strcmp(name, "line") == 0)
4235 {
4236 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4237 return -1;
4238
4239 return 0;
4240 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004241 else if (strcmp(name, "buffer") == 0)
4242 {
4243 int count;
4244
4245 if (value->ob_type != &BufferType)
4246 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004247 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004248 return -1;
4249 }
4250
4251 if (CheckBuffer((BufferObject *)(value)))
4252 return -1;
4253 count = ((BufferObject *)(value))->buf->b_fnum;
4254
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004255 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004256 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4257 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004258 if (VimTryEnd())
4259 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004260 PyErr_SetVim(_("failed to switch to given buffer"));
4261 return -1;
4262 }
4263
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004264 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004265 }
4266 else if (strcmp(name, "window") == 0)
4267 {
4268 int count;
4269
4270 if (value->ob_type != &WindowType)
4271 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004272 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004273 return -1;
4274 }
4275
4276 if (CheckWindow((WindowObject *)(value)))
4277 return -1;
4278 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4279
4280 if (!count)
4281 {
4282 PyErr_SetString(PyExc_ValueError,
4283 _("failed to find window in the current tab page"));
4284 return -1;
4285 }
4286
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004287 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004288 win_goto(((WindowObject *)(value))->win);
4289 if (((WindowObject *)(value))->win != curwin)
4290 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004291 if (VimTryEnd())
4292 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004293 PyErr_SetString(PyExc_RuntimeError,
4294 _("did not switch to the specified window"));
4295 return -1;
4296 }
4297
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004298 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004299 }
4300 else if (strcmp(name, "tabpage") == 0)
4301 {
4302 if (value->ob_type != &TabPageType)
4303 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004304 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004305 return -1;
4306 }
4307
4308 if (CheckTabPage((TabPageObject *)(value)))
4309 return -1;
4310
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004311 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004312 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4313 if (((TabPageObject *)(value))->tab != curtab)
4314 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004315 if (VimTryEnd())
4316 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004317 PyErr_SetString(PyExc_RuntimeError,
4318 _("did not switch to the specified tab page"));
4319 return -1;
4320 }
4321
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004322 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004323 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004324 else
4325 {
4326 PyErr_SetString(PyExc_AttributeError, name);
4327 return -1;
4328 }
4329}
4330
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004331static struct PyMethodDef CurrentMethods[] = {
4332 /* name, function, calling, documentation */
4333 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4334 { NULL, NULL, 0, NULL}
4335};
4336
Bram Moolenaardb913952012-06-29 12:54:53 +02004337 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004338init_range_cmd(exarg_T *eap)
4339{
4340 RangeStart = eap->line1;
4341 RangeEnd = eap->line2;
4342}
4343
4344 static void
4345init_range_eval(typval_T *rettv UNUSED)
4346{
4347 RangeStart = (PyInt) curwin->w_cursor.lnum;
4348 RangeEnd = RangeStart;
4349}
4350
4351 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004352run_cmd(const char *cmd, void *arg UNUSED
4353#ifdef PY_CAN_RECURSE
4354 , PyGILState_STATE *pygilstate UNUSED
4355#endif
4356 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004357{
4358 PyRun_SimpleString((char *) cmd);
4359}
4360
4361static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4362static int code_hdr_len = 30;
4363
4364 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004365run_do(const char *cmd, void *arg UNUSED
4366#ifdef PY_CAN_RECURSE
4367 , PyGILState_STATE *pygilstate
4368#endif
4369 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004370{
4371 PyInt lnum;
4372 size_t len;
4373 char *code;
4374 int status;
4375 PyObject *pyfunc, *pymain;
4376
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004377 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004378 {
4379 EMSG(_("cannot save undo information"));
4380 return;
4381 }
4382
4383 len = code_hdr_len + STRLEN(cmd);
4384 code = PyMem_New(char, len + 1);
4385 memcpy(code, code_hdr, code_hdr_len);
4386 STRCPY(code + code_hdr_len, cmd);
4387 status = PyRun_SimpleString(code);
4388 PyMem_Free(code);
4389
4390 if (status)
4391 {
4392 EMSG(_("failed to run the code"));
4393 return;
4394 }
4395
4396 status = 0;
4397 pymain = PyImport_AddModule("__main__");
4398 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004399#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004400 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004401#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004402
4403 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4404 {
4405 PyObject *line, *linenr, *ret;
4406
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004407#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004408 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004409#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004410 if (!(line = GetBufferLine(curbuf, lnum)))
4411 goto err;
4412 if (!(linenr = PyInt_FromLong((long) lnum)))
4413 {
4414 Py_DECREF(line);
4415 goto err;
4416 }
4417 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4418 Py_DECREF(line);
4419 Py_DECREF(linenr);
4420 if (!ret)
4421 goto err;
4422
4423 if (ret != Py_None)
4424 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4425 goto err;
4426
4427 Py_XDECREF(ret);
4428 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004429#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004430 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004431#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004432 }
4433 goto out;
4434err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004435#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004436 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004437#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004438 PyErr_PrintEx(0);
4439 PythonIO_Flush();
4440 status = 1;
4441out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004442#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004443 if (!status)
4444 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004445#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004446 Py_DECREF(pyfunc);
4447 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4448 if (status)
4449 return;
4450 check_cursor();
4451 update_curbuf(NOT_VALID);
4452}
4453
4454 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004455run_eval(const char *cmd, typval_T *rettv
4456#ifdef PY_CAN_RECURSE
4457 , PyGILState_STATE *pygilstate UNUSED
4458#endif
4459 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004460{
4461 PyObject *r;
4462
4463 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4464 if (r == NULL)
4465 {
4466 if (PyErr_Occurred() && !msg_silent)
4467 PyErr_PrintEx(0);
4468 EMSG(_("E858: Eval did not return a valid python object"));
4469 }
4470 else
4471 {
4472 if (ConvertFromPyObject(r, rettv) == -1)
4473 EMSG(_("E859: Failed to convert returned python object to vim value"));
4474 Py_DECREF(r);
4475 }
4476 PyErr_Clear();
4477}
4478
4479 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004480set_ref_in_py(const int copyID)
4481{
4482 pylinkedlist_T *cur;
4483 dict_T *dd;
4484 list_T *ll;
4485
4486 if (lastdict != NULL)
4487 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4488 {
4489 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4490 if (dd->dv_copyID != copyID)
4491 {
4492 dd->dv_copyID = copyID;
4493 set_ref_in_ht(&dd->dv_hashtab, copyID);
4494 }
4495 }
4496
4497 if (lastlist != NULL)
4498 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4499 {
4500 ll = ((ListObject *) (cur->pll_obj))->list;
4501 if (ll->lv_copyID != copyID)
4502 {
4503 ll->lv_copyID = copyID;
4504 set_ref_in_list(ll, copyID);
4505 }
4506 }
4507}
4508
4509 static int
4510set_string_copy(char_u *str, typval_T *tv)
4511{
4512 tv->vval.v_string = vim_strsave(str);
4513 if (tv->vval.v_string == NULL)
4514 {
4515 PyErr_NoMemory();
4516 return -1;
4517 }
4518 return 0;
4519}
4520
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004521 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004522pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004523{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004524 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004525 char_u *key;
4526 dictitem_T *di;
4527 PyObject *keyObject;
4528 PyObject *valObject;
4529 Py_ssize_t iter = 0;
4530
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004531 if (!(dict = dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004532 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004533
4534 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004535 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004536
4537 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4538 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004539 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004540
Bram Moolenaara03e6312013-05-29 22:49:26 +02004541 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004542 {
4543 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004544 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004545 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004546
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004547 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004548 {
4549 dict_unref(dict);
4550 return -1;
4551 }
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004552 if (*key == NUL)
4553 {
4554 dict_unref(dict);
4555 Py_XDECREF(todecref);
4556 return -1;
4557 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004558
4559 di = dictitem_alloc(key);
4560
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004561 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004562
4563 if (di == NULL)
4564 {
4565 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004566 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004567 return -1;
4568 }
4569 di->di_tv.v_lock = 0;
4570
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004571 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004572 {
4573 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004574 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004575 return -1;
4576 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004577
4578 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004579 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004580 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004581 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004582 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004583 PyErr_SetVim(_("failed to add key to dictionary"));
4584 return -1;
4585 }
4586 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004587
4588 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004589 return 0;
4590}
4591
4592 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004593pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004594{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004595 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004596 char_u *key;
4597 dictitem_T *di;
4598 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004599 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004600 PyObject *keyObject;
4601 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004602
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004603 if (!(dict = dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004604 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004605
4606 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004607 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004608
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004609 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004610 {
4611 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004612 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004613 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004614
4615 if (!(iterator = PyObject_GetIter(list)))
4616 {
4617 dict_unref(dict);
4618 Py_DECREF(list);
4619 return -1;
4620 }
4621 Py_DECREF(list);
4622
4623 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004624 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004625 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004626
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004627 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004628 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004629 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004630 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004631 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004632 return -1;
4633 }
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004634 if (*key == NUL)
4635 {
4636 Py_DECREF(keyObject);
4637 Py_DECREF(iterator);
4638 Py_XDECREF(todecref);
4639 dict_unref(dict);
4640 return -1;
4641 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004642
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004643 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004644 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004645 Py_DECREF(keyObject);
4646 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004647 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004648 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004649 return -1;
4650 }
4651
4652 di = dictitem_alloc(key);
4653
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004654 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004655 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004656
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004657 if (di == NULL)
4658 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004659 Py_DECREF(iterator);
4660 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004661 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004662 PyErr_NoMemory();
4663 return -1;
4664 }
4665 di->di_tv.v_lock = 0;
4666
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004667 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004668 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004669 Py_DECREF(iterator);
4670 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004671 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004672 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004673 return -1;
4674 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004675
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004676 Py_DECREF(valObject);
4677
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004678 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004679 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004680 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004681 dictitem_free(di);
4682 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004683 PyErr_SetVim(_("failed to add key to dictionary"));
4684 return -1;
4685 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004686 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004687 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004688 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004689 return 0;
4690}
4691
4692 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004693pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004694{
4695 list_T *l;
4696
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004697 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004698 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004699
4700 tv->v_type = VAR_LIST;
4701 tv->vval.v_list = l;
4702
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004703 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004704 {
4705 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004706 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004707 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004708
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004709 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004710 return 0;
4711}
4712
Bram Moolenaardb913952012-06-29 12:54:53 +02004713typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4714
4715 static int
4716convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004717 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004718{
4719 PyObject *capsule;
4720 char hexBuf[sizeof(void *) * 2 + 3];
4721
4722 sprintf(hexBuf, "%p", obj);
4723
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004724# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004725 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004726# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004727 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004728# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004729 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004730 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004731# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004732 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004733# else
4734 capsule = PyCObject_FromVoidPtr(tv, NULL);
4735# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004736 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4737 {
4738 Py_DECREF(capsule);
4739 tv->v_type = VAR_UNKNOWN;
4740 return -1;
4741 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004742 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004743 {
4744 tv->v_type = VAR_UNKNOWN;
4745 return -1;
4746 }
4747 /* As we are not using copy_tv which increments reference count we must
4748 * do it ourself. */
4749 switch(tv->v_type)
4750 {
4751 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4752 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4753 }
4754 }
4755 else
4756 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004757 typval_T *v;
4758
4759# ifdef PY_USE_CAPSULE
4760 v = PyCapsule_GetPointer(capsule, NULL);
4761# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004762 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004763# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004764 copy_tv(v, tv);
4765 }
4766 return 0;
4767}
4768
4769 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02004770ConvertFromPyMapping(PyObject *obj, typval_T *tv)
4771{
4772 PyObject *lookup_dict;
4773 int r;
4774
4775 if (!(lookup_dict = PyDict_New()))
4776 return -1;
4777
4778 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
4779 {
4780 tv->v_type = VAR_DICT;
4781 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4782 ++tv->vval.v_dict->dv_refcount;
4783 r = 0;
4784 }
4785 else if (PyDict_Check(obj))
4786 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
4787 else if (PyMapping_Check(obj))
4788 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
4789 else
4790 {
4791 PyErr_SetString(PyExc_TypeError,
4792 _("unable to convert object to vim dictionary"));
4793 r = -1;
4794 }
4795 Py_DECREF(lookup_dict);
4796 return r;
4797}
4798
4799 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02004800ConvertFromPyObject(PyObject *obj, typval_T *tv)
4801{
4802 PyObject *lookup_dict;
4803 int r;
4804
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004805 if (!(lookup_dict = PyDict_New()))
4806 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004807 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4808 Py_DECREF(lookup_dict);
4809 return r;
4810}
4811
4812 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004813_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004814{
Bram Moolenaara9922d62013-05-30 13:01:18 +02004815 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02004816 {
4817 tv->v_type = VAR_DICT;
4818 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4819 ++tv->vval.v_dict->dv_refcount;
4820 }
4821 else if (obj->ob_type == &ListType)
4822 {
4823 tv->v_type = VAR_LIST;
4824 tv->vval.v_list = (((ListObject *)(obj))->list);
4825 ++tv->vval.v_list->lv_refcount;
4826 }
4827 else if (obj->ob_type == &FunctionType)
4828 {
4829 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4830 return -1;
4831
4832 tv->v_type = VAR_FUNC;
4833 func_ref(tv->vval.v_string);
4834 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004835 else if (PyBytes_Check(obj))
4836 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004837 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004838
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004839 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4840 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004841 if (result == NULL)
4842 return -1;
4843
4844 if (set_string_copy(result, tv) == -1)
4845 return -1;
4846
4847 tv->v_type = VAR_STRING;
4848 }
4849 else if (PyUnicode_Check(obj))
4850 {
4851 PyObject *bytes;
4852 char_u *result;
4853
Bram Moolenaardb913952012-06-29 12:54:53 +02004854 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4855 if (bytes == NULL)
4856 return -1;
4857
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004858 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4859 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004860 if (result == NULL)
4861 return -1;
4862
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004863 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004864 {
4865 Py_XDECREF(bytes);
4866 return -1;
4867 }
4868 Py_XDECREF(bytes);
4869
4870 tv->v_type = VAR_STRING;
4871 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004872#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004873 else if (PyInt_Check(obj))
4874 {
4875 tv->v_type = VAR_NUMBER;
4876 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4877 }
4878#endif
4879 else if (PyLong_Check(obj))
4880 {
4881 tv->v_type = VAR_NUMBER;
4882 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4883 }
4884 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004885 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004886#ifdef FEAT_FLOAT
4887 else if (PyFloat_Check(obj))
4888 {
4889 tv->v_type = VAR_FLOAT;
4890 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4891 }
4892#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004893 else if (PyObject_HasAttrString(obj, "keys"))
4894 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004895 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004896 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004897 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004898 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004899 else
4900 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004901 PyErr_SetString(PyExc_TypeError,
4902 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004903 return -1;
4904 }
4905 return 0;
4906}
4907
4908 static PyObject *
4909ConvertToPyObject(typval_T *tv)
4910{
4911 if (tv == NULL)
4912 {
4913 PyErr_SetVim(_("NULL reference passed"));
4914 return NULL;
4915 }
4916 switch (tv->v_type)
4917 {
4918 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004919 return PyBytes_FromString(tv->vval.v_string == NULL
4920 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004921 case VAR_NUMBER:
4922 return PyLong_FromLong((long) tv->vval.v_number);
4923#ifdef FEAT_FLOAT
4924 case VAR_FLOAT:
4925 return PyFloat_FromDouble((double) tv->vval.v_float);
4926#endif
4927 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004928 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02004929 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02004930 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004931 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02004932 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004933 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004934 case VAR_UNKNOWN:
4935 Py_INCREF(Py_None);
4936 return Py_None;
4937 default:
4938 PyErr_SetVim(_("internal error: invalid value type"));
4939 return NULL;
4940 }
4941}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004942
4943typedef struct
4944{
4945 PyObject_HEAD
4946} CurrentObject;
4947static PyTypeObject CurrentType;
4948
4949 static void
4950init_structs(void)
4951{
4952 vim_memset(&OutputType, 0, sizeof(OutputType));
4953 OutputType.tp_name = "vim.message";
4954 OutputType.tp_basicsize = sizeof(OutputObject);
4955 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4956 OutputType.tp_doc = "vim message object";
4957 OutputType.tp_methods = OutputMethods;
4958#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004959 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4960 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004961 OutputType.tp_alloc = call_PyType_GenericAlloc;
4962 OutputType.tp_new = call_PyType_GenericNew;
4963 OutputType.tp_free = call_PyObject_Free;
4964#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004965 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4966 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004967#endif
4968
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004969 vim_memset(&IterType, 0, sizeof(IterType));
4970 IterType.tp_name = "vim.iter";
4971 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02004972 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004973 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004974 IterType.tp_iter = (getiterfunc)IterIter;
4975 IterType.tp_iternext = (iternextfunc)IterNext;
4976 IterType.tp_dealloc = (destructor)IterDestructor;
4977 IterType.tp_traverse = (traverseproc)IterTraverse;
4978 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004979
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004980 vim_memset(&BufferType, 0, sizeof(BufferType));
4981 BufferType.tp_name = "vim.buffer";
4982 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004983 BufferType.tp_dealloc = (destructor)BufferDestructor;
4984 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004985 BufferType.tp_as_sequence = &BufferAsSeq;
4986 BufferType.tp_as_mapping = &BufferAsMapping;
4987 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4988 BufferType.tp_doc = "vim buffer object";
4989 BufferType.tp_methods = BufferMethods;
4990#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004991 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004992 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004993 BufferType.tp_alloc = call_PyType_GenericAlloc;
4994 BufferType.tp_new = call_PyType_GenericNew;
4995 BufferType.tp_free = call_PyObject_Free;
4996#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004997 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004998 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004999#endif
5000
5001 vim_memset(&WindowType, 0, sizeof(WindowType));
5002 WindowType.tp_name = "vim.window";
5003 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005004 WindowType.tp_dealloc = (destructor)WindowDestructor;
5005 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005006 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005007 WindowType.tp_doc = "vim Window object";
5008 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005009 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5010 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005011#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005012 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5013 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005014 WindowType.tp_alloc = call_PyType_GenericAlloc;
5015 WindowType.tp_new = call_PyType_GenericNew;
5016 WindowType.tp_free = call_PyObject_Free;
5017#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005018 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5019 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005020#endif
5021
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005022 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5023 TabPageType.tp_name = "vim.tabpage";
5024 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005025 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5026 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005027 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5028 TabPageType.tp_doc = "vim tab page object";
5029 TabPageType.tp_methods = TabPageMethods;
5030#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005031 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005032 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5033 TabPageType.tp_new = call_PyType_GenericNew;
5034 TabPageType.tp_free = call_PyObject_Free;
5035#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005036 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005037#endif
5038
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005039 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5040 BufMapType.tp_name = "vim.bufferlist";
5041 BufMapType.tp_basicsize = sizeof(BufMapObject);
5042 BufMapType.tp_as_mapping = &BufMapAsMapping;
5043 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005044 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005045 BufferType.tp_doc = "vim buffer list";
5046
5047 vim_memset(&WinListType, 0, sizeof(WinListType));
5048 WinListType.tp_name = "vim.windowlist";
5049 WinListType.tp_basicsize = sizeof(WinListType);
5050 WinListType.tp_as_sequence = &WinListAsSeq;
5051 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5052 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005053 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005054
5055 vim_memset(&TabListType, 0, sizeof(TabListType));
5056 TabListType.tp_name = "vim.tabpagelist";
5057 TabListType.tp_basicsize = sizeof(TabListType);
5058 TabListType.tp_as_sequence = &TabListAsSeq;
5059 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5060 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005061
5062 vim_memset(&RangeType, 0, sizeof(RangeType));
5063 RangeType.tp_name = "vim.range";
5064 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005065 RangeType.tp_dealloc = (destructor)RangeDestructor;
5066 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005067 RangeType.tp_as_sequence = &RangeAsSeq;
5068 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005069 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005070 RangeType.tp_doc = "vim Range object";
5071 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005072 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5073 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005074#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005075 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005076 RangeType.tp_alloc = call_PyType_GenericAlloc;
5077 RangeType.tp_new = call_PyType_GenericNew;
5078 RangeType.tp_free = call_PyObject_Free;
5079#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005080 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005081#endif
5082
5083 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5084 CurrentType.tp_name = "vim.currentdata";
5085 CurrentType.tp_basicsize = sizeof(CurrentObject);
5086 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5087 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005088 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005089#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005090 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5091 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005092#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005093 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5094 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005095#endif
5096
5097 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5098 DictionaryType.tp_name = "vim.dictionary";
5099 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005100 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005101 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005102 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005103 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005104 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5105 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005106 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5107 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5108 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005109#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005110 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5111 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005112#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005113 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5114 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005115#endif
5116
5117 vim_memset(&ListType, 0, sizeof(ListType));
5118 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005119 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005120 ListType.tp_basicsize = sizeof(ListObject);
5121 ListType.tp_as_sequence = &ListAsSeq;
5122 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005123 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005124 ListType.tp_doc = "list pushing modifications to vim structure";
5125 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005126 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005127 ListType.tp_new = (newfunc)ListConstructor;
5128 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005129#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005130 ListType.tp_getattro = (getattrofunc)ListGetattro;
5131 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005132#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005133 ListType.tp_getattr = (getattrfunc)ListGetattr;
5134 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005135#endif
5136
5137 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005138 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005139 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005140 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5141 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005142 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005143 FunctionType.tp_doc = "object that calls vim function";
5144 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005145 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005146 FunctionType.tp_new = (newfunc)FunctionConstructor;
5147 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005148#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005149 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005150#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005151 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005152#endif
5153
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005154 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5155 OptionsType.tp_name = "vim.options";
5156 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005157 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005158 OptionsType.tp_doc = "object for manipulating options";
5159 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005160 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5161 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5162 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005163
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005164#if PY_MAJOR_VERSION >= 3
5165 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5166 vimmodule.m_name = "vim";
5167 vimmodule.m_doc = "Vim Python interface\n";
5168 vimmodule.m_size = -1;
5169 vimmodule.m_methods = VimMethods;
5170#endif
5171}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005172
5173#define PYTYPE_READY(type) \
5174 if (PyType_Ready(&type)) \
5175 return -1;
5176
5177 static int
5178init_types()
5179{
5180 PYTYPE_READY(IterType);
5181 PYTYPE_READY(BufferType);
5182 PYTYPE_READY(RangeType);
5183 PYTYPE_READY(WindowType);
5184 PYTYPE_READY(TabPageType);
5185 PYTYPE_READY(BufMapType);
5186 PYTYPE_READY(WinListType);
5187 PYTYPE_READY(TabListType);
5188 PYTYPE_READY(CurrentType);
5189 PYTYPE_READY(DictionaryType);
5190 PYTYPE_READY(ListType);
5191 PYTYPE_READY(FunctionType);
5192 PYTYPE_READY(OptionsType);
5193 PYTYPE_READY(OutputType);
5194 return 0;
5195}
5196
5197static BufMapObject TheBufferMap =
5198{
5199 PyObject_HEAD_INIT(&BufMapType)
5200};
5201
5202static WinListObject TheWindowList =
5203{
5204 PyObject_HEAD_INIT(&WinListType)
5205 NULL
5206};
5207
5208static CurrentObject TheCurrent =
5209{
5210 PyObject_HEAD_INIT(&CurrentType)
5211};
5212
5213static TabListObject TheTabPageList =
5214{
5215 PyObject_HEAD_INIT(&TabListType)
5216};
5217
5218static struct numeric_constant {
5219 char *name;
5220 int value;
5221} numeric_constants[] = {
5222 {"VAR_LOCKED", VAR_LOCKED},
5223 {"VAR_FIXED", VAR_FIXED},
5224 {"VAR_SCOPE", VAR_SCOPE},
5225 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5226};
5227
5228static struct object_constant {
5229 char *name;
5230 PyObject *value;
5231} object_constants[] = {
5232 {"buffers", (PyObject *)(void *)&TheBufferMap},
5233 {"windows", (PyObject *)(void *)&TheWindowList},
5234 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5235 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005236
5237 {"Buffer", (PyObject *)&BufferType},
5238 {"Range", (PyObject *)&RangeType},
5239 {"Window", (PyObject *)&WindowType},
5240 {"TabPage", (PyObject *)&TabPageType},
5241 {"Dictionary", (PyObject *)&DictionaryType},
5242 {"List", (PyObject *)&ListType},
5243 {"Function", (PyObject *)&FunctionType},
5244 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005245};
5246
5247typedef int (*object_adder)(PyObject *, const char *, PyObject *);
5248
5249#define ADD_OBJECT(m, name, obj) \
5250 if (add_object(m, name, obj)) \
5251 return -1;
5252
5253#define ADD_CHECKED_OBJECT(m, name, obj) \
5254 { \
5255 PyObject *value = obj; \
5256 if (!value) \
5257 return -1; \
5258 ADD_OBJECT(m, name, value); \
5259 }
5260
5261 static int
5262populate_module(PyObject *m, object_adder add_object)
5263{
5264 int i;
5265
5266 for (i = 0; i < (int)(sizeof(numeric_constants)
5267 / sizeof(struct numeric_constant));
5268 ++i)
5269 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5270 PyInt_FromLong(numeric_constants[i].value));
5271
5272 for (i = 0; i < (int)(sizeof(object_constants)
5273 / sizeof(struct object_constant));
5274 ++i)
5275 {
5276 PyObject *value;
5277
5278 value = object_constants[i].value;
5279 Py_INCREF(value);
5280 ADD_OBJECT(m, object_constants[i].name, value);
5281 }
5282
5283 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5284 return -1;
5285 ADD_OBJECT(m, "error", VimError);
5286
Bram Moolenaara9922d62013-05-30 13:01:18 +02005287 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5288 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005289 ADD_CHECKED_OBJECT(m, "options",
5290 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
5291 return 0;
5292}