blob: ae2c6bc968792aa67134c333d5c59122f6b45666 [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
Bram Moolenaar35eacd72013-05-30 22:06:33 +020029#define RAISE_NO_EMPTY_KEYS PyErr_SetString(PyExc_ValueError, \
30 _("empty keys are not allowed"))
31
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020032#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
33#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020034#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020035
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020036typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020037typedef void (*runner)(const char *, void *
38#ifdef PY_CAN_RECURSE
39 , PyGILState_STATE *
40#endif
41 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020042
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020043static int ConvertFromPyObject(PyObject *, typval_T *);
44static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020045static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020046static PyObject *WindowNew(win_T *, tabpage_T *);
47static PyObject *BufferNew (buf_T *);
48static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020049
50static PyInt RangeStart;
51static PyInt RangeEnd;
52
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020053static PyObject *globals;
54
Bram Moolenaarf4258302013-06-02 18:20:17 +020055static PyObject *py_chdir;
56static PyObject *py_fchdir;
57static PyObject *py_getcwd;
58
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020059/*
60 * obtain a lock on the Vim data structures
61 */
62 static void
63Python_Lock_Vim(void)
64{
65}
66
67/*
68 * release a lock on the Vim data structures
69 */
70 static void
71Python_Release_Vim(void)
72{
73}
74
Bram Moolenaare9ba5162013-05-29 22:02:22 +020075/*
76 * The "todecref" argument holds a pointer to PyObject * that must be
77 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
78 * was needed to generate returned value is object.
79 *
80 * Use Py_XDECREF to decrement reference count.
81 */
82 static char_u *
83StringToChars(PyObject *object, PyObject **todecref)
84{
85 char_u *p;
86 PyObject *bytes = NULL;
87
88 if (PyBytes_Check(object))
89 {
90
91 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
92 return NULL;
93 if (p == NULL)
94 return NULL;
95
96 *todecref = NULL;
97 }
98 else if (PyUnicode_Check(object))
99 {
100 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
101 if (bytes == NULL)
102 return NULL;
103
104 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
105 return NULL;
106 if (p == NULL)
107 return NULL;
108
109 *todecref = bytes;
110 }
111 else
112 {
113 PyErr_SetString(PyExc_TypeError, _("object must be string"));
114 return NULL;
115 }
116
117 return (char_u *) p;
118}
119
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200120 static int
121add_string(PyObject *list, char *s)
122{
123 PyObject *string;
124
125 if (!(string = PyString_FromString(s)))
126 return -1;
127 if (PyList_Append(list, string))
128 {
129 Py_DECREF(string);
130 return -1;
131 }
132
133 Py_DECREF(string);
134 return 0;
135}
136
137 static PyObject *
138ObjectDir(PyObject *self, char **attributes)
139{
140 PyMethodDef *method;
141 char **attr;
142 PyObject *r;
143
144 if (!(r = PyList_New(0)))
145 return NULL;
146
147 if (self)
148 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
149 if (add_string(r, (char *) method->ml_name))
150 {
151 Py_DECREF(r);
152 return NULL;
153 }
154
155 for (attr = attributes ; *attr ; ++attr)
156 if (add_string(r, *attr))
157 {
158 Py_DECREF(r);
159 return NULL;
160 }
161
162#if PY_MAJOR_VERSION < 3
163 if (add_string(r, "__members__"))
164 {
165 Py_DECREF(r);
166 return NULL;
167 }
168#endif
169
170 return r;
171}
172
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200173/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200174 */
175
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200176/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200177typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200178
179static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200180
181typedef struct
182{
183 PyObject_HEAD
184 long softspace;
185 long error;
186} OutputObject;
187
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200188static char *OutputAttrs[] = {
189 "softspace",
190 NULL
191};
192
193 static PyObject *
194OutputDir(PyObject *self)
195{
196 return ObjectDir(self, OutputAttrs);
197}
198
Bram Moolenaar77045652012-09-21 13:46:06 +0200199 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200200OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200201{
202 if (val == NULL)
203 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200204 PyErr_SetString(PyExc_AttributeError,
205 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200206 return -1;
207 }
208
209 if (strcmp(name, "softspace") == 0)
210 {
211 if (!PyInt_Check(val))
212 {
213 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
214 return -1;
215 }
216
Bram Moolenaard6e39182013-05-21 18:30:34 +0200217 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200218 return 0;
219 }
220
221 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
222 return -1;
223}
224
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200225/* Buffer IO, we write one whole line at a time. */
226static garray_T io_ga = {0, 0, 1, 80, NULL};
227static writefn old_fn = NULL;
228
229 static void
230PythonIO_Flush(void)
231{
232 if (old_fn != NULL && io_ga.ga_len > 0)
233 {
234 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
235 old_fn((char_u *)io_ga.ga_data);
236 }
237 io_ga.ga_len = 0;
238}
239
240 static void
241writer(writefn fn, char_u *str, PyInt n)
242{
243 char_u *ptr;
244
245 /* Flush when switching output function. */
246 if (fn != old_fn)
247 PythonIO_Flush();
248 old_fn = fn;
249
250 /* Write each NL separated line. Text after the last NL is kept for
251 * writing later. */
252 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
253 {
254 PyInt len = ptr - str;
255
256 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
257 break;
258
259 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
260 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
261 fn((char_u *)io_ga.ga_data);
262 str = ptr + 1;
263 n -= len + 1;
264 io_ga.ga_len = 0;
265 }
266
267 /* Put the remaining text into io_ga for later printing. */
268 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
269 {
270 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
271 io_ga.ga_len += (int)n;
272 }
273}
274
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200275 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200276OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200277{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200278 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200279 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200280 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200281
Bram Moolenaar27564802011-09-07 19:30:21 +0200282 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200283 return NULL;
284
285 Py_BEGIN_ALLOW_THREADS
286 Python_Lock_Vim();
287 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
288 Python_Release_Vim();
289 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200290 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200291
292 Py_INCREF(Py_None);
293 return Py_None;
294}
295
296 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200297OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200298{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200299 PyObject *seq;
300 PyObject *iterator;
301 PyObject *item;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200302 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200303
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200304 if (!PyArg_ParseTuple(args, "O", &seq))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200305 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200306
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200307 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200308 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200309
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200310 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200311 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200312 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200313 PyInt len;
314
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200315 if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
Bram Moolenaardb913952012-06-29 12:54:53 +0200316 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200317 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200318 Py_DECREF(iterator);
319 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200320 return NULL;
321 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200322 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200323
324 Py_BEGIN_ALLOW_THREADS
325 Python_Lock_Vim();
326 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
327 Python_Release_Vim();
328 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200329 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200330 }
331
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200332 Py_DECREF(iterator);
333
334 /* Iterator may have finished due to an exception */
335 if (PyErr_Occurred())
336 return NULL;
337
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200338 Py_INCREF(Py_None);
339 return Py_None;
340}
341
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100342 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200343OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100344{
345 /* do nothing */
346 Py_INCREF(Py_None);
347 return Py_None;
348}
349
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200350/***************/
351
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200352static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200353 /* name, function, calling, doc */
354 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
355 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
356 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200357 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200358 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200359};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200360
361static OutputObject Output =
362{
363 PyObject_HEAD_INIT(&OutputType)
364 0,
365 0
366};
367
368static OutputObject Error =
369{
370 PyObject_HEAD_INIT(&OutputType)
371 0,
372 1
373};
374
375 static int
376PythonIO_Init_io(void)
377{
378 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
379 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
380
381 if (PyErr_Occurred())
382 {
383 EMSG(_("E264: Python: Error initialising I/O objects"));
384 return -1;
385 }
386
387 return 0;
388}
389
390
391static PyObject *VimError;
392
393/* Check to see whether a Vim error has been reported, or a keyboard
394 * interrupt has been detected.
395 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200396
397 static void
398VimTryStart(void)
399{
400 ++trylevel;
401}
402
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200403 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200404VimTryEnd(void)
405{
406 --trylevel;
407 if (got_int)
408 {
409 PyErr_SetNone(PyExc_KeyboardInterrupt);
410 return 1;
411 }
412 else if (!did_throw)
413 return 0;
414 else if (PyErr_Occurred())
415 return 1;
416 else
417 {
418 PyErr_SetVim((char *) current_exception->value);
419 discard_current_exception();
420 return 1;
421 }
422}
423
424 static int
425VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200426{
427 if (got_int)
428 {
429 PyErr_SetNone(PyExc_KeyboardInterrupt);
430 return 1;
431 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200432 return 0;
433}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200434
435/* Vim module - Implementation
436 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200437
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200438 static PyObject *
439VimCommand(PyObject *self UNUSED, PyObject *args)
440{
441 char *cmd;
442 PyObject *result;
443
444 if (!PyArg_ParseTuple(args, "s", &cmd))
445 return NULL;
446
447 PyErr_Clear();
448
449 Py_BEGIN_ALLOW_THREADS
450 Python_Lock_Vim();
451
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200452 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200453 do_cmdline_cmd((char_u *)cmd);
454 update_screen(VALID);
455
456 Python_Release_Vim();
457 Py_END_ALLOW_THREADS
458
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200459 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200460 result = NULL;
461 else
462 result = Py_None;
463
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200464
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200465 Py_XINCREF(result);
466 return result;
467}
468
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200469/*
470 * Function to translate a typval_T into a PyObject; this will recursively
471 * translate lists/dictionaries into their Python equivalents.
472 *
473 * The depth parameter is to avoid infinite recursion, set it to 1 when
474 * you call VimToPython.
475 */
476 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200477VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200478{
479 PyObject *result;
480 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200481 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200482
483 /* Avoid infinite recursion */
484 if (depth > 100)
485 {
486 Py_INCREF(Py_None);
487 result = Py_None;
488 return result;
489 }
490
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200491 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200492 * then and we can use it again. */
493 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
494 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
495 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200496 sprintf(ptrBuf, "%p",
497 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
498 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200499
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200500 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200501 {
502 Py_INCREF(result);
503 return result;
504 }
505 }
506
507 if (our_tv->v_type == VAR_STRING)
508 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200509 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200510 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200511 }
512 else if (our_tv->v_type == VAR_NUMBER)
513 {
514 char buf[NUMBUFLEN];
515
516 /* For backwards compatibility numbers are stored as strings. */
517 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200518 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200519 }
520# ifdef FEAT_FLOAT
521 else if (our_tv->v_type == VAR_FLOAT)
522 {
523 char buf[NUMBUFLEN];
524
525 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200526 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200527 }
528# endif
529 else if (our_tv->v_type == VAR_LIST)
530 {
531 list_T *list = our_tv->vval.v_list;
532 listitem_T *curr;
533
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200534 if (list == NULL)
535 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200536
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200537 if (!(result = PyList_New(0)))
538 return NULL;
539
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200540 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200541 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200542 Py_DECREF(result);
543 return NULL;
544 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200545
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200546 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
547 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200548 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200549 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200550 Py_DECREF(result);
551 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200552 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200553 if (PyList_Append(result, newObj))
554 {
555 Py_DECREF(newObj);
556 Py_DECREF(result);
557 return NULL;
558 }
559 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200560 }
561 }
562 else if (our_tv->v_type == VAR_DICT)
563 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200564
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200565 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
566 long_u todo = ht->ht_used;
567 hashitem_T *hi;
568 dictitem_T *di;
569 if (our_tv->vval.v_dict == NULL)
570 return NULL;
571
572 if (!(result = PyDict_New()))
573 return NULL;
574
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200575 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200576 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200577 Py_DECREF(result);
578 return NULL;
579 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200580
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200581 for (hi = ht->ht_array; todo > 0; ++hi)
582 {
583 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200584 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200585 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200586
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200587 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200588 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200589 {
590 Py_DECREF(result);
591 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200592 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200593 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
594 {
595 Py_DECREF(result);
596 Py_DECREF(newObj);
597 return NULL;
598 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200599 }
600 }
601 }
602 else
603 {
604 Py_INCREF(Py_None);
605 result = Py_None;
606 }
607
608 return result;
609}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200610
611 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200612VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200614 char *expr;
615 typval_T *our_tv;
616 PyObject *result;
617 PyObject *lookup_dict;
618
619 if (!PyArg_ParseTuple(args, "s", &expr))
620 return NULL;
621
622 Py_BEGIN_ALLOW_THREADS
623 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200624 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200625 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200626 Python_Release_Vim();
627 Py_END_ALLOW_THREADS
628
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200629 if (VimTryEnd())
630 return NULL;
631
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200632 if (our_tv == NULL)
633 {
634 PyErr_SetVim(_("invalid expression"));
635 return NULL;
636 }
637
638 /* Convert the Vim type into a Python type. Create a dictionary that's
639 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200640 if (!(lookup_dict = PyDict_New()))
641 result = NULL;
642 else
643 {
644 result = VimToPython(our_tv, 1, lookup_dict);
645 Py_DECREF(lookup_dict);
646 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200647
648
649 Py_BEGIN_ALLOW_THREADS
650 Python_Lock_Vim();
651 free_tv(our_tv);
652 Python_Release_Vim();
653 Py_END_ALLOW_THREADS
654
655 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200656}
657
Bram Moolenaardb913952012-06-29 12:54:53 +0200658static PyObject *ConvertToPyObject(typval_T *);
659
660 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200661VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200662{
Bram Moolenaardb913952012-06-29 12:54:53 +0200663 char *expr;
664 typval_T *our_tv;
665 PyObject *result;
666
667 if (!PyArg_ParseTuple(args, "s", &expr))
668 return NULL;
669
670 Py_BEGIN_ALLOW_THREADS
671 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200672 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200673 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200674 Python_Release_Vim();
675 Py_END_ALLOW_THREADS
676
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200677 if (VimTryEnd())
678 return NULL;
679
Bram Moolenaardb913952012-06-29 12:54:53 +0200680 if (our_tv == NULL)
681 {
682 PyErr_SetVim(_("invalid expression"));
683 return NULL;
684 }
685
686 result = ConvertToPyObject(our_tv);
687 Py_BEGIN_ALLOW_THREADS
688 Python_Lock_Vim();
689 free_tv(our_tv);
690 Python_Release_Vim();
691 Py_END_ALLOW_THREADS
692
693 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200694}
695
696 static PyObject *
697VimStrwidth(PyObject *self UNUSED, PyObject *args)
698{
699 char *expr;
700
701 if (!PyArg_ParseTuple(args, "s", &expr))
702 return NULL;
703
Bram Moolenaara54bf402012-12-05 16:30:07 +0100704 return PyLong_FromLong(
705#ifdef FEAT_MBYTE
706 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
707#else
708 STRLEN(expr)
709#endif
710 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200711}
712
Bram Moolenaarf4258302013-06-02 18:20:17 +0200713 static PyObject *
714_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
715{
716 PyObject *r;
717 PyObject *newwd;
718 PyObject *todecref;
719 char_u *new_dir;
720
Bram Moolenaard4209d22013-06-05 20:34:15 +0200721 if (_chdir == NULL)
722 return NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200723 if (!(r = PyObject_Call(_chdir, args, kwargs)))
724 return NULL;
725
726 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
727 {
728 Py_DECREF(r);
729 return NULL;
730 }
731
732 if (!(new_dir = StringToChars(newwd, &todecref)))
733 {
734 Py_DECREF(r);
735 Py_DECREF(newwd);
736 return NULL;
737 }
738
739 VimTryStart();
740
741 if (vim_chdir(new_dir))
742 {
743 Py_DECREF(r);
744 Py_DECREF(newwd);
745 Py_XDECREF(todecref);
746
747 if (VimTryEnd())
748 return NULL;
749
750 PyErr_SetVim(_("failed to change directory"));
751 return NULL;
752 }
753
754 Py_DECREF(newwd);
755 Py_XDECREF(todecref);
756
757 post_chdir(FALSE);
758
759 if (VimTryEnd())
760 {
761 Py_DECREF(r);
762 return NULL;
763 }
764
765 return r;
766}
767
768 static PyObject *
769VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
770{
771 return _VimChdir(py_chdir, args, kwargs);
772}
773
774 static PyObject *
775VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
776{
777 return _VimChdir(py_fchdir, args, kwargs);
778}
779
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200780/*
781 * Vim module - Definitions
782 */
783
784static struct PyMethodDef VimMethods[] = {
Bram Moolenaarf4258302013-06-02 18:20:17 +0200785 /* name, function, calling, documentation */
786 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
787 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
788 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
789 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
790 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
791 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
792 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200793};
794
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200796 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200797 */
798
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200799static PyTypeObject IterType;
800
801typedef PyObject *(*nextfun)(void **);
802typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200803typedef int (*traversefun)(void *, visitproc, void *);
804typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200805
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200806/* Main purpose of this object is removing the need for do python
807 * initialization (i.e. PyType_Ready and setting type attributes) for a big
808 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200809
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200810typedef struct
811{
812 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200813 void *cur;
814 nextfun next;
815 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200816 traversefun traverse;
817 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200818} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200819
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200820 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200821IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
822 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200823{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200824 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200825
Bram Moolenaar774267b2013-05-21 20:51:59 +0200826 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200827 self->cur = start;
828 self->next = next;
829 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200830 self->traverse = traverse;
831 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200832
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200833 return (PyObject *)(self);
834}
835
836 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200837IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200838{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200839 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200840 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200841 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200842}
843
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200844 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200845IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200846{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200847 if (self->traverse != NULL)
848 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200849 else
850 return 0;
851}
852
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200853/* Mac OSX defines clear() somewhere. */
854#ifdef clear
855# undef clear
856#endif
857
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200858 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200859IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200860{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200861 if (self->clear != NULL)
862 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200863 else
864 return 0;
865}
866
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200867 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200868IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200869{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200870 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200871}
872
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200873 static PyObject *
874IterIter(PyObject *self)
875{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +0200876 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200877 return self;
878}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200879
Bram Moolenaardb913952012-06-29 12:54:53 +0200880typedef struct pylinkedlist_S {
881 struct pylinkedlist_S *pll_next;
882 struct pylinkedlist_S *pll_prev;
883 PyObject *pll_obj;
884} pylinkedlist_T;
885
886static pylinkedlist_T *lastdict = NULL;
887static pylinkedlist_T *lastlist = NULL;
888
889 static void
890pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
891{
892 if (ref->pll_prev == NULL)
893 {
894 if (ref->pll_next == NULL)
895 {
896 *last = NULL;
897 return;
898 }
899 }
900 else
901 ref->pll_prev->pll_next = ref->pll_next;
902
903 if (ref->pll_next == NULL)
904 *last = ref->pll_prev;
905 else
906 ref->pll_next->pll_prev = ref->pll_prev;
907}
908
909 static void
910pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
911{
912 if (*last == NULL)
913 ref->pll_prev = NULL;
914 else
915 {
916 (*last)->pll_next = ref;
917 ref->pll_prev = *last;
918 }
919 ref->pll_next = NULL;
920 ref->pll_obj = self;
921 *last = ref;
922}
923
924static PyTypeObject DictionaryType;
925
926typedef struct
927{
928 PyObject_HEAD
929 dict_T *dict;
930 pylinkedlist_T ref;
931} DictionaryObject;
932
Bram Moolenaara9922d62013-05-30 13:01:18 +0200933static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
934
935#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
936
Bram Moolenaardb913952012-06-29 12:54:53 +0200937 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +0200938DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +0200939{
940 DictionaryObject *self;
941
Bram Moolenaara9922d62013-05-30 13:01:18 +0200942 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200943 if (self == NULL)
944 return NULL;
945 self->dict = dict;
946 ++dict->dv_refcount;
947
948 pyll_add((PyObject *)(self), &self->ref, &lastdict);
949
950 return (PyObject *)(self);
951}
952
Bram Moolenaara9922d62013-05-30 13:01:18 +0200953 static dict_T *
954py_dict_alloc()
955{
956 dict_T *r;
957
958 if (!(r = dict_alloc()))
959 {
960 PyErr_NoMemory();
961 return NULL;
962 }
963 ++r->dv_refcount;
964
965 return r;
966}
967
968 static PyObject *
969DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
970{
971 DictionaryObject *self;
972 dict_T *dict;
973
974 if (!(dict = py_dict_alloc()))
975 return NULL;
976
977 self = (DictionaryObject *) DictionaryNew(subtype, dict);
978
979 --dict->dv_refcount;
980
981 if (kwargs || PyTuple_Size(args))
982 {
983 PyObject *tmp;
984 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
985 {
986 Py_DECREF(self);
987 return NULL;
988 }
989
990 Py_DECREF(tmp);
991 }
992
993 return (PyObject *)(self);
994}
995
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200996 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200997DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200998{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200999 pyll_remove(&self->ref, &lastdict);
1000 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001001
1002 DESTRUCTOR_FINISH(self);
1003}
1004
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001005static char *DictionaryAttrs[] = {
1006 "locked", "scope",
1007 NULL
1008};
1009
1010 static PyObject *
1011DictionaryDir(PyObject *self)
1012{
1013 return ObjectDir(self, DictionaryAttrs);
1014}
1015
Bram Moolenaardb913952012-06-29 12:54:53 +02001016 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001017DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001018{
1019 if (val == NULL)
1020 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001021 PyErr_SetString(PyExc_AttributeError,
1022 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001023 return -1;
1024 }
1025
1026 if (strcmp(name, "locked") == 0)
1027 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001028 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001029 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001030 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001031 return -1;
1032 }
1033 else
1034 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001035 int istrue = PyObject_IsTrue(val);
1036 if (istrue == -1)
1037 return -1;
1038 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001039 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001040 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001041 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001042 }
1043 return 0;
1044 }
1045 else
1046 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001047 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001048 return -1;
1049 }
1050}
1051
1052 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001053DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001054{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001055 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001056}
1057
Bram Moolenaara9922d62013-05-30 13:01:18 +02001058#define DICT_FLAG_HAS_DEFAULT 0x01
1059#define DICT_FLAG_POP 0x02
1060#define DICT_FLAG_NONE_DEFAULT 0x04
1061#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1062#define DICT_FLAG_RETURN_PAIR 0x10
1063
Bram Moolenaardb913952012-06-29 12:54:53 +02001064 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001065_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001066{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001067 PyObject *keyObject;
1068 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1069 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001070 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001071 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001072 dict_T *dict = self->dict;
1073 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001074 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001075
Bram Moolenaara9922d62013-05-30 13:01:18 +02001076 if (flags & DICT_FLAG_HAS_DEFAULT)
1077 {
1078 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1079 return NULL;
1080 }
1081 else
1082 keyObject = args;
1083
1084 if (flags & DICT_FLAG_RETURN_BOOL)
1085 defObject = Py_False;
1086
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001087 if (!(key = StringToChars(keyObject, &todecref)))
1088 return NULL;
1089
1090 if (*key == NUL)
1091 {
1092 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001093 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001094 return NULL;
1095 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001096
Bram Moolenaara9922d62013-05-30 13:01:18 +02001097 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001098
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001099 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001100
Bram Moolenaara9922d62013-05-30 13:01:18 +02001101 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001102 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001103 if (defObject)
1104 {
1105 Py_INCREF(defObject);
1106 return defObject;
1107 }
1108 else
1109 {
1110 PyErr_SetObject(PyExc_KeyError, keyObject);
1111 return NULL;
1112 }
1113 }
1114 else if (flags & DICT_FLAG_RETURN_BOOL)
1115 {
1116 Py_INCREF(Py_True);
1117 return Py_True;
1118 }
1119
1120 di = dict_lookup(hi);
1121
1122 if (!(r = ConvertToPyObject(&di->di_tv)))
1123 return NULL;
1124
1125 if (flags & DICT_FLAG_POP)
1126 {
1127 if (dict->dv_lock)
1128 {
1129 PyErr_SetVim(_("dict is locked"));
1130 Py_DECREF(r);
1131 return NULL;
1132 }
1133
1134 hash_remove(&dict->dv_hashtab, hi);
1135 dictitem_free(di);
1136 }
1137
Bram Moolenaara9922d62013-05-30 13:01:18 +02001138 return r;
1139}
1140
1141 static PyObject *
1142DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1143{
1144 return _DictionaryItem(self, keyObject, 0);
1145}
1146
1147 static int
1148DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1149{
1150 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1151 int r;
1152
1153 r = (rObj == Py_True);
1154
1155 Py_DECREF(Py_True);
1156
1157 return r;
1158}
1159
1160typedef struct
1161{
1162 hashitem_T *ht_array;
1163 long_u ht_used;
1164 hashtab_T *ht;
1165 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001166 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001167} dictiterinfo_T;
1168
1169 static PyObject *
1170DictionaryIterNext(dictiterinfo_T **dii)
1171{
1172 PyObject *r;
1173
1174 if (!(*dii)->todo)
1175 return NULL;
1176
1177 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1178 (*dii)->ht->ht_used != (*dii)->ht_used)
1179 {
1180 PyErr_SetString(PyExc_RuntimeError,
1181 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001182 return NULL;
1183 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001184
Bram Moolenaara9922d62013-05-30 13:01:18 +02001185 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1186 ++((*dii)->hi);
1187
1188 --((*dii)->todo);
1189
1190 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1191 return NULL;
1192
1193 return r;
1194}
1195
1196 static PyObject *
1197DictionaryIter(DictionaryObject *self)
1198{
1199 dictiterinfo_T *dii;
1200 hashtab_T *ht;
1201
1202 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1203 {
1204 PyErr_NoMemory();
1205 return NULL;
1206 }
1207
1208 ht = &self->dict->dv_hashtab;
1209 dii->ht_array = ht->ht_array;
1210 dii->ht_used = ht->ht_used;
1211 dii->ht = ht;
1212 dii->hi = dii->ht_array;
1213 dii->todo = dii->ht_used;
1214
1215 return IterNew(dii,
1216 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1217 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001218}
1219
1220 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001221DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001222{
1223 char_u *key;
1224 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001225 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001226 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001227 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001228
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001229 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001230 {
1231 PyErr_SetVim(_("dict is locked"));
1232 return -1;
1233 }
1234
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001235 if (!(key = StringToChars(keyObject, &todecref)))
1236 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001237
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001238 if (*key == NUL)
1239 {
1240 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001241 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001242 return -1;
1243 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001244
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001245 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001246
1247 if (valObject == NULL)
1248 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001249 hashitem_T *hi;
1250
Bram Moolenaardb913952012-06-29 12:54:53 +02001251 if (di == NULL)
1252 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001253 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001254 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001255 return -1;
1256 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001257 hi = hash_find(&dict->dv_hashtab, di->di_key);
1258 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001259 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001260 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001261 return 0;
1262 }
1263
1264 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001265 {
1266 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001267 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001268 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001269
1270 if (di == NULL)
1271 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001272 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001273 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001274 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001275 PyErr_NoMemory();
1276 return -1;
1277 }
1278 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001279 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001280
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001281 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001282 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001283 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001284 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001285 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001286 PyErr_SetVim(_("failed to add key to dictionary"));
1287 return -1;
1288 }
1289 }
1290 else
1291 clear_tv(&di->di_tv);
1292
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001293 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001294
1295 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001296 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001297 return 0;
1298}
1299
Bram Moolenaara9922d62013-05-30 13:01:18 +02001300typedef PyObject *(*hi_to_py)(hashitem_T *);
1301
Bram Moolenaardb913952012-06-29 12:54:53 +02001302 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001303DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001304{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001305 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001306 long_u todo = dict->dv_hashtab.ht_used;
1307 Py_ssize_t i = 0;
1308 PyObject *r;
1309 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001310 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001311
1312 r = PyList_New(todo);
1313 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1314 {
1315 if (!HASHITEM_EMPTY(hi))
1316 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001317 if (!(newObj = hiconvert(hi)))
1318 {
1319 Py_DECREF(r);
1320 return NULL;
1321 }
1322 if (PyList_SetItem(r, i, newObj))
1323 {
1324 Py_DECREF(r);
1325 Py_DECREF(newObj);
1326 return NULL;
1327 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001328 --todo;
1329 ++i;
1330 }
1331 }
1332 return r;
1333}
1334
Bram Moolenaara9922d62013-05-30 13:01:18 +02001335 static PyObject *
1336dict_key(hashitem_T *hi)
1337{
1338 return PyBytes_FromString((char *)(hi->hi_key));
1339}
1340
1341 static PyObject *
1342DictionaryListKeys(DictionaryObject *self)
1343{
1344 return DictionaryListObjects(self, dict_key);
1345}
1346
1347 static PyObject *
1348dict_val(hashitem_T *hi)
1349{
1350 dictitem_T *di;
1351
1352 di = dict_lookup(hi);
1353 return ConvertToPyObject(&di->di_tv);
1354}
1355
1356 static PyObject *
1357DictionaryListValues(DictionaryObject *self)
1358{
1359 return DictionaryListObjects(self, dict_val);
1360}
1361
1362 static PyObject *
1363dict_item(hashitem_T *hi)
1364{
1365 PyObject *keyObject;
1366 PyObject *valObject;
1367 PyObject *r;
1368
1369 if (!(keyObject = dict_key(hi)))
1370 return NULL;
1371
1372 if (!(valObject = dict_val(hi)))
1373 {
1374 Py_DECREF(keyObject);
1375 return NULL;
1376 }
1377
1378 r = Py_BuildValue("(OO)", keyObject, valObject);
1379
1380 Py_DECREF(keyObject);
1381 Py_DECREF(valObject);
1382
1383 return r;
1384}
1385
1386 static PyObject *
1387DictionaryListItems(DictionaryObject *self)
1388{
1389 return DictionaryListObjects(self, dict_item);
1390}
1391
1392 static PyObject *
1393DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1394{
1395 dict_T *dict = self->dict;
1396
1397 if (dict->dv_lock)
1398 {
1399 PyErr_SetVim(_("dict is locked"));
1400 return NULL;
1401 }
1402
1403 if (kwargs)
1404 {
1405 typval_T tv;
1406
1407 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1408 return NULL;
1409
1410 VimTryStart();
1411 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1412 clear_tv(&tv);
1413 if (VimTryEnd())
1414 return NULL;
1415 }
1416 else
1417 {
1418 PyObject *object;
1419
1420 if (!PyArg_Parse(args, "(O)", &object))
1421 return NULL;
1422
1423 if (PyObject_HasAttrString(object, "keys"))
1424 return DictionaryUpdate(self, NULL, object);
1425 else
1426 {
1427 PyObject *iterator;
1428 PyObject *item;
1429
1430 if (!(iterator = PyObject_GetIter(object)))
1431 return NULL;
1432
1433 while ((item = PyIter_Next(iterator)))
1434 {
1435 PyObject *fast;
1436 PyObject *keyObject;
1437 PyObject *valObject;
1438 PyObject *todecref;
1439 char_u *key;
1440 dictitem_T *di;
1441
1442 if (!(fast = PySequence_Fast(item, "")))
1443 {
1444 Py_DECREF(iterator);
1445 Py_DECREF(item);
1446 return NULL;
1447 }
1448
1449 Py_DECREF(item);
1450
1451 if (PySequence_Fast_GET_SIZE(fast) != 2)
1452 {
1453 Py_DECREF(iterator);
1454 Py_DECREF(fast);
1455 PyErr_SetString(PyExc_ValueError,
1456 _("expected sequence element of size 2"));
1457 return NULL;
1458 }
1459
1460 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1461
1462 if (!(key = StringToChars(keyObject, &todecref)))
1463 {
1464 Py_DECREF(iterator);
1465 Py_DECREF(fast);
1466 return NULL;
1467 }
1468
1469 di = dictitem_alloc(key);
1470
1471 Py_XDECREF(todecref);
1472
1473 if (di == NULL)
1474 {
1475 Py_DECREF(fast);
1476 Py_DECREF(iterator);
1477 PyErr_NoMemory();
1478 return NULL;
1479 }
1480 di->di_tv.v_lock = 0;
1481 di->di_tv.v_type = VAR_UNKNOWN;
1482
1483 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1484
1485 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1486 {
1487 Py_DECREF(iterator);
1488 Py_DECREF(fast);
1489 dictitem_free(di);
1490 return NULL;
1491 }
1492
1493 Py_DECREF(fast);
1494
1495 if (dict_add(dict, di) == FAIL)
1496 {
1497 Py_DECREF(iterator);
1498 dictitem_free(di);
1499 PyErr_SetVim(_("failed to add key to dictionary"));
1500 return NULL;
1501 }
1502 }
1503
1504 Py_DECREF(iterator);
1505
1506 /* Iterator may have finished due to an exception */
1507 if (PyErr_Occurred())
1508 return NULL;
1509 }
1510 }
1511 Py_INCREF(Py_None);
1512 return Py_None;
1513}
1514
1515 static PyObject *
1516DictionaryGet(DictionaryObject *self, PyObject *args)
1517{
1518 return _DictionaryItem(self, args,
1519 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1520}
1521
1522 static PyObject *
1523DictionaryPop(DictionaryObject *self, PyObject *args)
1524{
1525 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1526}
1527
1528 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001529DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001530{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001531 hashitem_T *hi;
1532 PyObject *r;
1533 PyObject *valObject;
1534 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001535
Bram Moolenaarde71b562013-06-02 17:41:54 +02001536 if (self->dict->dv_hashtab.ht_used == 0)
1537 {
1538 PyErr_SetNone(PyExc_KeyError);
1539 return NULL;
1540 }
1541
1542 hi = self->dict->dv_hashtab.ht_array;
1543 while (HASHITEM_EMPTY(hi))
1544 ++hi;
1545
1546 di = dict_lookup(hi);
1547
1548 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001549 return NULL;
1550
Bram Moolenaarde71b562013-06-02 17:41:54 +02001551 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
1552 {
1553 Py_DECREF(valObject);
1554 return NULL;
1555 }
1556
1557 hash_remove(&self->dict->dv_hashtab, hi);
1558 dictitem_free(di);
1559
1560 return r;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001561}
1562
1563 static PyObject *
1564DictionaryHasKey(DictionaryObject *self, PyObject *args)
1565{
1566 PyObject *keyObject;
1567
1568 if (!PyArg_ParseTuple(args, "O", &keyObject))
1569 return NULL;
1570
1571 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1572}
1573
1574static PySequenceMethods DictionaryAsSeq = {
1575 0, /* sq_length */
1576 0, /* sq_concat */
1577 0, /* sq_repeat */
1578 0, /* sq_item */
1579 0, /* sq_slice */
1580 0, /* sq_ass_item */
1581 0, /* sq_ass_slice */
1582 (objobjproc) DictionaryContains, /* sq_contains */
1583 0, /* sq_inplace_concat */
1584 0, /* sq_inplace_repeat */
1585};
1586
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001587static PyMappingMethods DictionaryAsMapping = {
1588 (lenfunc) DictionaryLength,
1589 (binaryfunc) DictionaryItem,
1590 (objobjargproc) DictionaryAssItem,
1591};
1592
Bram Moolenaardb913952012-06-29 12:54:53 +02001593static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001594 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001595 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1596 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1597 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1598 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1599 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02001600 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001601 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001602 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1603 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001604};
1605
1606static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001607static PySequenceMethods ListAsSeq;
1608static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001609
1610typedef struct
1611{
1612 PyObject_HEAD
1613 list_T *list;
1614 pylinkedlist_T ref;
1615} ListObject;
1616
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001617#define NEW_LIST(list) ListNew(&ListType, list)
1618
Bram Moolenaardb913952012-06-29 12:54:53 +02001619 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001620ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001621{
1622 ListObject *self;
1623
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001624 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001625 if (self == NULL)
1626 return NULL;
1627 self->list = list;
1628 ++list->lv_refcount;
1629
1630 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1631
1632 return (PyObject *)(self);
1633}
1634
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001635 static list_T *
1636py_list_alloc()
1637{
1638 list_T *r;
1639
1640 if (!(r = list_alloc()))
1641 {
1642 PyErr_NoMemory();
1643 return NULL;
1644 }
1645 ++r->lv_refcount;
1646
1647 return r;
1648}
1649
1650 static int
1651list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1652{
1653 PyObject *iterator;
1654 PyObject *item;
1655 listitem_T *li;
1656
1657 if (!(iterator = PyObject_GetIter(obj)))
1658 return -1;
1659
1660 while ((item = PyIter_Next(iterator)))
1661 {
1662 if (!(li = listitem_alloc()))
1663 {
1664 PyErr_NoMemory();
1665 Py_DECREF(item);
1666 Py_DECREF(iterator);
1667 return -1;
1668 }
1669 li->li_tv.v_lock = 0;
1670 li->li_tv.v_type = VAR_UNKNOWN;
1671
1672 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1673 {
1674 Py_DECREF(item);
1675 Py_DECREF(iterator);
1676 listitem_free(li);
1677 return -1;
1678 }
1679
1680 Py_DECREF(item);
1681
1682 list_append(l, li);
1683 }
1684
1685 Py_DECREF(iterator);
1686
1687 /* Iterator may have finished due to an exception */
1688 if (PyErr_Occurred())
1689 return -1;
1690
1691 return 0;
1692}
1693
1694 static PyObject *
1695ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1696{
1697 list_T *list;
1698 PyObject *obj = NULL;
1699
1700 if (kwargs)
1701 {
1702 PyErr_SetString(PyExc_TypeError,
1703 _("list constructor does not accept keyword arguments"));
1704 return NULL;
1705 }
1706
1707 if (!PyArg_ParseTuple(args, "|O", &obj))
1708 return NULL;
1709
1710 if (!(list = py_list_alloc()))
1711 return NULL;
1712
1713 if (obj)
1714 {
1715 PyObject *lookup_dict;
1716
1717 if (!(lookup_dict = PyDict_New()))
1718 {
1719 list_unref(list);
1720 return NULL;
1721 }
1722
1723 if (list_py_concat(list, obj, lookup_dict) == -1)
1724 {
1725 Py_DECREF(lookup_dict);
1726 list_unref(list);
1727 return NULL;
1728 }
1729
1730 Py_DECREF(lookup_dict);
1731 }
1732
1733 return ListNew(subtype, list);
1734}
1735
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001736 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001737ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001738{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001739 pyll_remove(&self->ref, &lastlist);
1740 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001741
1742 DESTRUCTOR_FINISH(self);
1743}
1744
Bram Moolenaardb913952012-06-29 12:54:53 +02001745 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001746ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001747{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001748 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001749}
1750
1751 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001752ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001753{
1754 listitem_T *li;
1755
Bram Moolenaard6e39182013-05-21 18:30:34 +02001756 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001757 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001758 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001759 return NULL;
1760 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001761 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001762 if (li == NULL)
1763 {
1764 PyErr_SetVim(_("internal error: failed to get vim list item"));
1765 return NULL;
1766 }
1767 return ConvertToPyObject(&li->li_tv);
1768}
1769
1770#define PROC_RANGE \
1771 if (last < 0) {\
1772 if (last < -size) \
1773 last = 0; \
1774 else \
1775 last += size; \
1776 } \
1777 if (first < 0) \
1778 first = 0; \
1779 if (first > size) \
1780 first = size; \
1781 if (last > size) \
1782 last = size;
1783
1784 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001785ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001786{
1787 PyInt i;
1788 PyInt size = ListLength(self);
1789 PyInt n;
1790 PyObject *list;
1791 int reversed = 0;
1792
1793 PROC_RANGE
1794 if (first >= last)
1795 first = last;
1796
1797 n = last-first;
1798 list = PyList_New(n);
1799 if (list == NULL)
1800 return NULL;
1801
1802 for (i = 0; i < n; ++i)
1803 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001804 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001805 if (item == NULL)
1806 {
1807 Py_DECREF(list);
1808 return NULL;
1809 }
1810
1811 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1812 {
1813 Py_DECREF(item);
1814 Py_DECREF(list);
1815 return NULL;
1816 }
1817 }
1818
1819 return list;
1820}
1821
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001822typedef struct
1823{
1824 listwatch_T lw;
1825 list_T *list;
1826} listiterinfo_T;
1827
1828 static void
1829ListIterDestruct(listiterinfo_T *lii)
1830{
1831 list_rem_watch(lii->list, &lii->lw);
1832 PyMem_Free(lii);
1833}
1834
1835 static PyObject *
1836ListIterNext(listiterinfo_T **lii)
1837{
1838 PyObject *r;
1839
1840 if (!((*lii)->lw.lw_item))
1841 return NULL;
1842
1843 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1844 return NULL;
1845
1846 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1847
1848 return r;
1849}
1850
1851 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001852ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001853{
1854 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001855 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001856
1857 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1858 {
1859 PyErr_NoMemory();
1860 return NULL;
1861 }
1862
1863 list_add_watch(l, &lii->lw);
1864 lii->lw.lw_item = l->lv_first;
1865 lii->list = l;
1866
1867 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001868 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1869 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001870}
1871
Bram Moolenaardb913952012-06-29 12:54:53 +02001872 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001873ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001874{
1875 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001876 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001877 listitem_T *li;
1878 Py_ssize_t length = ListLength(self);
1879
1880 if (l->lv_lock)
1881 {
1882 PyErr_SetVim(_("list is locked"));
1883 return -1;
1884 }
1885 if (index>length || (index==length && obj==NULL))
1886 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001887 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001888 return -1;
1889 }
1890
1891 if (obj == NULL)
1892 {
1893 li = list_find(l, (long) index);
1894 list_remove(l, li, li);
1895 clear_tv(&li->li_tv);
1896 vim_free(li);
1897 return 0;
1898 }
1899
1900 if (ConvertFromPyObject(obj, &tv) == -1)
1901 return -1;
1902
1903 if (index == length)
1904 {
1905 if (list_append_tv(l, &tv) == FAIL)
1906 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001907 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001908 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001909 return -1;
1910 }
1911 }
1912 else
1913 {
1914 li = list_find(l, (long) index);
1915 clear_tv(&li->li_tv);
1916 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001917 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001918 }
1919 return 0;
1920}
1921
1922 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001923ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001924{
1925 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001926 PyObject *iterator;
1927 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02001928 listitem_T *li;
1929 listitem_T *next;
1930 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001931 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001932 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02001933
1934 if (l->lv_lock)
1935 {
1936 PyErr_SetVim(_("list is locked"));
1937 return -1;
1938 }
1939
1940 PROC_RANGE
1941
1942 if (first == size)
1943 li = NULL;
1944 else
1945 {
1946 li = list_find(l, (long) first);
1947 if (li == NULL)
1948 {
1949 PyErr_SetVim(_("internal error: no vim list item"));
1950 return -1;
1951 }
1952 if (last > first)
1953 {
1954 i = last - first;
1955 while (i-- && li != NULL)
1956 {
1957 next = li->li_next;
1958 listitem_remove(l, li);
1959 li = next;
1960 }
1961 }
1962 }
1963
1964 if (obj == NULL)
1965 return 0;
1966
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001967 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001968 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001969
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001970 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001971 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001972 if (ConvertFromPyObject(item, &v) == -1)
1973 {
1974 Py_DECREF(iterator);
1975 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001976 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001977 }
1978 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001979 if (list_insert_tv(l, &v, li) == FAIL)
1980 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001981 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001982 PyErr_SetVim(_("internal error: failed to add item to list"));
1983 return -1;
1984 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001985 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001986 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001987 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02001988 return 0;
1989}
1990
1991 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001992ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001993{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001994 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001995 PyObject *lookup_dict;
1996
1997 if (l->lv_lock)
1998 {
1999 PyErr_SetVim(_("list is locked"));
2000 return NULL;
2001 }
2002
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002003 if (!(lookup_dict = PyDict_New()))
2004 return NULL;
2005
Bram Moolenaardb913952012-06-29 12:54:53 +02002006 if (list_py_concat(l, obj, lookup_dict) == -1)
2007 {
2008 Py_DECREF(lookup_dict);
2009 return NULL;
2010 }
2011 Py_DECREF(lookup_dict);
2012
2013 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002014 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002015}
2016
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002017static char *ListAttrs[] = {
2018 "locked",
2019 NULL
2020};
2021
2022 static PyObject *
2023ListDir(PyObject *self)
2024{
2025 return ObjectDir(self, ListAttrs);
2026}
2027
Bram Moolenaar66b79852012-09-21 14:00:35 +02002028 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002029ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002030{
2031 if (val == NULL)
2032 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002033 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002034 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002035 return -1;
2036 }
2037
2038 if (strcmp(name, "locked") == 0)
2039 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002040 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002041 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002042 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002043 return -1;
2044 }
2045 else
2046 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002047 int istrue = PyObject_IsTrue(val);
2048 if (istrue == -1)
2049 return -1;
2050 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002051 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002052 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002053 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002054 }
2055 return 0;
2056 }
2057 else
2058 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002059 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002060 return -1;
2061 }
2062}
2063
Bram Moolenaardb913952012-06-29 12:54:53 +02002064static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002065 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2066 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2067 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002068};
2069
2070typedef struct
2071{
2072 PyObject_HEAD
2073 char_u *name;
2074} FunctionObject;
2075
2076static PyTypeObject FunctionType;
2077
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002078#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2079
Bram Moolenaardb913952012-06-29 12:54:53 +02002080 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002081FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002082{
2083 FunctionObject *self;
2084
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002085 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2086
Bram Moolenaardb913952012-06-29 12:54:53 +02002087 if (self == NULL)
2088 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002089
2090 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002091 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002092 if (!translated_function_exists(name))
2093 {
2094 PyErr_SetString(PyExc_ValueError,
2095 _("unnamed function does not exist"));
2096 return NULL;
2097 }
2098 self->name = vim_strsave(name);
2099 func_ref(self->name);
2100 }
2101 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002102 if ((self->name = get_expanded_name(name,
2103 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2104 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002105 {
Bram Moolenaar018acca2013-05-30 13:37:28 +02002106 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2107 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002108 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002109
2110 return (PyObject *)(self);
2111}
2112
2113 static PyObject *
2114FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2115{
2116 PyObject *self;
2117 char_u *name;
2118
2119 if (kwargs)
2120 {
2121 PyErr_SetString(PyExc_TypeError,
2122 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002123 return NULL;
2124 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002125
2126 if (!PyArg_ParseTuple(args, "s", &name))
2127 return NULL;
2128
2129 self = FunctionNew(subtype, name);
2130
2131 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002132}
2133
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002134 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002135FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002136{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002137 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002138 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002139
2140 DESTRUCTOR_FINISH(self);
2141}
2142
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002143static char *FunctionAttrs[] = {
2144 "softspace",
2145 NULL
2146};
2147
2148 static PyObject *
2149FunctionDir(PyObject *self)
2150{
2151 return ObjectDir(self, FunctionAttrs);
2152}
2153
Bram Moolenaardb913952012-06-29 12:54:53 +02002154 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002155FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002156{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002157 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002158 typval_T args;
2159 typval_T selfdicttv;
2160 typval_T rettv;
2161 dict_T *selfdict = NULL;
2162 PyObject *selfdictObject;
2163 PyObject *result;
2164 int error;
2165
2166 if (ConvertFromPyObject(argsObject, &args) == -1)
2167 return NULL;
2168
2169 if (kwargs != NULL)
2170 {
2171 selfdictObject = PyDict_GetItemString(kwargs, "self");
2172 if (selfdictObject != NULL)
2173 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002174 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002175 {
2176 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002177 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002178 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002179 selfdict = selfdicttv.vval.v_dict;
2180 }
2181 }
2182
Bram Moolenaar71700b82013-05-15 17:49:05 +02002183 Py_BEGIN_ALLOW_THREADS
2184 Python_Lock_Vim();
2185
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002186 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002187 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002188
2189 Python_Release_Vim();
2190 Py_END_ALLOW_THREADS
2191
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002192 if (VimTryEnd())
2193 result = NULL;
2194 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002195 {
2196 result = NULL;
2197 PyErr_SetVim(_("failed to run function"));
2198 }
2199 else
2200 result = ConvertToPyObject(&rettv);
2201
Bram Moolenaardb913952012-06-29 12:54:53 +02002202 clear_tv(&args);
2203 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002204 if (selfdict != NULL)
2205 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002206
2207 return result;
2208}
2209
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002210 static PyObject *
2211FunctionRepr(FunctionObject *self)
2212{
2213 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2214}
2215
Bram Moolenaardb913952012-06-29 12:54:53 +02002216static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002217 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2218 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002219};
2220
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002221/*
2222 * Options object
2223 */
2224
2225static PyTypeObject OptionsType;
2226
2227typedef int (*checkfun)(void *);
2228
2229typedef struct
2230{
2231 PyObject_HEAD
2232 int opt_type;
2233 void *from;
2234 checkfun Check;
2235 PyObject *fromObj;
2236} OptionsObject;
2237
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002238 static int
2239dummy_check(void *arg UNUSED)
2240{
2241 return 0;
2242}
2243
2244 static PyObject *
2245OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2246{
2247 OptionsObject *self;
2248
Bram Moolenaar774267b2013-05-21 20:51:59 +02002249 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002250 if (self == NULL)
2251 return NULL;
2252
2253 self->opt_type = opt_type;
2254 self->from = from;
2255 self->Check = Check;
2256 self->fromObj = fromObj;
2257 if (fromObj)
2258 Py_INCREF(fromObj);
2259
2260 return (PyObject *)(self);
2261}
2262
2263 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002264OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002265{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002266 PyObject_GC_UnTrack((void *)(self));
2267 Py_XDECREF(self->fromObj);
2268 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002269}
2270
2271 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002272OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002273{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002274 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002275 return 0;
2276}
2277
2278 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002279OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002280{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002281 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002282 return 0;
2283}
2284
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002285 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002286OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002287{
2288 char_u *key;
2289 int flags;
2290 long numval;
2291 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002292 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002293
Bram Moolenaard6e39182013-05-21 18:30:34 +02002294 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002295 return NULL;
2296
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002297 if (!(key = StringToChars(keyObject, &todecref)))
2298 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002299
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002300 if (*key == NUL)
2301 {
2302 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002303 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002304 return NULL;
2305 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002306
2307 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002308 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002309
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002310 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002311
2312 if (flags == 0)
2313 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002314 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002315 return NULL;
2316 }
2317
2318 if (flags & SOPT_UNSET)
2319 {
2320 Py_INCREF(Py_None);
2321 return Py_None;
2322 }
2323 else if (flags & SOPT_BOOL)
2324 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002325 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002326 r = numval ? Py_True : Py_False;
2327 Py_INCREF(r);
2328 return r;
2329 }
2330 else if (flags & SOPT_NUM)
2331 return PyInt_FromLong(numval);
2332 else if (flags & SOPT_STRING)
2333 {
2334 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002335 {
2336 PyObject *r = PyBytes_FromString((char *) stringval);
2337 vim_free(stringval);
2338 return r;
2339 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002340 else
2341 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002342 PyErr_SetString(PyExc_RuntimeError,
2343 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002344 return NULL;
2345 }
2346 }
2347 else
2348 {
2349 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2350 return NULL;
2351 }
2352}
2353
2354 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002355set_option_value_err(key, numval, stringval, opt_flags)
2356 char_u *key;
2357 int numval;
2358 char_u *stringval;
2359 int opt_flags;
2360{
2361 char_u *errmsg;
2362
2363 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2364 {
2365 if (VimTryEnd())
2366 return FAIL;
2367 PyErr_SetVim((char *)errmsg);
2368 return FAIL;
2369 }
2370 return OK;
2371}
2372
2373 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002374set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2375 char_u *key;
2376 int numval;
2377 char_u *stringval;
2378 int opt_flags;
2379 int opt_type;
2380 void *from;
2381{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002382 win_T *save_curwin = NULL;
2383 tabpage_T *save_curtab = NULL;
2384 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002385 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002386
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002387 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002388 switch (opt_type)
2389 {
2390 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002391 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2392 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002393 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002394 if (VimTryEnd())
2395 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002396 PyErr_SetVim("Problem while switching windows.");
2397 return -1;
2398 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002399 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002400 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002401 if (r == FAIL)
2402 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002403 break;
2404 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002405 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002406 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002407 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002408 if (r == FAIL)
2409 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002410 break;
2411 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002412 r = set_option_value_err(key, numval, stringval, opt_flags);
2413 if (r == FAIL)
2414 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002415 break;
2416 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002417 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002418}
2419
2420 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002421OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002422{
2423 char_u *key;
2424 int flags;
2425 int opt_flags;
2426 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002427 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002428
Bram Moolenaard6e39182013-05-21 18:30:34 +02002429 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002430 return -1;
2431
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002432 if (!(key = StringToChars(keyObject, &todecref)))
2433 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002434
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002435 if (*key == NUL)
2436 {
2437 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002438 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002439 return -1;
2440 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002441
2442 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002443 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002444
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002445 if (flags == 0)
2446 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002447 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002448 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002449 return -1;
2450 }
2451
2452 if (valObject == NULL)
2453 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002454 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002455 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002456 PyErr_SetString(PyExc_ValueError,
2457 _("unable to unset global option"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002458 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002459 return -1;
2460 }
2461 else if (!(flags & SOPT_GLOBAL))
2462 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002463 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2464 "without global value"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002465 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002466 return -1;
2467 }
2468 else
2469 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002470 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002471 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002472 return 0;
2473 }
2474 }
2475
Bram Moolenaard6e39182013-05-21 18:30:34 +02002476 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002477
2478 if (flags & SOPT_BOOL)
2479 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002480 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002481
Bram Moolenaarb983f752013-05-15 16:11:50 +02002482 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002483 r = -1;
2484 else
2485 r = set_option_value_for(key, istrue, NULL,
2486 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002487 }
2488 else if (flags & SOPT_NUM)
2489 {
2490 int val;
2491
2492#if PY_MAJOR_VERSION < 3
2493 if (PyInt_Check(valObject))
2494 val = PyInt_AsLong(valObject);
2495 else
2496#endif
2497 if (PyLong_Check(valObject))
2498 val = PyLong_AsLong(valObject);
2499 else
2500 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002501 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002502 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002503 return -1;
2504 }
2505
2506 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002507 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002508 }
2509 else
2510 {
2511 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002512 PyObject *todecref;
2513
2514 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002515 r = set_option_value_for(key, 0, val, opt_flags,
2516 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002517 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002518 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002519 }
2520
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002521 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002522
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002523 return r;
2524}
2525
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002526static PyMappingMethods OptionsAsMapping = {
2527 (lenfunc) NULL,
2528 (binaryfunc) OptionsItem,
2529 (objobjargproc) OptionsAssItem,
2530};
2531
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002532/* Tabpage object
2533 */
2534
2535typedef struct
2536{
2537 PyObject_HEAD
2538 tabpage_T *tab;
2539} TabPageObject;
2540
2541static PyObject *WinListNew(TabPageObject *tabObject);
2542
2543static PyTypeObject TabPageType;
2544
2545 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002546CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002547{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002548 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002549 {
2550 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2551 return -1;
2552 }
2553
2554 return 0;
2555}
2556
2557 static PyObject *
2558TabPageNew(tabpage_T *tab)
2559{
2560 TabPageObject *self;
2561
2562 if (TAB_PYTHON_REF(tab))
2563 {
2564 self = TAB_PYTHON_REF(tab);
2565 Py_INCREF(self);
2566 }
2567 else
2568 {
2569 self = PyObject_NEW(TabPageObject, &TabPageType);
2570 if (self == NULL)
2571 return NULL;
2572 self->tab = tab;
2573 TAB_PYTHON_REF(tab) = self;
2574 }
2575
2576 return (PyObject *)(self);
2577}
2578
2579 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002580TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002581{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002582 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2583 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002584
2585 DESTRUCTOR_FINISH(self);
2586}
2587
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002588static char *TabPageAttrs[] = {
2589 "windows", "number", "vars", "window", "valid",
2590 NULL
2591};
2592
2593 static PyObject *
2594TabPageDir(PyObject *self)
2595{
2596 return ObjectDir(self, TabPageAttrs);
2597}
2598
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002599 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002600TabPageAttrValid(TabPageObject *self, char *name)
2601{
2602 PyObject *r;
2603
2604 if (strcmp(name, "valid") != 0)
2605 return NULL;
2606
2607 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2608 Py_INCREF(r);
2609 return r;
2610}
2611
2612 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002613TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002614{
2615 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002616 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002617 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002618 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002619 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002620 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002621 else if (strcmp(name, "window") == 0)
2622 {
2623 /* For current tab window.c does not bother to set or update tp_curwin
2624 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002625 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002626 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002627 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002628 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002629 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002630 else if (strcmp(name, "__members__") == 0)
2631 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002632 return NULL;
2633}
2634
2635 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002636TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002637{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002638 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002639 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002640 else
2641 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002642 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002643
2644 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002645 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2646 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002647 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002648 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002649 }
2650}
2651
2652static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002653 /* name, function, calling, documentation */
2654 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2655 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002656};
2657
2658/*
2659 * Window list object
2660 */
2661
2662static PyTypeObject TabListType;
2663static PySequenceMethods TabListAsSeq;
2664
2665typedef struct
2666{
2667 PyObject_HEAD
2668} TabListObject;
2669
2670 static PyInt
2671TabListLength(PyObject *self UNUSED)
2672{
2673 tabpage_T *tp = first_tabpage;
2674 PyInt n = 0;
2675
2676 while (tp != NULL)
2677 {
2678 ++n;
2679 tp = tp->tp_next;
2680 }
2681
2682 return n;
2683}
2684
2685 static PyObject *
2686TabListItem(PyObject *self UNUSED, PyInt n)
2687{
2688 tabpage_T *tp;
2689
2690 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2691 if (n == 0)
2692 return TabPageNew(tp);
2693
2694 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2695 return NULL;
2696}
2697
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002698/* Window object
2699 */
2700
2701typedef struct
2702{
2703 PyObject_HEAD
2704 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002705 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002706} WindowObject;
2707
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002708static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002709
2710 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002711CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002712{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002713 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002714 {
2715 PyErr_SetVim(_("attempt to refer to deleted window"));
2716 return -1;
2717 }
2718
2719 return 0;
2720}
2721
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002722 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002723WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002724{
2725 /* We need to handle deletion of windows underneath us.
2726 * If we add a "w_python*_ref" field to the win_T structure,
2727 * then we can get at it in win_free() in vim. We then
2728 * need to create only ONE Python object per window - if
2729 * we try to create a second, just INCREF the existing one
2730 * and return it. The (single) Python object referring to
2731 * the window is stored in "w_python*_ref".
2732 * On a win_free() we set the Python object's win_T* field
2733 * to an invalid value. We trap all uses of a window
2734 * object, and reject them if the win_T* field is invalid.
2735 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002736 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002737 * w_python_ref and w_python3_ref fields respectively.
2738 */
2739
2740 WindowObject *self;
2741
2742 if (WIN_PYTHON_REF(win))
2743 {
2744 self = WIN_PYTHON_REF(win);
2745 Py_INCREF(self);
2746 }
2747 else
2748 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002749 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002750 if (self == NULL)
2751 return NULL;
2752 self->win = win;
2753 WIN_PYTHON_REF(win) = self;
2754 }
2755
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002756 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2757
Bram Moolenaar971db462013-05-12 18:44:48 +02002758 return (PyObject *)(self);
2759}
2760
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002761 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002762WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002763{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002764 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002765 if (self->win && self->win != INVALID_WINDOW_VALUE)
2766 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002767 Py_XDECREF(((PyObject *)(self->tabObject)));
2768 PyObject_GC_Del((void *)(self));
2769}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002770
Bram Moolenaar774267b2013-05-21 20:51:59 +02002771 static int
2772WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2773{
2774 Py_VISIT(((PyObject *)(self->tabObject)));
2775 return 0;
2776}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002777
Bram Moolenaar774267b2013-05-21 20:51:59 +02002778 static int
2779WindowClear(WindowObject *self)
2780{
2781 Py_CLEAR(self->tabObject);
2782 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002783}
2784
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002785 static win_T *
2786get_firstwin(TabPageObject *tabObject)
2787{
2788 if (tabObject)
2789 {
2790 if (CheckTabPage(tabObject))
2791 return NULL;
2792 /* For current tab window.c does not bother to set or update tp_firstwin
2793 */
2794 else if (tabObject->tab == curtab)
2795 return firstwin;
2796 else
2797 return tabObject->tab->tp_firstwin;
2798 }
2799 else
2800 return firstwin;
2801}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002802static char *WindowAttrs[] = {
2803 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2804 "tabpage", "valid",
2805 NULL
2806};
2807
2808 static PyObject *
2809WindowDir(PyObject *self)
2810{
2811 return ObjectDir(self, WindowAttrs);
2812}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002813
Bram Moolenaar971db462013-05-12 18:44:48 +02002814 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002815WindowAttrValid(WindowObject *self, char *name)
2816{
2817 PyObject *r;
2818
2819 if (strcmp(name, "valid") != 0)
2820 return NULL;
2821
2822 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2823 Py_INCREF(r);
2824 return r;
2825}
2826
2827 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002828WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002829{
2830 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002831 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002832 else if (strcmp(name, "cursor") == 0)
2833 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002834 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002835
2836 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2837 }
2838 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002839 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002840#ifdef FEAT_WINDOWS
2841 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002842 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002843#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002844#ifdef FEAT_VERTSPLIT
2845 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002846 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002847 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002848 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002849#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002850 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002851 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002852 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002853 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2854 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002855 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002856 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002857 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002858 return NULL;
2859 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002860 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002861 }
2862 else if (strcmp(name, "tabpage") == 0)
2863 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002864 Py_INCREF(self->tabObject);
2865 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002866 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002867 else if (strcmp(name, "__members__") == 0)
2868 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002869 else
2870 return NULL;
2871}
2872
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002873 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002874WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002875{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002876 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002877 return -1;
2878
2879 if (strcmp(name, "buffer") == 0)
2880 {
2881 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2882 return -1;
2883 }
2884 else if (strcmp(name, "cursor") == 0)
2885 {
2886 long lnum;
2887 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002888
2889 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2890 return -1;
2891
Bram Moolenaard6e39182013-05-21 18:30:34 +02002892 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002893 {
2894 PyErr_SetVim(_("cursor position outside buffer"));
2895 return -1;
2896 }
2897
2898 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002899 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002900 return -1;
2901
Bram Moolenaard6e39182013-05-21 18:30:34 +02002902 self->win->w_cursor.lnum = lnum;
2903 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002904#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002905 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002906#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002907 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002908 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002909
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002910 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002911 return 0;
2912 }
2913 else if (strcmp(name, "height") == 0)
2914 {
2915 int height;
2916 win_T *savewin;
2917
2918 if (!PyArg_Parse(val, "i", &height))
2919 return -1;
2920
2921#ifdef FEAT_GUI
2922 need_mouse_correct = TRUE;
2923#endif
2924 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002925 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002926
2927 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002928 win_setheight(height);
2929 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002930 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002931 return -1;
2932
2933 return 0;
2934 }
2935#ifdef FEAT_VERTSPLIT
2936 else if (strcmp(name, "width") == 0)
2937 {
2938 int width;
2939 win_T *savewin;
2940
2941 if (!PyArg_Parse(val, "i", &width))
2942 return -1;
2943
2944#ifdef FEAT_GUI
2945 need_mouse_correct = TRUE;
2946#endif
2947 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002948 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002949
2950 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002951 win_setwidth(width);
2952 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002953 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002954 return -1;
2955
2956 return 0;
2957 }
2958#endif
2959 else
2960 {
2961 PyErr_SetString(PyExc_AttributeError, name);
2962 return -1;
2963 }
2964}
2965
2966 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002967WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002968{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002969 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002970 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002971 else
2972 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002973 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002974
Bram Moolenaar6d216452013-05-12 19:00:41 +02002975 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002976 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002977 (self));
2978 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002979 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002980 }
2981}
2982
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002983static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002984 /* name, function, calling, documentation */
2985 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2986 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002987};
2988
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002989/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002990 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002991 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002992
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002993static PyTypeObject WinListType;
2994static PySequenceMethods WinListAsSeq;
2995
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002996typedef struct
2997{
2998 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002999 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003000} WinListObject;
3001
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003002 static PyObject *
3003WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003004{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003005 WinListObject *self;
3006
3007 self = PyObject_NEW(WinListObject, &WinListType);
3008 self->tabObject = tabObject;
3009 Py_INCREF(tabObject);
3010
3011 return (PyObject *)(self);
3012}
3013
3014 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003015WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003016{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003017 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003018
3019 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003020 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003021 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003022 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003023
3024 DESTRUCTOR_FINISH(self);
3025}
3026
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003027 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003028WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003029{
3030 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003031 PyInt n = 0;
3032
Bram Moolenaard6e39182013-05-21 18:30:34 +02003033 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003034 return -1;
3035
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003036 while (w != NULL)
3037 {
3038 ++n;
3039 w = W_NEXT(w);
3040 }
3041
3042 return n;
3043}
3044
3045 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003046WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003047{
3048 win_T *w;
3049
Bram Moolenaard6e39182013-05-21 18:30:34 +02003050 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003051 return NULL;
3052
3053 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003054 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003055 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003056
3057 PyErr_SetString(PyExc_IndexError, _("no such window"));
3058 return NULL;
3059}
3060
3061/* Convert a Python string into a Vim line.
3062 *
3063 * The result is in allocated memory. All internal nulls are replaced by
3064 * newline characters. It is an error for the string to contain newline
3065 * characters.
3066 *
3067 * On errors, the Python exception data is set, and NULL is returned.
3068 */
3069 static char *
3070StringToLine(PyObject *obj)
3071{
3072 const char *str;
3073 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003074 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003075 PyInt len;
3076 PyInt i;
3077 char *p;
3078
3079 if (obj == NULL || !PyString_Check(obj))
3080 {
3081 PyErr_BadArgument();
3082 return NULL;
3083 }
3084
Bram Moolenaar19e60942011-06-19 00:27:51 +02003085 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
3086 str = PyString_AsString(bytes);
3087 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003088
3089 /*
3090 * Error checking: String must not contain newlines, as we
3091 * are replacing a single line, and we must replace it with
3092 * a single line.
3093 * A trailing newline is removed, so that append(f.readlines()) works.
3094 */
3095 p = memchr(str, '\n', len);
3096 if (p != NULL)
3097 {
3098 if (p == str + len - 1)
3099 --len;
3100 else
3101 {
3102 PyErr_SetVim(_("string cannot contain newlines"));
3103 return NULL;
3104 }
3105 }
3106
3107 /* Create a copy of the string, with internal nulls replaced by
3108 * newline characters, as is the vim convention.
3109 */
3110 save = (char *)alloc((unsigned)(len+1));
3111 if (save == NULL)
3112 {
3113 PyErr_NoMemory();
3114 return NULL;
3115 }
3116
3117 for (i = 0; i < len; ++i)
3118 {
3119 if (str[i] == '\0')
3120 save[i] = '\n';
3121 else
3122 save[i] = str[i];
3123 }
3124
3125 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003126 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003127
3128 return save;
3129}
3130
3131/* Get a line from the specified buffer. The line number is
3132 * in Vim format (1-based). The line is returned as a Python
3133 * string object.
3134 */
3135 static PyObject *
3136GetBufferLine(buf_T *buf, PyInt n)
3137{
3138 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3139}
3140
3141
3142/* Get a list of lines from the specified buffer. The line numbers
3143 * are in Vim format (1-based). The range is from lo up to, but not
3144 * including, hi. The list is returned as a Python list of string objects.
3145 */
3146 static PyObject *
3147GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3148{
3149 PyInt i;
3150 PyInt n = hi - lo;
3151 PyObject *list = PyList_New(n);
3152
3153 if (list == NULL)
3154 return NULL;
3155
3156 for (i = 0; i < n; ++i)
3157 {
3158 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3159
3160 /* Error check - was the Python string creation OK? */
3161 if (str == NULL)
3162 {
3163 Py_DECREF(list);
3164 return NULL;
3165 }
3166
3167 /* Set the list item */
3168 if (PyList_SetItem(list, i, str))
3169 {
3170 Py_DECREF(str);
3171 Py_DECREF(list);
3172 return NULL;
3173 }
3174 }
3175
3176 /* The ownership of the Python list is passed to the caller (ie,
3177 * the caller should Py_DECREF() the object when it is finished
3178 * with it).
3179 */
3180
3181 return list;
3182}
3183
3184/*
3185 * Check if deleting lines made the cursor position invalid.
3186 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3187 * deleted).
3188 */
3189 static void
3190py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3191{
3192 if (curwin->w_cursor.lnum >= lo)
3193 {
3194 /* Adjust the cursor position if it's in/after the changed
3195 * lines. */
3196 if (curwin->w_cursor.lnum >= hi)
3197 {
3198 curwin->w_cursor.lnum += extra;
3199 check_cursor_col();
3200 }
3201 else if (extra < 0)
3202 {
3203 curwin->w_cursor.lnum = lo;
3204 check_cursor();
3205 }
3206 else
3207 check_cursor_col();
3208 changed_cline_bef_curs();
3209 }
3210 invalidate_botline();
3211}
3212
Bram Moolenaar19e60942011-06-19 00:27:51 +02003213/*
3214 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003215 * in Vim format (1-based). The replacement line is given as
3216 * a Python string object. The object is checked for validity
3217 * and correct format. Errors are returned as a value of FAIL.
3218 * The return value is OK on success.
3219 * If OK is returned and len_change is not NULL, *len_change
3220 * is set to the change in the buffer length.
3221 */
3222 static int
3223SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3224{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003225 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003226 * There are three cases:
3227 * 1. NULL, or None - this is a deletion.
3228 * 2. A string - this is a replacement.
3229 * 3. Anything else - this is an error.
3230 */
3231 if (line == Py_None || line == NULL)
3232 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003233 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003234
3235 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003236 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003237
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003238 VimTryStart();
3239
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003240 if (u_savedel((linenr_T)n, 1L) == FAIL)
3241 PyErr_SetVim(_("cannot save undo information"));
3242 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3243 PyErr_SetVim(_("cannot delete line"));
3244 else
3245 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003246 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003247 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3248 deleted_lines_mark((linenr_T)n, 1L);
3249 }
3250
Bram Moolenaar105bc352013-05-17 16:03:57 +02003251 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003252
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003253 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003254 return FAIL;
3255
3256 if (len_change)
3257 *len_change = -1;
3258
3259 return OK;
3260 }
3261 else if (PyString_Check(line))
3262 {
3263 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003264 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003265
3266 if (save == NULL)
3267 return FAIL;
3268
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003269 VimTryStart();
3270
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003271 /* We do not need to free "save" if ml_replace() consumes it. */
3272 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003273 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003274
3275 if (u_savesub((linenr_T)n) == FAIL)
3276 {
3277 PyErr_SetVim(_("cannot save undo information"));
3278 vim_free(save);
3279 }
3280 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3281 {
3282 PyErr_SetVim(_("cannot replace line"));
3283 vim_free(save);
3284 }
3285 else
3286 changed_bytes((linenr_T)n, 0);
3287
Bram Moolenaar105bc352013-05-17 16:03:57 +02003288 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003289
3290 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003291 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003292 check_cursor_col();
3293
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003294 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003295 return FAIL;
3296
3297 if (len_change)
3298 *len_change = 0;
3299
3300 return OK;
3301 }
3302 else
3303 {
3304 PyErr_BadArgument();
3305 return FAIL;
3306 }
3307}
3308
Bram Moolenaar19e60942011-06-19 00:27:51 +02003309/* Replace a range of lines in the specified buffer. The line numbers are in
3310 * Vim format (1-based). The range is from lo up to, but not including, hi.
3311 * The replacement lines are given as a Python list of string objects. The
3312 * list is checked for validity and correct format. Errors are returned as a
3313 * value of FAIL. The return value is OK on success.
3314 * If OK is returned and len_change is not NULL, *len_change
3315 * is set to the change in the buffer length.
3316 */
3317 static int
3318SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3319{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003320 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003321 * There are three cases:
3322 * 1. NULL, or None - this is a deletion.
3323 * 2. A list - this is a replacement.
3324 * 3. Anything else - this is an error.
3325 */
3326 if (list == Py_None || list == NULL)
3327 {
3328 PyInt i;
3329 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003330 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003331
3332 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003333 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003334 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003335
3336 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3337 PyErr_SetVim(_("cannot save undo information"));
3338 else
3339 {
3340 for (i = 0; i < n; ++i)
3341 {
3342 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3343 {
3344 PyErr_SetVim(_("cannot delete line"));
3345 break;
3346 }
3347 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003348 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003349 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3350 deleted_lines_mark((linenr_T)lo, (long)i);
3351 }
3352
Bram Moolenaar105bc352013-05-17 16:03:57 +02003353 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003354
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003355 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003356 return FAIL;
3357
3358 if (len_change)
3359 *len_change = -n;
3360
3361 return OK;
3362 }
3363 else if (PyList_Check(list))
3364 {
3365 PyInt i;
3366 PyInt new_len = PyList_Size(list);
3367 PyInt old_len = hi - lo;
3368 PyInt extra = 0; /* lines added to text, can be negative */
3369 char **array;
3370 buf_T *savebuf;
3371
3372 if (new_len == 0) /* avoid allocating zero bytes */
3373 array = NULL;
3374 else
3375 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003376 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003377 if (array == NULL)
3378 {
3379 PyErr_NoMemory();
3380 return FAIL;
3381 }
3382 }
3383
3384 for (i = 0; i < new_len; ++i)
3385 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003386 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003387
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003388 if (!(line = PyList_GetItem(list, i)) ||
3389 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003390 {
3391 while (i)
3392 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003393 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003394 return FAIL;
3395 }
3396 }
3397
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003398 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003399 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003400
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003401 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003402 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003403
3404 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3405 PyErr_SetVim(_("cannot save undo information"));
3406
3407 /* If the size of the range is reducing (ie, new_len < old_len) we
3408 * need to delete some old_len. We do this at the start, by
3409 * repeatedly deleting line "lo".
3410 */
3411 if (!PyErr_Occurred())
3412 {
3413 for (i = 0; i < old_len - new_len; ++i)
3414 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3415 {
3416 PyErr_SetVim(_("cannot delete line"));
3417 break;
3418 }
3419 extra -= i;
3420 }
3421
3422 /* For as long as possible, replace the existing old_len with the
3423 * new old_len. This is a more efficient operation, as it requires
3424 * less memory allocation and freeing.
3425 */
3426 if (!PyErr_Occurred())
3427 {
3428 for (i = 0; i < old_len && i < new_len; ++i)
3429 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3430 == FAIL)
3431 {
3432 PyErr_SetVim(_("cannot replace line"));
3433 break;
3434 }
3435 }
3436 else
3437 i = 0;
3438
3439 /* Now we may need to insert the remaining new old_len. If we do, we
3440 * must free the strings as we finish with them (we can't pass the
3441 * responsibility to vim in this case).
3442 */
3443 if (!PyErr_Occurred())
3444 {
3445 while (i < new_len)
3446 {
3447 if (ml_append((linenr_T)(lo + i - 1),
3448 (char_u *)array[i], 0, FALSE) == FAIL)
3449 {
3450 PyErr_SetVim(_("cannot insert line"));
3451 break;
3452 }
3453 vim_free(array[i]);
3454 ++i;
3455 ++extra;
3456 }
3457 }
3458
3459 /* Free any left-over old_len, as a result of an error */
3460 while (i < new_len)
3461 {
3462 vim_free(array[i]);
3463 ++i;
3464 }
3465
3466 /* Free the array of old_len. All of its contents have now
3467 * been dealt with (either freed, or the responsibility passed
3468 * to vim.
3469 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003470 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003471
3472 /* Adjust marks. Invalidate any which lie in the
3473 * changed range, and move any in the remainder of the buffer.
3474 */
3475 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3476 (long)MAXLNUM, (long)extra);
3477 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3478
Bram Moolenaar105bc352013-05-17 16:03:57 +02003479 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003480 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3481
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003482 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003483 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003484
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003485 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003486 return FAIL;
3487
3488 if (len_change)
3489 *len_change = new_len - old_len;
3490
3491 return OK;
3492 }
3493 else
3494 {
3495 PyErr_BadArgument();
3496 return FAIL;
3497 }
3498}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003499
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003500/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003501 * The line number is in Vim format (1-based). The lines to be inserted are
3502 * given as a Python list of string objects or as a single string. The lines
3503 * to be added are checked for validity and correct format. Errors are
3504 * returned as a value of FAIL. The return value is OK on success.
3505 * If OK is returned and len_change is not NULL, *len_change
3506 * is set to the change in the buffer length.
3507 */
3508 static int
3509InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3510{
3511 /* First of all, we check the type of the supplied Python object.
3512 * It must be a string or a list, or the call is in error.
3513 */
3514 if (PyString_Check(lines))
3515 {
3516 char *str = StringToLine(lines);
3517 buf_T *savebuf;
3518
3519 if (str == NULL)
3520 return FAIL;
3521
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003522 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003523 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003524 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003525
3526 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3527 PyErr_SetVim(_("cannot save undo information"));
3528 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3529 PyErr_SetVim(_("cannot insert line"));
3530 else
3531 appended_lines_mark((linenr_T)n, 1L);
3532
3533 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003534 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003535 update_screen(VALID);
3536
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003537 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003538 return FAIL;
3539
3540 if (len_change)
3541 *len_change = 1;
3542
3543 return OK;
3544 }
3545 else if (PyList_Check(lines))
3546 {
3547 PyInt i;
3548 PyInt size = PyList_Size(lines);
3549 char **array;
3550 buf_T *savebuf;
3551
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003552 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003553 if (array == NULL)
3554 {
3555 PyErr_NoMemory();
3556 return FAIL;
3557 }
3558
3559 for (i = 0; i < size; ++i)
3560 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003561 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003562
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003563 if (!(line = PyList_GetItem(lines, i)) ||
3564 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003565 {
3566 while (i)
3567 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003568 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003569 return FAIL;
3570 }
3571 }
3572
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003573 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003574 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003575 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003576
3577 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3578 PyErr_SetVim(_("cannot save undo information"));
3579 else
3580 {
3581 for (i = 0; i < size; ++i)
3582 {
3583 if (ml_append((linenr_T)(n + i),
3584 (char_u *)array[i], 0, FALSE) == FAIL)
3585 {
3586 PyErr_SetVim(_("cannot insert line"));
3587
3588 /* Free the rest of the lines */
3589 while (i < size)
3590 vim_free(array[i++]);
3591
3592 break;
3593 }
3594 vim_free(array[i]);
3595 }
3596 if (i > 0)
3597 appended_lines_mark((linenr_T)n, (long)i);
3598 }
3599
3600 /* Free the array of lines. All of its contents have now
3601 * been freed.
3602 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003603 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003604
Bram Moolenaar105bc352013-05-17 16:03:57 +02003605 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003606 update_screen(VALID);
3607
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003608 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003609 return FAIL;
3610
3611 if (len_change)
3612 *len_change = size;
3613
3614 return OK;
3615 }
3616 else
3617 {
3618 PyErr_BadArgument();
3619 return FAIL;
3620 }
3621}
3622
3623/*
3624 * Common routines for buffers and line ranges
3625 * -------------------------------------------
3626 */
3627
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003628typedef struct
3629{
3630 PyObject_HEAD
3631 buf_T *buf;
3632} BufferObject;
3633
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003634 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003635CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003636{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003637 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003638 {
3639 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3640 return -1;
3641 }
3642
3643 return 0;
3644}
3645
3646 static PyObject *
3647RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3648{
3649 if (CheckBuffer(self))
3650 return NULL;
3651
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003652 if (end == -1)
3653 end = self->buf->b_ml.ml_line_count;
3654
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003655 if (n < 0)
3656 n += end - start + 1;
3657
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003658 if (n < 0 || n > end - start)
3659 {
3660 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3661 return NULL;
3662 }
3663
3664 return GetBufferLine(self->buf, n+start);
3665}
3666
3667 static PyObject *
3668RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3669{
3670 PyInt size;
3671
3672 if (CheckBuffer(self))
3673 return NULL;
3674
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003675 if (end == -1)
3676 end = self->buf->b_ml.ml_line_count;
3677
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003678 size = end - start + 1;
3679
3680 if (lo < 0)
3681 lo = 0;
3682 else if (lo > size)
3683 lo = size;
3684 if (hi < 0)
3685 hi = 0;
3686 if (hi < lo)
3687 hi = lo;
3688 else if (hi > size)
3689 hi = size;
3690
3691 return GetBufferLineList(self->buf, lo+start, hi+start);
3692}
3693
3694 static PyInt
3695RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3696{
3697 PyInt len_change;
3698
3699 if (CheckBuffer(self))
3700 return -1;
3701
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003702 if (end == -1)
3703 end = self->buf->b_ml.ml_line_count;
3704
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003705 if (n < 0)
3706 n += end - start + 1;
3707
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003708 if (n < 0 || n > end - start)
3709 {
3710 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3711 return -1;
3712 }
3713
3714 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3715 return -1;
3716
3717 if (new_end)
3718 *new_end = end + len_change;
3719
3720 return 0;
3721}
3722
Bram Moolenaar19e60942011-06-19 00:27:51 +02003723 static PyInt
3724RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3725{
3726 PyInt size;
3727 PyInt len_change;
3728
3729 /* Self must be a valid buffer */
3730 if (CheckBuffer(self))
3731 return -1;
3732
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003733 if (end == -1)
3734 end = self->buf->b_ml.ml_line_count;
3735
Bram Moolenaar19e60942011-06-19 00:27:51 +02003736 /* Sort out the slice range */
3737 size = end - start + 1;
3738
3739 if (lo < 0)
3740 lo = 0;
3741 else if (lo > size)
3742 lo = size;
3743 if (hi < 0)
3744 hi = 0;
3745 if (hi < lo)
3746 hi = lo;
3747 else if (hi > size)
3748 hi = size;
3749
3750 if (SetBufferLineList(self->buf, lo + start, hi + start,
3751 val, &len_change) == FAIL)
3752 return -1;
3753
3754 if (new_end)
3755 *new_end = end + len_change;
3756
3757 return 0;
3758}
3759
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003760
3761 static PyObject *
3762RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3763{
3764 PyObject *lines;
3765 PyInt len_change;
3766 PyInt max;
3767 PyInt n;
3768
3769 if (CheckBuffer(self))
3770 return NULL;
3771
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003772 if (end == -1)
3773 end = self->buf->b_ml.ml_line_count;
3774
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003775 max = n = end - start + 1;
3776
3777 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3778 return NULL;
3779
3780 if (n < 0 || n > max)
3781 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003782 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003783 return NULL;
3784 }
3785
3786 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3787 return NULL;
3788
3789 if (new_end)
3790 *new_end = end + len_change;
3791
3792 Py_INCREF(Py_None);
3793 return Py_None;
3794}
3795
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003796/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003797 */
3798
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003799static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003800static PySequenceMethods RangeAsSeq;
3801static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003802
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003803typedef struct
3804{
3805 PyObject_HEAD
3806 BufferObject *buf;
3807 PyInt start;
3808 PyInt end;
3809} RangeObject;
3810
3811 static PyObject *
3812RangeNew(buf_T *buf, PyInt start, PyInt end)
3813{
3814 BufferObject *bufr;
3815 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003816 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003817 if (self == NULL)
3818 return NULL;
3819
3820 bufr = (BufferObject *)BufferNew(buf);
3821 if (bufr == NULL)
3822 {
3823 Py_DECREF(self);
3824 return NULL;
3825 }
3826 Py_INCREF(bufr);
3827
3828 self->buf = bufr;
3829 self->start = start;
3830 self->end = end;
3831
3832 return (PyObject *)(self);
3833}
3834
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003835 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003836RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003837{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003838 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003839 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003840 PyObject_GC_Del((void *)(self));
3841}
3842
3843 static int
3844RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3845{
3846 Py_VISIT(((PyObject *)(self->buf)));
3847 return 0;
3848}
3849
3850 static int
3851RangeClear(RangeObject *self)
3852{
3853 Py_CLEAR(self->buf);
3854 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003855}
3856
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003857 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003858RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003859{
3860 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003861 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003862 return -1; /* ??? */
3863
Bram Moolenaard6e39182013-05-21 18:30:34 +02003864 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003865}
3866
3867 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003868RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003869{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003870 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003871}
3872
3873 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003874RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003875{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003876 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003877}
3878
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003879static char *RangeAttrs[] = {
3880 "start", "end",
3881 NULL
3882};
3883
3884 static PyObject *
3885RangeDir(PyObject *self)
3886{
3887 return ObjectDir(self, RangeAttrs);
3888}
3889
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003890 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003891RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003892{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003893 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003894}
3895
3896 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003897RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003898{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003899 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003900 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
3901 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003902 else
3903 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003904 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003905
3906 if (name == NULL)
3907 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003908
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003909 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02003910 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003911 }
3912}
3913
3914static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003915 /* name, function, calling, documentation */
3916 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003917 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3918 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003919};
3920
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003921static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003922static PySequenceMethods BufferAsSeq;
3923static PyMappingMethods BufferAsMapping;
3924
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003925 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003926BufferNew(buf_T *buf)
3927{
3928 /* We need to handle deletion of buffers underneath us.
3929 * If we add a "b_python*_ref" field to the buf_T structure,
3930 * then we can get at it in buf_freeall() in vim. We then
3931 * need to create only ONE Python object per buffer - if
3932 * we try to create a second, just INCREF the existing one
3933 * and return it. The (single) Python object referring to
3934 * the buffer is stored in "b_python*_ref".
3935 * Question: what to do on a buf_freeall(). We'll probably
3936 * have to either delete the Python object (DECREF it to
3937 * zero - a bad idea, as it leaves dangling refs!) or
3938 * set the buf_T * value to an invalid value (-1?), which
3939 * means we need checks in all access functions... Bah.
3940 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003941 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003942 * b_python_ref and b_python3_ref fields respectively.
3943 */
3944
3945 BufferObject *self;
3946
3947 if (BUF_PYTHON_REF(buf) != NULL)
3948 {
3949 self = BUF_PYTHON_REF(buf);
3950 Py_INCREF(self);
3951 }
3952 else
3953 {
3954 self = PyObject_NEW(BufferObject, &BufferType);
3955 if (self == NULL)
3956 return NULL;
3957 self->buf = buf;
3958 BUF_PYTHON_REF(buf) = self;
3959 }
3960
3961 return (PyObject *)(self);
3962}
3963
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003964 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003965BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003966{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003967 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3968 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003969
3970 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003971}
3972
Bram Moolenaar971db462013-05-12 18:44:48 +02003973 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003974BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003975{
3976 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003977 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003978 return -1; /* ??? */
3979
Bram Moolenaard6e39182013-05-21 18:30:34 +02003980 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003981}
3982
3983 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003984BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003985{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003986 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003987}
3988
3989 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003990BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003991{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003992 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003993}
3994
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003995static char *BufferAttrs[] = {
3996 "name", "number", "vars", "options", "valid",
3997 NULL
3998};
3999
4000 static PyObject *
4001BufferDir(PyObject *self)
4002{
4003 return ObjectDir(self, BufferAttrs);
4004}
4005
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004006 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004007BufferAttrValid(BufferObject *self, char *name)
4008{
4009 PyObject *r;
4010
4011 if (strcmp(name, "valid") != 0)
4012 return NULL;
4013
4014 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4015 Py_INCREF(r);
4016 return r;
4017}
4018
4019 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004020BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004021{
4022 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004023 return PyString_FromString((self->buf->b_ffname == NULL
4024 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004025 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004026 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004027 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004028 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004029 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004030 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4031 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004032 else if (strcmp(name, "__members__") == 0)
4033 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004034 else
4035 return NULL;
4036}
4037
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004038 static int
4039BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4040{
4041 if (CheckBuffer(self))
4042 return -1;
4043
4044 if (strcmp(name, "name") == 0)
4045 {
4046 char_u *val;
4047 aco_save_T aco;
4048 int r;
4049 PyObject *todecref;
4050
4051 if (!(val = StringToChars(valObject, &todecref)))
4052 return -1;
4053
4054 VimTryStart();
4055 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4056 aucmd_prepbuf(&aco, self->buf);
4057 r = rename_buffer(val);
4058 aucmd_restbuf(&aco);
4059 Py_XDECREF(todecref);
4060 if (VimTryEnd())
4061 return -1;
4062
4063 if (r == FAIL)
4064 {
4065 PyErr_SetVim(_("failed to rename buffer"));
4066 return -1;
4067 }
4068 return 0;
4069 }
4070 else
4071 {
4072 PyErr_SetString(PyExc_AttributeError, name);
4073 return -1;
4074 }
4075}
4076
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004077 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004078BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004080 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081}
4082
4083 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004084BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085{
4086 pos_T *posp;
4087 char *pmark;
4088 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004089 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004090
Bram Moolenaard6e39182013-05-21 18:30:34 +02004091 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092 return NULL;
4093
4094 if (!PyArg_ParseTuple(args, "s", &pmark))
4095 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004096
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004097 if (STRLEN(pmark) != 1)
4098 {
4099 PyErr_SetString(PyExc_ValueError,
4100 _("mark name must be a single character"));
4101 return NULL;
4102 }
4103
4104 mark = *pmark;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004105 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004106 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004108 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004109 if (VimTryEnd())
4110 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004111
4112 if (posp == NULL)
4113 {
4114 PyErr_SetVim(_("invalid mark name"));
4115 return NULL;
4116 }
4117
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004118 if (posp->lnum <= 0)
4119 {
4120 /* Or raise an error? */
4121 Py_INCREF(Py_None);
4122 return Py_None;
4123 }
4124
4125 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4126}
4127
4128 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004129BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004130{
4131 PyInt start;
4132 PyInt end;
4133
Bram Moolenaard6e39182013-05-21 18:30:34 +02004134 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004135 return NULL;
4136
4137 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4138 return NULL;
4139
Bram Moolenaard6e39182013-05-21 18:30:34 +02004140 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004141}
4142
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004143 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004144BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004145{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004146 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004147 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004148 else
4149 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004150 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004151
4152 if (name == NULL)
4153 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004154
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004155 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004156 }
4157}
4158
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004159static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004160 /* name, function, calling, documentation */
4161 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4162 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4163 {"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 +02004164 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4165 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004166};
4167
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004168/*
4169 * Buffer list object - Implementation
4170 */
4171
4172static PyTypeObject BufMapType;
4173
4174typedef struct
4175{
4176 PyObject_HEAD
4177} BufMapObject;
4178
4179 static PyInt
4180BufMapLength(PyObject *self UNUSED)
4181{
4182 buf_T *b = firstbuf;
4183 PyInt n = 0;
4184
4185 while (b)
4186 {
4187 ++n;
4188 b = b->b_next;
4189 }
4190
4191 return n;
4192}
4193
4194 static PyObject *
4195BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4196{
4197 buf_T *b;
4198 int bnr;
4199
4200#if PY_MAJOR_VERSION < 3
4201 if (PyInt_Check(keyObject))
4202 bnr = PyInt_AsLong(keyObject);
4203 else
4204#endif
4205 if (PyLong_Check(keyObject))
4206 bnr = PyLong_AsLong(keyObject);
4207 else
4208 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004209 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004210 return NULL;
4211 }
4212
4213 b = buflist_findnr(bnr);
4214
4215 if (b)
4216 return BufferNew(b);
4217 else
4218 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004219 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004220 return NULL;
4221 }
4222}
4223
4224 static void
4225BufMapIterDestruct(PyObject *buffer)
4226{
4227 /* Iteration was stopped before all buffers were processed */
4228 if (buffer)
4229 {
4230 Py_DECREF(buffer);
4231 }
4232}
4233
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004234 static int
4235BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4236{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004237 if (buffer)
4238 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004239 return 0;
4240}
4241
4242 static int
4243BufMapIterClear(PyObject **buffer)
4244{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004245 if (*buffer)
4246 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004247 return 0;
4248}
4249
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004250 static PyObject *
4251BufMapIterNext(PyObject **buffer)
4252{
4253 PyObject *next;
4254 PyObject *r;
4255
4256 if (!*buffer)
4257 return NULL;
4258
4259 r = *buffer;
4260
4261 if (CheckBuffer((BufferObject *)(r)))
4262 {
4263 *buffer = NULL;
4264 return NULL;
4265 }
4266
4267 if (!((BufferObject *)(r))->buf->b_next)
4268 next = NULL;
4269 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4270 return NULL;
4271 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004272 /* Do not increment reference: we no longer hold it (decref), but whoever
4273 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004274 return r;
4275}
4276
4277 static PyObject *
4278BufMapIter(PyObject *self UNUSED)
4279{
4280 PyObject *buffer;
4281
4282 buffer = BufferNew(firstbuf);
4283 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004284 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4285 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004286}
4287
4288static PyMappingMethods BufMapAsMapping = {
4289 (lenfunc) BufMapLength,
4290 (binaryfunc) BufMapItem,
4291 (objobjargproc) 0,
4292};
4293
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004294/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004295 */
4296
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004297static char *CurrentAttrs[] = {
4298 "buffer", "window", "line", "range", "tabpage",
4299 NULL
4300};
4301
4302 static PyObject *
4303CurrentDir(PyObject *self)
4304{
4305 return ObjectDir(self, CurrentAttrs);
4306}
4307
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004308 static PyObject *
4309CurrentGetattr(PyObject *self UNUSED, char *name)
4310{
4311 if (strcmp(name, "buffer") == 0)
4312 return (PyObject *)BufferNew(curbuf);
4313 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004314 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004315 else if (strcmp(name, "tabpage") == 0)
4316 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004317 else if (strcmp(name, "line") == 0)
4318 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4319 else if (strcmp(name, "range") == 0)
4320 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004321 else if (strcmp(name, "__members__") == 0)
4322 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004323 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004324#if PY_MAJOR_VERSION < 3
4325 return Py_FindMethod(WindowMethods, self, name);
4326#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004327 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004328#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004329}
4330
4331 static int
4332CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4333{
4334 if (strcmp(name, "line") == 0)
4335 {
4336 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4337 return -1;
4338
4339 return 0;
4340 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004341 else if (strcmp(name, "buffer") == 0)
4342 {
4343 int count;
4344
4345 if (value->ob_type != &BufferType)
4346 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004347 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004348 return -1;
4349 }
4350
4351 if (CheckBuffer((BufferObject *)(value)))
4352 return -1;
4353 count = ((BufferObject *)(value))->buf->b_fnum;
4354
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004355 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004356 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4357 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004358 if (VimTryEnd())
4359 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004360 PyErr_SetVim(_("failed to switch to given buffer"));
4361 return -1;
4362 }
4363
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004364 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004365 }
4366 else if (strcmp(name, "window") == 0)
4367 {
4368 int count;
4369
4370 if (value->ob_type != &WindowType)
4371 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004372 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004373 return -1;
4374 }
4375
4376 if (CheckWindow((WindowObject *)(value)))
4377 return -1;
4378 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4379
4380 if (!count)
4381 {
4382 PyErr_SetString(PyExc_ValueError,
4383 _("failed to find window in the current tab page"));
4384 return -1;
4385 }
4386
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004387 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004388 win_goto(((WindowObject *)(value))->win);
4389 if (((WindowObject *)(value))->win != curwin)
4390 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004391 if (VimTryEnd())
4392 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004393 PyErr_SetString(PyExc_RuntimeError,
4394 _("did not switch to the specified window"));
4395 return -1;
4396 }
4397
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004398 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004399 }
4400 else if (strcmp(name, "tabpage") == 0)
4401 {
4402 if (value->ob_type != &TabPageType)
4403 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004404 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004405 return -1;
4406 }
4407
4408 if (CheckTabPage((TabPageObject *)(value)))
4409 return -1;
4410
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004411 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004412 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4413 if (((TabPageObject *)(value))->tab != curtab)
4414 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004415 if (VimTryEnd())
4416 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004417 PyErr_SetString(PyExc_RuntimeError,
4418 _("did not switch to the specified tab page"));
4419 return -1;
4420 }
4421
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004422 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004423 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004424 else
4425 {
4426 PyErr_SetString(PyExc_AttributeError, name);
4427 return -1;
4428 }
4429}
4430
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004431static struct PyMethodDef CurrentMethods[] = {
4432 /* name, function, calling, documentation */
4433 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4434 { NULL, NULL, 0, NULL}
4435};
4436
Bram Moolenaardb913952012-06-29 12:54:53 +02004437 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004438init_range_cmd(exarg_T *eap)
4439{
4440 RangeStart = eap->line1;
4441 RangeEnd = eap->line2;
4442}
4443
4444 static void
4445init_range_eval(typval_T *rettv UNUSED)
4446{
4447 RangeStart = (PyInt) curwin->w_cursor.lnum;
4448 RangeEnd = RangeStart;
4449}
4450
4451 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004452run_cmd(const char *cmd, void *arg UNUSED
4453#ifdef PY_CAN_RECURSE
4454 , PyGILState_STATE *pygilstate UNUSED
4455#endif
4456 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004457{
4458 PyRun_SimpleString((char *) cmd);
4459}
4460
4461static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4462static int code_hdr_len = 30;
4463
4464 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004465run_do(const char *cmd, void *arg UNUSED
4466#ifdef PY_CAN_RECURSE
4467 , PyGILState_STATE *pygilstate
4468#endif
4469 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004470{
4471 PyInt lnum;
4472 size_t len;
4473 char *code;
4474 int status;
4475 PyObject *pyfunc, *pymain;
4476
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004477 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004478 {
4479 EMSG(_("cannot save undo information"));
4480 return;
4481 }
4482
4483 len = code_hdr_len + STRLEN(cmd);
4484 code = PyMem_New(char, len + 1);
4485 memcpy(code, code_hdr, code_hdr_len);
4486 STRCPY(code + code_hdr_len, cmd);
4487 status = PyRun_SimpleString(code);
4488 PyMem_Free(code);
4489
4490 if (status)
4491 {
4492 EMSG(_("failed to run the code"));
4493 return;
4494 }
4495
4496 status = 0;
4497 pymain = PyImport_AddModule("__main__");
4498 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004499#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004500 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004501#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004502
4503 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4504 {
4505 PyObject *line, *linenr, *ret;
4506
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004507#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004508 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004509#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004510 if (!(line = GetBufferLine(curbuf, lnum)))
4511 goto err;
4512 if (!(linenr = PyInt_FromLong((long) lnum)))
4513 {
4514 Py_DECREF(line);
4515 goto err;
4516 }
4517 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4518 Py_DECREF(line);
4519 Py_DECREF(linenr);
4520 if (!ret)
4521 goto err;
4522
4523 if (ret != Py_None)
4524 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4525 goto err;
4526
4527 Py_XDECREF(ret);
4528 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004529#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004530 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004531#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004532 }
4533 goto out;
4534err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004535#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004536 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004537#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004538 PyErr_PrintEx(0);
4539 PythonIO_Flush();
4540 status = 1;
4541out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004542#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004543 if (!status)
4544 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004545#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004546 Py_DECREF(pyfunc);
4547 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4548 if (status)
4549 return;
4550 check_cursor();
4551 update_curbuf(NOT_VALID);
4552}
4553
4554 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004555run_eval(const char *cmd, typval_T *rettv
4556#ifdef PY_CAN_RECURSE
4557 , PyGILState_STATE *pygilstate UNUSED
4558#endif
4559 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004560{
4561 PyObject *r;
4562
4563 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4564 if (r == NULL)
4565 {
4566 if (PyErr_Occurred() && !msg_silent)
4567 PyErr_PrintEx(0);
4568 EMSG(_("E858: Eval did not return a valid python object"));
4569 }
4570 else
4571 {
4572 if (ConvertFromPyObject(r, rettv) == -1)
4573 EMSG(_("E859: Failed to convert returned python object to vim value"));
4574 Py_DECREF(r);
4575 }
4576 PyErr_Clear();
4577}
4578
4579 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004580set_ref_in_py(const int copyID)
4581{
4582 pylinkedlist_T *cur;
4583 dict_T *dd;
4584 list_T *ll;
4585
4586 if (lastdict != NULL)
4587 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4588 {
4589 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4590 if (dd->dv_copyID != copyID)
4591 {
4592 dd->dv_copyID = copyID;
4593 set_ref_in_ht(&dd->dv_hashtab, copyID);
4594 }
4595 }
4596
4597 if (lastlist != NULL)
4598 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4599 {
4600 ll = ((ListObject *) (cur->pll_obj))->list;
4601 if (ll->lv_copyID != copyID)
4602 {
4603 ll->lv_copyID = copyID;
4604 set_ref_in_list(ll, copyID);
4605 }
4606 }
4607}
4608
4609 static int
4610set_string_copy(char_u *str, typval_T *tv)
4611{
4612 tv->vval.v_string = vim_strsave(str);
4613 if (tv->vval.v_string == NULL)
4614 {
4615 PyErr_NoMemory();
4616 return -1;
4617 }
4618 return 0;
4619}
4620
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004621 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004622pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004623{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004624 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004625 char_u *key;
4626 dictitem_T *di;
4627 PyObject *keyObject;
4628 PyObject *valObject;
4629 Py_ssize_t iter = 0;
4630
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004631 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004632 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004633
4634 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004635 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004636
4637 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4638 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004639 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004640
Bram Moolenaara03e6312013-05-29 22:49:26 +02004641 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004642 {
4643 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004644 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004645 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004646
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004647 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004648 {
4649 dict_unref(dict);
4650 return -1;
4651 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004652
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004653 if (*key == NUL)
4654 {
4655 dict_unref(dict);
4656 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004657 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004658 return -1;
4659 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004660
4661 di = dictitem_alloc(key);
4662
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004663 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004664
4665 if (di == NULL)
4666 {
4667 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004668 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004669 return -1;
4670 }
4671 di->di_tv.v_lock = 0;
4672
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004673 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004674 {
4675 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004676 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004677 return -1;
4678 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004679
4680 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004681 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004682 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004683 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004684 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004685 PyErr_SetVim(_("failed to add key to dictionary"));
4686 return -1;
4687 }
4688 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004689
4690 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004691 return 0;
4692}
4693
4694 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004695pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004696{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004697 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004698 char_u *key;
4699 dictitem_T *di;
4700 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004701 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004702 PyObject *keyObject;
4703 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004704
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004705 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004706 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004707
4708 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004709 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004710
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004711 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004712 {
4713 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004714 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004715 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004716
4717 if (!(iterator = PyObject_GetIter(list)))
4718 {
4719 dict_unref(dict);
4720 Py_DECREF(list);
4721 return -1;
4722 }
4723 Py_DECREF(list);
4724
4725 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004726 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004727 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004728
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004729 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004730 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004731 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004732 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004733 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004734 return -1;
4735 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004736
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004737 if (*key == NUL)
4738 {
4739 Py_DECREF(keyObject);
4740 Py_DECREF(iterator);
4741 Py_XDECREF(todecref);
4742 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004743 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004744 return -1;
4745 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004746
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004747 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004748 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004749 Py_DECREF(keyObject);
4750 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004751 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004752 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004753 return -1;
4754 }
4755
4756 di = dictitem_alloc(key);
4757
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004758 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004759 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004760
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004761 if (di == NULL)
4762 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004763 Py_DECREF(iterator);
4764 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004765 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004766 PyErr_NoMemory();
4767 return -1;
4768 }
4769 di->di_tv.v_lock = 0;
4770
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004771 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004772 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004773 Py_DECREF(iterator);
4774 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004775 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004776 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004777 return -1;
4778 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004779
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004780 Py_DECREF(valObject);
4781
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004782 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004783 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004784 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004785 dictitem_free(di);
4786 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004787 PyErr_SetVim(_("failed to add key to dictionary"));
4788 return -1;
4789 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004790 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004791 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004792 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004793 return 0;
4794}
4795
4796 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004797pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004798{
4799 list_T *l;
4800
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004801 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004802 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004803
4804 tv->v_type = VAR_LIST;
4805 tv->vval.v_list = l;
4806
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004807 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004808 {
4809 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004810 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004811 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004812
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004813 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004814 return 0;
4815}
4816
Bram Moolenaardb913952012-06-29 12:54:53 +02004817typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4818
4819 static int
4820convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004821 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004822{
4823 PyObject *capsule;
4824 char hexBuf[sizeof(void *) * 2 + 3];
4825
4826 sprintf(hexBuf, "%p", obj);
4827
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004828# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004829 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004830# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004831 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004832# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004833 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004834 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004835# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004836 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004837# else
4838 capsule = PyCObject_FromVoidPtr(tv, NULL);
4839# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004840 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4841 {
4842 Py_DECREF(capsule);
4843 tv->v_type = VAR_UNKNOWN;
4844 return -1;
4845 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004846 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004847 {
4848 tv->v_type = VAR_UNKNOWN;
4849 return -1;
4850 }
4851 /* As we are not using copy_tv which increments reference count we must
4852 * do it ourself. */
4853 switch(tv->v_type)
4854 {
4855 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4856 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4857 }
4858 }
4859 else
4860 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004861 typval_T *v;
4862
4863# ifdef PY_USE_CAPSULE
4864 v = PyCapsule_GetPointer(capsule, NULL);
4865# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004866 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004867# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004868 copy_tv(v, tv);
4869 }
4870 return 0;
4871}
4872
4873 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02004874ConvertFromPyMapping(PyObject *obj, typval_T *tv)
4875{
4876 PyObject *lookup_dict;
4877 int r;
4878
4879 if (!(lookup_dict = PyDict_New()))
4880 return -1;
4881
4882 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
4883 {
4884 tv->v_type = VAR_DICT;
4885 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4886 ++tv->vval.v_dict->dv_refcount;
4887 r = 0;
4888 }
4889 else if (PyDict_Check(obj))
4890 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
4891 else if (PyMapping_Check(obj))
4892 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
4893 else
4894 {
4895 PyErr_SetString(PyExc_TypeError,
4896 _("unable to convert object to vim dictionary"));
4897 r = -1;
4898 }
4899 Py_DECREF(lookup_dict);
4900 return r;
4901}
4902
4903 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02004904ConvertFromPyObject(PyObject *obj, typval_T *tv)
4905{
4906 PyObject *lookup_dict;
4907 int r;
4908
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004909 if (!(lookup_dict = PyDict_New()))
4910 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004911 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4912 Py_DECREF(lookup_dict);
4913 return r;
4914}
4915
4916 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004917_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004918{
Bram Moolenaara9922d62013-05-30 13:01:18 +02004919 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02004920 {
4921 tv->v_type = VAR_DICT;
4922 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4923 ++tv->vval.v_dict->dv_refcount;
4924 }
4925 else if (obj->ob_type == &ListType)
4926 {
4927 tv->v_type = VAR_LIST;
4928 tv->vval.v_list = (((ListObject *)(obj))->list);
4929 ++tv->vval.v_list->lv_refcount;
4930 }
4931 else if (obj->ob_type == &FunctionType)
4932 {
4933 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4934 return -1;
4935
4936 tv->v_type = VAR_FUNC;
4937 func_ref(tv->vval.v_string);
4938 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004939 else if (PyBytes_Check(obj))
4940 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004941 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004942
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004943 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4944 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004945 if (result == NULL)
4946 return -1;
4947
4948 if (set_string_copy(result, tv) == -1)
4949 return -1;
4950
4951 tv->v_type = VAR_STRING;
4952 }
4953 else if (PyUnicode_Check(obj))
4954 {
4955 PyObject *bytes;
4956 char_u *result;
4957
Bram Moolenaardb913952012-06-29 12:54:53 +02004958 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4959 if (bytes == NULL)
4960 return -1;
4961
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004962 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4963 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004964 if (result == NULL)
4965 return -1;
4966
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004967 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004968 {
4969 Py_XDECREF(bytes);
4970 return -1;
4971 }
4972 Py_XDECREF(bytes);
4973
4974 tv->v_type = VAR_STRING;
4975 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004976#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004977 else if (PyInt_Check(obj))
4978 {
4979 tv->v_type = VAR_NUMBER;
4980 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4981 }
4982#endif
4983 else if (PyLong_Check(obj))
4984 {
4985 tv->v_type = VAR_NUMBER;
4986 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4987 }
4988 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004989 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004990#ifdef FEAT_FLOAT
4991 else if (PyFloat_Check(obj))
4992 {
4993 tv->v_type = VAR_FLOAT;
4994 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4995 }
4996#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004997 else if (PyObject_HasAttrString(obj, "keys"))
4998 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004999 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005000 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005001 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005002 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005003 else
5004 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02005005 PyErr_SetString(PyExc_TypeError,
5006 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005007 return -1;
5008 }
5009 return 0;
5010}
5011
5012 static PyObject *
5013ConvertToPyObject(typval_T *tv)
5014{
5015 if (tv == NULL)
5016 {
5017 PyErr_SetVim(_("NULL reference passed"));
5018 return NULL;
5019 }
5020 switch (tv->v_type)
5021 {
5022 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005023 return PyBytes_FromString(tv->vval.v_string == NULL
5024 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005025 case VAR_NUMBER:
5026 return PyLong_FromLong((long) tv->vval.v_number);
5027#ifdef FEAT_FLOAT
5028 case VAR_FLOAT:
5029 return PyFloat_FromDouble((double) tv->vval.v_float);
5030#endif
5031 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005032 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005033 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005034 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005035 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005036 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005037 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005038 case VAR_UNKNOWN:
5039 Py_INCREF(Py_None);
5040 return Py_None;
5041 default:
5042 PyErr_SetVim(_("internal error: invalid value type"));
5043 return NULL;
5044 }
5045}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005046
5047typedef struct
5048{
5049 PyObject_HEAD
5050} CurrentObject;
5051static PyTypeObject CurrentType;
5052
5053 static void
5054init_structs(void)
5055{
5056 vim_memset(&OutputType, 0, sizeof(OutputType));
5057 OutputType.tp_name = "vim.message";
5058 OutputType.tp_basicsize = sizeof(OutputObject);
5059 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5060 OutputType.tp_doc = "vim message object";
5061 OutputType.tp_methods = OutputMethods;
5062#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005063 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5064 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005065 OutputType.tp_alloc = call_PyType_GenericAlloc;
5066 OutputType.tp_new = call_PyType_GenericNew;
5067 OutputType.tp_free = call_PyObject_Free;
5068#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5070 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005071#endif
5072
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005073 vim_memset(&IterType, 0, sizeof(IterType));
5074 IterType.tp_name = "vim.iter";
5075 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005076 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005077 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005078 IterType.tp_iter = (getiterfunc)IterIter;
5079 IterType.tp_iternext = (iternextfunc)IterNext;
5080 IterType.tp_dealloc = (destructor)IterDestructor;
5081 IterType.tp_traverse = (traverseproc)IterTraverse;
5082 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005083
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005084 vim_memset(&BufferType, 0, sizeof(BufferType));
5085 BufferType.tp_name = "vim.buffer";
5086 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005087 BufferType.tp_dealloc = (destructor)BufferDestructor;
5088 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005089 BufferType.tp_as_sequence = &BufferAsSeq;
5090 BufferType.tp_as_mapping = &BufferAsMapping;
5091 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5092 BufferType.tp_doc = "vim buffer object";
5093 BufferType.tp_methods = BufferMethods;
5094#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005095 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005096 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005097 BufferType.tp_alloc = call_PyType_GenericAlloc;
5098 BufferType.tp_new = call_PyType_GenericNew;
5099 BufferType.tp_free = call_PyObject_Free;
5100#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005101 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005102 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005103#endif
5104
5105 vim_memset(&WindowType, 0, sizeof(WindowType));
5106 WindowType.tp_name = "vim.window";
5107 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005108 WindowType.tp_dealloc = (destructor)WindowDestructor;
5109 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005110 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005111 WindowType.tp_doc = "vim Window object";
5112 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005113 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5114 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005115#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005116 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5117 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005118 WindowType.tp_alloc = call_PyType_GenericAlloc;
5119 WindowType.tp_new = call_PyType_GenericNew;
5120 WindowType.tp_free = call_PyObject_Free;
5121#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005122 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5123 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005124#endif
5125
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005126 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5127 TabPageType.tp_name = "vim.tabpage";
5128 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005129 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5130 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005131 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5132 TabPageType.tp_doc = "vim tab page object";
5133 TabPageType.tp_methods = TabPageMethods;
5134#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005135 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005136 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5137 TabPageType.tp_new = call_PyType_GenericNew;
5138 TabPageType.tp_free = call_PyObject_Free;
5139#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005140 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005141#endif
5142
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005143 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5144 BufMapType.tp_name = "vim.bufferlist";
5145 BufMapType.tp_basicsize = sizeof(BufMapObject);
5146 BufMapType.tp_as_mapping = &BufMapAsMapping;
5147 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005148 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005149 BufferType.tp_doc = "vim buffer list";
5150
5151 vim_memset(&WinListType, 0, sizeof(WinListType));
5152 WinListType.tp_name = "vim.windowlist";
5153 WinListType.tp_basicsize = sizeof(WinListType);
5154 WinListType.tp_as_sequence = &WinListAsSeq;
5155 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5156 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005157 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005158
5159 vim_memset(&TabListType, 0, sizeof(TabListType));
5160 TabListType.tp_name = "vim.tabpagelist";
5161 TabListType.tp_basicsize = sizeof(TabListType);
5162 TabListType.tp_as_sequence = &TabListAsSeq;
5163 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5164 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005165
5166 vim_memset(&RangeType, 0, sizeof(RangeType));
5167 RangeType.tp_name = "vim.range";
5168 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005169 RangeType.tp_dealloc = (destructor)RangeDestructor;
5170 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005171 RangeType.tp_as_sequence = &RangeAsSeq;
5172 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005173 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005174 RangeType.tp_doc = "vim Range object";
5175 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005176 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5177 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005178#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005179 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005180 RangeType.tp_alloc = call_PyType_GenericAlloc;
5181 RangeType.tp_new = call_PyType_GenericNew;
5182 RangeType.tp_free = call_PyObject_Free;
5183#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005184 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005185#endif
5186
5187 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5188 CurrentType.tp_name = "vim.currentdata";
5189 CurrentType.tp_basicsize = sizeof(CurrentObject);
5190 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5191 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005192 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005193#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005194 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5195 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005196#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005197 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5198 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005199#endif
5200
5201 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5202 DictionaryType.tp_name = "vim.dictionary";
5203 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005204 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005205 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005206 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005207 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005208 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5209 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005210 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5211 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5212 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005213#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005214 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5215 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005216#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005217 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5218 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005219#endif
5220
5221 vim_memset(&ListType, 0, sizeof(ListType));
5222 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005223 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005224 ListType.tp_basicsize = sizeof(ListObject);
5225 ListType.tp_as_sequence = &ListAsSeq;
5226 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005227 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005228 ListType.tp_doc = "list pushing modifications to vim structure";
5229 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005230 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005231 ListType.tp_new = (newfunc)ListConstructor;
5232 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005233#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005234 ListType.tp_getattro = (getattrofunc)ListGetattro;
5235 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005236#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005237 ListType.tp_getattr = (getattrfunc)ListGetattr;
5238 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005239#endif
5240
5241 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005242 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005243 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005244 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5245 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005246 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005247 FunctionType.tp_doc = "object that calls vim function";
5248 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005249 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005250 FunctionType.tp_new = (newfunc)FunctionConstructor;
5251 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005252#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005253 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005254#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005255 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005256#endif
5257
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005258 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5259 OptionsType.tp_name = "vim.options";
5260 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005261 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005262 OptionsType.tp_doc = "object for manipulating options";
5263 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005264 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5265 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5266 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005267
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005268#if PY_MAJOR_VERSION >= 3
5269 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5270 vimmodule.m_name = "vim";
5271 vimmodule.m_doc = "Vim Python interface\n";
5272 vimmodule.m_size = -1;
5273 vimmodule.m_methods = VimMethods;
5274#endif
5275}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005276
5277#define PYTYPE_READY(type) \
5278 if (PyType_Ready(&type)) \
5279 return -1;
5280
5281 static int
5282init_types()
5283{
5284 PYTYPE_READY(IterType);
5285 PYTYPE_READY(BufferType);
5286 PYTYPE_READY(RangeType);
5287 PYTYPE_READY(WindowType);
5288 PYTYPE_READY(TabPageType);
5289 PYTYPE_READY(BufMapType);
5290 PYTYPE_READY(WinListType);
5291 PYTYPE_READY(TabListType);
5292 PYTYPE_READY(CurrentType);
5293 PYTYPE_READY(DictionaryType);
5294 PYTYPE_READY(ListType);
5295 PYTYPE_READY(FunctionType);
5296 PYTYPE_READY(OptionsType);
5297 PYTYPE_READY(OutputType);
5298 return 0;
5299}
5300
5301static BufMapObject TheBufferMap =
5302{
5303 PyObject_HEAD_INIT(&BufMapType)
5304};
5305
5306static WinListObject TheWindowList =
5307{
5308 PyObject_HEAD_INIT(&WinListType)
5309 NULL
5310};
5311
5312static CurrentObject TheCurrent =
5313{
5314 PyObject_HEAD_INIT(&CurrentType)
5315};
5316
5317static TabListObject TheTabPageList =
5318{
5319 PyObject_HEAD_INIT(&TabListType)
5320};
5321
5322static struct numeric_constant {
5323 char *name;
5324 int value;
5325} numeric_constants[] = {
5326 {"VAR_LOCKED", VAR_LOCKED},
5327 {"VAR_FIXED", VAR_FIXED},
5328 {"VAR_SCOPE", VAR_SCOPE},
5329 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5330};
5331
5332static struct object_constant {
5333 char *name;
5334 PyObject *value;
5335} object_constants[] = {
5336 {"buffers", (PyObject *)(void *)&TheBufferMap},
5337 {"windows", (PyObject *)(void *)&TheWindowList},
5338 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5339 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005340
5341 {"Buffer", (PyObject *)&BufferType},
5342 {"Range", (PyObject *)&RangeType},
5343 {"Window", (PyObject *)&WindowType},
5344 {"TabPage", (PyObject *)&TabPageType},
5345 {"Dictionary", (PyObject *)&DictionaryType},
5346 {"List", (PyObject *)&ListType},
5347 {"Function", (PyObject *)&FunctionType},
5348 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005349};
5350
5351typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005352typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005353
5354#define ADD_OBJECT(m, name, obj) \
5355 if (add_object(m, name, obj)) \
5356 return -1;
5357
5358#define ADD_CHECKED_OBJECT(m, name, obj) \
5359 { \
5360 PyObject *value = obj; \
5361 if (!value) \
5362 return -1; \
5363 ADD_OBJECT(m, name, value); \
5364 }
5365
5366 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005367populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005368{
5369 int i;
Bram Moolenaarf4258302013-06-02 18:20:17 +02005370 PyObject *os;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005371
5372 for (i = 0; i < (int)(sizeof(numeric_constants)
5373 / sizeof(struct numeric_constant));
5374 ++i)
5375 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5376 PyInt_FromLong(numeric_constants[i].value));
5377
5378 for (i = 0; i < (int)(sizeof(object_constants)
5379 / sizeof(struct object_constant));
5380 ++i)
5381 {
5382 PyObject *value;
5383
5384 value = object_constants[i].value;
5385 Py_INCREF(value);
5386 ADD_OBJECT(m, object_constants[i].name, value);
5387 }
5388
5389 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5390 return -1;
5391 ADD_OBJECT(m, "error", VimError);
5392
Bram Moolenaara9922d62013-05-30 13:01:18 +02005393 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5394 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005395 ADD_CHECKED_OBJECT(m, "options",
5396 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005397
5398 if (!(os = PyImport_ImportModule("os")))
5399 return -1;
5400 ADD_OBJECT(m, "os", os);
5401
5402 if (!(py_getcwd = PyObject_GetAttrString(os, "getcwd")))
5403 return -1;
5404 ADD_OBJECT(m, "_getcwd", py_getcwd)
5405
5406 if (!(py_chdir = PyObject_GetAttrString(os, "chdir")))
5407 return -1;
5408 ADD_OBJECT(m, "_chdir", py_chdir);
5409 if (PyObject_SetAttrString(os, "chdir", get_attr(m, "chdir")))
5410 return -1;
5411
5412 if ((py_fchdir = PyObject_GetAttrString(os, "fchdir")))
5413 {
5414 ADD_OBJECT(m, "_fchdir", py_fchdir);
5415 if (PyObject_SetAttrString(os, "fchdir", get_attr(m, "fchdir")))
5416 return -1;
5417 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02005418 else
5419 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02005420
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005421 return 0;
5422}