blob: 73fa56cbc911631fe9d2c6969a384cc9b476827e [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;
1239 return -1;
1240 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001241
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001242 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001243
1244 if (valObject == NULL)
1245 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001246 hashitem_T *hi;
1247
Bram Moolenaardb913952012-06-29 12:54:53 +02001248 if (di == NULL)
1249 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001250 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001251 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001252 return -1;
1253 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001254 hi = hash_find(&dict->dv_hashtab, di->di_key);
1255 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001256 dictitem_free(di);
1257 return 0;
1258 }
1259
1260 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001261 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001262
1263 if (di == NULL)
1264 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001265 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001266 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001267 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001268 PyErr_NoMemory();
1269 return -1;
1270 }
1271 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001272 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001273
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001274 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001275 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001276 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001277 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001278 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001279 PyErr_SetVim(_("failed to add key to dictionary"));
1280 return -1;
1281 }
1282 }
1283 else
1284 clear_tv(&di->di_tv);
1285
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001286 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001287
1288 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001289 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001290 return 0;
1291}
1292
Bram Moolenaara9922d62013-05-30 13:01:18 +02001293typedef PyObject *(*hi_to_py)(hashitem_T *);
1294
Bram Moolenaardb913952012-06-29 12:54:53 +02001295 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001296DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001297{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001298 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001299 long_u todo = dict->dv_hashtab.ht_used;
1300 Py_ssize_t i = 0;
1301 PyObject *r;
1302 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001303 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001304
1305 r = PyList_New(todo);
1306 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1307 {
1308 if (!HASHITEM_EMPTY(hi))
1309 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001310 if (!(newObj = hiconvert(hi)))
1311 {
1312 Py_DECREF(r);
1313 return NULL;
1314 }
1315 if (PyList_SetItem(r, i, newObj))
1316 {
1317 Py_DECREF(r);
1318 Py_DECREF(newObj);
1319 return NULL;
1320 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001321 --todo;
1322 ++i;
1323 }
1324 }
1325 return r;
1326}
1327
Bram Moolenaara9922d62013-05-30 13:01:18 +02001328 static PyObject *
1329dict_key(hashitem_T *hi)
1330{
1331 return PyBytes_FromString((char *)(hi->hi_key));
1332}
1333
1334 static PyObject *
1335DictionaryListKeys(DictionaryObject *self)
1336{
1337 return DictionaryListObjects(self, dict_key);
1338}
1339
1340 static PyObject *
1341dict_val(hashitem_T *hi)
1342{
1343 dictitem_T *di;
1344
1345 di = dict_lookup(hi);
1346 return ConvertToPyObject(&di->di_tv);
1347}
1348
1349 static PyObject *
1350DictionaryListValues(DictionaryObject *self)
1351{
1352 return DictionaryListObjects(self, dict_val);
1353}
1354
1355 static PyObject *
1356dict_item(hashitem_T *hi)
1357{
1358 PyObject *keyObject;
1359 PyObject *valObject;
1360 PyObject *r;
1361
1362 if (!(keyObject = dict_key(hi)))
1363 return NULL;
1364
1365 if (!(valObject = dict_val(hi)))
1366 {
1367 Py_DECREF(keyObject);
1368 return NULL;
1369 }
1370
1371 r = Py_BuildValue("(OO)", keyObject, valObject);
1372
1373 Py_DECREF(keyObject);
1374 Py_DECREF(valObject);
1375
1376 return r;
1377}
1378
1379 static PyObject *
1380DictionaryListItems(DictionaryObject *self)
1381{
1382 return DictionaryListObjects(self, dict_item);
1383}
1384
1385 static PyObject *
1386DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1387{
1388 dict_T *dict = self->dict;
1389
1390 if (dict->dv_lock)
1391 {
1392 PyErr_SetVim(_("dict is locked"));
1393 return NULL;
1394 }
1395
1396 if (kwargs)
1397 {
1398 typval_T tv;
1399
1400 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1401 return NULL;
1402
1403 VimTryStart();
1404 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1405 clear_tv(&tv);
1406 if (VimTryEnd())
1407 return NULL;
1408 }
1409 else
1410 {
1411 PyObject *object;
1412
1413 if (!PyArg_Parse(args, "(O)", &object))
1414 return NULL;
1415
1416 if (PyObject_HasAttrString(object, "keys"))
1417 return DictionaryUpdate(self, NULL, object);
1418 else
1419 {
1420 PyObject *iterator;
1421 PyObject *item;
1422
1423 if (!(iterator = PyObject_GetIter(object)))
1424 return NULL;
1425
1426 while ((item = PyIter_Next(iterator)))
1427 {
1428 PyObject *fast;
1429 PyObject *keyObject;
1430 PyObject *valObject;
1431 PyObject *todecref;
1432 char_u *key;
1433 dictitem_T *di;
1434
1435 if (!(fast = PySequence_Fast(item, "")))
1436 {
1437 Py_DECREF(iterator);
1438 Py_DECREF(item);
1439 return NULL;
1440 }
1441
1442 Py_DECREF(item);
1443
1444 if (PySequence_Fast_GET_SIZE(fast) != 2)
1445 {
1446 Py_DECREF(iterator);
1447 Py_DECREF(fast);
1448 PyErr_SetString(PyExc_ValueError,
1449 _("expected sequence element of size 2"));
1450 return NULL;
1451 }
1452
1453 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1454
1455 if (!(key = StringToChars(keyObject, &todecref)))
1456 {
1457 Py_DECREF(iterator);
1458 Py_DECREF(fast);
1459 return NULL;
1460 }
1461
1462 di = dictitem_alloc(key);
1463
1464 Py_XDECREF(todecref);
1465
1466 if (di == NULL)
1467 {
1468 Py_DECREF(fast);
1469 Py_DECREF(iterator);
1470 PyErr_NoMemory();
1471 return NULL;
1472 }
1473 di->di_tv.v_lock = 0;
1474 di->di_tv.v_type = VAR_UNKNOWN;
1475
1476 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1477
1478 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1479 {
1480 Py_DECREF(iterator);
1481 Py_DECREF(fast);
1482 dictitem_free(di);
1483 return NULL;
1484 }
1485
1486 Py_DECREF(fast);
1487
1488 if (dict_add(dict, di) == FAIL)
1489 {
1490 Py_DECREF(iterator);
1491 dictitem_free(di);
1492 PyErr_SetVim(_("failed to add key to dictionary"));
1493 return NULL;
1494 }
1495 }
1496
1497 Py_DECREF(iterator);
1498
1499 /* Iterator may have finished due to an exception */
1500 if (PyErr_Occurred())
1501 return NULL;
1502 }
1503 }
1504 Py_INCREF(Py_None);
1505 return Py_None;
1506}
1507
1508 static PyObject *
1509DictionaryGet(DictionaryObject *self, PyObject *args)
1510{
1511 return _DictionaryItem(self, args,
1512 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1513}
1514
1515 static PyObject *
1516DictionaryPop(DictionaryObject *self, PyObject *args)
1517{
1518 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1519}
1520
1521 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001522DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001523{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001524 hashitem_T *hi;
1525 PyObject *r;
1526 PyObject *valObject;
1527 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001528
Bram Moolenaarde71b562013-06-02 17:41:54 +02001529 if (self->dict->dv_hashtab.ht_used == 0)
1530 {
1531 PyErr_SetNone(PyExc_KeyError);
1532 return NULL;
1533 }
1534
1535 hi = self->dict->dv_hashtab.ht_array;
1536 while (HASHITEM_EMPTY(hi))
1537 ++hi;
1538
1539 di = dict_lookup(hi);
1540
1541 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001542 return NULL;
1543
Bram Moolenaarde71b562013-06-02 17:41:54 +02001544 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
1545 {
1546 Py_DECREF(valObject);
1547 return NULL;
1548 }
1549
1550 hash_remove(&self->dict->dv_hashtab, hi);
1551 dictitem_free(di);
1552
1553 return r;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001554}
1555
1556 static PyObject *
1557DictionaryHasKey(DictionaryObject *self, PyObject *args)
1558{
1559 PyObject *keyObject;
1560
1561 if (!PyArg_ParseTuple(args, "O", &keyObject))
1562 return NULL;
1563
1564 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1565}
1566
1567static PySequenceMethods DictionaryAsSeq = {
1568 0, /* sq_length */
1569 0, /* sq_concat */
1570 0, /* sq_repeat */
1571 0, /* sq_item */
1572 0, /* sq_slice */
1573 0, /* sq_ass_item */
1574 0, /* sq_ass_slice */
1575 (objobjproc) DictionaryContains, /* sq_contains */
1576 0, /* sq_inplace_concat */
1577 0, /* sq_inplace_repeat */
1578};
1579
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001580static PyMappingMethods DictionaryAsMapping = {
1581 (lenfunc) DictionaryLength,
1582 (binaryfunc) DictionaryItem,
1583 (objobjargproc) DictionaryAssItem,
1584};
1585
Bram Moolenaardb913952012-06-29 12:54:53 +02001586static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001587 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001588 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1589 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1590 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1591 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1592 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02001593 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001594 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001595 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1596 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001597};
1598
1599static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001600static PySequenceMethods ListAsSeq;
1601static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001602
1603typedef struct
1604{
1605 PyObject_HEAD
1606 list_T *list;
1607 pylinkedlist_T ref;
1608} ListObject;
1609
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001610#define NEW_LIST(list) ListNew(&ListType, list)
1611
Bram Moolenaardb913952012-06-29 12:54:53 +02001612 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001613ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001614{
1615 ListObject *self;
1616
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001617 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001618 if (self == NULL)
1619 return NULL;
1620 self->list = list;
1621 ++list->lv_refcount;
1622
1623 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1624
1625 return (PyObject *)(self);
1626}
1627
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001628 static list_T *
1629py_list_alloc()
1630{
1631 list_T *r;
1632
1633 if (!(r = list_alloc()))
1634 {
1635 PyErr_NoMemory();
1636 return NULL;
1637 }
1638 ++r->lv_refcount;
1639
1640 return r;
1641}
1642
1643 static int
1644list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1645{
1646 PyObject *iterator;
1647 PyObject *item;
1648 listitem_T *li;
1649
1650 if (!(iterator = PyObject_GetIter(obj)))
1651 return -1;
1652
1653 while ((item = PyIter_Next(iterator)))
1654 {
1655 if (!(li = listitem_alloc()))
1656 {
1657 PyErr_NoMemory();
1658 Py_DECREF(item);
1659 Py_DECREF(iterator);
1660 return -1;
1661 }
1662 li->li_tv.v_lock = 0;
1663 li->li_tv.v_type = VAR_UNKNOWN;
1664
1665 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1666 {
1667 Py_DECREF(item);
1668 Py_DECREF(iterator);
1669 listitem_free(li);
1670 return -1;
1671 }
1672
1673 Py_DECREF(item);
1674
1675 list_append(l, li);
1676 }
1677
1678 Py_DECREF(iterator);
1679
1680 /* Iterator may have finished due to an exception */
1681 if (PyErr_Occurred())
1682 return -1;
1683
1684 return 0;
1685}
1686
1687 static PyObject *
1688ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1689{
1690 list_T *list;
1691 PyObject *obj = NULL;
1692
1693 if (kwargs)
1694 {
1695 PyErr_SetString(PyExc_TypeError,
1696 _("list constructor does not accept keyword arguments"));
1697 return NULL;
1698 }
1699
1700 if (!PyArg_ParseTuple(args, "|O", &obj))
1701 return NULL;
1702
1703 if (!(list = py_list_alloc()))
1704 return NULL;
1705
1706 if (obj)
1707 {
1708 PyObject *lookup_dict;
1709
1710 if (!(lookup_dict = PyDict_New()))
1711 {
1712 list_unref(list);
1713 return NULL;
1714 }
1715
1716 if (list_py_concat(list, obj, lookup_dict) == -1)
1717 {
1718 Py_DECREF(lookup_dict);
1719 list_unref(list);
1720 return NULL;
1721 }
1722
1723 Py_DECREF(lookup_dict);
1724 }
1725
1726 return ListNew(subtype, list);
1727}
1728
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001729 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001730ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001731{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001732 pyll_remove(&self->ref, &lastlist);
1733 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001734
1735 DESTRUCTOR_FINISH(self);
1736}
1737
Bram Moolenaardb913952012-06-29 12:54:53 +02001738 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001739ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001740{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001741 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001742}
1743
1744 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001745ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001746{
1747 listitem_T *li;
1748
Bram Moolenaard6e39182013-05-21 18:30:34 +02001749 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001750 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001751 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001752 return NULL;
1753 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001754 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001755 if (li == NULL)
1756 {
1757 PyErr_SetVim(_("internal error: failed to get vim list item"));
1758 return NULL;
1759 }
1760 return ConvertToPyObject(&li->li_tv);
1761}
1762
1763#define PROC_RANGE \
1764 if (last < 0) {\
1765 if (last < -size) \
1766 last = 0; \
1767 else \
1768 last += size; \
1769 } \
1770 if (first < 0) \
1771 first = 0; \
1772 if (first > size) \
1773 first = size; \
1774 if (last > size) \
1775 last = size;
1776
1777 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001778ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001779{
1780 PyInt i;
1781 PyInt size = ListLength(self);
1782 PyInt n;
1783 PyObject *list;
1784 int reversed = 0;
1785
1786 PROC_RANGE
1787 if (first >= last)
1788 first = last;
1789
1790 n = last-first;
1791 list = PyList_New(n);
1792 if (list == NULL)
1793 return NULL;
1794
1795 for (i = 0; i < n; ++i)
1796 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001797 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001798 if (item == NULL)
1799 {
1800 Py_DECREF(list);
1801 return NULL;
1802 }
1803
1804 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1805 {
1806 Py_DECREF(item);
1807 Py_DECREF(list);
1808 return NULL;
1809 }
1810 }
1811
1812 return list;
1813}
1814
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001815typedef struct
1816{
1817 listwatch_T lw;
1818 list_T *list;
1819} listiterinfo_T;
1820
1821 static void
1822ListIterDestruct(listiterinfo_T *lii)
1823{
1824 list_rem_watch(lii->list, &lii->lw);
1825 PyMem_Free(lii);
1826}
1827
1828 static PyObject *
1829ListIterNext(listiterinfo_T **lii)
1830{
1831 PyObject *r;
1832
1833 if (!((*lii)->lw.lw_item))
1834 return NULL;
1835
1836 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1837 return NULL;
1838
1839 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1840
1841 return r;
1842}
1843
1844 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001845ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001846{
1847 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001848 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001849
1850 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1851 {
1852 PyErr_NoMemory();
1853 return NULL;
1854 }
1855
1856 list_add_watch(l, &lii->lw);
1857 lii->lw.lw_item = l->lv_first;
1858 lii->list = l;
1859
1860 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001861 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1862 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001863}
1864
Bram Moolenaardb913952012-06-29 12:54:53 +02001865 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001866ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001867{
1868 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001869 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001870 listitem_T *li;
1871 Py_ssize_t length = ListLength(self);
1872
1873 if (l->lv_lock)
1874 {
1875 PyErr_SetVim(_("list is locked"));
1876 return -1;
1877 }
1878 if (index>length || (index==length && obj==NULL))
1879 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001880 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001881 return -1;
1882 }
1883
1884 if (obj == NULL)
1885 {
1886 li = list_find(l, (long) index);
1887 list_remove(l, li, li);
1888 clear_tv(&li->li_tv);
1889 vim_free(li);
1890 return 0;
1891 }
1892
1893 if (ConvertFromPyObject(obj, &tv) == -1)
1894 return -1;
1895
1896 if (index == length)
1897 {
1898 if (list_append_tv(l, &tv) == FAIL)
1899 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001900 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001901 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001902 return -1;
1903 }
1904 }
1905 else
1906 {
1907 li = list_find(l, (long) index);
1908 clear_tv(&li->li_tv);
1909 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001910 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001911 }
1912 return 0;
1913}
1914
1915 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001916ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001917{
1918 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001919 PyObject *iterator;
1920 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02001921 listitem_T *li;
1922 listitem_T *next;
1923 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001924 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001925 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02001926
1927 if (l->lv_lock)
1928 {
1929 PyErr_SetVim(_("list is locked"));
1930 return -1;
1931 }
1932
1933 PROC_RANGE
1934
1935 if (first == size)
1936 li = NULL;
1937 else
1938 {
1939 li = list_find(l, (long) first);
1940 if (li == NULL)
1941 {
1942 PyErr_SetVim(_("internal error: no vim list item"));
1943 return -1;
1944 }
1945 if (last > first)
1946 {
1947 i = last - first;
1948 while (i-- && li != NULL)
1949 {
1950 next = li->li_next;
1951 listitem_remove(l, li);
1952 li = next;
1953 }
1954 }
1955 }
1956
1957 if (obj == NULL)
1958 return 0;
1959
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001960 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001961 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001962
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001963 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001964 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001965 if (ConvertFromPyObject(item, &v) == -1)
1966 {
1967 Py_DECREF(iterator);
1968 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001969 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001970 }
1971 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001972 if (list_insert_tv(l, &v, li) == FAIL)
1973 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001974 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001975 PyErr_SetVim(_("internal error: failed to add item to list"));
1976 return -1;
1977 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001978 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001979 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001980 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02001981 return 0;
1982}
1983
1984 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001985ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001986{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001987 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001988 PyObject *lookup_dict;
1989
1990 if (l->lv_lock)
1991 {
1992 PyErr_SetVim(_("list is locked"));
1993 return NULL;
1994 }
1995
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001996 if (!(lookup_dict = PyDict_New()))
1997 return NULL;
1998
Bram Moolenaardb913952012-06-29 12:54:53 +02001999 if (list_py_concat(l, obj, lookup_dict) == -1)
2000 {
2001 Py_DECREF(lookup_dict);
2002 return NULL;
2003 }
2004 Py_DECREF(lookup_dict);
2005
2006 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002007 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002008}
2009
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002010static char *ListAttrs[] = {
2011 "locked",
2012 NULL
2013};
2014
2015 static PyObject *
2016ListDir(PyObject *self)
2017{
2018 return ObjectDir(self, ListAttrs);
2019}
2020
Bram Moolenaar66b79852012-09-21 14:00:35 +02002021 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002022ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002023{
2024 if (val == NULL)
2025 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002026 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002027 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002028 return -1;
2029 }
2030
2031 if (strcmp(name, "locked") == 0)
2032 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002033 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002034 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002035 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002036 return -1;
2037 }
2038 else
2039 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002040 int istrue = PyObject_IsTrue(val);
2041 if (istrue == -1)
2042 return -1;
2043 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002044 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002045 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002046 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002047 }
2048 return 0;
2049 }
2050 else
2051 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002052 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002053 return -1;
2054 }
2055}
2056
Bram Moolenaardb913952012-06-29 12:54:53 +02002057static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002058 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2059 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2060 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002061};
2062
2063typedef struct
2064{
2065 PyObject_HEAD
2066 char_u *name;
2067} FunctionObject;
2068
2069static PyTypeObject FunctionType;
2070
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002071#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2072
Bram Moolenaardb913952012-06-29 12:54:53 +02002073 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002074FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002075{
2076 FunctionObject *self;
2077
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002078 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2079
Bram Moolenaardb913952012-06-29 12:54:53 +02002080 if (self == NULL)
2081 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002082
2083 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002084 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002085 if (!translated_function_exists(name))
2086 {
2087 PyErr_SetString(PyExc_ValueError,
2088 _("unnamed function does not exist"));
2089 return NULL;
2090 }
2091 self->name = vim_strsave(name);
2092 func_ref(self->name);
2093 }
2094 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002095 if ((self->name = get_expanded_name(name,
2096 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2097 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002098 {
Bram Moolenaar018acca2013-05-30 13:37:28 +02002099 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2100 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002101 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002102
2103 return (PyObject *)(self);
2104}
2105
2106 static PyObject *
2107FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2108{
2109 PyObject *self;
2110 char_u *name;
2111
2112 if (kwargs)
2113 {
2114 PyErr_SetString(PyExc_TypeError,
2115 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002116 return NULL;
2117 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002118
2119 if (!PyArg_ParseTuple(args, "s", &name))
2120 return NULL;
2121
2122 self = FunctionNew(subtype, name);
2123
2124 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002125}
2126
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002127 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002128FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002129{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002130 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002131 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002132
2133 DESTRUCTOR_FINISH(self);
2134}
2135
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002136static char *FunctionAttrs[] = {
2137 "softspace",
2138 NULL
2139};
2140
2141 static PyObject *
2142FunctionDir(PyObject *self)
2143{
2144 return ObjectDir(self, FunctionAttrs);
2145}
2146
Bram Moolenaardb913952012-06-29 12:54:53 +02002147 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002148FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002149{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002150 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002151 typval_T args;
2152 typval_T selfdicttv;
2153 typval_T rettv;
2154 dict_T *selfdict = NULL;
2155 PyObject *selfdictObject;
2156 PyObject *result;
2157 int error;
2158
2159 if (ConvertFromPyObject(argsObject, &args) == -1)
2160 return NULL;
2161
2162 if (kwargs != NULL)
2163 {
2164 selfdictObject = PyDict_GetItemString(kwargs, "self");
2165 if (selfdictObject != NULL)
2166 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002167 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002168 {
2169 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002170 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002171 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002172 selfdict = selfdicttv.vval.v_dict;
2173 }
2174 }
2175
Bram Moolenaar71700b82013-05-15 17:49:05 +02002176 Py_BEGIN_ALLOW_THREADS
2177 Python_Lock_Vim();
2178
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002179 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002180 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002181
2182 Python_Release_Vim();
2183 Py_END_ALLOW_THREADS
2184
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002185 if (VimTryEnd())
2186 result = NULL;
2187 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002188 {
2189 result = NULL;
2190 PyErr_SetVim(_("failed to run function"));
2191 }
2192 else
2193 result = ConvertToPyObject(&rettv);
2194
Bram Moolenaardb913952012-06-29 12:54:53 +02002195 clear_tv(&args);
2196 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002197 if (selfdict != NULL)
2198 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002199
2200 return result;
2201}
2202
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002203 static PyObject *
2204FunctionRepr(FunctionObject *self)
2205{
2206 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2207}
2208
Bram Moolenaardb913952012-06-29 12:54:53 +02002209static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002210 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2211 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002212};
2213
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002214/*
2215 * Options object
2216 */
2217
2218static PyTypeObject OptionsType;
2219
2220typedef int (*checkfun)(void *);
2221
2222typedef struct
2223{
2224 PyObject_HEAD
2225 int opt_type;
2226 void *from;
2227 checkfun Check;
2228 PyObject *fromObj;
2229} OptionsObject;
2230
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002231 static int
2232dummy_check(void *arg UNUSED)
2233{
2234 return 0;
2235}
2236
2237 static PyObject *
2238OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2239{
2240 OptionsObject *self;
2241
Bram Moolenaar774267b2013-05-21 20:51:59 +02002242 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002243 if (self == NULL)
2244 return NULL;
2245
2246 self->opt_type = opt_type;
2247 self->from = from;
2248 self->Check = Check;
2249 self->fromObj = fromObj;
2250 if (fromObj)
2251 Py_INCREF(fromObj);
2252
2253 return (PyObject *)(self);
2254}
2255
2256 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002257OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002258{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002259 PyObject_GC_UnTrack((void *)(self));
2260 Py_XDECREF(self->fromObj);
2261 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002262}
2263
2264 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002265OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002266{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002267 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002268 return 0;
2269}
2270
2271 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002272OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002273{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002274 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002275 return 0;
2276}
2277
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002278 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002279OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002280{
2281 char_u *key;
2282 int flags;
2283 long numval;
2284 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002285 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002286
Bram Moolenaard6e39182013-05-21 18:30:34 +02002287 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002288 return NULL;
2289
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002290 if (!(key = StringToChars(keyObject, &todecref)))
2291 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002292
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002293 if (*key == NUL)
2294 {
2295 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002296 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002297 return NULL;
2298 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002299
2300 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002301 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002302
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002303 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002304
2305 if (flags == 0)
2306 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002307 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002308 return NULL;
2309 }
2310
2311 if (flags & SOPT_UNSET)
2312 {
2313 Py_INCREF(Py_None);
2314 return Py_None;
2315 }
2316 else if (flags & SOPT_BOOL)
2317 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002318 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002319 r = numval ? Py_True : Py_False;
2320 Py_INCREF(r);
2321 return r;
2322 }
2323 else if (flags & SOPT_NUM)
2324 return PyInt_FromLong(numval);
2325 else if (flags & SOPT_STRING)
2326 {
2327 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002328 {
2329 PyObject *r = PyBytes_FromString((char *) stringval);
2330 vim_free(stringval);
2331 return r;
2332 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002333 else
2334 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002335 PyErr_SetString(PyExc_RuntimeError,
2336 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002337 return NULL;
2338 }
2339 }
2340 else
2341 {
2342 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2343 return NULL;
2344 }
2345}
2346
2347 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002348set_option_value_err(key, numval, stringval, opt_flags)
2349 char_u *key;
2350 int numval;
2351 char_u *stringval;
2352 int opt_flags;
2353{
2354 char_u *errmsg;
2355
2356 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2357 {
2358 if (VimTryEnd())
2359 return FAIL;
2360 PyErr_SetVim((char *)errmsg);
2361 return FAIL;
2362 }
2363 return OK;
2364}
2365
2366 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002367set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2368 char_u *key;
2369 int numval;
2370 char_u *stringval;
2371 int opt_flags;
2372 int opt_type;
2373 void *from;
2374{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002375 win_T *save_curwin = NULL;
2376 tabpage_T *save_curtab = NULL;
2377 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002378 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002379
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002380 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002381 switch (opt_type)
2382 {
2383 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002384 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2385 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002386 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002387 if (VimTryEnd())
2388 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002389 PyErr_SetVim("Problem while switching windows.");
2390 return -1;
2391 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002392 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002393 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002394 if (r == FAIL)
2395 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002396 break;
2397 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002398 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002399 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002400 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002401 if (r == FAIL)
2402 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002403 break;
2404 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002405 r = set_option_value_err(key, numval, stringval, opt_flags);
2406 if (r == FAIL)
2407 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002408 break;
2409 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002410 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002411}
2412
2413 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002414OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002415{
2416 char_u *key;
2417 int flags;
2418 int opt_flags;
2419 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002420 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002421
Bram Moolenaard6e39182013-05-21 18:30:34 +02002422 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002423 return -1;
2424
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002425 if (!(key = StringToChars(keyObject, &todecref)))
2426 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002427
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002428 if (*key == NUL)
2429 {
2430 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002431 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002432 return -1;
2433 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002434
2435 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002436 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002437
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002438 if (flags == 0)
2439 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002440 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002441 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002442 return -1;
2443 }
2444
2445 if (valObject == NULL)
2446 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002447 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002448 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002449 PyErr_SetString(PyExc_ValueError,
2450 _("unable to unset global option"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002451 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002452 return -1;
2453 }
2454 else if (!(flags & SOPT_GLOBAL))
2455 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002456 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2457 "without global value"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002458 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002459 return -1;
2460 }
2461 else
2462 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002463 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002464 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002465 return 0;
2466 }
2467 }
2468
Bram Moolenaard6e39182013-05-21 18:30:34 +02002469 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002470
2471 if (flags & SOPT_BOOL)
2472 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002473 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002474
Bram Moolenaarb983f752013-05-15 16:11:50 +02002475 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002476 r = -1;
2477 else
2478 r = set_option_value_for(key, istrue, NULL,
2479 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002480 }
2481 else if (flags & SOPT_NUM)
2482 {
2483 int val;
2484
2485#if PY_MAJOR_VERSION < 3
2486 if (PyInt_Check(valObject))
2487 val = PyInt_AsLong(valObject);
2488 else
2489#endif
2490 if (PyLong_Check(valObject))
2491 val = PyLong_AsLong(valObject);
2492 else
2493 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002494 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002495 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002496 return -1;
2497 }
2498
2499 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002500 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002501 }
2502 else
2503 {
2504 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002505 PyObject *todecref;
2506
2507 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002508 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002509 r = set_option_value_for(key, 0, val, opt_flags,
2510 self->opt_type, self->from);
2511 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002512 }
2513 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002514 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002515 }
2516
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002517 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002518
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002519 return r;
2520}
2521
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002522static PyMappingMethods OptionsAsMapping = {
2523 (lenfunc) NULL,
2524 (binaryfunc) OptionsItem,
2525 (objobjargproc) OptionsAssItem,
2526};
2527
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002528/* Tabpage object
2529 */
2530
2531typedef struct
2532{
2533 PyObject_HEAD
2534 tabpage_T *tab;
2535} TabPageObject;
2536
2537static PyObject *WinListNew(TabPageObject *tabObject);
2538
2539static PyTypeObject TabPageType;
2540
2541 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002542CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002543{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002544 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002545 {
2546 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2547 return -1;
2548 }
2549
2550 return 0;
2551}
2552
2553 static PyObject *
2554TabPageNew(tabpage_T *tab)
2555{
2556 TabPageObject *self;
2557
2558 if (TAB_PYTHON_REF(tab))
2559 {
2560 self = TAB_PYTHON_REF(tab);
2561 Py_INCREF(self);
2562 }
2563 else
2564 {
2565 self = PyObject_NEW(TabPageObject, &TabPageType);
2566 if (self == NULL)
2567 return NULL;
2568 self->tab = tab;
2569 TAB_PYTHON_REF(tab) = self;
2570 }
2571
2572 return (PyObject *)(self);
2573}
2574
2575 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002576TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002577{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002578 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2579 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002580
2581 DESTRUCTOR_FINISH(self);
2582}
2583
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002584static char *TabPageAttrs[] = {
2585 "windows", "number", "vars", "window", "valid",
2586 NULL
2587};
2588
2589 static PyObject *
2590TabPageDir(PyObject *self)
2591{
2592 return ObjectDir(self, TabPageAttrs);
2593}
2594
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002595 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002596TabPageAttrValid(TabPageObject *self, char *name)
2597{
2598 PyObject *r;
2599
2600 if (strcmp(name, "valid") != 0)
2601 return NULL;
2602
2603 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2604 Py_INCREF(r);
2605 return r;
2606}
2607
2608 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002609TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002610{
2611 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002612 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002613 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002614 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002615 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002616 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002617 else if (strcmp(name, "window") == 0)
2618 {
2619 /* For current tab window.c does not bother to set or update tp_curwin
2620 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002621 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002622 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002623 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002624 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002625 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002626 else if (strcmp(name, "__members__") == 0)
2627 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002628 return NULL;
2629}
2630
2631 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002632TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002633{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002634 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002635 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002636 else
2637 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002638 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002639
2640 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002641 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2642 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002643 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002644 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002645 }
2646}
2647
2648static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002649 /* name, function, calling, documentation */
2650 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2651 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002652};
2653
2654/*
2655 * Window list object
2656 */
2657
2658static PyTypeObject TabListType;
2659static PySequenceMethods TabListAsSeq;
2660
2661typedef struct
2662{
2663 PyObject_HEAD
2664} TabListObject;
2665
2666 static PyInt
2667TabListLength(PyObject *self UNUSED)
2668{
2669 tabpage_T *tp = first_tabpage;
2670 PyInt n = 0;
2671
2672 while (tp != NULL)
2673 {
2674 ++n;
2675 tp = tp->tp_next;
2676 }
2677
2678 return n;
2679}
2680
2681 static PyObject *
2682TabListItem(PyObject *self UNUSED, PyInt n)
2683{
2684 tabpage_T *tp;
2685
2686 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2687 if (n == 0)
2688 return TabPageNew(tp);
2689
2690 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2691 return NULL;
2692}
2693
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002694/* Window object
2695 */
2696
2697typedef struct
2698{
2699 PyObject_HEAD
2700 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002701 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002702} WindowObject;
2703
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002704static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002705
2706 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002707CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002708{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002709 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002710 {
2711 PyErr_SetVim(_("attempt to refer to deleted window"));
2712 return -1;
2713 }
2714
2715 return 0;
2716}
2717
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002718 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002719WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002720{
2721 /* We need to handle deletion of windows underneath us.
2722 * If we add a "w_python*_ref" field to the win_T structure,
2723 * then we can get at it in win_free() in vim. We then
2724 * need to create only ONE Python object per window - if
2725 * we try to create a second, just INCREF the existing one
2726 * and return it. The (single) Python object referring to
2727 * the window is stored in "w_python*_ref".
2728 * On a win_free() we set the Python object's win_T* field
2729 * to an invalid value. We trap all uses of a window
2730 * object, and reject them if the win_T* field is invalid.
2731 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002732 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002733 * w_python_ref and w_python3_ref fields respectively.
2734 */
2735
2736 WindowObject *self;
2737
2738 if (WIN_PYTHON_REF(win))
2739 {
2740 self = WIN_PYTHON_REF(win);
2741 Py_INCREF(self);
2742 }
2743 else
2744 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002745 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002746 if (self == NULL)
2747 return NULL;
2748 self->win = win;
2749 WIN_PYTHON_REF(win) = self;
2750 }
2751
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002752 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2753
Bram Moolenaar971db462013-05-12 18:44:48 +02002754 return (PyObject *)(self);
2755}
2756
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002757 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002758WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002759{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002760 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002761 if (self->win && self->win != INVALID_WINDOW_VALUE)
2762 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002763 Py_XDECREF(((PyObject *)(self->tabObject)));
2764 PyObject_GC_Del((void *)(self));
2765}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002766
Bram Moolenaar774267b2013-05-21 20:51:59 +02002767 static int
2768WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2769{
2770 Py_VISIT(((PyObject *)(self->tabObject)));
2771 return 0;
2772}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002773
Bram Moolenaar774267b2013-05-21 20:51:59 +02002774 static int
2775WindowClear(WindowObject *self)
2776{
2777 Py_CLEAR(self->tabObject);
2778 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002779}
2780
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002781 static win_T *
2782get_firstwin(TabPageObject *tabObject)
2783{
2784 if (tabObject)
2785 {
2786 if (CheckTabPage(tabObject))
2787 return NULL;
2788 /* For current tab window.c does not bother to set or update tp_firstwin
2789 */
2790 else if (tabObject->tab == curtab)
2791 return firstwin;
2792 else
2793 return tabObject->tab->tp_firstwin;
2794 }
2795 else
2796 return firstwin;
2797}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002798static char *WindowAttrs[] = {
2799 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2800 "tabpage", "valid",
2801 NULL
2802};
2803
2804 static PyObject *
2805WindowDir(PyObject *self)
2806{
2807 return ObjectDir(self, WindowAttrs);
2808}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002809
Bram Moolenaar971db462013-05-12 18:44:48 +02002810 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002811WindowAttrValid(WindowObject *self, char *name)
2812{
2813 PyObject *r;
2814
2815 if (strcmp(name, "valid") != 0)
2816 return NULL;
2817
2818 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2819 Py_INCREF(r);
2820 return r;
2821}
2822
2823 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002824WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002825{
2826 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002827 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002828 else if (strcmp(name, "cursor") == 0)
2829 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002830 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002831
2832 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2833 }
2834 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002835 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002836#ifdef FEAT_WINDOWS
2837 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002838 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002839#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002840#ifdef FEAT_VERTSPLIT
2841 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002842 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002843 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002844 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002845#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002846 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002847 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002848 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002849 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2850 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002851 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002852 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002853 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002854 return NULL;
2855 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002856 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002857 }
2858 else if (strcmp(name, "tabpage") == 0)
2859 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002860 Py_INCREF(self->tabObject);
2861 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002862 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002863 else if (strcmp(name, "__members__") == 0)
2864 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002865 else
2866 return NULL;
2867}
2868
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002869 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002870WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002871{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002872 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002873 return -1;
2874
2875 if (strcmp(name, "buffer") == 0)
2876 {
2877 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2878 return -1;
2879 }
2880 else if (strcmp(name, "cursor") == 0)
2881 {
2882 long lnum;
2883 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002884
2885 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2886 return -1;
2887
Bram Moolenaard6e39182013-05-21 18:30:34 +02002888 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002889 {
2890 PyErr_SetVim(_("cursor position outside buffer"));
2891 return -1;
2892 }
2893
2894 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002895 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002896 return -1;
2897
Bram Moolenaard6e39182013-05-21 18:30:34 +02002898 self->win->w_cursor.lnum = lnum;
2899 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002900#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002901 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002902#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002903 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002904 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002905
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002906 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002907 return 0;
2908 }
2909 else if (strcmp(name, "height") == 0)
2910 {
2911 int height;
2912 win_T *savewin;
2913
2914 if (!PyArg_Parse(val, "i", &height))
2915 return -1;
2916
2917#ifdef FEAT_GUI
2918 need_mouse_correct = TRUE;
2919#endif
2920 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002921 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002922
2923 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002924 win_setheight(height);
2925 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002926 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002927 return -1;
2928
2929 return 0;
2930 }
2931#ifdef FEAT_VERTSPLIT
2932 else if (strcmp(name, "width") == 0)
2933 {
2934 int width;
2935 win_T *savewin;
2936
2937 if (!PyArg_Parse(val, "i", &width))
2938 return -1;
2939
2940#ifdef FEAT_GUI
2941 need_mouse_correct = TRUE;
2942#endif
2943 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002944 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002945
2946 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002947 win_setwidth(width);
2948 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002949 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002950 return -1;
2951
2952 return 0;
2953 }
2954#endif
2955 else
2956 {
2957 PyErr_SetString(PyExc_AttributeError, name);
2958 return -1;
2959 }
2960}
2961
2962 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002963WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002964{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002965 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002966 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002967 else
2968 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002969 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002970
Bram Moolenaar6d216452013-05-12 19:00:41 +02002971 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002972 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002973 (self));
2974 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002975 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002976 }
2977}
2978
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002979static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002980 /* name, function, calling, documentation */
2981 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2982 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002983};
2984
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002985/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002986 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002987 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002988
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002989static PyTypeObject WinListType;
2990static PySequenceMethods WinListAsSeq;
2991
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002992typedef struct
2993{
2994 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002995 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002996} WinListObject;
2997
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002998 static PyObject *
2999WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003000{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003001 WinListObject *self;
3002
3003 self = PyObject_NEW(WinListObject, &WinListType);
3004 self->tabObject = tabObject;
3005 Py_INCREF(tabObject);
3006
3007 return (PyObject *)(self);
3008}
3009
3010 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003011WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003012{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003013 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003014
3015 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003016 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003017 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003018 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003019
3020 DESTRUCTOR_FINISH(self);
3021}
3022
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003023 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003024WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003025{
3026 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003027 PyInt n = 0;
3028
Bram Moolenaard6e39182013-05-21 18:30:34 +02003029 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003030 return -1;
3031
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003032 while (w != NULL)
3033 {
3034 ++n;
3035 w = W_NEXT(w);
3036 }
3037
3038 return n;
3039}
3040
3041 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003042WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003043{
3044 win_T *w;
3045
Bram Moolenaard6e39182013-05-21 18:30:34 +02003046 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003047 return NULL;
3048
3049 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003050 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003051 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003052
3053 PyErr_SetString(PyExc_IndexError, _("no such window"));
3054 return NULL;
3055}
3056
3057/* Convert a Python string into a Vim line.
3058 *
3059 * The result is in allocated memory. All internal nulls are replaced by
3060 * newline characters. It is an error for the string to contain newline
3061 * characters.
3062 *
3063 * On errors, the Python exception data is set, and NULL is returned.
3064 */
3065 static char *
3066StringToLine(PyObject *obj)
3067{
3068 const char *str;
3069 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003070 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003071 PyInt len;
3072 PyInt i;
3073 char *p;
3074
3075 if (obj == NULL || !PyString_Check(obj))
3076 {
3077 PyErr_BadArgument();
3078 return NULL;
3079 }
3080
Bram Moolenaar19e60942011-06-19 00:27:51 +02003081 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
3082 str = PyString_AsString(bytes);
3083 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003084
3085 /*
3086 * Error checking: String must not contain newlines, as we
3087 * are replacing a single line, and we must replace it with
3088 * a single line.
3089 * A trailing newline is removed, so that append(f.readlines()) works.
3090 */
3091 p = memchr(str, '\n', len);
3092 if (p != NULL)
3093 {
3094 if (p == str + len - 1)
3095 --len;
3096 else
3097 {
3098 PyErr_SetVim(_("string cannot contain newlines"));
3099 return NULL;
3100 }
3101 }
3102
3103 /* Create a copy of the string, with internal nulls replaced by
3104 * newline characters, as is the vim convention.
3105 */
3106 save = (char *)alloc((unsigned)(len+1));
3107 if (save == NULL)
3108 {
3109 PyErr_NoMemory();
3110 return NULL;
3111 }
3112
3113 for (i = 0; i < len; ++i)
3114 {
3115 if (str[i] == '\0')
3116 save[i] = '\n';
3117 else
3118 save[i] = str[i];
3119 }
3120
3121 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003122 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003123
3124 return save;
3125}
3126
3127/* Get a line from the specified buffer. The line number is
3128 * in Vim format (1-based). The line is returned as a Python
3129 * string object.
3130 */
3131 static PyObject *
3132GetBufferLine(buf_T *buf, PyInt n)
3133{
3134 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3135}
3136
3137
3138/* Get a list of lines from the specified buffer. The line numbers
3139 * are in Vim format (1-based). The range is from lo up to, but not
3140 * including, hi. The list is returned as a Python list of string objects.
3141 */
3142 static PyObject *
3143GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3144{
3145 PyInt i;
3146 PyInt n = hi - lo;
3147 PyObject *list = PyList_New(n);
3148
3149 if (list == NULL)
3150 return NULL;
3151
3152 for (i = 0; i < n; ++i)
3153 {
3154 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3155
3156 /* Error check - was the Python string creation OK? */
3157 if (str == NULL)
3158 {
3159 Py_DECREF(list);
3160 return NULL;
3161 }
3162
3163 /* Set the list item */
3164 if (PyList_SetItem(list, i, str))
3165 {
3166 Py_DECREF(str);
3167 Py_DECREF(list);
3168 return NULL;
3169 }
3170 }
3171
3172 /* The ownership of the Python list is passed to the caller (ie,
3173 * the caller should Py_DECREF() the object when it is finished
3174 * with it).
3175 */
3176
3177 return list;
3178}
3179
3180/*
3181 * Check if deleting lines made the cursor position invalid.
3182 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3183 * deleted).
3184 */
3185 static void
3186py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3187{
3188 if (curwin->w_cursor.lnum >= lo)
3189 {
3190 /* Adjust the cursor position if it's in/after the changed
3191 * lines. */
3192 if (curwin->w_cursor.lnum >= hi)
3193 {
3194 curwin->w_cursor.lnum += extra;
3195 check_cursor_col();
3196 }
3197 else if (extra < 0)
3198 {
3199 curwin->w_cursor.lnum = lo;
3200 check_cursor();
3201 }
3202 else
3203 check_cursor_col();
3204 changed_cline_bef_curs();
3205 }
3206 invalidate_botline();
3207}
3208
Bram Moolenaar19e60942011-06-19 00:27:51 +02003209/*
3210 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003211 * in Vim format (1-based). The replacement line is given as
3212 * a Python string object. The object is checked for validity
3213 * and correct format. Errors are returned as a value of FAIL.
3214 * The return value is OK on success.
3215 * If OK is returned and len_change is not NULL, *len_change
3216 * is set to the change in the buffer length.
3217 */
3218 static int
3219SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3220{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003221 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003222 * There are three cases:
3223 * 1. NULL, or None - this is a deletion.
3224 * 2. A string - this is a replacement.
3225 * 3. Anything else - this is an error.
3226 */
3227 if (line == Py_None || line == NULL)
3228 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003229 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003230
3231 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003232 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003233
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003234 VimTryStart();
3235
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003236 if (u_savedel((linenr_T)n, 1L) == FAIL)
3237 PyErr_SetVim(_("cannot save undo information"));
3238 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3239 PyErr_SetVim(_("cannot delete line"));
3240 else
3241 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003242 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003243 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3244 deleted_lines_mark((linenr_T)n, 1L);
3245 }
3246
Bram Moolenaar105bc352013-05-17 16:03:57 +02003247 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003248
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003249 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003250 return FAIL;
3251
3252 if (len_change)
3253 *len_change = -1;
3254
3255 return OK;
3256 }
3257 else if (PyString_Check(line))
3258 {
3259 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003260 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003261
3262 if (save == NULL)
3263 return FAIL;
3264
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003265 VimTryStart();
3266
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003267 /* We do not need to free "save" if ml_replace() consumes it. */
3268 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003269 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003270
3271 if (u_savesub((linenr_T)n) == FAIL)
3272 {
3273 PyErr_SetVim(_("cannot save undo information"));
3274 vim_free(save);
3275 }
3276 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3277 {
3278 PyErr_SetVim(_("cannot replace line"));
3279 vim_free(save);
3280 }
3281 else
3282 changed_bytes((linenr_T)n, 0);
3283
Bram Moolenaar105bc352013-05-17 16:03:57 +02003284 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003285
3286 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003287 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003288 check_cursor_col();
3289
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003290 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003291 return FAIL;
3292
3293 if (len_change)
3294 *len_change = 0;
3295
3296 return OK;
3297 }
3298 else
3299 {
3300 PyErr_BadArgument();
3301 return FAIL;
3302 }
3303}
3304
Bram Moolenaar19e60942011-06-19 00:27:51 +02003305/* Replace a range of lines in the specified buffer. The line numbers are in
3306 * Vim format (1-based). The range is from lo up to, but not including, hi.
3307 * The replacement lines are given as a Python list of string objects. The
3308 * list is checked for validity and correct format. Errors are returned as a
3309 * value of FAIL. The return value is OK on success.
3310 * If OK is returned and len_change is not NULL, *len_change
3311 * is set to the change in the buffer length.
3312 */
3313 static int
3314SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3315{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003316 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003317 * There are three cases:
3318 * 1. NULL, or None - this is a deletion.
3319 * 2. A list - this is a replacement.
3320 * 3. Anything else - this is an error.
3321 */
3322 if (list == Py_None || list == NULL)
3323 {
3324 PyInt i;
3325 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003326 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003327
3328 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003329 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003330 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003331
3332 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3333 PyErr_SetVim(_("cannot save undo information"));
3334 else
3335 {
3336 for (i = 0; i < n; ++i)
3337 {
3338 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3339 {
3340 PyErr_SetVim(_("cannot delete line"));
3341 break;
3342 }
3343 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003344 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003345 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3346 deleted_lines_mark((linenr_T)lo, (long)i);
3347 }
3348
Bram Moolenaar105bc352013-05-17 16:03:57 +02003349 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003350
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003351 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003352 return FAIL;
3353
3354 if (len_change)
3355 *len_change = -n;
3356
3357 return OK;
3358 }
3359 else if (PyList_Check(list))
3360 {
3361 PyInt i;
3362 PyInt new_len = PyList_Size(list);
3363 PyInt old_len = hi - lo;
3364 PyInt extra = 0; /* lines added to text, can be negative */
3365 char **array;
3366 buf_T *savebuf;
3367
3368 if (new_len == 0) /* avoid allocating zero bytes */
3369 array = NULL;
3370 else
3371 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003372 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003373 if (array == NULL)
3374 {
3375 PyErr_NoMemory();
3376 return FAIL;
3377 }
3378 }
3379
3380 for (i = 0; i < new_len; ++i)
3381 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003382 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003383
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003384 if (!(line = PyList_GetItem(list, i)) ||
3385 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003386 {
3387 while (i)
3388 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003389 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003390 return FAIL;
3391 }
3392 }
3393
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003394 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003395 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003396
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003397 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003398 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003399
3400 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3401 PyErr_SetVim(_("cannot save undo information"));
3402
3403 /* If the size of the range is reducing (ie, new_len < old_len) we
3404 * need to delete some old_len. We do this at the start, by
3405 * repeatedly deleting line "lo".
3406 */
3407 if (!PyErr_Occurred())
3408 {
3409 for (i = 0; i < old_len - new_len; ++i)
3410 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3411 {
3412 PyErr_SetVim(_("cannot delete line"));
3413 break;
3414 }
3415 extra -= i;
3416 }
3417
3418 /* For as long as possible, replace the existing old_len with the
3419 * new old_len. This is a more efficient operation, as it requires
3420 * less memory allocation and freeing.
3421 */
3422 if (!PyErr_Occurred())
3423 {
3424 for (i = 0; i < old_len && i < new_len; ++i)
3425 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3426 == FAIL)
3427 {
3428 PyErr_SetVim(_("cannot replace line"));
3429 break;
3430 }
3431 }
3432 else
3433 i = 0;
3434
3435 /* Now we may need to insert the remaining new old_len. If we do, we
3436 * must free the strings as we finish with them (we can't pass the
3437 * responsibility to vim in this case).
3438 */
3439 if (!PyErr_Occurred())
3440 {
3441 while (i < new_len)
3442 {
3443 if (ml_append((linenr_T)(lo + i - 1),
3444 (char_u *)array[i], 0, FALSE) == FAIL)
3445 {
3446 PyErr_SetVim(_("cannot insert line"));
3447 break;
3448 }
3449 vim_free(array[i]);
3450 ++i;
3451 ++extra;
3452 }
3453 }
3454
3455 /* Free any left-over old_len, as a result of an error */
3456 while (i < new_len)
3457 {
3458 vim_free(array[i]);
3459 ++i;
3460 }
3461
3462 /* Free the array of old_len. All of its contents have now
3463 * been dealt with (either freed, or the responsibility passed
3464 * to vim.
3465 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003466 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003467
3468 /* Adjust marks. Invalidate any which lie in the
3469 * changed range, and move any in the remainder of the buffer.
3470 */
3471 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3472 (long)MAXLNUM, (long)extra);
3473 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3474
Bram Moolenaar105bc352013-05-17 16:03:57 +02003475 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003476 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3477
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003478 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003479 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003480
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003481 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003482 return FAIL;
3483
3484 if (len_change)
3485 *len_change = new_len - old_len;
3486
3487 return OK;
3488 }
3489 else
3490 {
3491 PyErr_BadArgument();
3492 return FAIL;
3493 }
3494}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003495
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003496/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003497 * The line number is in Vim format (1-based). The lines to be inserted are
3498 * given as a Python list of string objects or as a single string. The lines
3499 * to be added are checked for validity and correct format. Errors are
3500 * returned as a value of FAIL. The return value is OK on success.
3501 * If OK is returned and len_change is not NULL, *len_change
3502 * is set to the change in the buffer length.
3503 */
3504 static int
3505InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3506{
3507 /* First of all, we check the type of the supplied Python object.
3508 * It must be a string or a list, or the call is in error.
3509 */
3510 if (PyString_Check(lines))
3511 {
3512 char *str = StringToLine(lines);
3513 buf_T *savebuf;
3514
3515 if (str == NULL)
3516 return FAIL;
3517
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003518 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003519 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003520 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003521
3522 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3523 PyErr_SetVim(_("cannot save undo information"));
3524 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3525 PyErr_SetVim(_("cannot insert line"));
3526 else
3527 appended_lines_mark((linenr_T)n, 1L);
3528
3529 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003530 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003531 update_screen(VALID);
3532
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003533 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003534 return FAIL;
3535
3536 if (len_change)
3537 *len_change = 1;
3538
3539 return OK;
3540 }
3541 else if (PyList_Check(lines))
3542 {
3543 PyInt i;
3544 PyInt size = PyList_Size(lines);
3545 char **array;
3546 buf_T *savebuf;
3547
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003548 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003549 if (array == NULL)
3550 {
3551 PyErr_NoMemory();
3552 return FAIL;
3553 }
3554
3555 for (i = 0; i < size; ++i)
3556 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003557 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003558
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003559 if (!(line = PyList_GetItem(lines, i)) ||
3560 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003561 {
3562 while (i)
3563 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003564 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003565 return FAIL;
3566 }
3567 }
3568
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003569 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003570 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003571 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003572
3573 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3574 PyErr_SetVim(_("cannot save undo information"));
3575 else
3576 {
3577 for (i = 0; i < size; ++i)
3578 {
3579 if (ml_append((linenr_T)(n + i),
3580 (char_u *)array[i], 0, FALSE) == FAIL)
3581 {
3582 PyErr_SetVim(_("cannot insert line"));
3583
3584 /* Free the rest of the lines */
3585 while (i < size)
3586 vim_free(array[i++]);
3587
3588 break;
3589 }
3590 vim_free(array[i]);
3591 }
3592 if (i > 0)
3593 appended_lines_mark((linenr_T)n, (long)i);
3594 }
3595
3596 /* Free the array of lines. All of its contents have now
3597 * been freed.
3598 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003599 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003600
Bram Moolenaar105bc352013-05-17 16:03:57 +02003601 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003602 update_screen(VALID);
3603
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003604 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003605 return FAIL;
3606
3607 if (len_change)
3608 *len_change = size;
3609
3610 return OK;
3611 }
3612 else
3613 {
3614 PyErr_BadArgument();
3615 return FAIL;
3616 }
3617}
3618
3619/*
3620 * Common routines for buffers and line ranges
3621 * -------------------------------------------
3622 */
3623
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003624typedef struct
3625{
3626 PyObject_HEAD
3627 buf_T *buf;
3628} BufferObject;
3629
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003630 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003631CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003632{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003633 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003634 {
3635 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3636 return -1;
3637 }
3638
3639 return 0;
3640}
3641
3642 static PyObject *
3643RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3644{
3645 if (CheckBuffer(self))
3646 return NULL;
3647
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003648 if (end == -1)
3649 end = self->buf->b_ml.ml_line_count;
3650
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003651 if (n < 0)
3652 n += end - start + 1;
3653
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003654 if (n < 0 || n > end - start)
3655 {
3656 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3657 return NULL;
3658 }
3659
3660 return GetBufferLine(self->buf, n+start);
3661}
3662
3663 static PyObject *
3664RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3665{
3666 PyInt size;
3667
3668 if (CheckBuffer(self))
3669 return NULL;
3670
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003671 if (end == -1)
3672 end = self->buf->b_ml.ml_line_count;
3673
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003674 size = end - start + 1;
3675
3676 if (lo < 0)
3677 lo = 0;
3678 else if (lo > size)
3679 lo = size;
3680 if (hi < 0)
3681 hi = 0;
3682 if (hi < lo)
3683 hi = lo;
3684 else if (hi > size)
3685 hi = size;
3686
3687 return GetBufferLineList(self->buf, lo+start, hi+start);
3688}
3689
3690 static PyInt
3691RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3692{
3693 PyInt len_change;
3694
3695 if (CheckBuffer(self))
3696 return -1;
3697
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003698 if (end == -1)
3699 end = self->buf->b_ml.ml_line_count;
3700
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003701 if (n < 0)
3702 n += end - start + 1;
3703
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003704 if (n < 0 || n > end - start)
3705 {
3706 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3707 return -1;
3708 }
3709
3710 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3711 return -1;
3712
3713 if (new_end)
3714 *new_end = end + len_change;
3715
3716 return 0;
3717}
3718
Bram Moolenaar19e60942011-06-19 00:27:51 +02003719 static PyInt
3720RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3721{
3722 PyInt size;
3723 PyInt len_change;
3724
3725 /* Self must be a valid buffer */
3726 if (CheckBuffer(self))
3727 return -1;
3728
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003729 if (end == -1)
3730 end = self->buf->b_ml.ml_line_count;
3731
Bram Moolenaar19e60942011-06-19 00:27:51 +02003732 /* Sort out the slice range */
3733 size = end - start + 1;
3734
3735 if (lo < 0)
3736 lo = 0;
3737 else if (lo > size)
3738 lo = size;
3739 if (hi < 0)
3740 hi = 0;
3741 if (hi < lo)
3742 hi = lo;
3743 else if (hi > size)
3744 hi = size;
3745
3746 if (SetBufferLineList(self->buf, lo + start, hi + start,
3747 val, &len_change) == FAIL)
3748 return -1;
3749
3750 if (new_end)
3751 *new_end = end + len_change;
3752
3753 return 0;
3754}
3755
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003756
3757 static PyObject *
3758RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3759{
3760 PyObject *lines;
3761 PyInt len_change;
3762 PyInt max;
3763 PyInt n;
3764
3765 if (CheckBuffer(self))
3766 return NULL;
3767
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003768 if (end == -1)
3769 end = self->buf->b_ml.ml_line_count;
3770
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003771 max = n = end - start + 1;
3772
3773 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3774 return NULL;
3775
3776 if (n < 0 || n > max)
3777 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003778 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003779 return NULL;
3780 }
3781
3782 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3783 return NULL;
3784
3785 if (new_end)
3786 *new_end = end + len_change;
3787
3788 Py_INCREF(Py_None);
3789 return Py_None;
3790}
3791
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003792/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003793 */
3794
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003795static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003796static PySequenceMethods RangeAsSeq;
3797static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003798
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003799typedef struct
3800{
3801 PyObject_HEAD
3802 BufferObject *buf;
3803 PyInt start;
3804 PyInt end;
3805} RangeObject;
3806
3807 static PyObject *
3808RangeNew(buf_T *buf, PyInt start, PyInt end)
3809{
3810 BufferObject *bufr;
3811 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003812 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003813 if (self == NULL)
3814 return NULL;
3815
3816 bufr = (BufferObject *)BufferNew(buf);
3817 if (bufr == NULL)
3818 {
3819 Py_DECREF(self);
3820 return NULL;
3821 }
3822 Py_INCREF(bufr);
3823
3824 self->buf = bufr;
3825 self->start = start;
3826 self->end = end;
3827
3828 return (PyObject *)(self);
3829}
3830
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003831 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003832RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003833{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003834 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003835 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003836 PyObject_GC_Del((void *)(self));
3837}
3838
3839 static int
3840RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3841{
3842 Py_VISIT(((PyObject *)(self->buf)));
3843 return 0;
3844}
3845
3846 static int
3847RangeClear(RangeObject *self)
3848{
3849 Py_CLEAR(self->buf);
3850 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003851}
3852
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003853 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003854RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003855{
3856 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003857 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003858 return -1; /* ??? */
3859
Bram Moolenaard6e39182013-05-21 18:30:34 +02003860 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003861}
3862
3863 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003864RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003865{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003866 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003867}
3868
3869 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003870RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003871{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003872 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003873}
3874
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003875static char *RangeAttrs[] = {
3876 "start", "end",
3877 NULL
3878};
3879
3880 static PyObject *
3881RangeDir(PyObject *self)
3882{
3883 return ObjectDir(self, RangeAttrs);
3884}
3885
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003886 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003887RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003888{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003889 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003890}
3891
3892 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003893RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003894{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003895 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003896 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
3897 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003898 else
3899 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003900 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003901
3902 if (name == NULL)
3903 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003904
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003905 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02003906 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003907 }
3908}
3909
3910static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003911 /* name, function, calling, documentation */
3912 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003913 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3914 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003915};
3916
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003917static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003918static PySequenceMethods BufferAsSeq;
3919static PyMappingMethods BufferAsMapping;
3920
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003921 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003922BufferNew(buf_T *buf)
3923{
3924 /* We need to handle deletion of buffers underneath us.
3925 * If we add a "b_python*_ref" field to the buf_T structure,
3926 * then we can get at it in buf_freeall() in vim. We then
3927 * need to create only ONE Python object per buffer - if
3928 * we try to create a second, just INCREF the existing one
3929 * and return it. The (single) Python object referring to
3930 * the buffer is stored in "b_python*_ref".
3931 * Question: what to do on a buf_freeall(). We'll probably
3932 * have to either delete the Python object (DECREF it to
3933 * zero - a bad idea, as it leaves dangling refs!) or
3934 * set the buf_T * value to an invalid value (-1?), which
3935 * means we need checks in all access functions... Bah.
3936 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003937 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003938 * b_python_ref and b_python3_ref fields respectively.
3939 */
3940
3941 BufferObject *self;
3942
3943 if (BUF_PYTHON_REF(buf) != NULL)
3944 {
3945 self = BUF_PYTHON_REF(buf);
3946 Py_INCREF(self);
3947 }
3948 else
3949 {
3950 self = PyObject_NEW(BufferObject, &BufferType);
3951 if (self == NULL)
3952 return NULL;
3953 self->buf = buf;
3954 BUF_PYTHON_REF(buf) = self;
3955 }
3956
3957 return (PyObject *)(self);
3958}
3959
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003960 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003961BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003962{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003963 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3964 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003965
3966 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003967}
3968
Bram Moolenaar971db462013-05-12 18:44:48 +02003969 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003970BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003971{
3972 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003973 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003974 return -1; /* ??? */
3975
Bram Moolenaard6e39182013-05-21 18:30:34 +02003976 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003977}
3978
3979 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003980BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003981{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003982 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003983}
3984
3985 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003986BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003987{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003988 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003989}
3990
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003991static char *BufferAttrs[] = {
3992 "name", "number", "vars", "options", "valid",
3993 NULL
3994};
3995
3996 static PyObject *
3997BufferDir(PyObject *self)
3998{
3999 return ObjectDir(self, BufferAttrs);
4000}
4001
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004002 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004003BufferAttrValid(BufferObject *self, char *name)
4004{
4005 PyObject *r;
4006
4007 if (strcmp(name, "valid") != 0)
4008 return NULL;
4009
4010 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4011 Py_INCREF(r);
4012 return r;
4013}
4014
4015 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004016BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004017{
4018 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004019 return PyString_FromString((self->buf->b_ffname == NULL
4020 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004021 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004022 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004023 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004024 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004025 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004026 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4027 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004028 else if (strcmp(name, "__members__") == 0)
4029 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004030 else
4031 return NULL;
4032}
4033
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004034 static int
4035BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4036{
4037 if (CheckBuffer(self))
4038 return -1;
4039
4040 if (strcmp(name, "name") == 0)
4041 {
4042 char_u *val;
4043 aco_save_T aco;
4044 int r;
4045 PyObject *todecref;
4046
4047 if (!(val = StringToChars(valObject, &todecref)))
4048 return -1;
4049
4050 VimTryStart();
4051 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4052 aucmd_prepbuf(&aco, self->buf);
4053 r = rename_buffer(val);
4054 aucmd_restbuf(&aco);
4055 Py_XDECREF(todecref);
4056 if (VimTryEnd())
4057 return -1;
4058
4059 if (r == FAIL)
4060 {
4061 PyErr_SetVim(_("failed to rename buffer"));
4062 return -1;
4063 }
4064 return 0;
4065 }
4066 else
4067 {
4068 PyErr_SetString(PyExc_AttributeError, name);
4069 return -1;
4070 }
4071}
4072
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004073 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004074BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004075{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004076 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077}
4078
4079 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004080BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081{
4082 pos_T *posp;
4083 char *pmark;
4084 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004085 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086
Bram Moolenaard6e39182013-05-21 18:30:34 +02004087 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088 return NULL;
4089
4090 if (!PyArg_ParseTuple(args, "s", &pmark))
4091 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004093 if (STRLEN(pmark) != 1)
4094 {
4095 PyErr_SetString(PyExc_ValueError,
4096 _("mark name must be a single character"));
4097 return NULL;
4098 }
4099
4100 mark = *pmark;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004101 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004102 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004103 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004104 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004105 if (VimTryEnd())
4106 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107
4108 if (posp == NULL)
4109 {
4110 PyErr_SetVim(_("invalid mark name"));
4111 return NULL;
4112 }
4113
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 if (posp->lnum <= 0)
4115 {
4116 /* Or raise an error? */
4117 Py_INCREF(Py_None);
4118 return Py_None;
4119 }
4120
4121 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4122}
4123
4124 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004125BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126{
4127 PyInt start;
4128 PyInt end;
4129
Bram Moolenaard6e39182013-05-21 18:30:34 +02004130 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004131 return NULL;
4132
4133 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4134 return NULL;
4135
Bram Moolenaard6e39182013-05-21 18:30:34 +02004136 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004137}
4138
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004139 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004140BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004141{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004142 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004143 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004144 else
4145 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004146 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004147
4148 if (name == NULL)
4149 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004150
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004151 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004152 }
4153}
4154
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004155static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004156 /* name, function, calling, documentation */
4157 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4158 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4159 {"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 +02004160 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4161 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004162};
4163
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004164/*
4165 * Buffer list object - Implementation
4166 */
4167
4168static PyTypeObject BufMapType;
4169
4170typedef struct
4171{
4172 PyObject_HEAD
4173} BufMapObject;
4174
4175 static PyInt
4176BufMapLength(PyObject *self UNUSED)
4177{
4178 buf_T *b = firstbuf;
4179 PyInt n = 0;
4180
4181 while (b)
4182 {
4183 ++n;
4184 b = b->b_next;
4185 }
4186
4187 return n;
4188}
4189
4190 static PyObject *
4191BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4192{
4193 buf_T *b;
4194 int bnr;
4195
4196#if PY_MAJOR_VERSION < 3
4197 if (PyInt_Check(keyObject))
4198 bnr = PyInt_AsLong(keyObject);
4199 else
4200#endif
4201 if (PyLong_Check(keyObject))
4202 bnr = PyLong_AsLong(keyObject);
4203 else
4204 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004205 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004206 return NULL;
4207 }
4208
4209 b = buflist_findnr(bnr);
4210
4211 if (b)
4212 return BufferNew(b);
4213 else
4214 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004215 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004216 return NULL;
4217 }
4218}
4219
4220 static void
4221BufMapIterDestruct(PyObject *buffer)
4222{
4223 /* Iteration was stopped before all buffers were processed */
4224 if (buffer)
4225 {
4226 Py_DECREF(buffer);
4227 }
4228}
4229
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004230 static int
4231BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4232{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004233 if (buffer)
4234 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004235 return 0;
4236}
4237
4238 static int
4239BufMapIterClear(PyObject **buffer)
4240{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004241 if (*buffer)
4242 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004243 return 0;
4244}
4245
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004246 static PyObject *
4247BufMapIterNext(PyObject **buffer)
4248{
4249 PyObject *next;
4250 PyObject *r;
4251
4252 if (!*buffer)
4253 return NULL;
4254
4255 r = *buffer;
4256
4257 if (CheckBuffer((BufferObject *)(r)))
4258 {
4259 *buffer = NULL;
4260 return NULL;
4261 }
4262
4263 if (!((BufferObject *)(r))->buf->b_next)
4264 next = NULL;
4265 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4266 return NULL;
4267 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004268 /* Do not increment reference: we no longer hold it (decref), but whoever
4269 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004270 return r;
4271}
4272
4273 static PyObject *
4274BufMapIter(PyObject *self UNUSED)
4275{
4276 PyObject *buffer;
4277
4278 buffer = BufferNew(firstbuf);
4279 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004280 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4281 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004282}
4283
4284static PyMappingMethods BufMapAsMapping = {
4285 (lenfunc) BufMapLength,
4286 (binaryfunc) BufMapItem,
4287 (objobjargproc) 0,
4288};
4289
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004290/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004291 */
4292
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004293static char *CurrentAttrs[] = {
4294 "buffer", "window", "line", "range", "tabpage",
4295 NULL
4296};
4297
4298 static PyObject *
4299CurrentDir(PyObject *self)
4300{
4301 return ObjectDir(self, CurrentAttrs);
4302}
4303
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004304 static PyObject *
4305CurrentGetattr(PyObject *self UNUSED, char *name)
4306{
4307 if (strcmp(name, "buffer") == 0)
4308 return (PyObject *)BufferNew(curbuf);
4309 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004310 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004311 else if (strcmp(name, "tabpage") == 0)
4312 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004313 else if (strcmp(name, "line") == 0)
4314 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4315 else if (strcmp(name, "range") == 0)
4316 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004317 else if (strcmp(name, "__members__") == 0)
4318 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004319 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004320#if PY_MAJOR_VERSION < 3
4321 return Py_FindMethod(WindowMethods, self, name);
4322#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004323 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004324#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004325}
4326
4327 static int
4328CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4329{
4330 if (strcmp(name, "line") == 0)
4331 {
4332 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4333 return -1;
4334
4335 return 0;
4336 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004337 else if (strcmp(name, "buffer") == 0)
4338 {
4339 int count;
4340
4341 if (value->ob_type != &BufferType)
4342 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004343 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004344 return -1;
4345 }
4346
4347 if (CheckBuffer((BufferObject *)(value)))
4348 return -1;
4349 count = ((BufferObject *)(value))->buf->b_fnum;
4350
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004351 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004352 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4353 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004354 if (VimTryEnd())
4355 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004356 PyErr_SetVim(_("failed to switch to given buffer"));
4357 return -1;
4358 }
4359
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004360 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004361 }
4362 else if (strcmp(name, "window") == 0)
4363 {
4364 int count;
4365
4366 if (value->ob_type != &WindowType)
4367 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004368 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004369 return -1;
4370 }
4371
4372 if (CheckWindow((WindowObject *)(value)))
4373 return -1;
4374 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4375
4376 if (!count)
4377 {
4378 PyErr_SetString(PyExc_ValueError,
4379 _("failed to find window in the current tab page"));
4380 return -1;
4381 }
4382
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004383 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004384 win_goto(((WindowObject *)(value))->win);
4385 if (((WindowObject *)(value))->win != curwin)
4386 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004387 if (VimTryEnd())
4388 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004389 PyErr_SetString(PyExc_RuntimeError,
4390 _("did not switch to the specified window"));
4391 return -1;
4392 }
4393
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004394 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004395 }
4396 else if (strcmp(name, "tabpage") == 0)
4397 {
4398 if (value->ob_type != &TabPageType)
4399 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004400 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004401 return -1;
4402 }
4403
4404 if (CheckTabPage((TabPageObject *)(value)))
4405 return -1;
4406
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004407 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004408 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4409 if (((TabPageObject *)(value))->tab != curtab)
4410 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004411 if (VimTryEnd())
4412 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004413 PyErr_SetString(PyExc_RuntimeError,
4414 _("did not switch to the specified tab page"));
4415 return -1;
4416 }
4417
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004418 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004419 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004420 else
4421 {
4422 PyErr_SetString(PyExc_AttributeError, name);
4423 return -1;
4424 }
4425}
4426
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004427static struct PyMethodDef CurrentMethods[] = {
4428 /* name, function, calling, documentation */
4429 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4430 { NULL, NULL, 0, NULL}
4431};
4432
Bram Moolenaardb913952012-06-29 12:54:53 +02004433 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004434init_range_cmd(exarg_T *eap)
4435{
4436 RangeStart = eap->line1;
4437 RangeEnd = eap->line2;
4438}
4439
4440 static void
4441init_range_eval(typval_T *rettv UNUSED)
4442{
4443 RangeStart = (PyInt) curwin->w_cursor.lnum;
4444 RangeEnd = RangeStart;
4445}
4446
4447 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004448run_cmd(const char *cmd, void *arg UNUSED
4449#ifdef PY_CAN_RECURSE
4450 , PyGILState_STATE *pygilstate UNUSED
4451#endif
4452 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004453{
4454 PyRun_SimpleString((char *) cmd);
4455}
4456
4457static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4458static int code_hdr_len = 30;
4459
4460 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004461run_do(const char *cmd, void *arg UNUSED
4462#ifdef PY_CAN_RECURSE
4463 , PyGILState_STATE *pygilstate
4464#endif
4465 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004466{
4467 PyInt lnum;
4468 size_t len;
4469 char *code;
4470 int status;
4471 PyObject *pyfunc, *pymain;
4472
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004473 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004474 {
4475 EMSG(_("cannot save undo information"));
4476 return;
4477 }
4478
4479 len = code_hdr_len + STRLEN(cmd);
4480 code = PyMem_New(char, len + 1);
4481 memcpy(code, code_hdr, code_hdr_len);
4482 STRCPY(code + code_hdr_len, cmd);
4483 status = PyRun_SimpleString(code);
4484 PyMem_Free(code);
4485
4486 if (status)
4487 {
4488 EMSG(_("failed to run the code"));
4489 return;
4490 }
4491
4492 status = 0;
4493 pymain = PyImport_AddModule("__main__");
4494 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004495#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004496 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004497#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004498
4499 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4500 {
4501 PyObject *line, *linenr, *ret;
4502
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004503#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004504 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004505#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004506 if (!(line = GetBufferLine(curbuf, lnum)))
4507 goto err;
4508 if (!(linenr = PyInt_FromLong((long) lnum)))
4509 {
4510 Py_DECREF(line);
4511 goto err;
4512 }
4513 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4514 Py_DECREF(line);
4515 Py_DECREF(linenr);
4516 if (!ret)
4517 goto err;
4518
4519 if (ret != Py_None)
4520 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4521 goto err;
4522
4523 Py_XDECREF(ret);
4524 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004525#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004526 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004527#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004528 }
4529 goto out;
4530err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004531#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004532 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004533#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004534 PyErr_PrintEx(0);
4535 PythonIO_Flush();
4536 status = 1;
4537out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004538#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004539 if (!status)
4540 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004541#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004542 Py_DECREF(pyfunc);
4543 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4544 if (status)
4545 return;
4546 check_cursor();
4547 update_curbuf(NOT_VALID);
4548}
4549
4550 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004551run_eval(const char *cmd, typval_T *rettv
4552#ifdef PY_CAN_RECURSE
4553 , PyGILState_STATE *pygilstate UNUSED
4554#endif
4555 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004556{
4557 PyObject *r;
4558
4559 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4560 if (r == NULL)
4561 {
4562 if (PyErr_Occurred() && !msg_silent)
4563 PyErr_PrintEx(0);
4564 EMSG(_("E858: Eval did not return a valid python object"));
4565 }
4566 else
4567 {
4568 if (ConvertFromPyObject(r, rettv) == -1)
4569 EMSG(_("E859: Failed to convert returned python object to vim value"));
4570 Py_DECREF(r);
4571 }
4572 PyErr_Clear();
4573}
4574
4575 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004576set_ref_in_py(const int copyID)
4577{
4578 pylinkedlist_T *cur;
4579 dict_T *dd;
4580 list_T *ll;
4581
4582 if (lastdict != NULL)
4583 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4584 {
4585 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4586 if (dd->dv_copyID != copyID)
4587 {
4588 dd->dv_copyID = copyID;
4589 set_ref_in_ht(&dd->dv_hashtab, copyID);
4590 }
4591 }
4592
4593 if (lastlist != NULL)
4594 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4595 {
4596 ll = ((ListObject *) (cur->pll_obj))->list;
4597 if (ll->lv_copyID != copyID)
4598 {
4599 ll->lv_copyID = copyID;
4600 set_ref_in_list(ll, copyID);
4601 }
4602 }
4603}
4604
4605 static int
4606set_string_copy(char_u *str, typval_T *tv)
4607{
4608 tv->vval.v_string = vim_strsave(str);
4609 if (tv->vval.v_string == NULL)
4610 {
4611 PyErr_NoMemory();
4612 return -1;
4613 }
4614 return 0;
4615}
4616
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004617 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004618pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004619{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004620 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004621 char_u *key;
4622 dictitem_T *di;
4623 PyObject *keyObject;
4624 PyObject *valObject;
4625 Py_ssize_t iter = 0;
4626
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004627 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004628 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004629
4630 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004631 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004632
4633 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4634 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004635 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004636
Bram Moolenaara03e6312013-05-29 22:49:26 +02004637 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004638 {
4639 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004640 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004641 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004642
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004643 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004644 {
4645 dict_unref(dict);
4646 return -1;
4647 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004648
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004649 if (*key == NUL)
4650 {
4651 dict_unref(dict);
4652 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004653 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004654 return -1;
4655 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004656
4657 di = dictitem_alloc(key);
4658
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004659 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004660
4661 if (di == NULL)
4662 {
4663 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004664 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004665 return -1;
4666 }
4667 di->di_tv.v_lock = 0;
4668
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004669 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004670 {
4671 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004672 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004673 return -1;
4674 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004675
4676 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004677 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004678 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004679 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004680 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004681 PyErr_SetVim(_("failed to add key to dictionary"));
4682 return -1;
4683 }
4684 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004685
4686 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004687 return 0;
4688}
4689
4690 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004691pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004692{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004693 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004694 char_u *key;
4695 dictitem_T *di;
4696 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004697 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004698 PyObject *keyObject;
4699 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004700
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004701 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004702 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004703
4704 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004705 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004706
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004707 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004708 {
4709 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004710 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004711 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004712
4713 if (!(iterator = PyObject_GetIter(list)))
4714 {
4715 dict_unref(dict);
4716 Py_DECREF(list);
4717 return -1;
4718 }
4719 Py_DECREF(list);
4720
4721 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004722 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004723 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004724
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004725 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004726 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004727 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004728 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004729 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004730 return -1;
4731 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004732
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004733 if (*key == NUL)
4734 {
4735 Py_DECREF(keyObject);
4736 Py_DECREF(iterator);
4737 Py_XDECREF(todecref);
4738 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004739 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004740 return -1;
4741 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004742
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004743 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004744 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004745 Py_DECREF(keyObject);
4746 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004747 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004748 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004749 return -1;
4750 }
4751
4752 di = dictitem_alloc(key);
4753
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004754 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004755 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004756
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004757 if (di == NULL)
4758 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004759 Py_DECREF(iterator);
4760 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004761 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004762 PyErr_NoMemory();
4763 return -1;
4764 }
4765 di->di_tv.v_lock = 0;
4766
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004767 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004768 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004769 Py_DECREF(iterator);
4770 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004771 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004772 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004773 return -1;
4774 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004775
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004776 Py_DECREF(valObject);
4777
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004778 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004779 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004780 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004781 dictitem_free(di);
4782 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004783 PyErr_SetVim(_("failed to add key to dictionary"));
4784 return -1;
4785 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004786 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004787 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004788 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004789 return 0;
4790}
4791
4792 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004793pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004794{
4795 list_T *l;
4796
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004797 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004798 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004799
4800 tv->v_type = VAR_LIST;
4801 tv->vval.v_list = l;
4802
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004803 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004804 {
4805 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004806 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004807 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004808
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004809 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004810 return 0;
4811}
4812
Bram Moolenaardb913952012-06-29 12:54:53 +02004813typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4814
4815 static int
4816convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004817 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004818{
4819 PyObject *capsule;
4820 char hexBuf[sizeof(void *) * 2 + 3];
4821
4822 sprintf(hexBuf, "%p", obj);
4823
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004824# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004825 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004826# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004827 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004828# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004829 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004830 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004831# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004832 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004833# else
4834 capsule = PyCObject_FromVoidPtr(tv, NULL);
4835# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004836 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4837 {
4838 Py_DECREF(capsule);
4839 tv->v_type = VAR_UNKNOWN;
4840 return -1;
4841 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004842 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004843 {
4844 tv->v_type = VAR_UNKNOWN;
4845 return -1;
4846 }
4847 /* As we are not using copy_tv which increments reference count we must
4848 * do it ourself. */
4849 switch(tv->v_type)
4850 {
4851 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4852 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4853 }
4854 }
4855 else
4856 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004857 typval_T *v;
4858
4859# ifdef PY_USE_CAPSULE
4860 v = PyCapsule_GetPointer(capsule, NULL);
4861# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004862 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004863# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004864 copy_tv(v, tv);
4865 }
4866 return 0;
4867}
4868
4869 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02004870ConvertFromPyMapping(PyObject *obj, typval_T *tv)
4871{
4872 PyObject *lookup_dict;
4873 int r;
4874
4875 if (!(lookup_dict = PyDict_New()))
4876 return -1;
4877
4878 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
4879 {
4880 tv->v_type = VAR_DICT;
4881 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4882 ++tv->vval.v_dict->dv_refcount;
4883 r = 0;
4884 }
4885 else if (PyDict_Check(obj))
4886 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
4887 else if (PyMapping_Check(obj))
4888 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
4889 else
4890 {
4891 PyErr_SetString(PyExc_TypeError,
4892 _("unable to convert object to vim dictionary"));
4893 r = -1;
4894 }
4895 Py_DECREF(lookup_dict);
4896 return r;
4897}
4898
4899 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02004900ConvertFromPyObject(PyObject *obj, typval_T *tv)
4901{
4902 PyObject *lookup_dict;
4903 int r;
4904
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004905 if (!(lookup_dict = PyDict_New()))
4906 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004907 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4908 Py_DECREF(lookup_dict);
4909 return r;
4910}
4911
4912 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004913_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004914{
Bram Moolenaara9922d62013-05-30 13:01:18 +02004915 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02004916 {
4917 tv->v_type = VAR_DICT;
4918 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4919 ++tv->vval.v_dict->dv_refcount;
4920 }
4921 else if (obj->ob_type == &ListType)
4922 {
4923 tv->v_type = VAR_LIST;
4924 tv->vval.v_list = (((ListObject *)(obj))->list);
4925 ++tv->vval.v_list->lv_refcount;
4926 }
4927 else if (obj->ob_type == &FunctionType)
4928 {
4929 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4930 return -1;
4931
4932 tv->v_type = VAR_FUNC;
4933 func_ref(tv->vval.v_string);
4934 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004935 else if (PyBytes_Check(obj))
4936 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004937 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004938
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004939 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4940 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004941 if (result == NULL)
4942 return -1;
4943
4944 if (set_string_copy(result, tv) == -1)
4945 return -1;
4946
4947 tv->v_type = VAR_STRING;
4948 }
4949 else if (PyUnicode_Check(obj))
4950 {
4951 PyObject *bytes;
4952 char_u *result;
4953
Bram Moolenaardb913952012-06-29 12:54:53 +02004954 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4955 if (bytes == NULL)
4956 return -1;
4957
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004958 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4959 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004960 if (result == NULL)
4961 return -1;
4962
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004963 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004964 {
4965 Py_XDECREF(bytes);
4966 return -1;
4967 }
4968 Py_XDECREF(bytes);
4969
4970 tv->v_type = VAR_STRING;
4971 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004972#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004973 else if (PyInt_Check(obj))
4974 {
4975 tv->v_type = VAR_NUMBER;
4976 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4977 }
4978#endif
4979 else if (PyLong_Check(obj))
4980 {
4981 tv->v_type = VAR_NUMBER;
4982 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4983 }
4984 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004985 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004986#ifdef FEAT_FLOAT
4987 else if (PyFloat_Check(obj))
4988 {
4989 tv->v_type = VAR_FLOAT;
4990 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4991 }
4992#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004993 else if (PyObject_HasAttrString(obj, "keys"))
4994 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02004995 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004996 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004997 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004998 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004999 else
5000 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02005001 PyErr_SetString(PyExc_TypeError,
5002 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005003 return -1;
5004 }
5005 return 0;
5006}
5007
5008 static PyObject *
5009ConvertToPyObject(typval_T *tv)
5010{
5011 if (tv == NULL)
5012 {
5013 PyErr_SetVim(_("NULL reference passed"));
5014 return NULL;
5015 }
5016 switch (tv->v_type)
5017 {
5018 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005019 return PyBytes_FromString(tv->vval.v_string == NULL
5020 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005021 case VAR_NUMBER:
5022 return PyLong_FromLong((long) tv->vval.v_number);
5023#ifdef FEAT_FLOAT
5024 case VAR_FLOAT:
5025 return PyFloat_FromDouble((double) tv->vval.v_float);
5026#endif
5027 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005028 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005029 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005030 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005031 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005032 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005033 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005034 case VAR_UNKNOWN:
5035 Py_INCREF(Py_None);
5036 return Py_None;
5037 default:
5038 PyErr_SetVim(_("internal error: invalid value type"));
5039 return NULL;
5040 }
5041}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005042
5043typedef struct
5044{
5045 PyObject_HEAD
5046} CurrentObject;
5047static PyTypeObject CurrentType;
5048
5049 static void
5050init_structs(void)
5051{
5052 vim_memset(&OutputType, 0, sizeof(OutputType));
5053 OutputType.tp_name = "vim.message";
5054 OutputType.tp_basicsize = sizeof(OutputObject);
5055 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5056 OutputType.tp_doc = "vim message object";
5057 OutputType.tp_methods = OutputMethods;
5058#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005059 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5060 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005061 OutputType.tp_alloc = call_PyType_GenericAlloc;
5062 OutputType.tp_new = call_PyType_GenericNew;
5063 OutputType.tp_free = call_PyObject_Free;
5064#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005065 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5066 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005067#endif
5068
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005069 vim_memset(&IterType, 0, sizeof(IterType));
5070 IterType.tp_name = "vim.iter";
5071 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005072 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005073 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005074 IterType.tp_iter = (getiterfunc)IterIter;
5075 IterType.tp_iternext = (iternextfunc)IterNext;
5076 IterType.tp_dealloc = (destructor)IterDestructor;
5077 IterType.tp_traverse = (traverseproc)IterTraverse;
5078 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005079
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005080 vim_memset(&BufferType, 0, sizeof(BufferType));
5081 BufferType.tp_name = "vim.buffer";
5082 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005083 BufferType.tp_dealloc = (destructor)BufferDestructor;
5084 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005085 BufferType.tp_as_sequence = &BufferAsSeq;
5086 BufferType.tp_as_mapping = &BufferAsMapping;
5087 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5088 BufferType.tp_doc = "vim buffer object";
5089 BufferType.tp_methods = BufferMethods;
5090#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005091 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005092 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005093 BufferType.tp_alloc = call_PyType_GenericAlloc;
5094 BufferType.tp_new = call_PyType_GenericNew;
5095 BufferType.tp_free = call_PyObject_Free;
5096#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005097 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005098 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005099#endif
5100
5101 vim_memset(&WindowType, 0, sizeof(WindowType));
5102 WindowType.tp_name = "vim.window";
5103 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005104 WindowType.tp_dealloc = (destructor)WindowDestructor;
5105 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005106 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005107 WindowType.tp_doc = "vim Window object";
5108 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005109 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5110 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005111#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005112 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5113 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005114 WindowType.tp_alloc = call_PyType_GenericAlloc;
5115 WindowType.tp_new = call_PyType_GenericNew;
5116 WindowType.tp_free = call_PyObject_Free;
5117#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005118 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5119 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005120#endif
5121
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005122 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5123 TabPageType.tp_name = "vim.tabpage";
5124 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005125 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5126 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005127 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5128 TabPageType.tp_doc = "vim tab page object";
5129 TabPageType.tp_methods = TabPageMethods;
5130#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005131 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005132 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5133 TabPageType.tp_new = call_PyType_GenericNew;
5134 TabPageType.tp_free = call_PyObject_Free;
5135#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005136 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005137#endif
5138
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005139 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5140 BufMapType.tp_name = "vim.bufferlist";
5141 BufMapType.tp_basicsize = sizeof(BufMapObject);
5142 BufMapType.tp_as_mapping = &BufMapAsMapping;
5143 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005144 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005145 BufferType.tp_doc = "vim buffer list";
5146
5147 vim_memset(&WinListType, 0, sizeof(WinListType));
5148 WinListType.tp_name = "vim.windowlist";
5149 WinListType.tp_basicsize = sizeof(WinListType);
5150 WinListType.tp_as_sequence = &WinListAsSeq;
5151 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5152 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005153 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005154
5155 vim_memset(&TabListType, 0, sizeof(TabListType));
5156 TabListType.tp_name = "vim.tabpagelist";
5157 TabListType.tp_basicsize = sizeof(TabListType);
5158 TabListType.tp_as_sequence = &TabListAsSeq;
5159 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5160 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005161
5162 vim_memset(&RangeType, 0, sizeof(RangeType));
5163 RangeType.tp_name = "vim.range";
5164 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005165 RangeType.tp_dealloc = (destructor)RangeDestructor;
5166 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005167 RangeType.tp_as_sequence = &RangeAsSeq;
5168 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005169 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005170 RangeType.tp_doc = "vim Range object";
5171 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005172 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5173 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005174#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005175 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005176 RangeType.tp_alloc = call_PyType_GenericAlloc;
5177 RangeType.tp_new = call_PyType_GenericNew;
5178 RangeType.tp_free = call_PyObject_Free;
5179#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005180 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005181#endif
5182
5183 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5184 CurrentType.tp_name = "vim.currentdata";
5185 CurrentType.tp_basicsize = sizeof(CurrentObject);
5186 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5187 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005188 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005189#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005190 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5191 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005192#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005193 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5194 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005195#endif
5196
5197 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5198 DictionaryType.tp_name = "vim.dictionary";
5199 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005200 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005201 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005202 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005203 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005204 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5205 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005206 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5207 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5208 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005209#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005210 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5211 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005212#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005213 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5214 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005215#endif
5216
5217 vim_memset(&ListType, 0, sizeof(ListType));
5218 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005219 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005220 ListType.tp_basicsize = sizeof(ListObject);
5221 ListType.tp_as_sequence = &ListAsSeq;
5222 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005223 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005224 ListType.tp_doc = "list pushing modifications to vim structure";
5225 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005226 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005227 ListType.tp_new = (newfunc)ListConstructor;
5228 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005229#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005230 ListType.tp_getattro = (getattrofunc)ListGetattro;
5231 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005232#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005233 ListType.tp_getattr = (getattrfunc)ListGetattr;
5234 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005235#endif
5236
5237 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005238 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005239 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005240 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5241 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005242 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005243 FunctionType.tp_doc = "object that calls vim function";
5244 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005245 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005246 FunctionType.tp_new = (newfunc)FunctionConstructor;
5247 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005248#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005249 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005250#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005251 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005252#endif
5253
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005254 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5255 OptionsType.tp_name = "vim.options";
5256 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005257 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005258 OptionsType.tp_doc = "object for manipulating options";
5259 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005260 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5261 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5262 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005263
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005264#if PY_MAJOR_VERSION >= 3
5265 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5266 vimmodule.m_name = "vim";
5267 vimmodule.m_doc = "Vim Python interface\n";
5268 vimmodule.m_size = -1;
5269 vimmodule.m_methods = VimMethods;
5270#endif
5271}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005272
5273#define PYTYPE_READY(type) \
5274 if (PyType_Ready(&type)) \
5275 return -1;
5276
5277 static int
5278init_types()
5279{
5280 PYTYPE_READY(IterType);
5281 PYTYPE_READY(BufferType);
5282 PYTYPE_READY(RangeType);
5283 PYTYPE_READY(WindowType);
5284 PYTYPE_READY(TabPageType);
5285 PYTYPE_READY(BufMapType);
5286 PYTYPE_READY(WinListType);
5287 PYTYPE_READY(TabListType);
5288 PYTYPE_READY(CurrentType);
5289 PYTYPE_READY(DictionaryType);
5290 PYTYPE_READY(ListType);
5291 PYTYPE_READY(FunctionType);
5292 PYTYPE_READY(OptionsType);
5293 PYTYPE_READY(OutputType);
5294 return 0;
5295}
5296
5297static BufMapObject TheBufferMap =
5298{
5299 PyObject_HEAD_INIT(&BufMapType)
5300};
5301
5302static WinListObject TheWindowList =
5303{
5304 PyObject_HEAD_INIT(&WinListType)
5305 NULL
5306};
5307
5308static CurrentObject TheCurrent =
5309{
5310 PyObject_HEAD_INIT(&CurrentType)
5311};
5312
5313static TabListObject TheTabPageList =
5314{
5315 PyObject_HEAD_INIT(&TabListType)
5316};
5317
5318static struct numeric_constant {
5319 char *name;
5320 int value;
5321} numeric_constants[] = {
5322 {"VAR_LOCKED", VAR_LOCKED},
5323 {"VAR_FIXED", VAR_FIXED},
5324 {"VAR_SCOPE", VAR_SCOPE},
5325 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5326};
5327
5328static struct object_constant {
5329 char *name;
5330 PyObject *value;
5331} object_constants[] = {
5332 {"buffers", (PyObject *)(void *)&TheBufferMap},
5333 {"windows", (PyObject *)(void *)&TheWindowList},
5334 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5335 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005336
5337 {"Buffer", (PyObject *)&BufferType},
5338 {"Range", (PyObject *)&RangeType},
5339 {"Window", (PyObject *)&WindowType},
5340 {"TabPage", (PyObject *)&TabPageType},
5341 {"Dictionary", (PyObject *)&DictionaryType},
5342 {"List", (PyObject *)&ListType},
5343 {"Function", (PyObject *)&FunctionType},
5344 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005345};
5346
5347typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005348typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005349
5350#define ADD_OBJECT(m, name, obj) \
5351 if (add_object(m, name, obj)) \
5352 return -1;
5353
5354#define ADD_CHECKED_OBJECT(m, name, obj) \
5355 { \
5356 PyObject *value = obj; \
5357 if (!value) \
5358 return -1; \
5359 ADD_OBJECT(m, name, value); \
5360 }
5361
5362 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005363populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005364{
5365 int i;
Bram Moolenaarf4258302013-06-02 18:20:17 +02005366 PyObject *os;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005367
5368 for (i = 0; i < (int)(sizeof(numeric_constants)
5369 / sizeof(struct numeric_constant));
5370 ++i)
5371 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5372 PyInt_FromLong(numeric_constants[i].value));
5373
5374 for (i = 0; i < (int)(sizeof(object_constants)
5375 / sizeof(struct object_constant));
5376 ++i)
5377 {
5378 PyObject *value;
5379
5380 value = object_constants[i].value;
5381 Py_INCREF(value);
5382 ADD_OBJECT(m, object_constants[i].name, value);
5383 }
5384
5385 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5386 return -1;
5387 ADD_OBJECT(m, "error", VimError);
5388
Bram Moolenaara9922d62013-05-30 13:01:18 +02005389 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5390 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005391 ADD_CHECKED_OBJECT(m, "options",
5392 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005393
5394 if (!(os = PyImport_ImportModule("os")))
5395 return -1;
5396 ADD_OBJECT(m, "os", os);
5397
5398 if (!(py_getcwd = PyObject_GetAttrString(os, "getcwd")))
5399 return -1;
5400 ADD_OBJECT(m, "_getcwd", py_getcwd)
5401
5402 if (!(py_chdir = PyObject_GetAttrString(os, "chdir")))
5403 return -1;
5404 ADD_OBJECT(m, "_chdir", py_chdir);
5405 if (PyObject_SetAttrString(os, "chdir", get_attr(m, "chdir")))
5406 return -1;
5407
5408 if ((py_fchdir = PyObject_GetAttrString(os, "fchdir")))
5409 {
5410 ADD_OBJECT(m, "_fchdir", py_fchdir);
5411 if (PyObject_SetAttrString(os, "fchdir", get_attr(m, "fchdir")))
5412 return -1;
5413 }
5414
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005415 return 0;
5416}