blob: 99fdd80ce58348bb576ca9d96d9d97f07f16d3eb [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
721 if (!(r = PyObject_Call(_chdir, args, kwargs)))
722 return NULL;
723
724 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
725 {
726 Py_DECREF(r);
727 return NULL;
728 }
729
730 if (!(new_dir = StringToChars(newwd, &todecref)))
731 {
732 Py_DECREF(r);
733 Py_DECREF(newwd);
734 return NULL;
735 }
736
737 VimTryStart();
738
739 if (vim_chdir(new_dir))
740 {
741 Py_DECREF(r);
742 Py_DECREF(newwd);
743 Py_XDECREF(todecref);
744
745 if (VimTryEnd())
746 return NULL;
747
748 PyErr_SetVim(_("failed to change directory"));
749 return NULL;
750 }
751
752 Py_DECREF(newwd);
753 Py_XDECREF(todecref);
754
755 post_chdir(FALSE);
756
757 if (VimTryEnd())
758 {
759 Py_DECREF(r);
760 return NULL;
761 }
762
763 return r;
764}
765
766 static PyObject *
767VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
768{
769 return _VimChdir(py_chdir, args, kwargs);
770}
771
772 static PyObject *
773VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
774{
775 return _VimChdir(py_fchdir, args, kwargs);
776}
777
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200778/*
779 * Vim module - Definitions
780 */
781
782static struct PyMethodDef VimMethods[] = {
Bram Moolenaarf4258302013-06-02 18:20:17 +0200783 /* name, function, calling, documentation */
784 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
785 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
786 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
787 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
788 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
789 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
790 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791};
792
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200793/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200794 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795 */
796
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200797static PyTypeObject IterType;
798
799typedef PyObject *(*nextfun)(void **);
800typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200801typedef int (*traversefun)(void *, visitproc, void *);
802typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200803
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200804/* Main purpose of this object is removing the need for do python
805 * initialization (i.e. PyType_Ready and setting type attributes) for a big
806 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200807
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200808typedef struct
809{
810 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200811 void *cur;
812 nextfun next;
813 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200814 traversefun traverse;
815 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200816} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200817
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200818 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200819IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
820 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200821{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200822 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200823
Bram Moolenaar774267b2013-05-21 20:51:59 +0200824 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200825 self->cur = start;
826 self->next = next;
827 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200828 self->traverse = traverse;
829 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200830
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200831 return (PyObject *)(self);
832}
833
834 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200835IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200836{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200837 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200838 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200839 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200840}
841
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200842 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200843IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200844{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200845 if (self->traverse != NULL)
846 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200847 else
848 return 0;
849}
850
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200851/* Mac OSX defines clear() somewhere. */
852#ifdef clear
853# undef clear
854#endif
855
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200856 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200857IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200858{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200859 if (self->clear != NULL)
860 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200861 else
862 return 0;
863}
864
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200865 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200866IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200867{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200868 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200869}
870
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200871 static PyObject *
872IterIter(PyObject *self)
873{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +0200874 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200875 return self;
876}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200877
Bram Moolenaardb913952012-06-29 12:54:53 +0200878typedef struct pylinkedlist_S {
879 struct pylinkedlist_S *pll_next;
880 struct pylinkedlist_S *pll_prev;
881 PyObject *pll_obj;
882} pylinkedlist_T;
883
884static pylinkedlist_T *lastdict = NULL;
885static pylinkedlist_T *lastlist = NULL;
886
887 static void
888pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
889{
890 if (ref->pll_prev == NULL)
891 {
892 if (ref->pll_next == NULL)
893 {
894 *last = NULL;
895 return;
896 }
897 }
898 else
899 ref->pll_prev->pll_next = ref->pll_next;
900
901 if (ref->pll_next == NULL)
902 *last = ref->pll_prev;
903 else
904 ref->pll_next->pll_prev = ref->pll_prev;
905}
906
907 static void
908pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
909{
910 if (*last == NULL)
911 ref->pll_prev = NULL;
912 else
913 {
914 (*last)->pll_next = ref;
915 ref->pll_prev = *last;
916 }
917 ref->pll_next = NULL;
918 ref->pll_obj = self;
919 *last = ref;
920}
921
922static PyTypeObject DictionaryType;
923
924typedef struct
925{
926 PyObject_HEAD
927 dict_T *dict;
928 pylinkedlist_T ref;
929} DictionaryObject;
930
Bram Moolenaara9922d62013-05-30 13:01:18 +0200931static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
932
933#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
934
Bram Moolenaardb913952012-06-29 12:54:53 +0200935 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +0200936DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +0200937{
938 DictionaryObject *self;
939
Bram Moolenaara9922d62013-05-30 13:01:18 +0200940 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200941 if (self == NULL)
942 return NULL;
943 self->dict = dict;
944 ++dict->dv_refcount;
945
946 pyll_add((PyObject *)(self), &self->ref, &lastdict);
947
948 return (PyObject *)(self);
949}
950
Bram Moolenaara9922d62013-05-30 13:01:18 +0200951 static dict_T *
952py_dict_alloc()
953{
954 dict_T *r;
955
956 if (!(r = dict_alloc()))
957 {
958 PyErr_NoMemory();
959 return NULL;
960 }
961 ++r->dv_refcount;
962
963 return r;
964}
965
966 static PyObject *
967DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
968{
969 DictionaryObject *self;
970 dict_T *dict;
971
972 if (!(dict = py_dict_alloc()))
973 return NULL;
974
975 self = (DictionaryObject *) DictionaryNew(subtype, dict);
976
977 --dict->dv_refcount;
978
979 if (kwargs || PyTuple_Size(args))
980 {
981 PyObject *tmp;
982 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
983 {
984 Py_DECREF(self);
985 return NULL;
986 }
987
988 Py_DECREF(tmp);
989 }
990
991 return (PyObject *)(self);
992}
993
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200994 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200995DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200996{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200997 pyll_remove(&self->ref, &lastdict);
998 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200999
1000 DESTRUCTOR_FINISH(self);
1001}
1002
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001003static char *DictionaryAttrs[] = {
1004 "locked", "scope",
1005 NULL
1006};
1007
1008 static PyObject *
1009DictionaryDir(PyObject *self)
1010{
1011 return ObjectDir(self, DictionaryAttrs);
1012}
1013
Bram Moolenaardb913952012-06-29 12:54:53 +02001014 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001015DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001016{
1017 if (val == NULL)
1018 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001019 PyErr_SetString(PyExc_AttributeError,
1020 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001021 return -1;
1022 }
1023
1024 if (strcmp(name, "locked") == 0)
1025 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001026 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001027 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001028 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001029 return -1;
1030 }
1031 else
1032 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001033 int istrue = PyObject_IsTrue(val);
1034 if (istrue == -1)
1035 return -1;
1036 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001037 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001038 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001039 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001040 }
1041 return 0;
1042 }
1043 else
1044 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001045 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001046 return -1;
1047 }
1048}
1049
1050 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001051DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001052{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001053 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001054}
1055
Bram Moolenaara9922d62013-05-30 13:01:18 +02001056#define DICT_FLAG_HAS_DEFAULT 0x01
1057#define DICT_FLAG_POP 0x02
1058#define DICT_FLAG_NONE_DEFAULT 0x04
1059#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1060#define DICT_FLAG_RETURN_PAIR 0x10
1061
Bram Moolenaardb913952012-06-29 12:54:53 +02001062 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001063_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001064{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001065 PyObject *keyObject;
1066 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1067 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001068 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001069 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001070 dict_T *dict = self->dict;
1071 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001072 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001073
Bram Moolenaara9922d62013-05-30 13:01:18 +02001074 if (flags & DICT_FLAG_HAS_DEFAULT)
1075 {
1076 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1077 return NULL;
1078 }
1079 else
1080 keyObject = args;
1081
1082 if (flags & DICT_FLAG_RETURN_BOOL)
1083 defObject = Py_False;
1084
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001085 if (!(key = StringToChars(keyObject, &todecref)))
1086 return NULL;
1087
1088 if (*key == NUL)
1089 {
1090 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001091 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001092 return NULL;
1093 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001094
Bram Moolenaara9922d62013-05-30 13:01:18 +02001095 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001096
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001097 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001098
Bram Moolenaara9922d62013-05-30 13:01:18 +02001099 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001100 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001101 if (defObject)
1102 {
1103 Py_INCREF(defObject);
1104 return defObject;
1105 }
1106 else
1107 {
1108 PyErr_SetObject(PyExc_KeyError, keyObject);
1109 return NULL;
1110 }
1111 }
1112 else if (flags & DICT_FLAG_RETURN_BOOL)
1113 {
1114 Py_INCREF(Py_True);
1115 return Py_True;
1116 }
1117
1118 di = dict_lookup(hi);
1119
1120 if (!(r = ConvertToPyObject(&di->di_tv)))
1121 return NULL;
1122
1123 if (flags & DICT_FLAG_POP)
1124 {
1125 if (dict->dv_lock)
1126 {
1127 PyErr_SetVim(_("dict is locked"));
1128 Py_DECREF(r);
1129 return NULL;
1130 }
1131
1132 hash_remove(&dict->dv_hashtab, hi);
1133 dictitem_free(di);
1134 }
1135
Bram Moolenaara9922d62013-05-30 13:01:18 +02001136 return r;
1137}
1138
1139 static PyObject *
1140DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1141{
1142 return _DictionaryItem(self, keyObject, 0);
1143}
1144
1145 static int
1146DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1147{
1148 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1149 int r;
1150
1151 r = (rObj == Py_True);
1152
1153 Py_DECREF(Py_True);
1154
1155 return r;
1156}
1157
1158typedef struct
1159{
1160 hashitem_T *ht_array;
1161 long_u ht_used;
1162 hashtab_T *ht;
1163 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001164 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001165} dictiterinfo_T;
1166
1167 static PyObject *
1168DictionaryIterNext(dictiterinfo_T **dii)
1169{
1170 PyObject *r;
1171
1172 if (!(*dii)->todo)
1173 return NULL;
1174
1175 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1176 (*dii)->ht->ht_used != (*dii)->ht_used)
1177 {
1178 PyErr_SetString(PyExc_RuntimeError,
1179 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001180 return NULL;
1181 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001182
Bram Moolenaara9922d62013-05-30 13:01:18 +02001183 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1184 ++((*dii)->hi);
1185
1186 --((*dii)->todo);
1187
1188 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1189 return NULL;
1190
1191 return r;
1192}
1193
1194 static PyObject *
1195DictionaryIter(DictionaryObject *self)
1196{
1197 dictiterinfo_T *dii;
1198 hashtab_T *ht;
1199
1200 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1201 {
1202 PyErr_NoMemory();
1203 return NULL;
1204 }
1205
1206 ht = &self->dict->dv_hashtab;
1207 dii->ht_array = ht->ht_array;
1208 dii->ht_used = ht->ht_used;
1209 dii->ht = ht;
1210 dii->hi = dii->ht_array;
1211 dii->todo = dii->ht_used;
1212
1213 return IterNew(dii,
1214 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1215 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001216}
1217
1218 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001219DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001220{
1221 char_u *key;
1222 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001223 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001224 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001225 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001226
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001227 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001228 {
1229 PyErr_SetVim(_("dict is locked"));
1230 return -1;
1231 }
1232
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001233 if (!(key = StringToChars(keyObject, &todecref)))
1234 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001235
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001236 if (*key == NUL)
1237 {
1238 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001239 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001240 return -1;
1241 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001242
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001243 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001244
1245 if (valObject == NULL)
1246 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001247 hashitem_T *hi;
1248
Bram Moolenaardb913952012-06-29 12:54:53 +02001249 if (di == NULL)
1250 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001251 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001252 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001253 return -1;
1254 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001255 hi = hash_find(&dict->dv_hashtab, di->di_key);
1256 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001257 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001258 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001259 return 0;
1260 }
1261
1262 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001263 {
1264 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001265 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001266 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001267
1268 if (di == NULL)
1269 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001270 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001271 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001272 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001273 PyErr_NoMemory();
1274 return -1;
1275 }
1276 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001277 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001278
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001279 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001280 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001281 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001282 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001283 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001284 PyErr_SetVim(_("failed to add key to dictionary"));
1285 return -1;
1286 }
1287 }
1288 else
1289 clear_tv(&di->di_tv);
1290
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001291 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001292
1293 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001294 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001295 return 0;
1296}
1297
Bram Moolenaara9922d62013-05-30 13:01:18 +02001298typedef PyObject *(*hi_to_py)(hashitem_T *);
1299
Bram Moolenaardb913952012-06-29 12:54:53 +02001300 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001301DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001302{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001303 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001304 long_u todo = dict->dv_hashtab.ht_used;
1305 Py_ssize_t i = 0;
1306 PyObject *r;
1307 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001308 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001309
1310 r = PyList_New(todo);
1311 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1312 {
1313 if (!HASHITEM_EMPTY(hi))
1314 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001315 if (!(newObj = hiconvert(hi)))
1316 {
1317 Py_DECREF(r);
1318 return NULL;
1319 }
1320 if (PyList_SetItem(r, i, newObj))
1321 {
1322 Py_DECREF(r);
1323 Py_DECREF(newObj);
1324 return NULL;
1325 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001326 --todo;
1327 ++i;
1328 }
1329 }
1330 return r;
1331}
1332
Bram Moolenaara9922d62013-05-30 13:01:18 +02001333 static PyObject *
1334dict_key(hashitem_T *hi)
1335{
1336 return PyBytes_FromString((char *)(hi->hi_key));
1337}
1338
1339 static PyObject *
1340DictionaryListKeys(DictionaryObject *self)
1341{
1342 return DictionaryListObjects(self, dict_key);
1343}
1344
1345 static PyObject *
1346dict_val(hashitem_T *hi)
1347{
1348 dictitem_T *di;
1349
1350 di = dict_lookup(hi);
1351 return ConvertToPyObject(&di->di_tv);
1352}
1353
1354 static PyObject *
1355DictionaryListValues(DictionaryObject *self)
1356{
1357 return DictionaryListObjects(self, dict_val);
1358}
1359
1360 static PyObject *
1361dict_item(hashitem_T *hi)
1362{
1363 PyObject *keyObject;
1364 PyObject *valObject;
1365 PyObject *r;
1366
1367 if (!(keyObject = dict_key(hi)))
1368 return NULL;
1369
1370 if (!(valObject = dict_val(hi)))
1371 {
1372 Py_DECREF(keyObject);
1373 return NULL;
1374 }
1375
1376 r = Py_BuildValue("(OO)", keyObject, valObject);
1377
1378 Py_DECREF(keyObject);
1379 Py_DECREF(valObject);
1380
1381 return r;
1382}
1383
1384 static PyObject *
1385DictionaryListItems(DictionaryObject *self)
1386{
1387 return DictionaryListObjects(self, dict_item);
1388}
1389
1390 static PyObject *
1391DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1392{
1393 dict_T *dict = self->dict;
1394
1395 if (dict->dv_lock)
1396 {
1397 PyErr_SetVim(_("dict is locked"));
1398 return NULL;
1399 }
1400
1401 if (kwargs)
1402 {
1403 typval_T tv;
1404
1405 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1406 return NULL;
1407
1408 VimTryStart();
1409 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1410 clear_tv(&tv);
1411 if (VimTryEnd())
1412 return NULL;
1413 }
1414 else
1415 {
1416 PyObject *object;
1417
1418 if (!PyArg_Parse(args, "(O)", &object))
1419 return NULL;
1420
1421 if (PyObject_HasAttrString(object, "keys"))
1422 return DictionaryUpdate(self, NULL, object);
1423 else
1424 {
1425 PyObject *iterator;
1426 PyObject *item;
1427
1428 if (!(iterator = PyObject_GetIter(object)))
1429 return NULL;
1430
1431 while ((item = PyIter_Next(iterator)))
1432 {
1433 PyObject *fast;
1434 PyObject *keyObject;
1435 PyObject *valObject;
1436 PyObject *todecref;
1437 char_u *key;
1438 dictitem_T *di;
1439
1440 if (!(fast = PySequence_Fast(item, "")))
1441 {
1442 Py_DECREF(iterator);
1443 Py_DECREF(item);
1444 return NULL;
1445 }
1446
1447 Py_DECREF(item);
1448
1449 if (PySequence_Fast_GET_SIZE(fast) != 2)
1450 {
1451 Py_DECREF(iterator);
1452 Py_DECREF(fast);
1453 PyErr_SetString(PyExc_ValueError,
1454 _("expected sequence element of size 2"));
1455 return NULL;
1456 }
1457
1458 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1459
1460 if (!(key = StringToChars(keyObject, &todecref)))
1461 {
1462 Py_DECREF(iterator);
1463 Py_DECREF(fast);
1464 return NULL;
1465 }
1466
1467 di = dictitem_alloc(key);
1468
1469 Py_XDECREF(todecref);
1470
1471 if (di == NULL)
1472 {
1473 Py_DECREF(fast);
1474 Py_DECREF(iterator);
1475 PyErr_NoMemory();
1476 return NULL;
1477 }
1478 di->di_tv.v_lock = 0;
1479 di->di_tv.v_type = VAR_UNKNOWN;
1480
1481 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1482
1483 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1484 {
1485 Py_DECREF(iterator);
1486 Py_DECREF(fast);
1487 dictitem_free(di);
1488 return NULL;
1489 }
1490
1491 Py_DECREF(fast);
1492
1493 if (dict_add(dict, di) == FAIL)
1494 {
1495 Py_DECREF(iterator);
1496 dictitem_free(di);
1497 PyErr_SetVim(_("failed to add key to dictionary"));
1498 return NULL;
1499 }
1500 }
1501
1502 Py_DECREF(iterator);
1503
1504 /* Iterator may have finished due to an exception */
1505 if (PyErr_Occurred())
1506 return NULL;
1507 }
1508 }
1509 Py_INCREF(Py_None);
1510 return Py_None;
1511}
1512
1513 static PyObject *
1514DictionaryGet(DictionaryObject *self, PyObject *args)
1515{
1516 return _DictionaryItem(self, args,
1517 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1518}
1519
1520 static PyObject *
1521DictionaryPop(DictionaryObject *self, PyObject *args)
1522{
1523 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1524}
1525
1526 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001527DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001528{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001529 hashitem_T *hi;
1530 PyObject *r;
1531 PyObject *valObject;
1532 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001533
Bram Moolenaarde71b562013-06-02 17:41:54 +02001534 if (self->dict->dv_hashtab.ht_used == 0)
1535 {
1536 PyErr_SetNone(PyExc_KeyError);
1537 return NULL;
1538 }
1539
1540 hi = self->dict->dv_hashtab.ht_array;
1541 while (HASHITEM_EMPTY(hi))
1542 ++hi;
1543
1544 di = dict_lookup(hi);
1545
1546 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001547 return NULL;
1548
Bram Moolenaarde71b562013-06-02 17:41:54 +02001549 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
1550 {
1551 Py_DECREF(valObject);
1552 return NULL;
1553 }
1554
1555 hash_remove(&self->dict->dv_hashtab, hi);
1556 dictitem_free(di);
1557
1558 return r;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001559}
1560
1561 static PyObject *
1562DictionaryHasKey(DictionaryObject *self, PyObject *args)
1563{
1564 PyObject *keyObject;
1565
1566 if (!PyArg_ParseTuple(args, "O", &keyObject))
1567 return NULL;
1568
1569 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1570}
1571
1572static PySequenceMethods DictionaryAsSeq = {
1573 0, /* sq_length */
1574 0, /* sq_concat */
1575 0, /* sq_repeat */
1576 0, /* sq_item */
1577 0, /* sq_slice */
1578 0, /* sq_ass_item */
1579 0, /* sq_ass_slice */
1580 (objobjproc) DictionaryContains, /* sq_contains */
1581 0, /* sq_inplace_concat */
1582 0, /* sq_inplace_repeat */
1583};
1584
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001585static PyMappingMethods DictionaryAsMapping = {
1586 (lenfunc) DictionaryLength,
1587 (binaryfunc) DictionaryItem,
1588 (objobjargproc) DictionaryAssItem,
1589};
1590
Bram Moolenaardb913952012-06-29 12:54:53 +02001591static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001592 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001593 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1594 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1595 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1596 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1597 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02001598 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001599 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001600 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1601 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001602};
1603
1604static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001605static PySequenceMethods ListAsSeq;
1606static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001607
1608typedef struct
1609{
1610 PyObject_HEAD
1611 list_T *list;
1612 pylinkedlist_T ref;
1613} ListObject;
1614
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001615#define NEW_LIST(list) ListNew(&ListType, list)
1616
Bram Moolenaardb913952012-06-29 12:54:53 +02001617 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001618ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001619{
1620 ListObject *self;
1621
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001622 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001623 if (self == NULL)
1624 return NULL;
1625 self->list = list;
1626 ++list->lv_refcount;
1627
1628 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1629
1630 return (PyObject *)(self);
1631}
1632
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001633 static list_T *
1634py_list_alloc()
1635{
1636 list_T *r;
1637
1638 if (!(r = list_alloc()))
1639 {
1640 PyErr_NoMemory();
1641 return NULL;
1642 }
1643 ++r->lv_refcount;
1644
1645 return r;
1646}
1647
1648 static int
1649list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1650{
1651 PyObject *iterator;
1652 PyObject *item;
1653 listitem_T *li;
1654
1655 if (!(iterator = PyObject_GetIter(obj)))
1656 return -1;
1657
1658 while ((item = PyIter_Next(iterator)))
1659 {
1660 if (!(li = listitem_alloc()))
1661 {
1662 PyErr_NoMemory();
1663 Py_DECREF(item);
1664 Py_DECREF(iterator);
1665 return -1;
1666 }
1667 li->li_tv.v_lock = 0;
1668 li->li_tv.v_type = VAR_UNKNOWN;
1669
1670 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1671 {
1672 Py_DECREF(item);
1673 Py_DECREF(iterator);
1674 listitem_free(li);
1675 return -1;
1676 }
1677
1678 Py_DECREF(item);
1679
1680 list_append(l, li);
1681 }
1682
1683 Py_DECREF(iterator);
1684
1685 /* Iterator may have finished due to an exception */
1686 if (PyErr_Occurred())
1687 return -1;
1688
1689 return 0;
1690}
1691
1692 static PyObject *
1693ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1694{
1695 list_T *list;
1696 PyObject *obj = NULL;
1697
1698 if (kwargs)
1699 {
1700 PyErr_SetString(PyExc_TypeError,
1701 _("list constructor does not accept keyword arguments"));
1702 return NULL;
1703 }
1704
1705 if (!PyArg_ParseTuple(args, "|O", &obj))
1706 return NULL;
1707
1708 if (!(list = py_list_alloc()))
1709 return NULL;
1710
1711 if (obj)
1712 {
1713 PyObject *lookup_dict;
1714
1715 if (!(lookup_dict = PyDict_New()))
1716 {
1717 list_unref(list);
1718 return NULL;
1719 }
1720
1721 if (list_py_concat(list, obj, lookup_dict) == -1)
1722 {
1723 Py_DECREF(lookup_dict);
1724 list_unref(list);
1725 return NULL;
1726 }
1727
1728 Py_DECREF(lookup_dict);
1729 }
1730
1731 return ListNew(subtype, list);
1732}
1733
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001734 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001735ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001736{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001737 pyll_remove(&self->ref, &lastlist);
1738 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001739
1740 DESTRUCTOR_FINISH(self);
1741}
1742
Bram Moolenaardb913952012-06-29 12:54:53 +02001743 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001744ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001745{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001746 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001747}
1748
1749 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001750ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001751{
1752 listitem_T *li;
1753
Bram Moolenaard6e39182013-05-21 18:30:34 +02001754 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001755 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001756 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001757 return NULL;
1758 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001759 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001760 if (li == NULL)
1761 {
1762 PyErr_SetVim(_("internal error: failed to get vim list item"));
1763 return NULL;
1764 }
1765 return ConvertToPyObject(&li->li_tv);
1766}
1767
1768#define PROC_RANGE \
1769 if (last < 0) {\
1770 if (last < -size) \
1771 last = 0; \
1772 else \
1773 last += size; \
1774 } \
1775 if (first < 0) \
1776 first = 0; \
1777 if (first > size) \
1778 first = size; \
1779 if (last > size) \
1780 last = size;
1781
1782 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001783ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001784{
1785 PyInt i;
1786 PyInt size = ListLength(self);
1787 PyInt n;
1788 PyObject *list;
1789 int reversed = 0;
1790
1791 PROC_RANGE
1792 if (first >= last)
1793 first = last;
1794
1795 n = last-first;
1796 list = PyList_New(n);
1797 if (list == NULL)
1798 return NULL;
1799
1800 for (i = 0; i < n; ++i)
1801 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001802 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001803 if (item == NULL)
1804 {
1805 Py_DECREF(list);
1806 return NULL;
1807 }
1808
1809 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1810 {
1811 Py_DECREF(item);
1812 Py_DECREF(list);
1813 return NULL;
1814 }
1815 }
1816
1817 return list;
1818}
1819
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001820typedef struct
1821{
1822 listwatch_T lw;
1823 list_T *list;
1824} listiterinfo_T;
1825
1826 static void
1827ListIterDestruct(listiterinfo_T *lii)
1828{
1829 list_rem_watch(lii->list, &lii->lw);
1830 PyMem_Free(lii);
1831}
1832
1833 static PyObject *
1834ListIterNext(listiterinfo_T **lii)
1835{
1836 PyObject *r;
1837
1838 if (!((*lii)->lw.lw_item))
1839 return NULL;
1840
1841 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1842 return NULL;
1843
1844 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1845
1846 return r;
1847}
1848
1849 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001850ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001851{
1852 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001853 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001854
1855 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1856 {
1857 PyErr_NoMemory();
1858 return NULL;
1859 }
1860
1861 list_add_watch(l, &lii->lw);
1862 lii->lw.lw_item = l->lv_first;
1863 lii->list = l;
1864
1865 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001866 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1867 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001868}
1869
Bram Moolenaardb913952012-06-29 12:54:53 +02001870 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001871ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001872{
1873 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001874 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001875 listitem_T *li;
1876 Py_ssize_t length = ListLength(self);
1877
1878 if (l->lv_lock)
1879 {
1880 PyErr_SetVim(_("list is locked"));
1881 return -1;
1882 }
1883 if (index>length || (index==length && obj==NULL))
1884 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001885 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001886 return -1;
1887 }
1888
1889 if (obj == NULL)
1890 {
1891 li = list_find(l, (long) index);
1892 list_remove(l, li, li);
1893 clear_tv(&li->li_tv);
1894 vim_free(li);
1895 return 0;
1896 }
1897
1898 if (ConvertFromPyObject(obj, &tv) == -1)
1899 return -1;
1900
1901 if (index == length)
1902 {
1903 if (list_append_tv(l, &tv) == FAIL)
1904 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001905 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001906 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001907 return -1;
1908 }
1909 }
1910 else
1911 {
1912 li = list_find(l, (long) index);
1913 clear_tv(&li->li_tv);
1914 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001915 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001916 }
1917 return 0;
1918}
1919
1920 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001921ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001922{
1923 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001924 PyObject *iterator;
1925 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02001926 listitem_T *li;
1927 listitem_T *next;
1928 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001929 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001930 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02001931
1932 if (l->lv_lock)
1933 {
1934 PyErr_SetVim(_("list is locked"));
1935 return -1;
1936 }
1937
1938 PROC_RANGE
1939
1940 if (first == size)
1941 li = NULL;
1942 else
1943 {
1944 li = list_find(l, (long) first);
1945 if (li == NULL)
1946 {
1947 PyErr_SetVim(_("internal error: no vim list item"));
1948 return -1;
1949 }
1950 if (last > first)
1951 {
1952 i = last - first;
1953 while (i-- && li != NULL)
1954 {
1955 next = li->li_next;
1956 listitem_remove(l, li);
1957 li = next;
1958 }
1959 }
1960 }
1961
1962 if (obj == NULL)
1963 return 0;
1964
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001965 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001966 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001967
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001968 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001969 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001970 if (ConvertFromPyObject(item, &v) == -1)
1971 {
1972 Py_DECREF(iterator);
1973 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001974 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001975 }
1976 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001977 if (list_insert_tv(l, &v, li) == FAIL)
1978 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001979 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001980 PyErr_SetVim(_("internal error: failed to add item to list"));
1981 return -1;
1982 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001983 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001984 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001985 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02001986 return 0;
1987}
1988
1989 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001990ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001991{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001992 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001993 PyObject *lookup_dict;
1994
1995 if (l->lv_lock)
1996 {
1997 PyErr_SetVim(_("list is locked"));
1998 return NULL;
1999 }
2000
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002001 if (!(lookup_dict = PyDict_New()))
2002 return NULL;
2003
Bram Moolenaardb913952012-06-29 12:54:53 +02002004 if (list_py_concat(l, obj, lookup_dict) == -1)
2005 {
2006 Py_DECREF(lookup_dict);
2007 return NULL;
2008 }
2009 Py_DECREF(lookup_dict);
2010
2011 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002012 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002013}
2014
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002015static char *ListAttrs[] = {
2016 "locked",
2017 NULL
2018};
2019
2020 static PyObject *
2021ListDir(PyObject *self)
2022{
2023 return ObjectDir(self, ListAttrs);
2024}
2025
Bram Moolenaar66b79852012-09-21 14:00:35 +02002026 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002027ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002028{
2029 if (val == NULL)
2030 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002031 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002032 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002033 return -1;
2034 }
2035
2036 if (strcmp(name, "locked") == 0)
2037 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002038 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002039 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002040 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002041 return -1;
2042 }
2043 else
2044 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002045 int istrue = PyObject_IsTrue(val);
2046 if (istrue == -1)
2047 return -1;
2048 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002049 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002050 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002051 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002052 }
2053 return 0;
2054 }
2055 else
2056 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002057 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002058 return -1;
2059 }
2060}
2061
Bram Moolenaardb913952012-06-29 12:54:53 +02002062static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002063 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2064 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2065 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002066};
2067
2068typedef struct
2069{
2070 PyObject_HEAD
2071 char_u *name;
2072} FunctionObject;
2073
2074static PyTypeObject FunctionType;
2075
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002076#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2077
Bram Moolenaardb913952012-06-29 12:54:53 +02002078 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002079FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002080{
2081 FunctionObject *self;
2082
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002083 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2084
Bram Moolenaardb913952012-06-29 12:54:53 +02002085 if (self == NULL)
2086 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002087
2088 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002089 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002090 if (!translated_function_exists(name))
2091 {
2092 PyErr_SetString(PyExc_ValueError,
2093 _("unnamed function does not exist"));
2094 return NULL;
2095 }
2096 self->name = vim_strsave(name);
2097 func_ref(self->name);
2098 }
2099 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002100 if ((self->name = get_expanded_name(name,
2101 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2102 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002103 {
Bram Moolenaar018acca2013-05-30 13:37:28 +02002104 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2105 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002106 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002107
2108 return (PyObject *)(self);
2109}
2110
2111 static PyObject *
2112FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2113{
2114 PyObject *self;
2115 char_u *name;
2116
2117 if (kwargs)
2118 {
2119 PyErr_SetString(PyExc_TypeError,
2120 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002121 return NULL;
2122 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002123
2124 if (!PyArg_ParseTuple(args, "s", &name))
2125 return NULL;
2126
2127 self = FunctionNew(subtype, name);
2128
2129 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002130}
2131
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002132 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002133FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002134{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002135 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002136 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002137
2138 DESTRUCTOR_FINISH(self);
2139}
2140
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002141static char *FunctionAttrs[] = {
2142 "softspace",
2143 NULL
2144};
2145
2146 static PyObject *
2147FunctionDir(PyObject *self)
2148{
2149 return ObjectDir(self, FunctionAttrs);
2150}
2151
Bram Moolenaardb913952012-06-29 12:54:53 +02002152 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002153FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002154{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002155 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002156 typval_T args;
2157 typval_T selfdicttv;
2158 typval_T rettv;
2159 dict_T *selfdict = NULL;
2160 PyObject *selfdictObject;
2161 PyObject *result;
2162 int error;
2163
2164 if (ConvertFromPyObject(argsObject, &args) == -1)
2165 return NULL;
2166
2167 if (kwargs != NULL)
2168 {
2169 selfdictObject = PyDict_GetItemString(kwargs, "self");
2170 if (selfdictObject != NULL)
2171 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002172 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002173 {
2174 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002175 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002176 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002177 selfdict = selfdicttv.vval.v_dict;
2178 }
2179 }
2180
Bram Moolenaar71700b82013-05-15 17:49:05 +02002181 Py_BEGIN_ALLOW_THREADS
2182 Python_Lock_Vim();
2183
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002184 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002185 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002186
2187 Python_Release_Vim();
2188 Py_END_ALLOW_THREADS
2189
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002190 if (VimTryEnd())
2191 result = NULL;
2192 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002193 {
2194 result = NULL;
2195 PyErr_SetVim(_("failed to run function"));
2196 }
2197 else
2198 result = ConvertToPyObject(&rettv);
2199
Bram Moolenaardb913952012-06-29 12:54:53 +02002200 clear_tv(&args);
2201 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002202 if (selfdict != NULL)
2203 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002204
2205 return result;
2206}
2207
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002208 static PyObject *
2209FunctionRepr(FunctionObject *self)
2210{
2211 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2212}
2213
Bram Moolenaardb913952012-06-29 12:54:53 +02002214static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002215 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2216 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002217};
2218
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002219/*
2220 * Options object
2221 */
2222
2223static PyTypeObject OptionsType;
2224
2225typedef int (*checkfun)(void *);
2226
2227typedef struct
2228{
2229 PyObject_HEAD
2230 int opt_type;
2231 void *from;
2232 checkfun Check;
2233 PyObject *fromObj;
2234} OptionsObject;
2235
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002236 static int
2237dummy_check(void *arg UNUSED)
2238{
2239 return 0;
2240}
2241
2242 static PyObject *
2243OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2244{
2245 OptionsObject *self;
2246
Bram Moolenaar774267b2013-05-21 20:51:59 +02002247 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002248 if (self == NULL)
2249 return NULL;
2250
2251 self->opt_type = opt_type;
2252 self->from = from;
2253 self->Check = Check;
2254 self->fromObj = fromObj;
2255 if (fromObj)
2256 Py_INCREF(fromObj);
2257
2258 return (PyObject *)(self);
2259}
2260
2261 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002262OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002263{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002264 PyObject_GC_UnTrack((void *)(self));
2265 Py_XDECREF(self->fromObj);
2266 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002267}
2268
2269 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002270OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002271{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002272 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002273 return 0;
2274}
2275
2276 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002277OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002278{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002279 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002280 return 0;
2281}
2282
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002283 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002284OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002285{
2286 char_u *key;
2287 int flags;
2288 long numval;
2289 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002290 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002291
Bram Moolenaard6e39182013-05-21 18:30:34 +02002292 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002293 return NULL;
2294
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002295 if (!(key = StringToChars(keyObject, &todecref)))
2296 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002297
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002298 if (*key == NUL)
2299 {
2300 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002301 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002302 return NULL;
2303 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002304
2305 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002306 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002307
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002308 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002309
2310 if (flags == 0)
2311 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002312 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002313 return NULL;
2314 }
2315
2316 if (flags & SOPT_UNSET)
2317 {
2318 Py_INCREF(Py_None);
2319 return Py_None;
2320 }
2321 else if (flags & SOPT_BOOL)
2322 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002323 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002324 r = numval ? Py_True : Py_False;
2325 Py_INCREF(r);
2326 return r;
2327 }
2328 else if (flags & SOPT_NUM)
2329 return PyInt_FromLong(numval);
2330 else if (flags & SOPT_STRING)
2331 {
2332 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002333 {
2334 PyObject *r = PyBytes_FromString((char *) stringval);
2335 vim_free(stringval);
2336 return r;
2337 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002338 else
2339 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002340 PyErr_SetString(PyExc_RuntimeError,
2341 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002342 return NULL;
2343 }
2344 }
2345 else
2346 {
2347 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2348 return NULL;
2349 }
2350}
2351
2352 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002353set_option_value_err(key, numval, stringval, opt_flags)
2354 char_u *key;
2355 int numval;
2356 char_u *stringval;
2357 int opt_flags;
2358{
2359 char_u *errmsg;
2360
2361 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2362 {
2363 if (VimTryEnd())
2364 return FAIL;
2365 PyErr_SetVim((char *)errmsg);
2366 return FAIL;
2367 }
2368 return OK;
2369}
2370
2371 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002372set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2373 char_u *key;
2374 int numval;
2375 char_u *stringval;
2376 int opt_flags;
2377 int opt_type;
2378 void *from;
2379{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002380 win_T *save_curwin = NULL;
2381 tabpage_T *save_curtab = NULL;
2382 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002383 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002384
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002385 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002386 switch (opt_type)
2387 {
2388 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002389 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2390 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002391 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002392 if (VimTryEnd())
2393 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002394 PyErr_SetVim("Problem while switching windows.");
2395 return -1;
2396 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002397 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002398 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002399 if (r == FAIL)
2400 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002401 break;
2402 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002403 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002404 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002405 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002406 if (r == FAIL)
2407 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002408 break;
2409 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002410 r = set_option_value_err(key, numval, stringval, opt_flags);
2411 if (r == FAIL)
2412 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002413 break;
2414 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002415 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002416}
2417
2418 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002419OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002420{
2421 char_u *key;
2422 int flags;
2423 int opt_flags;
2424 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002425 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002426
Bram Moolenaard6e39182013-05-21 18:30:34 +02002427 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002428 return -1;
2429
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002430 if (!(key = StringToChars(keyObject, &todecref)))
2431 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002432
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002433 if (*key == NUL)
2434 {
2435 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002436 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002437 return -1;
2438 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002439
2440 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002441 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002442
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002443 if (flags == 0)
2444 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002445 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002446 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002447 return -1;
2448 }
2449
2450 if (valObject == NULL)
2451 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002452 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002453 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002454 PyErr_SetString(PyExc_ValueError,
2455 _("unable to unset global option"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002456 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002457 return -1;
2458 }
2459 else if (!(flags & SOPT_GLOBAL))
2460 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002461 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2462 "without global value"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002463 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002464 return -1;
2465 }
2466 else
2467 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002468 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002469 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002470 return 0;
2471 }
2472 }
2473
Bram Moolenaard6e39182013-05-21 18:30:34 +02002474 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002475
2476 if (flags & SOPT_BOOL)
2477 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002478 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002479
Bram Moolenaarb983f752013-05-15 16:11:50 +02002480 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002481 r = -1;
2482 else
2483 r = set_option_value_for(key, istrue, NULL,
2484 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002485 }
2486 else if (flags & SOPT_NUM)
2487 {
2488 int val;
2489
2490#if PY_MAJOR_VERSION < 3
2491 if (PyInt_Check(valObject))
2492 val = PyInt_AsLong(valObject);
2493 else
2494#endif
2495 if (PyLong_Check(valObject))
2496 val = PyLong_AsLong(valObject);
2497 else
2498 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002499 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002500 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002501 return -1;
2502 }
2503
2504 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002505 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002506 }
2507 else
2508 {
2509 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002510 PyObject *todecref;
2511
2512 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002513 r = set_option_value_for(key, 0, val, opt_flags,
2514 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002515 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002516 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002517 }
2518
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002519 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002520
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002521 return r;
2522}
2523
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002524static PyMappingMethods OptionsAsMapping = {
2525 (lenfunc) NULL,
2526 (binaryfunc) OptionsItem,
2527 (objobjargproc) OptionsAssItem,
2528};
2529
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002530/* Tabpage object
2531 */
2532
2533typedef struct
2534{
2535 PyObject_HEAD
2536 tabpage_T *tab;
2537} TabPageObject;
2538
2539static PyObject *WinListNew(TabPageObject *tabObject);
2540
2541static PyTypeObject TabPageType;
2542
2543 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002544CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002545{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002546 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002547 {
2548 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2549 return -1;
2550 }
2551
2552 return 0;
2553}
2554
2555 static PyObject *
2556TabPageNew(tabpage_T *tab)
2557{
2558 TabPageObject *self;
2559
2560 if (TAB_PYTHON_REF(tab))
2561 {
2562 self = TAB_PYTHON_REF(tab);
2563 Py_INCREF(self);
2564 }
2565 else
2566 {
2567 self = PyObject_NEW(TabPageObject, &TabPageType);
2568 if (self == NULL)
2569 return NULL;
2570 self->tab = tab;
2571 TAB_PYTHON_REF(tab) = self;
2572 }
2573
2574 return (PyObject *)(self);
2575}
2576
2577 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002578TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002579{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002580 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2581 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002582
2583 DESTRUCTOR_FINISH(self);
2584}
2585
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002586static char *TabPageAttrs[] = {
2587 "windows", "number", "vars", "window", "valid",
2588 NULL
2589};
2590
2591 static PyObject *
2592TabPageDir(PyObject *self)
2593{
2594 return ObjectDir(self, TabPageAttrs);
2595}
2596
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002597 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002598TabPageAttrValid(TabPageObject *self, char *name)
2599{
2600 PyObject *r;
2601
2602 if (strcmp(name, "valid") != 0)
2603 return NULL;
2604
2605 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2606 Py_INCREF(r);
2607 return r;
2608}
2609
2610 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002611TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002612{
2613 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002614 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002615 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002616 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002617 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002618 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002619 else if (strcmp(name, "window") == 0)
2620 {
2621 /* For current tab window.c does not bother to set or update tp_curwin
2622 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002623 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002624 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002625 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002626 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002627 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002628 else if (strcmp(name, "__members__") == 0)
2629 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002630 return NULL;
2631}
2632
2633 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002634TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002635{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002636 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002637 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002638 else
2639 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002640 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002641
2642 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002643 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2644 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002645 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002646 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002647 }
2648}
2649
2650static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002651 /* name, function, calling, documentation */
2652 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2653 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002654};
2655
2656/*
2657 * Window list object
2658 */
2659
2660static PyTypeObject TabListType;
2661static PySequenceMethods TabListAsSeq;
2662
2663typedef struct
2664{
2665 PyObject_HEAD
2666} TabListObject;
2667
2668 static PyInt
2669TabListLength(PyObject *self UNUSED)
2670{
2671 tabpage_T *tp = first_tabpage;
2672 PyInt n = 0;
2673
2674 while (tp != NULL)
2675 {
2676 ++n;
2677 tp = tp->tp_next;
2678 }
2679
2680 return n;
2681}
2682
2683 static PyObject *
2684TabListItem(PyObject *self UNUSED, PyInt n)
2685{
2686 tabpage_T *tp;
2687
2688 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2689 if (n == 0)
2690 return TabPageNew(tp);
2691
2692 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2693 return NULL;
2694}
2695
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002696/* Window object
2697 */
2698
2699typedef struct
2700{
2701 PyObject_HEAD
2702 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002703 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002704} WindowObject;
2705
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002706static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002707
2708 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002709CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002710{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002711 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002712 {
2713 PyErr_SetVim(_("attempt to refer to deleted window"));
2714 return -1;
2715 }
2716
2717 return 0;
2718}
2719
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002720 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002721WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002722{
2723 /* We need to handle deletion of windows underneath us.
2724 * If we add a "w_python*_ref" field to the win_T structure,
2725 * then we can get at it in win_free() in vim. We then
2726 * need to create only ONE Python object per window - if
2727 * we try to create a second, just INCREF the existing one
2728 * and return it. The (single) Python object referring to
2729 * the window is stored in "w_python*_ref".
2730 * On a win_free() we set the Python object's win_T* field
2731 * to an invalid value. We trap all uses of a window
2732 * object, and reject them if the win_T* field is invalid.
2733 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002734 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002735 * w_python_ref and w_python3_ref fields respectively.
2736 */
2737
2738 WindowObject *self;
2739
2740 if (WIN_PYTHON_REF(win))
2741 {
2742 self = WIN_PYTHON_REF(win);
2743 Py_INCREF(self);
2744 }
2745 else
2746 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002747 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002748 if (self == NULL)
2749 return NULL;
2750 self->win = win;
2751 WIN_PYTHON_REF(win) = self;
2752 }
2753
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002754 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2755
Bram Moolenaar971db462013-05-12 18:44:48 +02002756 return (PyObject *)(self);
2757}
2758
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002759 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002760WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002761{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002762 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002763 if (self->win && self->win != INVALID_WINDOW_VALUE)
2764 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002765 Py_XDECREF(((PyObject *)(self->tabObject)));
2766 PyObject_GC_Del((void *)(self));
2767}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002768
Bram Moolenaar774267b2013-05-21 20:51:59 +02002769 static int
2770WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2771{
2772 Py_VISIT(((PyObject *)(self->tabObject)));
2773 return 0;
2774}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002775
Bram Moolenaar774267b2013-05-21 20:51:59 +02002776 static int
2777WindowClear(WindowObject *self)
2778{
2779 Py_CLEAR(self->tabObject);
2780 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002781}
2782
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002783 static win_T *
2784get_firstwin(TabPageObject *tabObject)
2785{
2786 if (tabObject)
2787 {
2788 if (CheckTabPage(tabObject))
2789 return NULL;
2790 /* For current tab window.c does not bother to set or update tp_firstwin
2791 */
2792 else if (tabObject->tab == curtab)
2793 return firstwin;
2794 else
2795 return tabObject->tab->tp_firstwin;
2796 }
2797 else
2798 return firstwin;
2799}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002800static char *WindowAttrs[] = {
2801 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2802 "tabpage", "valid",
2803 NULL
2804};
2805
2806 static PyObject *
2807WindowDir(PyObject *self)
2808{
2809 return ObjectDir(self, WindowAttrs);
2810}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002811
Bram Moolenaar971db462013-05-12 18:44:48 +02002812 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002813WindowAttrValid(WindowObject *self, char *name)
2814{
2815 PyObject *r;
2816
2817 if (strcmp(name, "valid") != 0)
2818 return NULL;
2819
2820 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2821 Py_INCREF(r);
2822 return r;
2823}
2824
2825 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002826WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002827{
2828 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002829 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002830 else if (strcmp(name, "cursor") == 0)
2831 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002832 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002833
2834 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2835 }
2836 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002837 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002838#ifdef FEAT_WINDOWS
2839 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002840 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002841#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002842#ifdef FEAT_VERTSPLIT
2843 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002844 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002845 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002846 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002847#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002848 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002849 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002850 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002851 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2852 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002853 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002854 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002855 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002856 return NULL;
2857 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002858 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002859 }
2860 else if (strcmp(name, "tabpage") == 0)
2861 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002862 Py_INCREF(self->tabObject);
2863 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002864 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002865 else if (strcmp(name, "__members__") == 0)
2866 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002867 else
2868 return NULL;
2869}
2870
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002871 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002872WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002873{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002874 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002875 return -1;
2876
2877 if (strcmp(name, "buffer") == 0)
2878 {
2879 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2880 return -1;
2881 }
2882 else if (strcmp(name, "cursor") == 0)
2883 {
2884 long lnum;
2885 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002886
2887 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2888 return -1;
2889
Bram Moolenaard6e39182013-05-21 18:30:34 +02002890 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002891 {
2892 PyErr_SetVim(_("cursor position outside buffer"));
2893 return -1;
2894 }
2895
2896 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002897 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002898 return -1;
2899
Bram Moolenaard6e39182013-05-21 18:30:34 +02002900 self->win->w_cursor.lnum = lnum;
2901 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002902#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002903 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002904#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002905 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002906 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002907
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002908 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002909 return 0;
2910 }
2911 else if (strcmp(name, "height") == 0)
2912 {
2913 int height;
2914 win_T *savewin;
2915
2916 if (!PyArg_Parse(val, "i", &height))
2917 return -1;
2918
2919#ifdef FEAT_GUI
2920 need_mouse_correct = TRUE;
2921#endif
2922 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002923 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002924
2925 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002926 win_setheight(height);
2927 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002928 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002929 return -1;
2930
2931 return 0;
2932 }
2933#ifdef FEAT_VERTSPLIT
2934 else if (strcmp(name, "width") == 0)
2935 {
2936 int width;
2937 win_T *savewin;
2938
2939 if (!PyArg_Parse(val, "i", &width))
2940 return -1;
2941
2942#ifdef FEAT_GUI
2943 need_mouse_correct = TRUE;
2944#endif
2945 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002946 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002947
2948 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002949 win_setwidth(width);
2950 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002951 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002952 return -1;
2953
2954 return 0;
2955 }
2956#endif
2957 else
2958 {
2959 PyErr_SetString(PyExc_AttributeError, name);
2960 return -1;
2961 }
2962}
2963
2964 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002965WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002966{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002967 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002968 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002969 else
2970 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002971 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002972
Bram Moolenaar6d216452013-05-12 19:00:41 +02002973 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002974 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002975 (self));
2976 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002977 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002978 }
2979}
2980
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002981static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002982 /* name, function, calling, documentation */
2983 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2984 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002985};
2986
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002987/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002988 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002989 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002990
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002991static PyTypeObject WinListType;
2992static PySequenceMethods WinListAsSeq;
2993
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002994typedef struct
2995{
2996 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002997 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002998} WinListObject;
2999
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003000 static PyObject *
3001WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003002{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003003 WinListObject *self;
3004
3005 self = PyObject_NEW(WinListObject, &WinListType);
3006 self->tabObject = tabObject;
3007 Py_INCREF(tabObject);
3008
3009 return (PyObject *)(self);
3010}
3011
3012 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003013WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003014{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003015 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003016
3017 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003018 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003019 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003020 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003021
3022 DESTRUCTOR_FINISH(self);
3023}
3024
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003025 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003026WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003027{
3028 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003029 PyInt n = 0;
3030
Bram Moolenaard6e39182013-05-21 18:30:34 +02003031 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003032 return -1;
3033
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003034 while (w != NULL)
3035 {
3036 ++n;
3037 w = W_NEXT(w);
3038 }
3039
3040 return n;
3041}
3042
3043 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003044WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003045{
3046 win_T *w;
3047
Bram Moolenaard6e39182013-05-21 18:30:34 +02003048 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003049 return NULL;
3050
3051 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003052 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003053 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003054
3055 PyErr_SetString(PyExc_IndexError, _("no such window"));
3056 return NULL;
3057}
3058
3059/* Convert a Python string into a Vim line.
3060 *
3061 * The result is in allocated memory. All internal nulls are replaced by
3062 * newline characters. It is an error for the string to contain newline
3063 * characters.
3064 *
3065 * On errors, the Python exception data is set, and NULL is returned.
3066 */
3067 static char *
3068StringToLine(PyObject *obj)
3069{
3070 const char *str;
3071 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003072 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003073 PyInt len;
3074 PyInt i;
3075 char *p;
3076
3077 if (obj == NULL || !PyString_Check(obj))
3078 {
3079 PyErr_BadArgument();
3080 return NULL;
3081 }
3082
Bram Moolenaar19e60942011-06-19 00:27:51 +02003083 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
3084 str = PyString_AsString(bytes);
3085 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003086
3087 /*
3088 * Error checking: String must not contain newlines, as we
3089 * are replacing a single line, and we must replace it with
3090 * a single line.
3091 * A trailing newline is removed, so that append(f.readlines()) works.
3092 */
3093 p = memchr(str, '\n', len);
3094 if (p != NULL)
3095 {
3096 if (p == str + len - 1)
3097 --len;
3098 else
3099 {
3100 PyErr_SetVim(_("string cannot contain newlines"));
3101 return NULL;
3102 }
3103 }
3104
3105 /* Create a copy of the string, with internal nulls replaced by
3106 * newline characters, as is the vim convention.
3107 */
3108 save = (char *)alloc((unsigned)(len+1));
3109 if (save == NULL)
3110 {
3111 PyErr_NoMemory();
3112 return NULL;
3113 }
3114
3115 for (i = 0; i < len; ++i)
3116 {
3117 if (str[i] == '\0')
3118 save[i] = '\n';
3119 else
3120 save[i] = str[i];
3121 }
3122
3123 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003124 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003125
3126 return save;
3127}
3128
3129/* Get a line from the specified buffer. The line number is
3130 * in Vim format (1-based). The line is returned as a Python
3131 * string object.
3132 */
3133 static PyObject *
3134GetBufferLine(buf_T *buf, PyInt n)
3135{
3136 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3137}
3138
3139
3140/* Get a list of lines from the specified buffer. The line numbers
3141 * are in Vim format (1-based). The range is from lo up to, but not
3142 * including, hi. The list is returned as a Python list of string objects.
3143 */
3144 static PyObject *
3145GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3146{
3147 PyInt i;
3148 PyInt n = hi - lo;
3149 PyObject *list = PyList_New(n);
3150
3151 if (list == NULL)
3152 return NULL;
3153
3154 for (i = 0; i < n; ++i)
3155 {
3156 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3157
3158 /* Error check - was the Python string creation OK? */
3159 if (str == NULL)
3160 {
3161 Py_DECREF(list);
3162 return NULL;
3163 }
3164
3165 /* Set the list item */
3166 if (PyList_SetItem(list, i, str))
3167 {
3168 Py_DECREF(str);
3169 Py_DECREF(list);
3170 return NULL;
3171 }
3172 }
3173
3174 /* The ownership of the Python list is passed to the caller (ie,
3175 * the caller should Py_DECREF() the object when it is finished
3176 * with it).
3177 */
3178
3179 return list;
3180}
3181
3182/*
3183 * Check if deleting lines made the cursor position invalid.
3184 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3185 * deleted).
3186 */
3187 static void
3188py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3189{
3190 if (curwin->w_cursor.lnum >= lo)
3191 {
3192 /* Adjust the cursor position if it's in/after the changed
3193 * lines. */
3194 if (curwin->w_cursor.lnum >= hi)
3195 {
3196 curwin->w_cursor.lnum += extra;
3197 check_cursor_col();
3198 }
3199 else if (extra < 0)
3200 {
3201 curwin->w_cursor.lnum = lo;
3202 check_cursor();
3203 }
3204 else
3205 check_cursor_col();
3206 changed_cline_bef_curs();
3207 }
3208 invalidate_botline();
3209}
3210
Bram Moolenaar19e60942011-06-19 00:27:51 +02003211/*
3212 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003213 * in Vim format (1-based). The replacement line is given as
3214 * a Python string object. The object is checked for validity
3215 * and correct format. Errors are returned as a value of FAIL.
3216 * The return value is OK on success.
3217 * If OK is returned and len_change is not NULL, *len_change
3218 * is set to the change in the buffer length.
3219 */
3220 static int
3221SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3222{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003223 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003224 * There are three cases:
3225 * 1. NULL, or None - this is a deletion.
3226 * 2. A string - this is a replacement.
3227 * 3. Anything else - this is an error.
3228 */
3229 if (line == Py_None || line == NULL)
3230 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003231 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003232
3233 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003234 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003235
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003236 VimTryStart();
3237
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003238 if (u_savedel((linenr_T)n, 1L) == FAIL)
3239 PyErr_SetVim(_("cannot save undo information"));
3240 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3241 PyErr_SetVim(_("cannot delete line"));
3242 else
3243 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003244 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003245 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3246 deleted_lines_mark((linenr_T)n, 1L);
3247 }
3248
Bram Moolenaar105bc352013-05-17 16:03:57 +02003249 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003250
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003251 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003252 return FAIL;
3253
3254 if (len_change)
3255 *len_change = -1;
3256
3257 return OK;
3258 }
3259 else if (PyString_Check(line))
3260 {
3261 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003262 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003263
3264 if (save == NULL)
3265 return FAIL;
3266
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003267 VimTryStart();
3268
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003269 /* We do not need to free "save" if ml_replace() consumes it. */
3270 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003271 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003272
3273 if (u_savesub((linenr_T)n) == FAIL)
3274 {
3275 PyErr_SetVim(_("cannot save undo information"));
3276 vim_free(save);
3277 }
3278 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3279 {
3280 PyErr_SetVim(_("cannot replace line"));
3281 vim_free(save);
3282 }
3283 else
3284 changed_bytes((linenr_T)n, 0);
3285
Bram Moolenaar105bc352013-05-17 16:03:57 +02003286 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003287
3288 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003289 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003290 check_cursor_col();
3291
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003292 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003293 return FAIL;
3294
3295 if (len_change)
3296 *len_change = 0;
3297
3298 return OK;
3299 }
3300 else
3301 {
3302 PyErr_BadArgument();
3303 return FAIL;
3304 }
3305}
3306
Bram Moolenaar19e60942011-06-19 00:27:51 +02003307/* Replace a range of lines in the specified buffer. The line numbers are in
3308 * Vim format (1-based). The range is from lo up to, but not including, hi.
3309 * The replacement lines are given as a Python list of string objects. The
3310 * list is checked for validity and correct format. Errors are returned as a
3311 * value of FAIL. The return value is OK on success.
3312 * If OK is returned and len_change is not NULL, *len_change
3313 * is set to the change in the buffer length.
3314 */
3315 static int
3316SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3317{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003318 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003319 * There are three cases:
3320 * 1. NULL, or None - this is a deletion.
3321 * 2. A list - this is a replacement.
3322 * 3. Anything else - this is an error.
3323 */
3324 if (list == Py_None || list == NULL)
3325 {
3326 PyInt i;
3327 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003328 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003329
3330 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003331 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003332 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003333
3334 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3335 PyErr_SetVim(_("cannot save undo information"));
3336 else
3337 {
3338 for (i = 0; i < n; ++i)
3339 {
3340 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3341 {
3342 PyErr_SetVim(_("cannot delete line"));
3343 break;
3344 }
3345 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003346 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003347 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3348 deleted_lines_mark((linenr_T)lo, (long)i);
3349 }
3350
Bram Moolenaar105bc352013-05-17 16:03:57 +02003351 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003352
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003353 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003354 return FAIL;
3355
3356 if (len_change)
3357 *len_change = -n;
3358
3359 return OK;
3360 }
3361 else if (PyList_Check(list))
3362 {
3363 PyInt i;
3364 PyInt new_len = PyList_Size(list);
3365 PyInt old_len = hi - lo;
3366 PyInt extra = 0; /* lines added to text, can be negative */
3367 char **array;
3368 buf_T *savebuf;
3369
3370 if (new_len == 0) /* avoid allocating zero bytes */
3371 array = NULL;
3372 else
3373 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003374 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003375 if (array == NULL)
3376 {
3377 PyErr_NoMemory();
3378 return FAIL;
3379 }
3380 }
3381
3382 for (i = 0; i < new_len; ++i)
3383 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003384 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003385
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003386 if (!(line = PyList_GetItem(list, i)) ||
3387 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003388 {
3389 while (i)
3390 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003391 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003392 return FAIL;
3393 }
3394 }
3395
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003396 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003397 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003398
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003399 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003400 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003401
3402 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3403 PyErr_SetVim(_("cannot save undo information"));
3404
3405 /* If the size of the range is reducing (ie, new_len < old_len) we
3406 * need to delete some old_len. We do this at the start, by
3407 * repeatedly deleting line "lo".
3408 */
3409 if (!PyErr_Occurred())
3410 {
3411 for (i = 0; i < old_len - new_len; ++i)
3412 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3413 {
3414 PyErr_SetVim(_("cannot delete line"));
3415 break;
3416 }
3417 extra -= i;
3418 }
3419
3420 /* For as long as possible, replace the existing old_len with the
3421 * new old_len. This is a more efficient operation, as it requires
3422 * less memory allocation and freeing.
3423 */
3424 if (!PyErr_Occurred())
3425 {
3426 for (i = 0; i < old_len && i < new_len; ++i)
3427 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3428 == FAIL)
3429 {
3430 PyErr_SetVim(_("cannot replace line"));
3431 break;
3432 }
3433 }
3434 else
3435 i = 0;
3436
3437 /* Now we may need to insert the remaining new old_len. If we do, we
3438 * must free the strings as we finish with them (we can't pass the
3439 * responsibility to vim in this case).
3440 */
3441 if (!PyErr_Occurred())
3442 {
3443 while (i < new_len)
3444 {
3445 if (ml_append((linenr_T)(lo + i - 1),
3446 (char_u *)array[i], 0, FALSE) == FAIL)
3447 {
3448 PyErr_SetVim(_("cannot insert line"));
3449 break;
3450 }
3451 vim_free(array[i]);
3452 ++i;
3453 ++extra;
3454 }
3455 }
3456
3457 /* Free any left-over old_len, as a result of an error */
3458 while (i < new_len)
3459 {
3460 vim_free(array[i]);
3461 ++i;
3462 }
3463
3464 /* Free the array of old_len. All of its contents have now
3465 * been dealt with (either freed, or the responsibility passed
3466 * to vim.
3467 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003468 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003469
3470 /* Adjust marks. Invalidate any which lie in the
3471 * changed range, and move any in the remainder of the buffer.
3472 */
3473 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3474 (long)MAXLNUM, (long)extra);
3475 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3476
Bram Moolenaar105bc352013-05-17 16:03:57 +02003477 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003478 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3479
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003480 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003481 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003482
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003483 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003484 return FAIL;
3485
3486 if (len_change)
3487 *len_change = new_len - old_len;
3488
3489 return OK;
3490 }
3491 else
3492 {
3493 PyErr_BadArgument();
3494 return FAIL;
3495 }
3496}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003497
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003498/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003499 * The line number is in Vim format (1-based). The lines to be inserted are
3500 * given as a Python list of string objects or as a single string. The lines
3501 * to be added are checked for validity and correct format. Errors are
3502 * returned as a value of FAIL. The return value is OK on success.
3503 * If OK is returned and len_change is not NULL, *len_change
3504 * is set to the change in the buffer length.
3505 */
3506 static int
3507InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3508{
3509 /* First of all, we check the type of the supplied Python object.
3510 * It must be a string or a list, or the call is in error.
3511 */
3512 if (PyString_Check(lines))
3513 {
3514 char *str = StringToLine(lines);
3515 buf_T *savebuf;
3516
3517 if (str == NULL)
3518 return FAIL;
3519
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003520 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003521 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003522 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003523
3524 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3525 PyErr_SetVim(_("cannot save undo information"));
3526 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3527 PyErr_SetVim(_("cannot insert line"));
3528 else
3529 appended_lines_mark((linenr_T)n, 1L);
3530
3531 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003532 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003533 update_screen(VALID);
3534
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003535 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003536 return FAIL;
3537
3538 if (len_change)
3539 *len_change = 1;
3540
3541 return OK;
3542 }
3543 else if (PyList_Check(lines))
3544 {
3545 PyInt i;
3546 PyInt size = PyList_Size(lines);
3547 char **array;
3548 buf_T *savebuf;
3549
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003550 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003551 if (array == NULL)
3552 {
3553 PyErr_NoMemory();
3554 return FAIL;
3555 }
3556
3557 for (i = 0; i < size; ++i)
3558 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003559 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003560
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003561 if (!(line = PyList_GetItem(lines, i)) ||
3562 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003563 {
3564 while (i)
3565 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003566 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003567 return FAIL;
3568 }
3569 }
3570
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003571 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003572 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003573 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003574
3575 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3576 PyErr_SetVim(_("cannot save undo information"));
3577 else
3578 {
3579 for (i = 0; i < size; ++i)
3580 {
3581 if (ml_append((linenr_T)(n + i),
3582 (char_u *)array[i], 0, FALSE) == FAIL)
3583 {
3584 PyErr_SetVim(_("cannot insert line"));
3585
3586 /* Free the rest of the lines */
3587 while (i < size)
3588 vim_free(array[i++]);
3589
3590 break;
3591 }
3592 vim_free(array[i]);
3593 }
3594 if (i > 0)
3595 appended_lines_mark((linenr_T)n, (long)i);
3596 }
3597
3598 /* Free the array of lines. All of its contents have now
3599 * been freed.
3600 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003601 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003602
Bram Moolenaar105bc352013-05-17 16:03:57 +02003603 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003604 update_screen(VALID);
3605
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003606 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003607 return FAIL;
3608
3609 if (len_change)
3610 *len_change = size;
3611
3612 return OK;
3613 }
3614 else
3615 {
3616 PyErr_BadArgument();
3617 return FAIL;
3618 }
3619}
3620
3621/*
3622 * Common routines for buffers and line ranges
3623 * -------------------------------------------
3624 */
3625
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003626typedef struct
3627{
3628 PyObject_HEAD
3629 buf_T *buf;
3630} BufferObject;
3631
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003632 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003633CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003634{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003635 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003636 {
3637 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3638 return -1;
3639 }
3640
3641 return 0;
3642}
3643
3644 static PyObject *
3645RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3646{
3647 if (CheckBuffer(self))
3648 return NULL;
3649
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003650 if (end == -1)
3651 end = self->buf->b_ml.ml_line_count;
3652
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003653 if (n < 0)
3654 n += end - start + 1;
3655
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003656 if (n < 0 || n > end - start)
3657 {
3658 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3659 return NULL;
3660 }
3661
3662 return GetBufferLine(self->buf, n+start);
3663}
3664
3665 static PyObject *
3666RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3667{
3668 PyInt size;
3669
3670 if (CheckBuffer(self))
3671 return NULL;
3672
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003673 if (end == -1)
3674 end = self->buf->b_ml.ml_line_count;
3675
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003676 size = end - start + 1;
3677
3678 if (lo < 0)
3679 lo = 0;
3680 else if (lo > size)
3681 lo = size;
3682 if (hi < 0)
3683 hi = 0;
3684 if (hi < lo)
3685 hi = lo;
3686 else if (hi > size)
3687 hi = size;
3688
3689 return GetBufferLineList(self->buf, lo+start, hi+start);
3690}
3691
3692 static PyInt
3693RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3694{
3695 PyInt len_change;
3696
3697 if (CheckBuffer(self))
3698 return -1;
3699
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003700 if (end == -1)
3701 end = self->buf->b_ml.ml_line_count;
3702
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003703 if (n < 0)
3704 n += end - start + 1;
3705
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003706 if (n < 0 || n > end - start)
3707 {
3708 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3709 return -1;
3710 }
3711
3712 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3713 return -1;
3714
3715 if (new_end)
3716 *new_end = end + len_change;
3717
3718 return 0;
3719}
3720
Bram Moolenaar19e60942011-06-19 00:27:51 +02003721 static PyInt
3722RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3723{
3724 PyInt size;
3725 PyInt len_change;
3726
3727 /* Self must be a valid buffer */
3728 if (CheckBuffer(self))
3729 return -1;
3730
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003731 if (end == -1)
3732 end = self->buf->b_ml.ml_line_count;
3733
Bram Moolenaar19e60942011-06-19 00:27:51 +02003734 /* Sort out the slice range */
3735 size = end - start + 1;
3736
3737 if (lo < 0)
3738 lo = 0;
3739 else if (lo > size)
3740 lo = size;
3741 if (hi < 0)
3742 hi = 0;
3743 if (hi < lo)
3744 hi = lo;
3745 else if (hi > size)
3746 hi = size;
3747
3748 if (SetBufferLineList(self->buf, lo + start, hi + start,
3749 val, &len_change) == FAIL)
3750 return -1;
3751
3752 if (new_end)
3753 *new_end = end + len_change;
3754
3755 return 0;
3756}
3757
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003758
3759 static PyObject *
3760RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3761{
3762 PyObject *lines;
3763 PyInt len_change;
3764 PyInt max;
3765 PyInt n;
3766
3767 if (CheckBuffer(self))
3768 return NULL;
3769
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003770 if (end == -1)
3771 end = self->buf->b_ml.ml_line_count;
3772
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003773 max = n = end - start + 1;
3774
3775 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3776 return NULL;
3777
3778 if (n < 0 || n > max)
3779 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003780 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003781 return NULL;
3782 }
3783
3784 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3785 return NULL;
3786
3787 if (new_end)
3788 *new_end = end + len_change;
3789
3790 Py_INCREF(Py_None);
3791 return Py_None;
3792}
3793
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003794/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003795 */
3796
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003797static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003798static PySequenceMethods RangeAsSeq;
3799static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003800
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003801typedef struct
3802{
3803 PyObject_HEAD
3804 BufferObject *buf;
3805 PyInt start;
3806 PyInt end;
3807} RangeObject;
3808
3809 static PyObject *
3810RangeNew(buf_T *buf, PyInt start, PyInt end)
3811{
3812 BufferObject *bufr;
3813 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003814 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003815 if (self == NULL)
3816 return NULL;
3817
3818 bufr = (BufferObject *)BufferNew(buf);
3819 if (bufr == NULL)
3820 {
3821 Py_DECREF(self);
3822 return NULL;
3823 }
3824 Py_INCREF(bufr);
3825
3826 self->buf = bufr;
3827 self->start = start;
3828 self->end = end;
3829
3830 return (PyObject *)(self);
3831}
3832
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003833 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003834RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003835{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003836 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003837 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003838 PyObject_GC_Del((void *)(self));
3839}
3840
3841 static int
3842RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3843{
3844 Py_VISIT(((PyObject *)(self->buf)));
3845 return 0;
3846}
3847
3848 static int
3849RangeClear(RangeObject *self)
3850{
3851 Py_CLEAR(self->buf);
3852 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003853}
3854
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003855 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003856RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003857{
3858 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003859 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003860 return -1; /* ??? */
3861
Bram Moolenaard6e39182013-05-21 18:30:34 +02003862 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003863}
3864
3865 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003866RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003867{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003868 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003869}
3870
3871 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003872RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003873{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003874 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003875}
3876
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003877static char *RangeAttrs[] = {
3878 "start", "end",
3879 NULL
3880};
3881
3882 static PyObject *
3883RangeDir(PyObject *self)
3884{
3885 return ObjectDir(self, RangeAttrs);
3886}
3887
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003888 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003889RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003890{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003891 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003892}
3893
3894 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003895RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003896{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003897 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003898 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
3899 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003900 else
3901 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003902 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003903
3904 if (name == NULL)
3905 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003906
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003907 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02003908 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003909 }
3910}
3911
3912static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003913 /* name, function, calling, documentation */
3914 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003915 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3916 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003917};
3918
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003919static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003920static PySequenceMethods BufferAsSeq;
3921static PyMappingMethods BufferAsMapping;
3922
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003923 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003924BufferNew(buf_T *buf)
3925{
3926 /* We need to handle deletion of buffers underneath us.
3927 * If we add a "b_python*_ref" field to the buf_T structure,
3928 * then we can get at it in buf_freeall() in vim. We then
3929 * need to create only ONE Python object per buffer - if
3930 * we try to create a second, just INCREF the existing one
3931 * and return it. The (single) Python object referring to
3932 * the buffer is stored in "b_python*_ref".
3933 * Question: what to do on a buf_freeall(). We'll probably
3934 * have to either delete the Python object (DECREF it to
3935 * zero - a bad idea, as it leaves dangling refs!) or
3936 * set the buf_T * value to an invalid value (-1?), which
3937 * means we need checks in all access functions... Bah.
3938 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003939 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003940 * b_python_ref and b_python3_ref fields respectively.
3941 */
3942
3943 BufferObject *self;
3944
3945 if (BUF_PYTHON_REF(buf) != NULL)
3946 {
3947 self = BUF_PYTHON_REF(buf);
3948 Py_INCREF(self);
3949 }
3950 else
3951 {
3952 self = PyObject_NEW(BufferObject, &BufferType);
3953 if (self == NULL)
3954 return NULL;
3955 self->buf = buf;
3956 BUF_PYTHON_REF(buf) = self;
3957 }
3958
3959 return (PyObject *)(self);
3960}
3961
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003962 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003963BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003964{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003965 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3966 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003967
3968 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003969}
3970
Bram Moolenaar971db462013-05-12 18:44:48 +02003971 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003972BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003973{
3974 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003975 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003976 return -1; /* ??? */
3977
Bram Moolenaard6e39182013-05-21 18:30:34 +02003978 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003979}
3980
3981 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003982BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003983{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003984 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003985}
3986
3987 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003988BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003989{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003990 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003991}
3992
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003993static char *BufferAttrs[] = {
3994 "name", "number", "vars", "options", "valid",
3995 NULL
3996};
3997
3998 static PyObject *
3999BufferDir(PyObject *self)
4000{
4001 return ObjectDir(self, BufferAttrs);
4002}
4003
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004004 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004005BufferAttrValid(BufferObject *self, char *name)
4006{
4007 PyObject *r;
4008
4009 if (strcmp(name, "valid") != 0)
4010 return NULL;
4011
4012 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4013 Py_INCREF(r);
4014 return r;
4015}
4016
4017 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004018BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004019{
4020 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004021 return PyString_FromString((self->buf->b_ffname == NULL
4022 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004023 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004024 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004025 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004026 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004027 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004028 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4029 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004030 else if (strcmp(name, "__members__") == 0)
4031 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004032 else
4033 return NULL;
4034}
4035
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004036 static int
4037BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4038{
4039 if (CheckBuffer(self))
4040 return -1;
4041
4042 if (strcmp(name, "name") == 0)
4043 {
4044 char_u *val;
4045 aco_save_T aco;
4046 int r;
4047 PyObject *todecref;
4048
4049 if (!(val = StringToChars(valObject, &todecref)))
4050 return -1;
4051
4052 VimTryStart();
4053 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4054 aucmd_prepbuf(&aco, self->buf);
4055 r = rename_buffer(val);
4056 aucmd_restbuf(&aco);
4057 Py_XDECREF(todecref);
4058 if (VimTryEnd())
4059 return -1;
4060
4061 if (r == FAIL)
4062 {
4063 PyErr_SetVim(_("failed to rename buffer"));
4064 return -1;
4065 }
4066 return 0;
4067 }
4068 else
4069 {
4070 PyErr_SetString(PyExc_AttributeError, name);
4071 return -1;
4072 }
4073}
4074
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004075 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004076BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004078 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079}
4080
4081 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004082BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083{
4084 pos_T *posp;
4085 char *pmark;
4086 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004087 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088
Bram Moolenaard6e39182013-05-21 18:30:34 +02004089 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004090 return NULL;
4091
4092 if (!PyArg_ParseTuple(args, "s", &pmark))
4093 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004094
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004095 if (STRLEN(pmark) != 1)
4096 {
4097 PyErr_SetString(PyExc_ValueError,
4098 _("mark name must be a single character"));
4099 return NULL;
4100 }
4101
4102 mark = *pmark;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004103 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004104 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004105 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004106 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004107 if (VimTryEnd())
4108 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109
4110 if (posp == NULL)
4111 {
4112 PyErr_SetVim(_("invalid mark name"));
4113 return NULL;
4114 }
4115
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116 if (posp->lnum <= 0)
4117 {
4118 /* Or raise an error? */
4119 Py_INCREF(Py_None);
4120 return Py_None;
4121 }
4122
4123 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4124}
4125
4126 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004127BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004128{
4129 PyInt start;
4130 PyInt end;
4131
Bram Moolenaard6e39182013-05-21 18:30:34 +02004132 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004133 return NULL;
4134
4135 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4136 return NULL;
4137
Bram Moolenaard6e39182013-05-21 18:30:34 +02004138 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139}
4140
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004141 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004142BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004143{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004144 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004145 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004146 else
4147 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004148 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004149
4150 if (name == NULL)
4151 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004152
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004153 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004154 }
4155}
4156
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004157static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004158 /* name, function, calling, documentation */
4159 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4160 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4161 {"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 +02004162 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4163 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004164};
4165
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004166/*
4167 * Buffer list object - Implementation
4168 */
4169
4170static PyTypeObject BufMapType;
4171
4172typedef struct
4173{
4174 PyObject_HEAD
4175} BufMapObject;
4176
4177 static PyInt
4178BufMapLength(PyObject *self UNUSED)
4179{
4180 buf_T *b = firstbuf;
4181 PyInt n = 0;
4182
4183 while (b)
4184 {
4185 ++n;
4186 b = b->b_next;
4187 }
4188
4189 return n;
4190}
4191
4192 static PyObject *
4193BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4194{
4195 buf_T *b;
4196 int bnr;
4197
4198#if PY_MAJOR_VERSION < 3
4199 if (PyInt_Check(keyObject))
4200 bnr = PyInt_AsLong(keyObject);
4201 else
4202#endif
4203 if (PyLong_Check(keyObject))
4204 bnr = PyLong_AsLong(keyObject);
4205 else
4206 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004207 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004208 return NULL;
4209 }
4210
4211 b = buflist_findnr(bnr);
4212
4213 if (b)
4214 return BufferNew(b);
4215 else
4216 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004217 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004218 return NULL;
4219 }
4220}
4221
4222 static void
4223BufMapIterDestruct(PyObject *buffer)
4224{
4225 /* Iteration was stopped before all buffers were processed */
4226 if (buffer)
4227 {
4228 Py_DECREF(buffer);
4229 }
4230}
4231
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004232 static int
4233BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4234{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004235 if (buffer)
4236 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004237 return 0;
4238}
4239
4240 static int
4241BufMapIterClear(PyObject **buffer)
4242{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004243 if (*buffer)
4244 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004245 return 0;
4246}
4247
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004248 static PyObject *
4249BufMapIterNext(PyObject **buffer)
4250{
4251 PyObject *next;
4252 PyObject *r;
4253
4254 if (!*buffer)
4255 return NULL;
4256
4257 r = *buffer;
4258
4259 if (CheckBuffer((BufferObject *)(r)))
4260 {
4261 *buffer = NULL;
4262 return NULL;
4263 }
4264
4265 if (!((BufferObject *)(r))->buf->b_next)
4266 next = NULL;
4267 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4268 return NULL;
4269 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004270 /* Do not increment reference: we no longer hold it (decref), but whoever
4271 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004272 return r;
4273}
4274
4275 static PyObject *
4276BufMapIter(PyObject *self UNUSED)
4277{
4278 PyObject *buffer;
4279
4280 buffer = BufferNew(firstbuf);
4281 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004282 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4283 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004284}
4285
4286static PyMappingMethods BufMapAsMapping = {
4287 (lenfunc) BufMapLength,
4288 (binaryfunc) BufMapItem,
4289 (objobjargproc) 0,
4290};
4291
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004292/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004293 */
4294
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004295static char *CurrentAttrs[] = {
4296 "buffer", "window", "line", "range", "tabpage",
4297 NULL
4298};
4299
4300 static PyObject *
4301CurrentDir(PyObject *self)
4302{
4303 return ObjectDir(self, CurrentAttrs);
4304}
4305
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004306 static PyObject *
4307CurrentGetattr(PyObject *self UNUSED, char *name)
4308{
4309 if (strcmp(name, "buffer") == 0)
4310 return (PyObject *)BufferNew(curbuf);
4311 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004312 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004313 else if (strcmp(name, "tabpage") == 0)
4314 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004315 else if (strcmp(name, "line") == 0)
4316 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4317 else if (strcmp(name, "range") == 0)
4318 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004319 else if (strcmp(name, "__members__") == 0)
4320 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004321 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004322#if PY_MAJOR_VERSION < 3
4323 return Py_FindMethod(WindowMethods, self, name);
4324#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004325 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004326#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004327}
4328
4329 static int
4330CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4331{
4332 if (strcmp(name, "line") == 0)
4333 {
4334 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4335 return -1;
4336
4337 return 0;
4338 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004339 else if (strcmp(name, "buffer") == 0)
4340 {
4341 int count;
4342
4343 if (value->ob_type != &BufferType)
4344 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004345 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004346 return -1;
4347 }
4348
4349 if (CheckBuffer((BufferObject *)(value)))
4350 return -1;
4351 count = ((BufferObject *)(value))->buf->b_fnum;
4352
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004353 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004354 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4355 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004356 if (VimTryEnd())
4357 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004358 PyErr_SetVim(_("failed to switch to given buffer"));
4359 return -1;
4360 }
4361
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004362 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004363 }
4364 else if (strcmp(name, "window") == 0)
4365 {
4366 int count;
4367
4368 if (value->ob_type != &WindowType)
4369 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004370 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004371 return -1;
4372 }
4373
4374 if (CheckWindow((WindowObject *)(value)))
4375 return -1;
4376 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4377
4378 if (!count)
4379 {
4380 PyErr_SetString(PyExc_ValueError,
4381 _("failed to find window in the current tab page"));
4382 return -1;
4383 }
4384
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004385 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004386 win_goto(((WindowObject *)(value))->win);
4387 if (((WindowObject *)(value))->win != curwin)
4388 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004389 if (VimTryEnd())
4390 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004391 PyErr_SetString(PyExc_RuntimeError,
4392 _("did not switch to the specified window"));
4393 return -1;
4394 }
4395
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004396 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004397 }
4398 else if (strcmp(name, "tabpage") == 0)
4399 {
4400 if (value->ob_type != &TabPageType)
4401 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004402 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004403 return -1;
4404 }
4405
4406 if (CheckTabPage((TabPageObject *)(value)))
4407 return -1;
4408
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004409 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004410 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4411 if (((TabPageObject *)(value))->tab != curtab)
4412 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004413 if (VimTryEnd())
4414 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004415 PyErr_SetString(PyExc_RuntimeError,
4416 _("did not switch to the specified tab page"));
4417 return -1;
4418 }
4419
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004420 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004421 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004422 else
4423 {
4424 PyErr_SetString(PyExc_AttributeError, name);
4425 return -1;
4426 }
4427}
4428
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004429static struct PyMethodDef CurrentMethods[] = {
4430 /* name, function, calling, documentation */
4431 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4432 { NULL, NULL, 0, NULL}
4433};
4434
Bram Moolenaardb913952012-06-29 12:54:53 +02004435 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004436init_range_cmd(exarg_T *eap)
4437{
4438 RangeStart = eap->line1;
4439 RangeEnd = eap->line2;
4440}
4441
4442 static void
4443init_range_eval(typval_T *rettv UNUSED)
4444{
4445 RangeStart = (PyInt) curwin->w_cursor.lnum;
4446 RangeEnd = RangeStart;
4447}
4448
4449 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004450run_cmd(const char *cmd, void *arg UNUSED
4451#ifdef PY_CAN_RECURSE
4452 , PyGILState_STATE *pygilstate UNUSED
4453#endif
4454 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004455{
4456 PyRun_SimpleString((char *) cmd);
4457}
4458
4459static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4460static int code_hdr_len = 30;
4461
4462 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004463run_do(const char *cmd, void *arg UNUSED
4464#ifdef PY_CAN_RECURSE
4465 , PyGILState_STATE *pygilstate
4466#endif
4467 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004468{
4469 PyInt lnum;
4470 size_t len;
4471 char *code;
4472 int status;
4473 PyObject *pyfunc, *pymain;
4474
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004475 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004476 {
4477 EMSG(_("cannot save undo information"));
4478 return;
4479 }
4480
4481 len = code_hdr_len + STRLEN(cmd);
4482 code = PyMem_New(char, len + 1);
4483 memcpy(code, code_hdr, code_hdr_len);
4484 STRCPY(code + code_hdr_len, cmd);
4485 status = PyRun_SimpleString(code);
4486 PyMem_Free(code);
4487
4488 if (status)
4489 {
4490 EMSG(_("failed to run the code"));
4491 return;
4492 }
4493
4494 status = 0;
4495 pymain = PyImport_AddModule("__main__");
4496 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004497#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004498 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004499#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004500
4501 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4502 {
4503 PyObject *line, *linenr, *ret;
4504
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004505#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004506 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004507#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004508 if (!(line = GetBufferLine(curbuf, lnum)))
4509 goto err;
4510 if (!(linenr = PyInt_FromLong((long) lnum)))
4511 {
4512 Py_DECREF(line);
4513 goto err;
4514 }
4515 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4516 Py_DECREF(line);
4517 Py_DECREF(linenr);
4518 if (!ret)
4519 goto err;
4520
4521 if (ret != Py_None)
4522 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4523 goto err;
4524
4525 Py_XDECREF(ret);
4526 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004527#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004528 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004529#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004530 }
4531 goto out;
4532err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004533#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004534 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004535#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004536 PyErr_PrintEx(0);
4537 PythonIO_Flush();
4538 status = 1;
4539out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004540#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004541 if (!status)
4542 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004543#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004544 Py_DECREF(pyfunc);
4545 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4546 if (status)
4547 return;
4548 check_cursor();
4549 update_curbuf(NOT_VALID);
4550}
4551
4552 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004553run_eval(const char *cmd, typval_T *rettv
4554#ifdef PY_CAN_RECURSE
4555 , PyGILState_STATE *pygilstate UNUSED
4556#endif
4557 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004558{
4559 PyObject *r;
4560
4561 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4562 if (r == NULL)
4563 {
4564 if (PyErr_Occurred() && !msg_silent)
4565 PyErr_PrintEx(0);
4566 EMSG(_("E858: Eval did not return a valid python object"));
4567 }
4568 else
4569 {
4570 if (ConvertFromPyObject(r, rettv) == -1)
4571 EMSG(_("E859: Failed to convert returned python object to vim value"));
4572 Py_DECREF(r);
4573 }
4574 PyErr_Clear();
4575}
4576
4577 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004578set_ref_in_py(const int copyID)
4579{
4580 pylinkedlist_T *cur;
4581 dict_T *dd;
4582 list_T *ll;
4583
4584 if (lastdict != NULL)
4585 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4586 {
4587 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4588 if (dd->dv_copyID != copyID)
4589 {
4590 dd->dv_copyID = copyID;
4591 set_ref_in_ht(&dd->dv_hashtab, copyID);
4592 }
4593 }
4594
4595 if (lastlist != NULL)
4596 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4597 {
4598 ll = ((ListObject *) (cur->pll_obj))->list;
4599 if (ll->lv_copyID != copyID)
4600 {
4601 ll->lv_copyID = copyID;
4602 set_ref_in_list(ll, copyID);
4603 }
4604 }
4605}
4606
4607 static int
4608set_string_copy(char_u *str, typval_T *tv)
4609{
4610 tv->vval.v_string = vim_strsave(str);
4611 if (tv->vval.v_string == NULL)
4612 {
4613 PyErr_NoMemory();
4614 return -1;
4615 }
4616 return 0;
4617}
4618
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004619 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004620pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004621{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004622 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004623 char_u *key;
4624 dictitem_T *di;
4625 PyObject *keyObject;
4626 PyObject *valObject;
4627 Py_ssize_t iter = 0;
4628
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004629 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004630 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004631
4632 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004633 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004634
4635 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4636 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004637 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004638
Bram Moolenaara03e6312013-05-29 22:49:26 +02004639 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004640 {
4641 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004642 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004643 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004644
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004645 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004646 {
4647 dict_unref(dict);
4648 return -1;
4649 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004650
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004651 if (*key == NUL)
4652 {
4653 dict_unref(dict);
4654 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004655 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004656 return -1;
4657 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004658
4659 di = dictitem_alloc(key);
4660
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004661 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004662
4663 if (di == NULL)
4664 {
4665 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004666 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004667 return -1;
4668 }
4669 di->di_tv.v_lock = 0;
4670
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004671 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004672 {
4673 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004674 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004675 return -1;
4676 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004677
4678 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004679 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004680 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004681 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004682 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004683 PyErr_SetVim(_("failed to add key to dictionary"));
4684 return -1;
4685 }
4686 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004687
4688 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004689 return 0;
4690}
4691
4692 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004693pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004694{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004695 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004696 char_u *key;
4697 dictitem_T *di;
4698 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004699 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004700 PyObject *keyObject;
4701 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004702
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004703 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004704 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004705
4706 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004707 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004708
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004709 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004710 {
4711 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004712 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004713 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004714
4715 if (!(iterator = PyObject_GetIter(list)))
4716 {
4717 dict_unref(dict);
4718 Py_DECREF(list);
4719 return -1;
4720 }
4721 Py_DECREF(list);
4722
4723 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004724 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004725 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004726
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004727 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004728 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004729 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004730 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004731 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004732 return -1;
4733 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004734
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004735 if (*key == NUL)
4736 {
4737 Py_DECREF(keyObject);
4738 Py_DECREF(iterator);
4739 Py_XDECREF(todecref);
4740 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004741 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004742 return -1;
4743 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004744
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004745 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004746 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004747 Py_DECREF(keyObject);
4748 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004749 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004750 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004751 return -1;
4752 }
4753
4754 di = dictitem_alloc(key);
4755
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004756 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004757 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004758
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004759 if (di == NULL)
4760 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004761 Py_DECREF(iterator);
4762 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004763 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004764 PyErr_NoMemory();
4765 return -1;
4766 }
4767 di->di_tv.v_lock = 0;
4768
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004769 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004770 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004771 Py_DECREF(iterator);
4772 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004773 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004774 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004775 return -1;
4776 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004777
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004778 Py_DECREF(valObject);
4779
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004780 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004781 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004782 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004783 dictitem_free(di);
4784 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004785 PyErr_SetVim(_("failed to add key to dictionary"));
4786 return -1;
4787 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004788 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004789 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004790 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004791 return 0;
4792}
4793
4794 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004795pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004796{
4797 list_T *l;
4798
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004799 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004800 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004801
4802 tv->v_type = VAR_LIST;
4803 tv->vval.v_list = l;
4804
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004805 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004806 {
4807 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004808 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004809 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004810
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004811 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004812 return 0;
4813}
4814
Bram Moolenaardb913952012-06-29 12:54:53 +02004815typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4816
4817 static int
4818convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004819 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004820{
4821 PyObject *capsule;
4822 char hexBuf[sizeof(void *) * 2 + 3];
4823
4824 sprintf(hexBuf, "%p", obj);
4825
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004826# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004827 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004828# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004829 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004830# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004831 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004832 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004833# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004834 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004835# else
4836 capsule = PyCObject_FromVoidPtr(tv, NULL);
4837# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004838 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4839 {
4840 Py_DECREF(capsule);
4841 tv->v_type = VAR_UNKNOWN;
4842 return -1;
4843 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004844 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004845 {
4846 tv->v_type = VAR_UNKNOWN;
4847 return -1;
4848 }
4849 /* As we are not using copy_tv which increments reference count we must
4850 * do it ourself. */
4851 switch(tv->v_type)
4852 {
4853 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4854 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4855 }
4856 }
4857 else
4858 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004859 typval_T *v;
4860
4861# ifdef PY_USE_CAPSULE
4862 v = PyCapsule_GetPointer(capsule, NULL);
4863# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004864 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004865# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004866 copy_tv(v, tv);
4867 }
4868 return 0;
4869}
4870
4871 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02004872ConvertFromPyMapping(PyObject *obj, typval_T *tv)
4873{
4874 PyObject *lookup_dict;
4875 int r;
4876
4877 if (!(lookup_dict = PyDict_New()))
4878 return -1;
4879
4880 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
4881 {
4882 tv->v_type = VAR_DICT;
4883 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4884 ++tv->vval.v_dict->dv_refcount;
4885 r = 0;
4886 }
4887 else if (PyDict_Check(obj))
4888 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
4889 else if (PyMapping_Check(obj))
4890 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
4891 else
4892 {
4893 PyErr_SetString(PyExc_TypeError,
4894 _("unable to convert object to vim dictionary"));
4895 r = -1;
4896 }
4897 Py_DECREF(lookup_dict);
4898 return r;
4899}
4900
4901 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02004902ConvertFromPyObject(PyObject *obj, typval_T *tv)
4903{
4904 PyObject *lookup_dict;
4905 int r;
4906
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004907 if (!(lookup_dict = PyDict_New()))
4908 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004909 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4910 Py_DECREF(lookup_dict);
4911 return r;
4912}
4913
4914 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004915_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004916{
Bram Moolenaara9922d62013-05-30 13:01:18 +02004917 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02004918 {
4919 tv->v_type = VAR_DICT;
4920 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4921 ++tv->vval.v_dict->dv_refcount;
4922 }
4923 else if (obj->ob_type == &ListType)
4924 {
4925 tv->v_type = VAR_LIST;
4926 tv->vval.v_list = (((ListObject *)(obj))->list);
4927 ++tv->vval.v_list->lv_refcount;
4928 }
4929 else if (obj->ob_type == &FunctionType)
4930 {
4931 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4932 return -1;
4933
4934 tv->v_type = VAR_FUNC;
4935 func_ref(tv->vval.v_string);
4936 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004937 else if (PyBytes_Check(obj))
4938 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004939 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004940
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004941 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4942 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004943 if (result == NULL)
4944 return -1;
4945
4946 if (set_string_copy(result, tv) == -1)
4947 return -1;
4948
4949 tv->v_type = VAR_STRING;
4950 }
4951 else if (PyUnicode_Check(obj))
4952 {
4953 PyObject *bytes;
4954 char_u *result;
4955
Bram Moolenaardb913952012-06-29 12:54:53 +02004956 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4957 if (bytes == NULL)
4958 return -1;
4959
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004960 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4961 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004962 if (result == NULL)
4963 return -1;
4964
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004965 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004966 {
4967 Py_XDECREF(bytes);
4968 return -1;
4969 }
4970 Py_XDECREF(bytes);
4971
4972 tv->v_type = VAR_STRING;
4973 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004974#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004975 else if (PyInt_Check(obj))
4976 {
4977 tv->v_type = VAR_NUMBER;
4978 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4979 }
4980#endif
4981 else if (PyLong_Check(obj))
4982 {
4983 tv->v_type = VAR_NUMBER;
4984 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4985 }
4986 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004987 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004988#ifdef FEAT_FLOAT
4989 else if (PyFloat_Check(obj))
4990 {
4991 tv->v_type = VAR_FLOAT;
4992 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4993 }
4994#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004995 else if (PyObject_HasAttrString(obj, "keys"))
4996 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004997 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004998 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004999 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005000 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005001 else
5002 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02005003 PyErr_SetString(PyExc_TypeError,
5004 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005005 return -1;
5006 }
5007 return 0;
5008}
5009
5010 static PyObject *
5011ConvertToPyObject(typval_T *tv)
5012{
5013 if (tv == NULL)
5014 {
5015 PyErr_SetVim(_("NULL reference passed"));
5016 return NULL;
5017 }
5018 switch (tv->v_type)
5019 {
5020 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005021 return PyBytes_FromString(tv->vval.v_string == NULL
5022 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005023 case VAR_NUMBER:
5024 return PyLong_FromLong((long) tv->vval.v_number);
5025#ifdef FEAT_FLOAT
5026 case VAR_FLOAT:
5027 return PyFloat_FromDouble((double) tv->vval.v_float);
5028#endif
5029 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005030 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005031 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005032 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005033 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005034 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005035 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005036 case VAR_UNKNOWN:
5037 Py_INCREF(Py_None);
5038 return Py_None;
5039 default:
5040 PyErr_SetVim(_("internal error: invalid value type"));
5041 return NULL;
5042 }
5043}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005044
5045typedef struct
5046{
5047 PyObject_HEAD
5048} CurrentObject;
5049static PyTypeObject CurrentType;
5050
5051 static void
5052init_structs(void)
5053{
5054 vim_memset(&OutputType, 0, sizeof(OutputType));
5055 OutputType.tp_name = "vim.message";
5056 OutputType.tp_basicsize = sizeof(OutputObject);
5057 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5058 OutputType.tp_doc = "vim message object";
5059 OutputType.tp_methods = OutputMethods;
5060#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005061 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5062 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005063 OutputType.tp_alloc = call_PyType_GenericAlloc;
5064 OutputType.tp_new = call_PyType_GenericNew;
5065 OutputType.tp_free = call_PyObject_Free;
5066#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005067 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5068 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005069#endif
5070
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005071 vim_memset(&IterType, 0, sizeof(IterType));
5072 IterType.tp_name = "vim.iter";
5073 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005074 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005075 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005076 IterType.tp_iter = (getiterfunc)IterIter;
5077 IterType.tp_iternext = (iternextfunc)IterNext;
5078 IterType.tp_dealloc = (destructor)IterDestructor;
5079 IterType.tp_traverse = (traverseproc)IterTraverse;
5080 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005081
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005082 vim_memset(&BufferType, 0, sizeof(BufferType));
5083 BufferType.tp_name = "vim.buffer";
5084 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005085 BufferType.tp_dealloc = (destructor)BufferDestructor;
5086 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005087 BufferType.tp_as_sequence = &BufferAsSeq;
5088 BufferType.tp_as_mapping = &BufferAsMapping;
5089 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5090 BufferType.tp_doc = "vim buffer object";
5091 BufferType.tp_methods = BufferMethods;
5092#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005093 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005094 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005095 BufferType.tp_alloc = call_PyType_GenericAlloc;
5096 BufferType.tp_new = call_PyType_GenericNew;
5097 BufferType.tp_free = call_PyObject_Free;
5098#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005099 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005100 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005101#endif
5102
5103 vim_memset(&WindowType, 0, sizeof(WindowType));
5104 WindowType.tp_name = "vim.window";
5105 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005106 WindowType.tp_dealloc = (destructor)WindowDestructor;
5107 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005108 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005109 WindowType.tp_doc = "vim Window object";
5110 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005111 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5112 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005113#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005114 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5115 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005116 WindowType.tp_alloc = call_PyType_GenericAlloc;
5117 WindowType.tp_new = call_PyType_GenericNew;
5118 WindowType.tp_free = call_PyObject_Free;
5119#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005120 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5121 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005122#endif
5123
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005124 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5125 TabPageType.tp_name = "vim.tabpage";
5126 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005127 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5128 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005129 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5130 TabPageType.tp_doc = "vim tab page object";
5131 TabPageType.tp_methods = TabPageMethods;
5132#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005133 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005134 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5135 TabPageType.tp_new = call_PyType_GenericNew;
5136 TabPageType.tp_free = call_PyObject_Free;
5137#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005138 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005139#endif
5140
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005141 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5142 BufMapType.tp_name = "vim.bufferlist";
5143 BufMapType.tp_basicsize = sizeof(BufMapObject);
5144 BufMapType.tp_as_mapping = &BufMapAsMapping;
5145 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005146 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005147 BufferType.tp_doc = "vim buffer list";
5148
5149 vim_memset(&WinListType, 0, sizeof(WinListType));
5150 WinListType.tp_name = "vim.windowlist";
5151 WinListType.tp_basicsize = sizeof(WinListType);
5152 WinListType.tp_as_sequence = &WinListAsSeq;
5153 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5154 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005155 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005156
5157 vim_memset(&TabListType, 0, sizeof(TabListType));
5158 TabListType.tp_name = "vim.tabpagelist";
5159 TabListType.tp_basicsize = sizeof(TabListType);
5160 TabListType.tp_as_sequence = &TabListAsSeq;
5161 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5162 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005163
5164 vim_memset(&RangeType, 0, sizeof(RangeType));
5165 RangeType.tp_name = "vim.range";
5166 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005167 RangeType.tp_dealloc = (destructor)RangeDestructor;
5168 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005169 RangeType.tp_as_sequence = &RangeAsSeq;
5170 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005171 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005172 RangeType.tp_doc = "vim Range object";
5173 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005174 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5175 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005176#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005177 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005178 RangeType.tp_alloc = call_PyType_GenericAlloc;
5179 RangeType.tp_new = call_PyType_GenericNew;
5180 RangeType.tp_free = call_PyObject_Free;
5181#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005182 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005183#endif
5184
5185 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5186 CurrentType.tp_name = "vim.currentdata";
5187 CurrentType.tp_basicsize = sizeof(CurrentObject);
5188 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5189 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005190 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005191#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005192 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5193 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005194#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005195 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5196 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005197#endif
5198
5199 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5200 DictionaryType.tp_name = "vim.dictionary";
5201 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005202 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005203 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005204 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005205 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005206 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5207 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005208 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5209 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5210 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005211#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005212 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5213 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005214#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005215 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5216 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005217#endif
5218
5219 vim_memset(&ListType, 0, sizeof(ListType));
5220 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005221 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005222 ListType.tp_basicsize = sizeof(ListObject);
5223 ListType.tp_as_sequence = &ListAsSeq;
5224 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005225 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005226 ListType.tp_doc = "list pushing modifications to vim structure";
5227 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005228 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005229 ListType.tp_new = (newfunc)ListConstructor;
5230 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005231#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005232 ListType.tp_getattro = (getattrofunc)ListGetattro;
5233 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005234#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005235 ListType.tp_getattr = (getattrfunc)ListGetattr;
5236 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005237#endif
5238
5239 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005240 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005241 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005242 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5243 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005244 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005245 FunctionType.tp_doc = "object that calls vim function";
5246 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005247 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005248 FunctionType.tp_new = (newfunc)FunctionConstructor;
5249 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005250#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005251 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005252#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005253 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005254#endif
5255
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005256 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5257 OptionsType.tp_name = "vim.options";
5258 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005259 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005260 OptionsType.tp_doc = "object for manipulating options";
5261 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005262 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5263 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5264 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005265
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005266#if PY_MAJOR_VERSION >= 3
5267 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5268 vimmodule.m_name = "vim";
5269 vimmodule.m_doc = "Vim Python interface\n";
5270 vimmodule.m_size = -1;
5271 vimmodule.m_methods = VimMethods;
5272#endif
5273}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005274
5275#define PYTYPE_READY(type) \
5276 if (PyType_Ready(&type)) \
5277 return -1;
5278
5279 static int
5280init_types()
5281{
5282 PYTYPE_READY(IterType);
5283 PYTYPE_READY(BufferType);
5284 PYTYPE_READY(RangeType);
5285 PYTYPE_READY(WindowType);
5286 PYTYPE_READY(TabPageType);
5287 PYTYPE_READY(BufMapType);
5288 PYTYPE_READY(WinListType);
5289 PYTYPE_READY(TabListType);
5290 PYTYPE_READY(CurrentType);
5291 PYTYPE_READY(DictionaryType);
5292 PYTYPE_READY(ListType);
5293 PYTYPE_READY(FunctionType);
5294 PYTYPE_READY(OptionsType);
5295 PYTYPE_READY(OutputType);
5296 return 0;
5297}
5298
5299static BufMapObject TheBufferMap =
5300{
5301 PyObject_HEAD_INIT(&BufMapType)
5302};
5303
5304static WinListObject TheWindowList =
5305{
5306 PyObject_HEAD_INIT(&WinListType)
5307 NULL
5308};
5309
5310static CurrentObject TheCurrent =
5311{
5312 PyObject_HEAD_INIT(&CurrentType)
5313};
5314
5315static TabListObject TheTabPageList =
5316{
5317 PyObject_HEAD_INIT(&TabListType)
5318};
5319
5320static struct numeric_constant {
5321 char *name;
5322 int value;
5323} numeric_constants[] = {
5324 {"VAR_LOCKED", VAR_LOCKED},
5325 {"VAR_FIXED", VAR_FIXED},
5326 {"VAR_SCOPE", VAR_SCOPE},
5327 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5328};
5329
5330static struct object_constant {
5331 char *name;
5332 PyObject *value;
5333} object_constants[] = {
5334 {"buffers", (PyObject *)(void *)&TheBufferMap},
5335 {"windows", (PyObject *)(void *)&TheWindowList},
5336 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5337 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005338
5339 {"Buffer", (PyObject *)&BufferType},
5340 {"Range", (PyObject *)&RangeType},
5341 {"Window", (PyObject *)&WindowType},
5342 {"TabPage", (PyObject *)&TabPageType},
5343 {"Dictionary", (PyObject *)&DictionaryType},
5344 {"List", (PyObject *)&ListType},
5345 {"Function", (PyObject *)&FunctionType},
5346 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005347};
5348
5349typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005350typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005351
5352#define ADD_OBJECT(m, name, obj) \
5353 if (add_object(m, name, obj)) \
5354 return -1;
5355
5356#define ADD_CHECKED_OBJECT(m, name, obj) \
5357 { \
5358 PyObject *value = obj; \
5359 if (!value) \
5360 return -1; \
5361 ADD_OBJECT(m, name, value); \
5362 }
5363
5364 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005365populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005366{
5367 int i;
Bram Moolenaarf4258302013-06-02 18:20:17 +02005368 PyObject *os;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005369
5370 for (i = 0; i < (int)(sizeof(numeric_constants)
5371 / sizeof(struct numeric_constant));
5372 ++i)
5373 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5374 PyInt_FromLong(numeric_constants[i].value));
5375
5376 for (i = 0; i < (int)(sizeof(object_constants)
5377 / sizeof(struct object_constant));
5378 ++i)
5379 {
5380 PyObject *value;
5381
5382 value = object_constants[i].value;
5383 Py_INCREF(value);
5384 ADD_OBJECT(m, object_constants[i].name, value);
5385 }
5386
5387 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5388 return -1;
5389 ADD_OBJECT(m, "error", VimError);
5390
Bram Moolenaara9922d62013-05-30 13:01:18 +02005391 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5392 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005393 ADD_CHECKED_OBJECT(m, "options",
5394 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005395
5396 if (!(os = PyImport_ImportModule("os")))
5397 return -1;
5398 ADD_OBJECT(m, "os", os);
5399
5400 if (!(py_getcwd = PyObject_GetAttrString(os, "getcwd")))
5401 return -1;
5402 ADD_OBJECT(m, "_getcwd", py_getcwd)
5403
5404 if (!(py_chdir = PyObject_GetAttrString(os, "chdir")))
5405 return -1;
5406 ADD_OBJECT(m, "_chdir", py_chdir);
5407 if (PyObject_SetAttrString(os, "chdir", get_attr(m, "chdir")))
5408 return -1;
5409
5410 if ((py_fchdir = PyObject_GetAttrString(os, "fchdir")))
5411 {
5412 ADD_OBJECT(m, "_fchdir", py_fchdir);
5413 if (PyObject_SetAttrString(os, "fchdir", get_attr(m, "fchdir")))
5414 return -1;
5415 }
5416
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005417 return 0;
5418}