blob: 1cb8f9969ab040b9879eda9d1db41fa22b074c6a [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020016#if PY_VERSION_HEX < 0x02050000
17typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
18#endif
19
Bram Moolenaar91805fc2011-06-26 04:01:44 +020020#ifdef FEAT_MBYTE
21# define ENC_OPT p_enc
22#else
23# define ENC_OPT "latin1"
24#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020025#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020026
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020027#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
28
29#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
30#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020031#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020032
Bram Moolenaare9ba5162013-05-29 22:02:22 +020033#define DICTKEY_DECL \
34 PyObject *dictkey_todecref;
Bram Moolenaara03e6312013-05-29 22:49:26 +020035#define DICTKEY_GET(err, decref) \
Bram Moolenaare9ba5162013-05-29 22:02:22 +020036 if (!(key = StringToChars(keyObject, &dictkey_todecref))) \
Bram Moolenaara03e6312013-05-29 22:49:26 +020037 { \
38 if (decref) \
39 { \
40 Py_DECREF(keyObject); \
41 } \
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +020042 return err; \
Bram Moolenaara03e6312013-05-29 22:49:26 +020043 } \
44 if (decref && !dictkey_todecref) \
45 dictkey_todecref = keyObject; \
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +020046 if (*key == NUL) \
47 { \
48 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
49 return err; \
50 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +020051#define DICTKEY_UNREF \
52 Py_XDECREF(dictkey_todecref);
53
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020054typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020055typedef void (*runner)(const char *, void *
56#ifdef PY_CAN_RECURSE
57 , PyGILState_STATE *
58#endif
59 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061static int ConvertFromPyObject(PyObject *, typval_T *);
62static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020063static PyObject *WindowNew(win_T *, tabpage_T *);
64static PyObject *BufferNew (buf_T *);
65static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020066
67static PyInt RangeStart;
68static PyInt RangeEnd;
69
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020070static PyObject *globals;
71
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020072/*
73 * obtain a lock on the Vim data structures
74 */
75 static void
76Python_Lock_Vim(void)
77{
78}
79
80/*
81 * release a lock on the Vim data structures
82 */
83 static void
84Python_Release_Vim(void)
85{
86}
87
Bram Moolenaare9ba5162013-05-29 22:02:22 +020088/*
89 * The "todecref" argument holds a pointer to PyObject * that must be
90 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
91 * was needed to generate returned value is object.
92 *
93 * Use Py_XDECREF to decrement reference count.
94 */
95 static char_u *
96StringToChars(PyObject *object, PyObject **todecref)
97{
98 char_u *p;
99 PyObject *bytes = NULL;
100
101 if (PyBytes_Check(object))
102 {
103
104 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
105 return NULL;
106 if (p == NULL)
107 return NULL;
108
109 *todecref = NULL;
110 }
111 else if (PyUnicode_Check(object))
112 {
113 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
114 if (bytes == NULL)
115 return NULL;
116
117 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
118 return NULL;
119 if (p == NULL)
120 return NULL;
121
122 *todecref = bytes;
123 }
124 else
125 {
126 PyErr_SetString(PyExc_TypeError, _("object must be string"));
127 return NULL;
128 }
129
130 return (char_u *) p;
131}
132
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200133 static int
134add_string(PyObject *list, char *s)
135{
136 PyObject *string;
137
138 if (!(string = PyString_FromString(s)))
139 return -1;
140 if (PyList_Append(list, string))
141 {
142 Py_DECREF(string);
143 return -1;
144 }
145
146 Py_DECREF(string);
147 return 0;
148}
149
150 static PyObject *
151ObjectDir(PyObject *self, char **attributes)
152{
153 PyMethodDef *method;
154 char **attr;
155 PyObject *r;
156
157 if (!(r = PyList_New(0)))
158 return NULL;
159
160 if (self)
161 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
162 if (add_string(r, (char *) method->ml_name))
163 {
164 Py_DECREF(r);
165 return NULL;
166 }
167
168 for (attr = attributes ; *attr ; ++attr)
169 if (add_string(r, *attr))
170 {
171 Py_DECREF(r);
172 return NULL;
173 }
174
175#if PY_MAJOR_VERSION < 3
176 if (add_string(r, "__members__"))
177 {
178 Py_DECREF(r);
179 return NULL;
180 }
181#endif
182
183 return r;
184}
185
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200186/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200187 */
188
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200189/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200190typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200191
192static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200193
194typedef struct
195{
196 PyObject_HEAD
197 long softspace;
198 long error;
199} OutputObject;
200
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200201static char *OutputAttrs[] = {
202 "softspace",
203 NULL
204};
205
206 static PyObject *
207OutputDir(PyObject *self)
208{
209 return ObjectDir(self, OutputAttrs);
210}
211
Bram Moolenaar77045652012-09-21 13:46:06 +0200212 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200213OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200214{
215 if (val == NULL)
216 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200217 PyErr_SetString(PyExc_AttributeError,
218 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200219 return -1;
220 }
221
222 if (strcmp(name, "softspace") == 0)
223 {
224 if (!PyInt_Check(val))
225 {
226 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
227 return -1;
228 }
229
Bram Moolenaard6e39182013-05-21 18:30:34 +0200230 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200231 return 0;
232 }
233
234 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
235 return -1;
236}
237
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200238/* Buffer IO, we write one whole line at a time. */
239static garray_T io_ga = {0, 0, 1, 80, NULL};
240static writefn old_fn = NULL;
241
242 static void
243PythonIO_Flush(void)
244{
245 if (old_fn != NULL && io_ga.ga_len > 0)
246 {
247 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
248 old_fn((char_u *)io_ga.ga_data);
249 }
250 io_ga.ga_len = 0;
251}
252
253 static void
254writer(writefn fn, char_u *str, PyInt n)
255{
256 char_u *ptr;
257
258 /* Flush when switching output function. */
259 if (fn != old_fn)
260 PythonIO_Flush();
261 old_fn = fn;
262
263 /* Write each NL separated line. Text after the last NL is kept for
264 * writing later. */
265 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
266 {
267 PyInt len = ptr - str;
268
269 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
270 break;
271
272 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
273 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
274 fn((char_u *)io_ga.ga_data);
275 str = ptr + 1;
276 n -= len + 1;
277 io_ga.ga_len = 0;
278 }
279
280 /* Put the remaining text into io_ga for later printing. */
281 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
282 {
283 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
284 io_ga.ga_len += (int)n;
285 }
286}
287
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200288 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200289OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200290{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200291 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200292 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200293 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200294
Bram Moolenaar27564802011-09-07 19:30:21 +0200295 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200296 return NULL;
297
298 Py_BEGIN_ALLOW_THREADS
299 Python_Lock_Vim();
300 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
301 Python_Release_Vim();
302 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200303 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200304
305 Py_INCREF(Py_None);
306 return Py_None;
307}
308
309 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200310OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200311{
312 PyInt n;
313 PyInt i;
314 PyObject *list;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200315 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200316
317 if (!PyArg_ParseTuple(args, "O", &list))
318 return NULL;
319 Py_INCREF(list);
320
Bram Moolenaardb913952012-06-29 12:54:53 +0200321 if (!PyList_Check(list))
322 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200323 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
324 Py_DECREF(list);
325 return NULL;
326 }
327
328 n = PyList_Size(list);
329
330 for (i = 0; i < n; ++i)
331 {
332 PyObject *line = PyList_GetItem(list, i);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200333 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200334 PyInt len;
335
Bram Moolenaardb913952012-06-29 12:54:53 +0200336 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
337 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200338 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
339 Py_DECREF(list);
340 return NULL;
341 }
342
343 Py_BEGIN_ALLOW_THREADS
344 Python_Lock_Vim();
345 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
346 Python_Release_Vim();
347 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200348 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200349 }
350
351 Py_DECREF(list);
352 Py_INCREF(Py_None);
353 return Py_None;
354}
355
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100356 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200357OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100358{
359 /* do nothing */
360 Py_INCREF(Py_None);
361 return Py_None;
362}
363
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200364/***************/
365
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200366static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200367 /* name, function, calling, doc */
368 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
369 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
370 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200371 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200372 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200373};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200374
375static OutputObject Output =
376{
377 PyObject_HEAD_INIT(&OutputType)
378 0,
379 0
380};
381
382static OutputObject Error =
383{
384 PyObject_HEAD_INIT(&OutputType)
385 0,
386 1
387};
388
389 static int
390PythonIO_Init_io(void)
391{
392 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
393 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
394
395 if (PyErr_Occurred())
396 {
397 EMSG(_("E264: Python: Error initialising I/O objects"));
398 return -1;
399 }
400
401 return 0;
402}
403
404
405static PyObject *VimError;
406
407/* Check to see whether a Vim error has been reported, or a keyboard
408 * interrupt has been detected.
409 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200410
411 static void
412VimTryStart(void)
413{
414 ++trylevel;
415}
416
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200418VimTryEnd(void)
419{
420 --trylevel;
421 if (got_int)
422 {
423 PyErr_SetNone(PyExc_KeyboardInterrupt);
424 return 1;
425 }
426 else if (!did_throw)
427 return 0;
428 else if (PyErr_Occurred())
429 return 1;
430 else
431 {
432 PyErr_SetVim((char *) current_exception->value);
433 discard_current_exception();
434 return 1;
435 }
436}
437
438 static int
439VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440{
441 if (got_int)
442 {
443 PyErr_SetNone(PyExc_KeyboardInterrupt);
444 return 1;
445 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446 return 0;
447}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200448
449/* Vim module - Implementation
450 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200451
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200452 static PyObject *
453VimCommand(PyObject *self UNUSED, PyObject *args)
454{
455 char *cmd;
456 PyObject *result;
457
458 if (!PyArg_ParseTuple(args, "s", &cmd))
459 return NULL;
460
461 PyErr_Clear();
462
463 Py_BEGIN_ALLOW_THREADS
464 Python_Lock_Vim();
465
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200466 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200467 do_cmdline_cmd((char_u *)cmd);
468 update_screen(VALID);
469
470 Python_Release_Vim();
471 Py_END_ALLOW_THREADS
472
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200473 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200474 result = NULL;
475 else
476 result = Py_None;
477
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200478
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200479 Py_XINCREF(result);
480 return result;
481}
482
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200483/*
484 * Function to translate a typval_T into a PyObject; this will recursively
485 * translate lists/dictionaries into their Python equivalents.
486 *
487 * The depth parameter is to avoid infinite recursion, set it to 1 when
488 * you call VimToPython.
489 */
490 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200491VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200492{
493 PyObject *result;
494 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200495 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200496
497 /* Avoid infinite recursion */
498 if (depth > 100)
499 {
500 Py_INCREF(Py_None);
501 result = Py_None;
502 return result;
503 }
504
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200505 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200506 * then and we can use it again. */
507 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
508 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
509 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200510 sprintf(ptrBuf, "%p",
511 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
512 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200513
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200514 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200515 {
516 Py_INCREF(result);
517 return result;
518 }
519 }
520
521 if (our_tv->v_type == VAR_STRING)
522 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200523 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200524 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200525 }
526 else if (our_tv->v_type == VAR_NUMBER)
527 {
528 char buf[NUMBUFLEN];
529
530 /* For backwards compatibility numbers are stored as strings. */
531 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200532 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200533 }
534# ifdef FEAT_FLOAT
535 else if (our_tv->v_type == VAR_FLOAT)
536 {
537 char buf[NUMBUFLEN];
538
539 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200540 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200541 }
542# endif
543 else if (our_tv->v_type == VAR_LIST)
544 {
545 list_T *list = our_tv->vval.v_list;
546 listitem_T *curr;
547
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200548 if (list == NULL)
549 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200550
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200551 if (!(result = PyList_New(0)))
552 return NULL;
553
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200554 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200555 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200556 Py_DECREF(result);
557 return NULL;
558 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200559
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200560 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
561 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200562 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200563 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200564 Py_DECREF(result);
565 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200566 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200567 if (PyList_Append(result, newObj))
568 {
569 Py_DECREF(newObj);
570 Py_DECREF(result);
571 return NULL;
572 }
573 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200574 }
575 }
576 else if (our_tv->v_type == VAR_DICT)
577 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200578
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200579 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
580 long_u todo = ht->ht_used;
581 hashitem_T *hi;
582 dictitem_T *di;
583 if (our_tv->vval.v_dict == NULL)
584 return NULL;
585
586 if (!(result = PyDict_New()))
587 return NULL;
588
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200589 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200590 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200591 Py_DECREF(result);
592 return NULL;
593 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200594
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200595 for (hi = ht->ht_array; todo > 0; ++hi)
596 {
597 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200598 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200599 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200600
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200601 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200602 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200603 {
604 Py_DECREF(result);
605 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200606 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200607 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
608 {
609 Py_DECREF(result);
610 Py_DECREF(newObj);
611 return NULL;
612 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613 }
614 }
615 }
616 else
617 {
618 Py_INCREF(Py_None);
619 result = Py_None;
620 }
621
622 return result;
623}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200624
625 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200626VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200627{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628 char *expr;
629 typval_T *our_tv;
630 PyObject *result;
631 PyObject *lookup_dict;
632
633 if (!PyArg_ParseTuple(args, "s", &expr))
634 return NULL;
635
636 Py_BEGIN_ALLOW_THREADS
637 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200638 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200639 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200640 Python_Release_Vim();
641 Py_END_ALLOW_THREADS
642
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200643 if (VimTryEnd())
644 return NULL;
645
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646 if (our_tv == NULL)
647 {
648 PyErr_SetVim(_("invalid expression"));
649 return NULL;
650 }
651
652 /* Convert the Vim type into a Python type. Create a dictionary that's
653 * used to check for recursive loops. */
654 lookup_dict = PyDict_New();
655 result = VimToPython(our_tv, 1, lookup_dict);
656 Py_DECREF(lookup_dict);
657
658
659 Py_BEGIN_ALLOW_THREADS
660 Python_Lock_Vim();
661 free_tv(our_tv);
662 Python_Release_Vim();
663 Py_END_ALLOW_THREADS
664
665 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200666}
667
Bram Moolenaardb913952012-06-29 12:54:53 +0200668static PyObject *ConvertToPyObject(typval_T *);
669
670 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200671VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200672{
Bram Moolenaardb913952012-06-29 12:54:53 +0200673 char *expr;
674 typval_T *our_tv;
675 PyObject *result;
676
677 if (!PyArg_ParseTuple(args, "s", &expr))
678 return NULL;
679
680 Py_BEGIN_ALLOW_THREADS
681 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200682 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200683 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200684 Python_Release_Vim();
685 Py_END_ALLOW_THREADS
686
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200687 if (VimTryEnd())
688 return NULL;
689
Bram Moolenaardb913952012-06-29 12:54:53 +0200690 if (our_tv == NULL)
691 {
692 PyErr_SetVim(_("invalid expression"));
693 return NULL;
694 }
695
696 result = ConvertToPyObject(our_tv);
697 Py_BEGIN_ALLOW_THREADS
698 Python_Lock_Vim();
699 free_tv(our_tv);
700 Python_Release_Vim();
701 Py_END_ALLOW_THREADS
702
703 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200704}
705
706 static PyObject *
707VimStrwidth(PyObject *self UNUSED, PyObject *args)
708{
709 char *expr;
710
711 if (!PyArg_ParseTuple(args, "s", &expr))
712 return NULL;
713
Bram Moolenaara54bf402012-12-05 16:30:07 +0100714 return PyLong_FromLong(
715#ifdef FEAT_MBYTE
716 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
717#else
718 STRLEN(expr)
719#endif
720 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200721}
722
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200723/*
724 * Vim module - Definitions
725 */
726
727static struct PyMethodDef VimMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200728 /* name, function, calling, documentation */
729 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
730 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
731 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
732 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
733 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200734};
735
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200736/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200737 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200738 */
739
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200740static PyTypeObject IterType;
741
742typedef PyObject *(*nextfun)(void **);
743typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200744typedef int (*traversefun)(void *, visitproc, void *);
745typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200746
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200747/* Main purpose of this object is removing the need for do python
748 * initialization (i.e. PyType_Ready and setting type attributes) for a big
749 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200750
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200751typedef struct
752{
753 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200754 void *cur;
755 nextfun next;
756 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200757 traversefun traverse;
758 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200759} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200760
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200761 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200762IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
763 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200764{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200765 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200766
Bram Moolenaar774267b2013-05-21 20:51:59 +0200767 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200768 self->cur = start;
769 self->next = next;
770 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200771 self->traverse = traverse;
772 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200774 return (PyObject *)(self);
775}
776
777 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200778IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200779{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200780 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200781 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200782 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200783}
784
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200785 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200786IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200787{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200788 if (self->traverse != NULL)
789 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200790 else
791 return 0;
792}
793
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200794/* Mac OSX defines clear() somewhere. */
795#ifdef clear
796# undef clear
797#endif
798
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200799 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200800IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200801{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200802 if (self->clear != NULL)
803 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200804 else
805 return 0;
806}
807
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200808 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200809IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200811 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200812}
813
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200814 static PyObject *
815IterIter(PyObject *self)
816{
817 return self;
818}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200819
Bram Moolenaardb913952012-06-29 12:54:53 +0200820typedef struct pylinkedlist_S {
821 struct pylinkedlist_S *pll_next;
822 struct pylinkedlist_S *pll_prev;
823 PyObject *pll_obj;
824} pylinkedlist_T;
825
826static pylinkedlist_T *lastdict = NULL;
827static pylinkedlist_T *lastlist = NULL;
828
829 static void
830pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
831{
832 if (ref->pll_prev == NULL)
833 {
834 if (ref->pll_next == NULL)
835 {
836 *last = NULL;
837 return;
838 }
839 }
840 else
841 ref->pll_prev->pll_next = ref->pll_next;
842
843 if (ref->pll_next == NULL)
844 *last = ref->pll_prev;
845 else
846 ref->pll_next->pll_prev = ref->pll_prev;
847}
848
849 static void
850pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
851{
852 if (*last == NULL)
853 ref->pll_prev = NULL;
854 else
855 {
856 (*last)->pll_next = ref;
857 ref->pll_prev = *last;
858 }
859 ref->pll_next = NULL;
860 ref->pll_obj = self;
861 *last = ref;
862}
863
864static PyTypeObject DictionaryType;
865
866typedef struct
867{
868 PyObject_HEAD
869 dict_T *dict;
870 pylinkedlist_T ref;
871} DictionaryObject;
872
873 static PyObject *
874DictionaryNew(dict_T *dict)
875{
876 DictionaryObject *self;
877
878 self = PyObject_NEW(DictionaryObject, &DictionaryType);
879 if (self == NULL)
880 return NULL;
881 self->dict = dict;
882 ++dict->dv_refcount;
883
884 pyll_add((PyObject *)(self), &self->ref, &lastdict);
885
886 return (PyObject *)(self);
887}
888
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200889 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200890DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200891{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200892 pyll_remove(&self->ref, &lastdict);
893 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200894
895 DESTRUCTOR_FINISH(self);
896}
897
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200898static char *DictionaryAttrs[] = {
899 "locked", "scope",
900 NULL
901};
902
903 static PyObject *
904DictionaryDir(PyObject *self)
905{
906 return ObjectDir(self, DictionaryAttrs);
907}
908
Bram Moolenaardb913952012-06-29 12:54:53 +0200909 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200910DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200911{
912 if (val == NULL)
913 {
914 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
915 return -1;
916 }
917
918 if (strcmp(name, "locked") == 0)
919 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200920 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200921 {
922 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
923 return -1;
924 }
925 else
926 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200927 int istrue = PyObject_IsTrue(val);
928 if (istrue == -1)
929 return -1;
930 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200931 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200932 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200933 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200934 }
935 return 0;
936 }
937 else
938 {
939 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
940 return -1;
941 }
942}
943
944 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200945DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200946{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200947 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +0200948}
949
950 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200951DictionaryItem(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200952{
953 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200954 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200955 DICTKEY_DECL
956
Bram Moolenaara03e6312013-05-29 22:49:26 +0200957 DICTKEY_GET(NULL, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +0200958
Bram Moolenaard6e39182013-05-21 18:30:34 +0200959 di = dict_find(self->dict, key, -1);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200960
Bram Moolenaar696c2112012-09-21 13:43:14 +0200961 DICTKEY_UNREF
962
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200963 if (di == NULL)
964 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200965 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200966 return NULL;
967 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200968
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200969 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200970}
971
972 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200973DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200974{
975 char_u *key;
976 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200977 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +0200978 dictitem_T *di;
979 DICTKEY_DECL
980
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200981 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +0200982 {
983 PyErr_SetVim(_("dict is locked"));
984 return -1;
985 }
986
Bram Moolenaara03e6312013-05-29 22:49:26 +0200987 DICTKEY_GET(-1, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +0200988
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200989 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +0200990
991 if (valObject == NULL)
992 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200993 hashitem_T *hi;
994
Bram Moolenaardb913952012-06-29 12:54:53 +0200995 if (di == NULL)
996 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200997 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200998 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +0200999 return -1;
1000 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001001 hi = hash_find(&dict->dv_hashtab, di->di_key);
1002 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001003 dictitem_free(di);
1004 return 0;
1005 }
1006
1007 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001008 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001009
1010 if (di == NULL)
1011 {
1012 di = dictitem_alloc(key);
1013 if (di == NULL)
1014 {
1015 PyErr_NoMemory();
1016 return -1;
1017 }
1018 di->di_tv.v_lock = 0;
1019
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001020 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001021 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001022 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +02001023 vim_free(di);
1024 PyErr_SetVim(_("failed to add key to dictionary"));
1025 return -1;
1026 }
1027 }
1028 else
1029 clear_tv(&di->di_tv);
1030
1031 DICTKEY_UNREF
1032
1033 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001034 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001035 return 0;
1036}
1037
1038 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001039DictionaryListKeys(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001040{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001041 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001042 long_u todo = dict->dv_hashtab.ht_used;
1043 Py_ssize_t i = 0;
1044 PyObject *r;
1045 hashitem_T *hi;
1046
1047 r = PyList_New(todo);
1048 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1049 {
1050 if (!HASHITEM_EMPTY(hi))
1051 {
1052 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
1053 --todo;
1054 ++i;
1055 }
1056 }
1057 return r;
1058}
1059
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001060static PyMappingMethods DictionaryAsMapping = {
1061 (lenfunc) DictionaryLength,
1062 (binaryfunc) DictionaryItem,
1063 (objobjargproc) DictionaryAssItem,
1064};
1065
Bram Moolenaardb913952012-06-29 12:54:53 +02001066static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001067 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001068 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1069 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001070};
1071
1072static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001073static PySequenceMethods ListAsSeq;
1074static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001075
1076typedef struct
1077{
1078 PyObject_HEAD
1079 list_T *list;
1080 pylinkedlist_T ref;
1081} ListObject;
1082
1083 static PyObject *
1084ListNew(list_T *list)
1085{
1086 ListObject *self;
1087
1088 self = PyObject_NEW(ListObject, &ListType);
1089 if (self == NULL)
1090 return NULL;
1091 self->list = list;
1092 ++list->lv_refcount;
1093
1094 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1095
1096 return (PyObject *)(self);
1097}
1098
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001099 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001100ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001101{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001102 pyll_remove(&self->ref, &lastlist);
1103 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001104
1105 DESTRUCTOR_FINISH(self);
1106}
1107
Bram Moolenaardb913952012-06-29 12:54:53 +02001108 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001109list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001110{
1111 Py_ssize_t i;
1112 Py_ssize_t lsize = PySequence_Size(obj);
1113 PyObject *litem;
1114 listitem_T *li;
1115
1116 for(i=0; i<lsize; i++)
1117 {
1118 li = listitem_alloc();
1119 if (li == NULL)
1120 {
1121 PyErr_NoMemory();
1122 return -1;
1123 }
1124 li->li_tv.v_lock = 0;
1125
1126 litem = PySequence_GetItem(obj, i);
1127 if (litem == NULL)
1128 return -1;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001129 if (_ConvertFromPyObject(litem, &li->li_tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001130 return -1;
1131
1132 list_append(l, li);
1133 }
1134 return 0;
1135}
1136
Bram Moolenaardb913952012-06-29 12:54:53 +02001137 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001138ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001139{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001140 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001141}
1142
1143 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001144ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001145{
1146 listitem_T *li;
1147
Bram Moolenaard6e39182013-05-21 18:30:34 +02001148 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001149 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001150 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001151 return NULL;
1152 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001153 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001154 if (li == NULL)
1155 {
1156 PyErr_SetVim(_("internal error: failed to get vim list item"));
1157 return NULL;
1158 }
1159 return ConvertToPyObject(&li->li_tv);
1160}
1161
1162#define PROC_RANGE \
1163 if (last < 0) {\
1164 if (last < -size) \
1165 last = 0; \
1166 else \
1167 last += size; \
1168 } \
1169 if (first < 0) \
1170 first = 0; \
1171 if (first > size) \
1172 first = size; \
1173 if (last > size) \
1174 last = size;
1175
1176 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001177ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001178{
1179 PyInt i;
1180 PyInt size = ListLength(self);
1181 PyInt n;
1182 PyObject *list;
1183 int reversed = 0;
1184
1185 PROC_RANGE
1186 if (first >= last)
1187 first = last;
1188
1189 n = last-first;
1190 list = PyList_New(n);
1191 if (list == NULL)
1192 return NULL;
1193
1194 for (i = 0; i < n; ++i)
1195 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001196 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001197 if (item == NULL)
1198 {
1199 Py_DECREF(list);
1200 return NULL;
1201 }
1202
1203 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1204 {
1205 Py_DECREF(item);
1206 Py_DECREF(list);
1207 return NULL;
1208 }
1209 }
1210
1211 return list;
1212}
1213
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001214typedef struct
1215{
1216 listwatch_T lw;
1217 list_T *list;
1218} listiterinfo_T;
1219
1220 static void
1221ListIterDestruct(listiterinfo_T *lii)
1222{
1223 list_rem_watch(lii->list, &lii->lw);
1224 PyMem_Free(lii);
1225}
1226
1227 static PyObject *
1228ListIterNext(listiterinfo_T **lii)
1229{
1230 PyObject *r;
1231
1232 if (!((*lii)->lw.lw_item))
1233 return NULL;
1234
1235 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1236 return NULL;
1237
1238 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1239
1240 return r;
1241}
1242
1243 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001244ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001245{
1246 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001247 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001248
1249 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1250 {
1251 PyErr_NoMemory();
1252 return NULL;
1253 }
1254
1255 list_add_watch(l, &lii->lw);
1256 lii->lw.lw_item = l->lv_first;
1257 lii->list = l;
1258
1259 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001260 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1261 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001262}
1263
Bram Moolenaardb913952012-06-29 12:54:53 +02001264 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001265ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001266{
1267 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001268 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001269 listitem_T *li;
1270 Py_ssize_t length = ListLength(self);
1271
1272 if (l->lv_lock)
1273 {
1274 PyErr_SetVim(_("list is locked"));
1275 return -1;
1276 }
1277 if (index>length || (index==length && obj==NULL))
1278 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001279 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001280 return -1;
1281 }
1282
1283 if (obj == NULL)
1284 {
1285 li = list_find(l, (long) index);
1286 list_remove(l, li, li);
1287 clear_tv(&li->li_tv);
1288 vim_free(li);
1289 return 0;
1290 }
1291
1292 if (ConvertFromPyObject(obj, &tv) == -1)
1293 return -1;
1294
1295 if (index == length)
1296 {
1297 if (list_append_tv(l, &tv) == FAIL)
1298 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001299 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001300 PyErr_SetVim(_("Failed to add item to list"));
1301 return -1;
1302 }
1303 }
1304 else
1305 {
1306 li = list_find(l, (long) index);
1307 clear_tv(&li->li_tv);
1308 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001309 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001310 }
1311 return 0;
1312}
1313
1314 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001315ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001316{
1317 PyInt size = ListLength(self);
1318 Py_ssize_t i;
1319 Py_ssize_t lsize;
1320 PyObject *litem;
1321 listitem_T *li;
1322 listitem_T *next;
1323 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001324 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001325
1326 if (l->lv_lock)
1327 {
1328 PyErr_SetVim(_("list is locked"));
1329 return -1;
1330 }
1331
1332 PROC_RANGE
1333
1334 if (first == size)
1335 li = NULL;
1336 else
1337 {
1338 li = list_find(l, (long) first);
1339 if (li == NULL)
1340 {
1341 PyErr_SetVim(_("internal error: no vim list item"));
1342 return -1;
1343 }
1344 if (last > first)
1345 {
1346 i = last - first;
1347 while (i-- && li != NULL)
1348 {
1349 next = li->li_next;
1350 listitem_remove(l, li);
1351 li = next;
1352 }
1353 }
1354 }
1355
1356 if (obj == NULL)
1357 return 0;
1358
1359 if (!PyList_Check(obj))
1360 {
1361 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1362 return -1;
1363 }
1364
1365 lsize = PyList_Size(obj);
1366
1367 for(i=0; i<lsize; i++)
1368 {
1369 litem = PyList_GetItem(obj, i);
1370 if (litem == NULL)
1371 return -1;
1372 if (ConvertFromPyObject(litem, &v) == -1)
1373 return -1;
1374 if (list_insert_tv(l, &v, li) == FAIL)
1375 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001376 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001377 PyErr_SetVim(_("internal error: failed to add item to list"));
1378 return -1;
1379 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001380 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001381 }
1382 return 0;
1383}
1384
1385 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001386ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001387{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001388 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001389 PyObject *lookup_dict;
1390
1391 if (l->lv_lock)
1392 {
1393 PyErr_SetVim(_("list is locked"));
1394 return NULL;
1395 }
1396
1397 if (!PySequence_Check(obj))
1398 {
1399 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1400 return NULL;
1401 }
1402
1403 lookup_dict = PyDict_New();
1404 if (list_py_concat(l, obj, lookup_dict) == -1)
1405 {
1406 Py_DECREF(lookup_dict);
1407 return NULL;
1408 }
1409 Py_DECREF(lookup_dict);
1410
1411 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001412 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001413}
1414
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001415static char *ListAttrs[] = {
1416 "locked",
1417 NULL
1418};
1419
1420 static PyObject *
1421ListDir(PyObject *self)
1422{
1423 return ObjectDir(self, ListAttrs);
1424}
1425
Bram Moolenaar66b79852012-09-21 14:00:35 +02001426 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001427ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001428{
1429 if (val == NULL)
1430 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001431 PyErr_SetString(PyExc_AttributeError,
1432 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001433 return -1;
1434 }
1435
1436 if (strcmp(name, "locked") == 0)
1437 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001438 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001439 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001440 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001441 return -1;
1442 }
1443 else
1444 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001445 int istrue = PyObject_IsTrue(val);
1446 if (istrue == -1)
1447 return -1;
1448 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001449 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001450 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001451 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001452 }
1453 return 0;
1454 }
1455 else
1456 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001457 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001458 return -1;
1459 }
1460}
1461
Bram Moolenaardb913952012-06-29 12:54:53 +02001462static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001463 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1464 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
1465 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001466};
1467
1468typedef struct
1469{
1470 PyObject_HEAD
1471 char_u *name;
1472} FunctionObject;
1473
1474static PyTypeObject FunctionType;
1475
1476 static PyObject *
1477FunctionNew(char_u *name)
1478{
1479 FunctionObject *self;
1480
1481 self = PyObject_NEW(FunctionObject, &FunctionType);
1482 if (self == NULL)
1483 return NULL;
1484 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1485 if (self->name == NULL)
1486 {
1487 PyErr_NoMemory();
1488 return NULL;
1489 }
1490 STRCPY(self->name, name);
1491 func_ref(name);
1492 return (PyObject *)(self);
1493}
1494
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001495 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001496FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001497{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001498 func_unref(self->name);
1499 PyMem_Free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001500
1501 DESTRUCTOR_FINISH(self);
1502}
1503
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001504static char *FunctionAttrs[] = {
1505 "softspace",
1506 NULL
1507};
1508
1509 static PyObject *
1510FunctionDir(PyObject *self)
1511{
1512 return ObjectDir(self, FunctionAttrs);
1513}
1514
Bram Moolenaardb913952012-06-29 12:54:53 +02001515 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001516FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02001517{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001518 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02001519 typval_T args;
1520 typval_T selfdicttv;
1521 typval_T rettv;
1522 dict_T *selfdict = NULL;
1523 PyObject *selfdictObject;
1524 PyObject *result;
1525 int error;
1526
1527 if (ConvertFromPyObject(argsObject, &args) == -1)
1528 return NULL;
1529
1530 if (kwargs != NULL)
1531 {
1532 selfdictObject = PyDict_GetItemString(kwargs, "self");
1533 if (selfdictObject != NULL)
1534 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001535 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001536 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001537 PyErr_SetString(PyExc_TypeError,
1538 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001539 clear_tv(&args);
1540 return NULL;
1541 }
1542 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001543 {
1544 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02001545 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001546 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001547 selfdict = selfdicttv.vval.v_dict;
1548 }
1549 }
1550
Bram Moolenaar71700b82013-05-15 17:49:05 +02001551 Py_BEGIN_ALLOW_THREADS
1552 Python_Lock_Vim();
1553
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001554 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02001555 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001556
1557 Python_Release_Vim();
1558 Py_END_ALLOW_THREADS
1559
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001560 if (VimTryEnd())
1561 result = NULL;
1562 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02001563 {
1564 result = NULL;
1565 PyErr_SetVim(_("failed to run function"));
1566 }
1567 else
1568 result = ConvertToPyObject(&rettv);
1569
Bram Moolenaardb913952012-06-29 12:54:53 +02001570 clear_tv(&args);
1571 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001572 if (selfdict != NULL)
1573 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001574
1575 return result;
1576}
1577
1578static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001579 {"__call__",(PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1580 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
1581 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001582};
1583
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001584/*
1585 * Options object
1586 */
1587
1588static PyTypeObject OptionsType;
1589
1590typedef int (*checkfun)(void *);
1591
1592typedef struct
1593{
1594 PyObject_HEAD
1595 int opt_type;
1596 void *from;
1597 checkfun Check;
1598 PyObject *fromObj;
1599} OptionsObject;
1600
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001601 static int
1602dummy_check(void *arg UNUSED)
1603{
1604 return 0;
1605}
1606
1607 static PyObject *
1608OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1609{
1610 OptionsObject *self;
1611
Bram Moolenaar774267b2013-05-21 20:51:59 +02001612 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001613 if (self == NULL)
1614 return NULL;
1615
1616 self->opt_type = opt_type;
1617 self->from = from;
1618 self->Check = Check;
1619 self->fromObj = fromObj;
1620 if (fromObj)
1621 Py_INCREF(fromObj);
1622
1623 return (PyObject *)(self);
1624}
1625
1626 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001627OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001628{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001629 PyObject_GC_UnTrack((void *)(self));
1630 Py_XDECREF(self->fromObj);
1631 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001632}
1633
1634 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001635OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001636{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001637 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001638 return 0;
1639}
1640
1641 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001642OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001643{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001644 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001645 return 0;
1646}
1647
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001648 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001649OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001650{
1651 char_u *key;
1652 int flags;
1653 long numval;
1654 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001655 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001656
Bram Moolenaard6e39182013-05-21 18:30:34 +02001657 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001658 return NULL;
1659
Bram Moolenaara03e6312013-05-29 22:49:26 +02001660 DICTKEY_GET(NULL, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001661
1662 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001663 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001664
1665 DICTKEY_UNREF
1666
1667 if (flags == 0)
1668 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001669 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001670 return NULL;
1671 }
1672
1673 if (flags & SOPT_UNSET)
1674 {
1675 Py_INCREF(Py_None);
1676 return Py_None;
1677 }
1678 else if (flags & SOPT_BOOL)
1679 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001680 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001681 r = numval ? Py_True : Py_False;
1682 Py_INCREF(r);
1683 return r;
1684 }
1685 else if (flags & SOPT_NUM)
1686 return PyInt_FromLong(numval);
1687 else if (flags & SOPT_STRING)
1688 {
1689 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001690 {
1691 PyObject *r = PyBytes_FromString((char *) stringval);
1692 vim_free(stringval);
1693 return r;
1694 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001695 else
1696 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001697 PyErr_SetString(PyExc_RuntimeError,
1698 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001699 return NULL;
1700 }
1701 }
1702 else
1703 {
1704 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1705 return NULL;
1706 }
1707}
1708
1709 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001710set_option_value_err(key, numval, stringval, opt_flags)
1711 char_u *key;
1712 int numval;
1713 char_u *stringval;
1714 int opt_flags;
1715{
1716 char_u *errmsg;
1717
1718 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
1719 {
1720 if (VimTryEnd())
1721 return FAIL;
1722 PyErr_SetVim((char *)errmsg);
1723 return FAIL;
1724 }
1725 return OK;
1726}
1727
1728 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001729set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1730 char_u *key;
1731 int numval;
1732 char_u *stringval;
1733 int opt_flags;
1734 int opt_type;
1735 void *from;
1736{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001737 win_T *save_curwin = NULL;
1738 tabpage_T *save_curtab = NULL;
1739 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001740 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001741
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001742 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001743 switch (opt_type)
1744 {
1745 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001746 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1747 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001748 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001749 if (VimTryEnd())
1750 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001751 PyErr_SetVim("Problem while switching windows.");
1752 return -1;
1753 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001754 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001755 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001756 if (r == FAIL)
1757 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001758 break;
1759 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001760 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001761 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001762 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001763 if (r == FAIL)
1764 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001765 break;
1766 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001767 r = set_option_value_err(key, numval, stringval, opt_flags);
1768 if (r == FAIL)
1769 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001770 break;
1771 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001772 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001773}
1774
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001775 static void *
1776py_memsave(void *p, size_t len)
1777{
1778 void *r;
1779 if (!(r = PyMem_Malloc(len)))
1780 return NULL;
1781 mch_memmove(r, p, len);
1782 return r;
1783}
1784
1785#define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1))
1786
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001787 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001788OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001789{
1790 char_u *key;
1791 int flags;
1792 int opt_flags;
1793 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001794 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001795
Bram Moolenaard6e39182013-05-21 18:30:34 +02001796 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001797 return -1;
1798
Bram Moolenaara03e6312013-05-29 22:49:26 +02001799 DICTKEY_GET(-1, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001800
1801 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001802 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001803
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001804 if (flags == 0)
1805 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001806 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001807 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001808 return -1;
1809 }
1810
1811 if (valObject == NULL)
1812 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001813 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001814 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001815 PyErr_SetString(PyExc_ValueError,
1816 _("unable to unset global option"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001817 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001818 return -1;
1819 }
1820 else if (!(flags & SOPT_GLOBAL))
1821 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001822 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1823 "without global value"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001824 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001825 return -1;
1826 }
1827 else
1828 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001829 unset_global_local_option(key, self->from);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001830 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001831 return 0;
1832 }
1833 }
1834
Bram Moolenaard6e39182013-05-21 18:30:34 +02001835 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001836
1837 if (flags & SOPT_BOOL)
1838 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001839 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001840
Bram Moolenaarb983f752013-05-15 16:11:50 +02001841 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001842 r = -1;
1843 else
1844 r = set_option_value_for(key, istrue, NULL,
1845 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001846 }
1847 else if (flags & SOPT_NUM)
1848 {
1849 int val;
1850
1851#if PY_MAJOR_VERSION < 3
1852 if (PyInt_Check(valObject))
1853 val = PyInt_AsLong(valObject);
1854 else
1855#endif
1856 if (PyLong_Check(valObject))
1857 val = PyLong_AsLong(valObject);
1858 else
1859 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001860 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001861 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001862 return -1;
1863 }
1864
1865 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001866 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001867 }
1868 else
1869 {
1870 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001871 PyObject *todecref;
1872
1873 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001874 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001875 r = set_option_value_for(key, 0, val, opt_flags,
1876 self->opt_type, self->from);
1877 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001878 }
1879 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001880 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001881 }
1882
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001883 DICTKEY_UNREF
1884
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001885 return r;
1886}
1887
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001888static PyMappingMethods OptionsAsMapping = {
1889 (lenfunc) NULL,
1890 (binaryfunc) OptionsItem,
1891 (objobjargproc) OptionsAssItem,
1892};
1893
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001894/* Tabpage object
1895 */
1896
1897typedef struct
1898{
1899 PyObject_HEAD
1900 tabpage_T *tab;
1901} TabPageObject;
1902
1903static PyObject *WinListNew(TabPageObject *tabObject);
1904
1905static PyTypeObject TabPageType;
1906
1907 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001908CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001909{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001910 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001911 {
1912 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1913 return -1;
1914 }
1915
1916 return 0;
1917}
1918
1919 static PyObject *
1920TabPageNew(tabpage_T *tab)
1921{
1922 TabPageObject *self;
1923
1924 if (TAB_PYTHON_REF(tab))
1925 {
1926 self = TAB_PYTHON_REF(tab);
1927 Py_INCREF(self);
1928 }
1929 else
1930 {
1931 self = PyObject_NEW(TabPageObject, &TabPageType);
1932 if (self == NULL)
1933 return NULL;
1934 self->tab = tab;
1935 TAB_PYTHON_REF(tab) = self;
1936 }
1937
1938 return (PyObject *)(self);
1939}
1940
1941 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001942TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001943{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001944 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
1945 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001946
1947 DESTRUCTOR_FINISH(self);
1948}
1949
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001950static char *TabPageAttrs[] = {
1951 "windows", "number", "vars", "window", "valid",
1952 NULL
1953};
1954
1955 static PyObject *
1956TabPageDir(PyObject *self)
1957{
1958 return ObjectDir(self, TabPageAttrs);
1959}
1960
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001961 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001962TabPageAttrValid(TabPageObject *self, char *name)
1963{
1964 PyObject *r;
1965
1966 if (strcmp(name, "valid") != 0)
1967 return NULL;
1968
1969 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
1970 Py_INCREF(r);
1971 return r;
1972}
1973
1974 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001975TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001976{
1977 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001978 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001979 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001980 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001981 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001982 return DictionaryNew(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001983 else if (strcmp(name, "window") == 0)
1984 {
1985 /* For current tab window.c does not bother to set or update tp_curwin
1986 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02001987 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001988 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001989 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001990 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001991 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001992 else if (strcmp(name, "__members__") == 0)
1993 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001994 return NULL;
1995}
1996
1997 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001998TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001999{
2000 static char repr[100];
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002001
Bram Moolenaard6e39182013-05-21 18:30:34 +02002002 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002003 {
2004 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
2005 return PyString_FromString(repr);
2006 }
2007 else
2008 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002009 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002010
2011 if (t == 0)
2012 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
2013 (self));
2014 else
2015 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
2016
2017 return PyString_FromString(repr);
2018 }
2019}
2020
2021static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002022 /* name, function, calling, documentation */
2023 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2024 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002025};
2026
2027/*
2028 * Window list object
2029 */
2030
2031static PyTypeObject TabListType;
2032static PySequenceMethods TabListAsSeq;
2033
2034typedef struct
2035{
2036 PyObject_HEAD
2037} TabListObject;
2038
2039 static PyInt
2040TabListLength(PyObject *self UNUSED)
2041{
2042 tabpage_T *tp = first_tabpage;
2043 PyInt n = 0;
2044
2045 while (tp != NULL)
2046 {
2047 ++n;
2048 tp = tp->tp_next;
2049 }
2050
2051 return n;
2052}
2053
2054 static PyObject *
2055TabListItem(PyObject *self UNUSED, PyInt n)
2056{
2057 tabpage_T *tp;
2058
2059 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2060 if (n == 0)
2061 return TabPageNew(tp);
2062
2063 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2064 return NULL;
2065}
2066
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002067/* Window object
2068 */
2069
2070typedef struct
2071{
2072 PyObject_HEAD
2073 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002074 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002075} WindowObject;
2076
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002077static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002078
2079 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002080CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002081{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002082 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002083 {
2084 PyErr_SetVim(_("attempt to refer to deleted window"));
2085 return -1;
2086 }
2087
2088 return 0;
2089}
2090
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002091 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002092WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002093{
2094 /* We need to handle deletion of windows underneath us.
2095 * If we add a "w_python*_ref" field to the win_T structure,
2096 * then we can get at it in win_free() in vim. We then
2097 * need to create only ONE Python object per window - if
2098 * we try to create a second, just INCREF the existing one
2099 * and return it. The (single) Python object referring to
2100 * the window is stored in "w_python*_ref".
2101 * On a win_free() we set the Python object's win_T* field
2102 * to an invalid value. We trap all uses of a window
2103 * object, and reject them if the win_T* field is invalid.
2104 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002105 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002106 * w_python_ref and w_python3_ref fields respectively.
2107 */
2108
2109 WindowObject *self;
2110
2111 if (WIN_PYTHON_REF(win))
2112 {
2113 self = WIN_PYTHON_REF(win);
2114 Py_INCREF(self);
2115 }
2116 else
2117 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002118 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002119 if (self == NULL)
2120 return NULL;
2121 self->win = win;
2122 WIN_PYTHON_REF(win) = self;
2123 }
2124
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002125 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2126
Bram Moolenaar971db462013-05-12 18:44:48 +02002127 return (PyObject *)(self);
2128}
2129
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002130 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002131WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002132{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002133 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002134 if (self->win && self->win != INVALID_WINDOW_VALUE)
2135 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002136 Py_XDECREF(((PyObject *)(self->tabObject)));
2137 PyObject_GC_Del((void *)(self));
2138}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002139
Bram Moolenaar774267b2013-05-21 20:51:59 +02002140 static int
2141WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2142{
2143 Py_VISIT(((PyObject *)(self->tabObject)));
2144 return 0;
2145}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002146
Bram Moolenaar774267b2013-05-21 20:51:59 +02002147 static int
2148WindowClear(WindowObject *self)
2149{
2150 Py_CLEAR(self->tabObject);
2151 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002152}
2153
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002154 static win_T *
2155get_firstwin(TabPageObject *tabObject)
2156{
2157 if (tabObject)
2158 {
2159 if (CheckTabPage(tabObject))
2160 return NULL;
2161 /* For current tab window.c does not bother to set or update tp_firstwin
2162 */
2163 else if (tabObject->tab == curtab)
2164 return firstwin;
2165 else
2166 return tabObject->tab->tp_firstwin;
2167 }
2168 else
2169 return firstwin;
2170}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002171static char *WindowAttrs[] = {
2172 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2173 "tabpage", "valid",
2174 NULL
2175};
2176
2177 static PyObject *
2178WindowDir(PyObject *self)
2179{
2180 return ObjectDir(self, WindowAttrs);
2181}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002182
Bram Moolenaar971db462013-05-12 18:44:48 +02002183 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002184WindowAttrValid(WindowObject *self, char *name)
2185{
2186 PyObject *r;
2187
2188 if (strcmp(name, "valid") != 0)
2189 return NULL;
2190
2191 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2192 Py_INCREF(r);
2193 return r;
2194}
2195
2196 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002197WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002198{
2199 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002200 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002201 else if (strcmp(name, "cursor") == 0)
2202 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002203 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002204
2205 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2206 }
2207 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002208 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002209#ifdef FEAT_WINDOWS
2210 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002211 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002212#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002213#ifdef FEAT_VERTSPLIT
2214 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002215 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002216 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002217 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002218#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002219 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002220 return DictionaryNew(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002221 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002222 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2223 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002224 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002225 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002226 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002227 return NULL;
2228 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002229 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002230 }
2231 else if (strcmp(name, "tabpage") == 0)
2232 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002233 Py_INCREF(self->tabObject);
2234 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002235 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002236 else if (strcmp(name, "__members__") == 0)
2237 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002238 else
2239 return NULL;
2240}
2241
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002242 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002243WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002244{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002245 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002246 return -1;
2247
2248 if (strcmp(name, "buffer") == 0)
2249 {
2250 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2251 return -1;
2252 }
2253 else if (strcmp(name, "cursor") == 0)
2254 {
2255 long lnum;
2256 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002257
2258 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2259 return -1;
2260
Bram Moolenaard6e39182013-05-21 18:30:34 +02002261 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002262 {
2263 PyErr_SetVim(_("cursor position outside buffer"));
2264 return -1;
2265 }
2266
2267 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002268 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002269 return -1;
2270
Bram Moolenaard6e39182013-05-21 18:30:34 +02002271 self->win->w_cursor.lnum = lnum;
2272 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002273#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002274 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002275#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002276 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002277 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002278
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002279 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002280 return 0;
2281 }
2282 else if (strcmp(name, "height") == 0)
2283 {
2284 int height;
2285 win_T *savewin;
2286
2287 if (!PyArg_Parse(val, "i", &height))
2288 return -1;
2289
2290#ifdef FEAT_GUI
2291 need_mouse_correct = TRUE;
2292#endif
2293 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002294 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002295
2296 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002297 win_setheight(height);
2298 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002299 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002300 return -1;
2301
2302 return 0;
2303 }
2304#ifdef FEAT_VERTSPLIT
2305 else if (strcmp(name, "width") == 0)
2306 {
2307 int width;
2308 win_T *savewin;
2309
2310 if (!PyArg_Parse(val, "i", &width))
2311 return -1;
2312
2313#ifdef FEAT_GUI
2314 need_mouse_correct = TRUE;
2315#endif
2316 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002317 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002318
2319 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002320 win_setwidth(width);
2321 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002322 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002323 return -1;
2324
2325 return 0;
2326 }
2327#endif
2328 else
2329 {
2330 PyErr_SetString(PyExc_AttributeError, name);
2331 return -1;
2332 }
2333}
2334
2335 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002336WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002337{
2338 static char repr[100];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002339
Bram Moolenaard6e39182013-05-21 18:30:34 +02002340 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002341 {
2342 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2343 return PyString_FromString(repr);
2344 }
2345 else
2346 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002347 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002348
Bram Moolenaar6d216452013-05-12 19:00:41 +02002349 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002350 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2351 (self));
2352 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002353 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002354
2355 return PyString_FromString(repr);
2356 }
2357}
2358
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002359static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002360 /* name, function, calling, documentation */
2361 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2362 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002363};
2364
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002365/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002366 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002367 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002368
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002369static PyTypeObject WinListType;
2370static PySequenceMethods WinListAsSeq;
2371
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002372typedef struct
2373{
2374 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002375 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002376} WinListObject;
2377
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002378 static PyObject *
2379WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002380{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002381 WinListObject *self;
2382
2383 self = PyObject_NEW(WinListObject, &WinListType);
2384 self->tabObject = tabObject;
2385 Py_INCREF(tabObject);
2386
2387 return (PyObject *)(self);
2388}
2389
2390 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002391WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002392{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002393 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002394
2395 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02002396 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002397 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02002398 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002399
2400 DESTRUCTOR_FINISH(self);
2401}
2402
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002403 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002404WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002405{
2406 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002407 PyInt n = 0;
2408
Bram Moolenaard6e39182013-05-21 18:30:34 +02002409 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002410 return -1;
2411
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002412 while (w != NULL)
2413 {
2414 ++n;
2415 w = W_NEXT(w);
2416 }
2417
2418 return n;
2419}
2420
2421 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002422WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002423{
2424 win_T *w;
2425
Bram Moolenaard6e39182013-05-21 18:30:34 +02002426 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002427 return NULL;
2428
2429 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002430 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002431 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002432
2433 PyErr_SetString(PyExc_IndexError, _("no such window"));
2434 return NULL;
2435}
2436
2437/* Convert a Python string into a Vim line.
2438 *
2439 * The result is in allocated memory. All internal nulls are replaced by
2440 * newline characters. It is an error for the string to contain newline
2441 * characters.
2442 *
2443 * On errors, the Python exception data is set, and NULL is returned.
2444 */
2445 static char *
2446StringToLine(PyObject *obj)
2447{
2448 const char *str;
2449 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002450 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002451 PyInt len;
2452 PyInt i;
2453 char *p;
2454
2455 if (obj == NULL || !PyString_Check(obj))
2456 {
2457 PyErr_BadArgument();
2458 return NULL;
2459 }
2460
Bram Moolenaar19e60942011-06-19 00:27:51 +02002461 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2462 str = PyString_AsString(bytes);
2463 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002464
2465 /*
2466 * Error checking: String must not contain newlines, as we
2467 * are replacing a single line, and we must replace it with
2468 * a single line.
2469 * A trailing newline is removed, so that append(f.readlines()) works.
2470 */
2471 p = memchr(str, '\n', len);
2472 if (p != NULL)
2473 {
2474 if (p == str + len - 1)
2475 --len;
2476 else
2477 {
2478 PyErr_SetVim(_("string cannot contain newlines"));
2479 return NULL;
2480 }
2481 }
2482
2483 /* Create a copy of the string, with internal nulls replaced by
2484 * newline characters, as is the vim convention.
2485 */
2486 save = (char *)alloc((unsigned)(len+1));
2487 if (save == NULL)
2488 {
2489 PyErr_NoMemory();
2490 return NULL;
2491 }
2492
2493 for (i = 0; i < len; ++i)
2494 {
2495 if (str[i] == '\0')
2496 save[i] = '\n';
2497 else
2498 save[i] = str[i];
2499 }
2500
2501 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002502 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002503
2504 return save;
2505}
2506
2507/* Get a line from the specified buffer. The line number is
2508 * in Vim format (1-based). The line is returned as a Python
2509 * string object.
2510 */
2511 static PyObject *
2512GetBufferLine(buf_T *buf, PyInt n)
2513{
2514 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2515}
2516
2517
2518/* Get a list of lines from the specified buffer. The line numbers
2519 * are in Vim format (1-based). The range is from lo up to, but not
2520 * including, hi. The list is returned as a Python list of string objects.
2521 */
2522 static PyObject *
2523GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2524{
2525 PyInt i;
2526 PyInt n = hi - lo;
2527 PyObject *list = PyList_New(n);
2528
2529 if (list == NULL)
2530 return NULL;
2531
2532 for (i = 0; i < n; ++i)
2533 {
2534 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2535
2536 /* Error check - was the Python string creation OK? */
2537 if (str == NULL)
2538 {
2539 Py_DECREF(list);
2540 return NULL;
2541 }
2542
2543 /* Set the list item */
2544 if (PyList_SetItem(list, i, str))
2545 {
2546 Py_DECREF(str);
2547 Py_DECREF(list);
2548 return NULL;
2549 }
2550 }
2551
2552 /* The ownership of the Python list is passed to the caller (ie,
2553 * the caller should Py_DECREF() the object when it is finished
2554 * with it).
2555 */
2556
2557 return list;
2558}
2559
2560/*
2561 * Check if deleting lines made the cursor position invalid.
2562 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2563 * deleted).
2564 */
2565 static void
2566py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2567{
2568 if (curwin->w_cursor.lnum >= lo)
2569 {
2570 /* Adjust the cursor position if it's in/after the changed
2571 * lines. */
2572 if (curwin->w_cursor.lnum >= hi)
2573 {
2574 curwin->w_cursor.lnum += extra;
2575 check_cursor_col();
2576 }
2577 else if (extra < 0)
2578 {
2579 curwin->w_cursor.lnum = lo;
2580 check_cursor();
2581 }
2582 else
2583 check_cursor_col();
2584 changed_cline_bef_curs();
2585 }
2586 invalidate_botline();
2587}
2588
Bram Moolenaar19e60942011-06-19 00:27:51 +02002589/*
2590 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002591 * in Vim format (1-based). The replacement line is given as
2592 * a Python string object. The object is checked for validity
2593 * and correct format. Errors are returned as a value of FAIL.
2594 * The return value is OK on success.
2595 * If OK is returned and len_change is not NULL, *len_change
2596 * is set to the change in the buffer length.
2597 */
2598 static int
2599SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2600{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002601 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002602 * There are three cases:
2603 * 1. NULL, or None - this is a deletion.
2604 * 2. A string - this is a replacement.
2605 * 3. Anything else - this is an error.
2606 */
2607 if (line == Py_None || line == NULL)
2608 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002609 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002610
2611 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002612 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002613
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002614 VimTryStart();
2615
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002616 if (u_savedel((linenr_T)n, 1L) == FAIL)
2617 PyErr_SetVim(_("cannot save undo information"));
2618 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2619 PyErr_SetVim(_("cannot delete line"));
2620 else
2621 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002622 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002623 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2624 deleted_lines_mark((linenr_T)n, 1L);
2625 }
2626
Bram Moolenaar105bc352013-05-17 16:03:57 +02002627 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002628
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002629 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002630 return FAIL;
2631
2632 if (len_change)
2633 *len_change = -1;
2634
2635 return OK;
2636 }
2637 else if (PyString_Check(line))
2638 {
2639 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002640 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002641
2642 if (save == NULL)
2643 return FAIL;
2644
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002645 VimTryStart();
2646
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002647 /* We do not need to free "save" if ml_replace() consumes it. */
2648 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002649 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002650
2651 if (u_savesub((linenr_T)n) == FAIL)
2652 {
2653 PyErr_SetVim(_("cannot save undo information"));
2654 vim_free(save);
2655 }
2656 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2657 {
2658 PyErr_SetVim(_("cannot replace line"));
2659 vim_free(save);
2660 }
2661 else
2662 changed_bytes((linenr_T)n, 0);
2663
Bram Moolenaar105bc352013-05-17 16:03:57 +02002664 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002665
2666 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002667 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002668 check_cursor_col();
2669
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002670 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002671 return FAIL;
2672
2673 if (len_change)
2674 *len_change = 0;
2675
2676 return OK;
2677 }
2678 else
2679 {
2680 PyErr_BadArgument();
2681 return FAIL;
2682 }
2683}
2684
Bram Moolenaar19e60942011-06-19 00:27:51 +02002685/* Replace a range of lines in the specified buffer. The line numbers are in
2686 * Vim format (1-based). The range is from lo up to, but not including, hi.
2687 * The replacement lines are given as a Python list of string objects. The
2688 * list is checked for validity and correct format. Errors are returned as a
2689 * value of FAIL. The return value is OK on success.
2690 * If OK is returned and len_change is not NULL, *len_change
2691 * is set to the change in the buffer length.
2692 */
2693 static int
2694SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2695{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002696 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002697 * There are three cases:
2698 * 1. NULL, or None - this is a deletion.
2699 * 2. A list - this is a replacement.
2700 * 3. Anything else - this is an error.
2701 */
2702 if (list == Py_None || list == NULL)
2703 {
2704 PyInt i;
2705 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002706 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002707
2708 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002709 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002710 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002711
2712 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2713 PyErr_SetVim(_("cannot save undo information"));
2714 else
2715 {
2716 for (i = 0; i < n; ++i)
2717 {
2718 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2719 {
2720 PyErr_SetVim(_("cannot delete line"));
2721 break;
2722 }
2723 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002724 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002725 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2726 deleted_lines_mark((linenr_T)lo, (long)i);
2727 }
2728
Bram Moolenaar105bc352013-05-17 16:03:57 +02002729 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002730
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002731 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002732 return FAIL;
2733
2734 if (len_change)
2735 *len_change = -n;
2736
2737 return OK;
2738 }
2739 else if (PyList_Check(list))
2740 {
2741 PyInt i;
2742 PyInt new_len = PyList_Size(list);
2743 PyInt old_len = hi - lo;
2744 PyInt extra = 0; /* lines added to text, can be negative */
2745 char **array;
2746 buf_T *savebuf;
2747
2748 if (new_len == 0) /* avoid allocating zero bytes */
2749 array = NULL;
2750 else
2751 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002752 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002753 if (array == NULL)
2754 {
2755 PyErr_NoMemory();
2756 return FAIL;
2757 }
2758 }
2759
2760 for (i = 0; i < new_len; ++i)
2761 {
2762 PyObject *line = PyList_GetItem(list, i);
2763
2764 array[i] = StringToLine(line);
2765 if (array[i] == NULL)
2766 {
2767 while (i)
2768 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002769 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002770 return FAIL;
2771 }
2772 }
2773
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002774 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02002775 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002776
2777 // START of region without "return". Must call restore_buffer()!
2778 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002779
2780 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2781 PyErr_SetVim(_("cannot save undo information"));
2782
2783 /* If the size of the range is reducing (ie, new_len < old_len) we
2784 * need to delete some old_len. We do this at the start, by
2785 * repeatedly deleting line "lo".
2786 */
2787 if (!PyErr_Occurred())
2788 {
2789 for (i = 0; i < old_len - new_len; ++i)
2790 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2791 {
2792 PyErr_SetVim(_("cannot delete line"));
2793 break;
2794 }
2795 extra -= i;
2796 }
2797
2798 /* For as long as possible, replace the existing old_len with the
2799 * new old_len. This is a more efficient operation, as it requires
2800 * less memory allocation and freeing.
2801 */
2802 if (!PyErr_Occurred())
2803 {
2804 for (i = 0; i < old_len && i < new_len; ++i)
2805 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2806 == FAIL)
2807 {
2808 PyErr_SetVim(_("cannot replace line"));
2809 break;
2810 }
2811 }
2812 else
2813 i = 0;
2814
2815 /* Now we may need to insert the remaining new old_len. If we do, we
2816 * must free the strings as we finish with them (we can't pass the
2817 * responsibility to vim in this case).
2818 */
2819 if (!PyErr_Occurred())
2820 {
2821 while (i < new_len)
2822 {
2823 if (ml_append((linenr_T)(lo + i - 1),
2824 (char_u *)array[i], 0, FALSE) == FAIL)
2825 {
2826 PyErr_SetVim(_("cannot insert line"));
2827 break;
2828 }
2829 vim_free(array[i]);
2830 ++i;
2831 ++extra;
2832 }
2833 }
2834
2835 /* Free any left-over old_len, as a result of an error */
2836 while (i < new_len)
2837 {
2838 vim_free(array[i]);
2839 ++i;
2840 }
2841
2842 /* Free the array of old_len. All of its contents have now
2843 * been dealt with (either freed, or the responsibility passed
2844 * to vim.
2845 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002846 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002847
2848 /* Adjust marks. Invalidate any which lie in the
2849 * changed range, and move any in the remainder of the buffer.
2850 */
2851 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2852 (long)MAXLNUM, (long)extra);
2853 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2854
Bram Moolenaar105bc352013-05-17 16:03:57 +02002855 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002856 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2857
Bram Moolenaar105bc352013-05-17 16:03:57 +02002858 // END of region without "return".
2859 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002860
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002861 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002862 return FAIL;
2863
2864 if (len_change)
2865 *len_change = new_len - old_len;
2866
2867 return OK;
2868 }
2869 else
2870 {
2871 PyErr_BadArgument();
2872 return FAIL;
2873 }
2874}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002875
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002876/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002877 * The line number is in Vim format (1-based). The lines to be inserted are
2878 * given as a Python list of string objects or as a single string. The lines
2879 * to be added are checked for validity and correct format. Errors are
2880 * returned as a value of FAIL. The return value is OK on success.
2881 * If OK is returned and len_change is not NULL, *len_change
2882 * is set to the change in the buffer length.
2883 */
2884 static int
2885InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2886{
2887 /* First of all, we check the type of the supplied Python object.
2888 * It must be a string or a list, or the call is in error.
2889 */
2890 if (PyString_Check(lines))
2891 {
2892 char *str = StringToLine(lines);
2893 buf_T *savebuf;
2894
2895 if (str == NULL)
2896 return FAIL;
2897
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002898 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002899 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002900 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002901
2902 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2903 PyErr_SetVim(_("cannot save undo information"));
2904 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2905 PyErr_SetVim(_("cannot insert line"));
2906 else
2907 appended_lines_mark((linenr_T)n, 1L);
2908
2909 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002910 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002911 update_screen(VALID);
2912
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002913 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002914 return FAIL;
2915
2916 if (len_change)
2917 *len_change = 1;
2918
2919 return OK;
2920 }
2921 else if (PyList_Check(lines))
2922 {
2923 PyInt i;
2924 PyInt size = PyList_Size(lines);
2925 char **array;
2926 buf_T *savebuf;
2927
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002928 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002929 if (array == NULL)
2930 {
2931 PyErr_NoMemory();
2932 return FAIL;
2933 }
2934
2935 for (i = 0; i < size; ++i)
2936 {
2937 PyObject *line = PyList_GetItem(lines, i);
2938 array[i] = StringToLine(line);
2939
2940 if (array[i] == NULL)
2941 {
2942 while (i)
2943 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002944 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002945 return FAIL;
2946 }
2947 }
2948
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002949 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002950 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002951 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002952
2953 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2954 PyErr_SetVim(_("cannot save undo information"));
2955 else
2956 {
2957 for (i = 0; i < size; ++i)
2958 {
2959 if (ml_append((linenr_T)(n + i),
2960 (char_u *)array[i], 0, FALSE) == FAIL)
2961 {
2962 PyErr_SetVim(_("cannot insert line"));
2963
2964 /* Free the rest of the lines */
2965 while (i < size)
2966 vim_free(array[i++]);
2967
2968 break;
2969 }
2970 vim_free(array[i]);
2971 }
2972 if (i > 0)
2973 appended_lines_mark((linenr_T)n, (long)i);
2974 }
2975
2976 /* Free the array of lines. All of its contents have now
2977 * been freed.
2978 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002979 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002980
Bram Moolenaar105bc352013-05-17 16:03:57 +02002981 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002982 update_screen(VALID);
2983
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002984 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002985 return FAIL;
2986
2987 if (len_change)
2988 *len_change = size;
2989
2990 return OK;
2991 }
2992 else
2993 {
2994 PyErr_BadArgument();
2995 return FAIL;
2996 }
2997}
2998
2999/*
3000 * Common routines for buffers and line ranges
3001 * -------------------------------------------
3002 */
3003
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003004typedef struct
3005{
3006 PyObject_HEAD
3007 buf_T *buf;
3008} BufferObject;
3009
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003010 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003011CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003012{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003013 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003014 {
3015 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3016 return -1;
3017 }
3018
3019 return 0;
3020}
3021
3022 static PyObject *
3023RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3024{
3025 if (CheckBuffer(self))
3026 return NULL;
3027
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003028 if (end == -1)
3029 end = self->buf->b_ml.ml_line_count;
3030
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003031 if (n < 0)
3032 n += end - start + 1;
3033
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003034 if (n < 0 || n > end - start)
3035 {
3036 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3037 return NULL;
3038 }
3039
3040 return GetBufferLine(self->buf, n+start);
3041}
3042
3043 static PyObject *
3044RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3045{
3046 PyInt size;
3047
3048 if (CheckBuffer(self))
3049 return NULL;
3050
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003051 if (end == -1)
3052 end = self->buf->b_ml.ml_line_count;
3053
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003054 size = end - start + 1;
3055
3056 if (lo < 0)
3057 lo = 0;
3058 else if (lo > size)
3059 lo = size;
3060 if (hi < 0)
3061 hi = 0;
3062 if (hi < lo)
3063 hi = lo;
3064 else if (hi > size)
3065 hi = size;
3066
3067 return GetBufferLineList(self->buf, lo+start, hi+start);
3068}
3069
3070 static PyInt
3071RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3072{
3073 PyInt len_change;
3074
3075 if (CheckBuffer(self))
3076 return -1;
3077
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003078 if (end == -1)
3079 end = self->buf->b_ml.ml_line_count;
3080
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003081 if (n < 0)
3082 n += end - start + 1;
3083
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003084 if (n < 0 || n > end - start)
3085 {
3086 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3087 return -1;
3088 }
3089
3090 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3091 return -1;
3092
3093 if (new_end)
3094 *new_end = end + len_change;
3095
3096 return 0;
3097}
3098
Bram Moolenaar19e60942011-06-19 00:27:51 +02003099 static PyInt
3100RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3101{
3102 PyInt size;
3103 PyInt len_change;
3104
3105 /* Self must be a valid buffer */
3106 if (CheckBuffer(self))
3107 return -1;
3108
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003109 if (end == -1)
3110 end = self->buf->b_ml.ml_line_count;
3111
Bram Moolenaar19e60942011-06-19 00:27:51 +02003112 /* Sort out the slice range */
3113 size = end - start + 1;
3114
3115 if (lo < 0)
3116 lo = 0;
3117 else if (lo > size)
3118 lo = size;
3119 if (hi < 0)
3120 hi = 0;
3121 if (hi < lo)
3122 hi = lo;
3123 else if (hi > size)
3124 hi = size;
3125
3126 if (SetBufferLineList(self->buf, lo + start, hi + start,
3127 val, &len_change) == FAIL)
3128 return -1;
3129
3130 if (new_end)
3131 *new_end = end + len_change;
3132
3133 return 0;
3134}
3135
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003136
3137 static PyObject *
3138RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3139{
3140 PyObject *lines;
3141 PyInt len_change;
3142 PyInt max;
3143 PyInt n;
3144
3145 if (CheckBuffer(self))
3146 return NULL;
3147
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003148 if (end == -1)
3149 end = self->buf->b_ml.ml_line_count;
3150
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003151 max = n = end - start + 1;
3152
3153 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3154 return NULL;
3155
3156 if (n < 0 || n > max)
3157 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003158 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003159 return NULL;
3160 }
3161
3162 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3163 return NULL;
3164
3165 if (new_end)
3166 *new_end = end + len_change;
3167
3168 Py_INCREF(Py_None);
3169 return Py_None;
3170}
3171
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003172/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003173 */
3174
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003175static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003176static PySequenceMethods RangeAsSeq;
3177static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003178
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003179typedef struct
3180{
3181 PyObject_HEAD
3182 BufferObject *buf;
3183 PyInt start;
3184 PyInt end;
3185} RangeObject;
3186
3187 static PyObject *
3188RangeNew(buf_T *buf, PyInt start, PyInt end)
3189{
3190 BufferObject *bufr;
3191 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003192 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003193 if (self == NULL)
3194 return NULL;
3195
3196 bufr = (BufferObject *)BufferNew(buf);
3197 if (bufr == NULL)
3198 {
3199 Py_DECREF(self);
3200 return NULL;
3201 }
3202 Py_INCREF(bufr);
3203
3204 self->buf = bufr;
3205 self->start = start;
3206 self->end = end;
3207
3208 return (PyObject *)(self);
3209}
3210
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003211 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003212RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003213{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003214 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003215 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003216 PyObject_GC_Del((void *)(self));
3217}
3218
3219 static int
3220RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3221{
3222 Py_VISIT(((PyObject *)(self->buf)));
3223 return 0;
3224}
3225
3226 static int
3227RangeClear(RangeObject *self)
3228{
3229 Py_CLEAR(self->buf);
3230 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003231}
3232
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003233 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003234RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003235{
3236 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003237 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003238 return -1; /* ??? */
3239
Bram Moolenaard6e39182013-05-21 18:30:34 +02003240 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003241}
3242
3243 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003244RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003245{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003246 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003247}
3248
3249 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003250RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003251{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003252 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003253}
3254
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003255static char *RangeAttrs[] = {
3256 "start", "end",
3257 NULL
3258};
3259
3260 static PyObject *
3261RangeDir(PyObject *self)
3262{
3263 return ObjectDir(self, RangeAttrs);
3264}
3265
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003266 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003267RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003268{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003269 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003270}
3271
3272 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003273RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003274{
3275 static char repr[100];
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003276
Bram Moolenaard6e39182013-05-21 18:30:34 +02003277 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003278 {
3279 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
3280 (self));
3281 return PyString_FromString(repr);
3282 }
3283 else
3284 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003285 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003286 int len;
3287
3288 if (name == NULL)
3289 name = "";
3290 len = (int)strlen(name);
3291
3292 if (len > 45)
3293 name = name + (45 - len);
3294
3295 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
3296 len > 45 ? "..." : "", name,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003297 self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003298
3299 return PyString_FromString(repr);
3300 }
3301}
3302
3303static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003304 /* name, function, calling, documentation */
3305 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003306 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3307 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003308};
3309
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003310static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003311static PySequenceMethods BufferAsSeq;
3312static PyMappingMethods BufferAsMapping;
3313
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003314 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003315BufferNew(buf_T *buf)
3316{
3317 /* We need to handle deletion of buffers underneath us.
3318 * If we add a "b_python*_ref" field to the buf_T structure,
3319 * then we can get at it in buf_freeall() in vim. We then
3320 * need to create only ONE Python object per buffer - if
3321 * we try to create a second, just INCREF the existing one
3322 * and return it. The (single) Python object referring to
3323 * the buffer is stored in "b_python*_ref".
3324 * Question: what to do on a buf_freeall(). We'll probably
3325 * have to either delete the Python object (DECREF it to
3326 * zero - a bad idea, as it leaves dangling refs!) or
3327 * set the buf_T * value to an invalid value (-1?), which
3328 * means we need checks in all access functions... Bah.
3329 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003330 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003331 * b_python_ref and b_python3_ref fields respectively.
3332 */
3333
3334 BufferObject *self;
3335
3336 if (BUF_PYTHON_REF(buf) != NULL)
3337 {
3338 self = BUF_PYTHON_REF(buf);
3339 Py_INCREF(self);
3340 }
3341 else
3342 {
3343 self = PyObject_NEW(BufferObject, &BufferType);
3344 if (self == NULL)
3345 return NULL;
3346 self->buf = buf;
3347 BUF_PYTHON_REF(buf) = self;
3348 }
3349
3350 return (PyObject *)(self);
3351}
3352
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003353 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003354BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003355{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003356 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3357 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003358
3359 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003360}
3361
Bram Moolenaar971db462013-05-12 18:44:48 +02003362 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003363BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003364{
3365 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003367 return -1; /* ??? */
3368
Bram Moolenaard6e39182013-05-21 18:30:34 +02003369 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003370}
3371
3372 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003373BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003374{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003375 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003376}
3377
3378 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003379BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003380{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003381 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003382}
3383
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003384static char *BufferAttrs[] = {
3385 "name", "number", "vars", "options", "valid",
3386 NULL
3387};
3388
3389 static PyObject *
3390BufferDir(PyObject *self)
3391{
3392 return ObjectDir(self, BufferAttrs);
3393}
3394
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003395 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003396BufferAttrValid(BufferObject *self, char *name)
3397{
3398 PyObject *r;
3399
3400 if (strcmp(name, "valid") != 0)
3401 return NULL;
3402
3403 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
3404 Py_INCREF(r);
3405 return r;
3406}
3407
3408 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003409BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003410{
3411 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02003412 return PyString_FromString((self->buf->b_ffname == NULL
3413 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003414 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003415 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003416 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003417 return DictionaryNew(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003418 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003419 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3420 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003421 else if (strcmp(name, "__members__") == 0)
3422 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003423 else
3424 return NULL;
3425}
3426
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003427 static int
3428BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3429{
3430 if (CheckBuffer(self))
3431 return -1;
3432
3433 if (strcmp(name, "name") == 0)
3434 {
3435 char_u *val;
3436 aco_save_T aco;
3437 int r;
3438 PyObject *todecref;
3439
3440 if (!(val = StringToChars(valObject, &todecref)))
3441 return -1;
3442
3443 VimTryStart();
3444 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3445 aucmd_prepbuf(&aco, self->buf);
3446 r = rename_buffer(val);
3447 aucmd_restbuf(&aco);
3448 Py_XDECREF(todecref);
3449 if (VimTryEnd())
3450 return -1;
3451
3452 if (r == FAIL)
3453 {
3454 PyErr_SetVim(_("failed to rename buffer"));
3455 return -1;
3456 }
3457 return 0;
3458 }
3459 else
3460 {
3461 PyErr_SetString(PyExc_AttributeError, name);
3462 return -1;
3463 }
3464}
3465
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003466 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003467BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003468{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003469 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003470}
3471
3472 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003473BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003474{
3475 pos_T *posp;
3476 char *pmark;
3477 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003478 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003479
Bram Moolenaard6e39182013-05-21 18:30:34 +02003480 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003481 return NULL;
3482
3483 if (!PyArg_ParseTuple(args, "s", &pmark))
3484 return NULL;
3485 mark = *pmark;
3486
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003487 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02003488 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003489 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003490 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003491 if (VimTryEnd())
3492 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003493
3494 if (posp == NULL)
3495 {
3496 PyErr_SetVim(_("invalid mark name"));
3497 return NULL;
3498 }
3499
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003500 if (posp->lnum <= 0)
3501 {
3502 /* Or raise an error? */
3503 Py_INCREF(Py_None);
3504 return Py_None;
3505 }
3506
3507 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3508}
3509
3510 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003511BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003512{
3513 PyInt start;
3514 PyInt end;
3515
Bram Moolenaard6e39182013-05-21 18:30:34 +02003516 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003517 return NULL;
3518
3519 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3520 return NULL;
3521
Bram Moolenaard6e39182013-05-21 18:30:34 +02003522 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003523}
3524
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003525 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003526BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003527{
3528 static char repr[100];
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003529
Bram Moolenaard6e39182013-05-21 18:30:34 +02003530 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003531 {
3532 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3533 return PyString_FromString(repr);
3534 }
3535 else
3536 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003537 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003538 PyInt len;
3539
3540 if (name == NULL)
3541 name = "";
3542 len = strlen(name);
3543
3544 if (len > 35)
3545 name = name + (35 - len);
3546
3547 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3548
3549 return PyString_FromString(repr);
3550 }
3551}
3552
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003553static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003554 /* name, function, calling, documentation */
3555 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
3556 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
3557 {"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 +02003558 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
3559 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003560};
3561
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003562/*
3563 * Buffer list object - Implementation
3564 */
3565
3566static PyTypeObject BufMapType;
3567
3568typedef struct
3569{
3570 PyObject_HEAD
3571} BufMapObject;
3572
3573 static PyInt
3574BufMapLength(PyObject *self UNUSED)
3575{
3576 buf_T *b = firstbuf;
3577 PyInt n = 0;
3578
3579 while (b)
3580 {
3581 ++n;
3582 b = b->b_next;
3583 }
3584
3585 return n;
3586}
3587
3588 static PyObject *
3589BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3590{
3591 buf_T *b;
3592 int bnr;
3593
3594#if PY_MAJOR_VERSION < 3
3595 if (PyInt_Check(keyObject))
3596 bnr = PyInt_AsLong(keyObject);
3597 else
3598#endif
3599 if (PyLong_Check(keyObject))
3600 bnr = PyLong_AsLong(keyObject);
3601 else
3602 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003603 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003604 return NULL;
3605 }
3606
3607 b = buflist_findnr(bnr);
3608
3609 if (b)
3610 return BufferNew(b);
3611 else
3612 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003613 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003614 return NULL;
3615 }
3616}
3617
3618 static void
3619BufMapIterDestruct(PyObject *buffer)
3620{
3621 /* Iteration was stopped before all buffers were processed */
3622 if (buffer)
3623 {
3624 Py_DECREF(buffer);
3625 }
3626}
3627
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003628 static int
3629BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3630{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003631 if (buffer)
3632 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003633 return 0;
3634}
3635
3636 static int
3637BufMapIterClear(PyObject **buffer)
3638{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003639 if (*buffer)
3640 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003641 return 0;
3642}
3643
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003644 static PyObject *
3645BufMapIterNext(PyObject **buffer)
3646{
3647 PyObject *next;
3648 PyObject *r;
3649
3650 if (!*buffer)
3651 return NULL;
3652
3653 r = *buffer;
3654
3655 if (CheckBuffer((BufferObject *)(r)))
3656 {
3657 *buffer = NULL;
3658 return NULL;
3659 }
3660
3661 if (!((BufferObject *)(r))->buf->b_next)
3662 next = NULL;
3663 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3664 return NULL;
3665 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02003666 /* Do not increment reference: we no longer hold it (decref), but whoever
3667 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003668 return r;
3669}
3670
3671 static PyObject *
3672BufMapIter(PyObject *self UNUSED)
3673{
3674 PyObject *buffer;
3675
3676 buffer = BufferNew(firstbuf);
3677 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003678 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3679 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003680}
3681
3682static PyMappingMethods BufMapAsMapping = {
3683 (lenfunc) BufMapLength,
3684 (binaryfunc) BufMapItem,
3685 (objobjargproc) 0,
3686};
3687
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003688/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003689 */
3690
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003691static char *CurrentAttrs[] = {
3692 "buffer", "window", "line", "range", "tabpage",
3693 NULL
3694};
3695
3696 static PyObject *
3697CurrentDir(PyObject *self)
3698{
3699 return ObjectDir(self, CurrentAttrs);
3700}
3701
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003702 static PyObject *
3703CurrentGetattr(PyObject *self UNUSED, char *name)
3704{
3705 if (strcmp(name, "buffer") == 0)
3706 return (PyObject *)BufferNew(curbuf);
3707 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003708 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003709 else if (strcmp(name, "tabpage") == 0)
3710 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003711 else if (strcmp(name, "line") == 0)
3712 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3713 else if (strcmp(name, "range") == 0)
3714 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003715 else if (strcmp(name, "__members__") == 0)
3716 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003717 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003718#if PY_MAJOR_VERSION < 3
3719 return Py_FindMethod(WindowMethods, self, name);
3720#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003721 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003722#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003723}
3724
3725 static int
3726CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3727{
3728 if (strcmp(name, "line") == 0)
3729 {
3730 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3731 return -1;
3732
3733 return 0;
3734 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003735 else if (strcmp(name, "buffer") == 0)
3736 {
3737 int count;
3738
3739 if (value->ob_type != &BufferType)
3740 {
3741 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3742 return -1;
3743 }
3744
3745 if (CheckBuffer((BufferObject *)(value)))
3746 return -1;
3747 count = ((BufferObject *)(value))->buf->b_fnum;
3748
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003749 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003750 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3751 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003752 if (VimTryEnd())
3753 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003754 PyErr_SetVim(_("failed to switch to given buffer"));
3755 return -1;
3756 }
3757
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003758 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003759 }
3760 else if (strcmp(name, "window") == 0)
3761 {
3762 int count;
3763
3764 if (value->ob_type != &WindowType)
3765 {
3766 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3767 return -1;
3768 }
3769
3770 if (CheckWindow((WindowObject *)(value)))
3771 return -1;
3772 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3773
3774 if (!count)
3775 {
3776 PyErr_SetString(PyExc_ValueError,
3777 _("failed to find window in the current tab page"));
3778 return -1;
3779 }
3780
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003781 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003782 win_goto(((WindowObject *)(value))->win);
3783 if (((WindowObject *)(value))->win != curwin)
3784 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003785 if (VimTryEnd())
3786 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003787 PyErr_SetString(PyExc_RuntimeError,
3788 _("did not switch to the specified window"));
3789 return -1;
3790 }
3791
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003792 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003793 }
3794 else if (strcmp(name, "tabpage") == 0)
3795 {
3796 if (value->ob_type != &TabPageType)
3797 {
3798 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3799 return -1;
3800 }
3801
3802 if (CheckTabPage((TabPageObject *)(value)))
3803 return -1;
3804
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003805 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003806 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3807 if (((TabPageObject *)(value))->tab != curtab)
3808 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003809 if (VimTryEnd())
3810 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003811 PyErr_SetString(PyExc_RuntimeError,
3812 _("did not switch to the specified tab page"));
3813 return -1;
3814 }
3815
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003816 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003817 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003818 else
3819 {
3820 PyErr_SetString(PyExc_AttributeError, name);
3821 return -1;
3822 }
3823}
3824
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003825static struct PyMethodDef CurrentMethods[] = {
3826 /* name, function, calling, documentation */
3827 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
3828 { NULL, NULL, 0, NULL}
3829};
3830
Bram Moolenaardb913952012-06-29 12:54:53 +02003831 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003832init_range_cmd(exarg_T *eap)
3833{
3834 RangeStart = eap->line1;
3835 RangeEnd = eap->line2;
3836}
3837
3838 static void
3839init_range_eval(typval_T *rettv UNUSED)
3840{
3841 RangeStart = (PyInt) curwin->w_cursor.lnum;
3842 RangeEnd = RangeStart;
3843}
3844
3845 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003846run_cmd(const char *cmd, void *arg UNUSED
3847#ifdef PY_CAN_RECURSE
3848 , PyGILState_STATE *pygilstate UNUSED
3849#endif
3850 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003851{
3852 PyRun_SimpleString((char *) cmd);
3853}
3854
3855static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
3856static int code_hdr_len = 30;
3857
3858 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003859run_do(const char *cmd, void *arg UNUSED
3860#ifdef PY_CAN_RECURSE
3861 , PyGILState_STATE *pygilstate
3862#endif
3863 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003864{
3865 PyInt lnum;
3866 size_t len;
3867 char *code;
3868 int status;
3869 PyObject *pyfunc, *pymain;
3870
Bram Moolenaar4ac66762013-05-28 22:31:46 +02003871 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003872 {
3873 EMSG(_("cannot save undo information"));
3874 return;
3875 }
3876
3877 len = code_hdr_len + STRLEN(cmd);
3878 code = PyMem_New(char, len + 1);
3879 memcpy(code, code_hdr, code_hdr_len);
3880 STRCPY(code + code_hdr_len, cmd);
3881 status = PyRun_SimpleString(code);
3882 PyMem_Free(code);
3883
3884 if (status)
3885 {
3886 EMSG(_("failed to run the code"));
3887 return;
3888 }
3889
3890 status = 0;
3891 pymain = PyImport_AddModule("__main__");
3892 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003893#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003894 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003895#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003896
3897 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
3898 {
3899 PyObject *line, *linenr, *ret;
3900
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003901#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003902 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003903#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003904 if (!(line = GetBufferLine(curbuf, lnum)))
3905 goto err;
3906 if (!(linenr = PyInt_FromLong((long) lnum)))
3907 {
3908 Py_DECREF(line);
3909 goto err;
3910 }
3911 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
3912 Py_DECREF(line);
3913 Py_DECREF(linenr);
3914 if (!ret)
3915 goto err;
3916
3917 if (ret != Py_None)
3918 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
3919 goto err;
3920
3921 Py_XDECREF(ret);
3922 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003923#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003924 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003925#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003926 }
3927 goto out;
3928err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003929#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003930 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003931#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003932 PyErr_PrintEx(0);
3933 PythonIO_Flush();
3934 status = 1;
3935out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003936#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003937 if (!status)
3938 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003939#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003940 Py_DECREF(pyfunc);
3941 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
3942 if (status)
3943 return;
3944 check_cursor();
3945 update_curbuf(NOT_VALID);
3946}
3947
3948 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003949run_eval(const char *cmd, typval_T *rettv
3950#ifdef PY_CAN_RECURSE
3951 , PyGILState_STATE *pygilstate UNUSED
3952#endif
3953 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003954{
3955 PyObject *r;
3956
3957 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
3958 if (r == NULL)
3959 {
3960 if (PyErr_Occurred() && !msg_silent)
3961 PyErr_PrintEx(0);
3962 EMSG(_("E858: Eval did not return a valid python object"));
3963 }
3964 else
3965 {
3966 if (ConvertFromPyObject(r, rettv) == -1)
3967 EMSG(_("E859: Failed to convert returned python object to vim value"));
3968 Py_DECREF(r);
3969 }
3970 PyErr_Clear();
3971}
3972
3973 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02003974set_ref_in_py(const int copyID)
3975{
3976 pylinkedlist_T *cur;
3977 dict_T *dd;
3978 list_T *ll;
3979
3980 if (lastdict != NULL)
3981 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3982 {
3983 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3984 if (dd->dv_copyID != copyID)
3985 {
3986 dd->dv_copyID = copyID;
3987 set_ref_in_ht(&dd->dv_hashtab, copyID);
3988 }
3989 }
3990
3991 if (lastlist != NULL)
3992 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3993 {
3994 ll = ((ListObject *) (cur->pll_obj))->list;
3995 if (ll->lv_copyID != copyID)
3996 {
3997 ll->lv_copyID = copyID;
3998 set_ref_in_list(ll, copyID);
3999 }
4000 }
4001}
4002
4003 static int
4004set_string_copy(char_u *str, typval_T *tv)
4005{
4006 tv->vval.v_string = vim_strsave(str);
4007 if (tv->vval.v_string == NULL)
4008 {
4009 PyErr_NoMemory();
4010 return -1;
4011 }
4012 return 0;
4013}
4014
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004015 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004016pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004017{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004018 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004019 char_u *key;
4020 dictitem_T *di;
4021 PyObject *keyObject;
4022 PyObject *valObject;
4023 Py_ssize_t iter = 0;
4024
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004025 dict = dict_alloc();
4026 if (dict == NULL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004027 {
4028 PyErr_NoMemory();
4029 return -1;
4030 }
4031
4032 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004033 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004034
4035 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4036 {
4037 DICTKEY_DECL
4038
Bram Moolenaara03e6312013-05-29 22:49:26 +02004039 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004040 return -1;
4041
Bram Moolenaara03e6312013-05-29 22:49:26 +02004042 DICTKEY_GET(-1, 0)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004043
4044 di = dictitem_alloc(key);
4045
4046 DICTKEY_UNREF
4047
4048 if (di == NULL)
4049 {
4050 PyErr_NoMemory();
4051 return -1;
4052 }
4053 di->di_tv.v_lock = 0;
4054
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004055 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004056 {
4057 vim_free(di);
4058 return -1;
4059 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004060
4061 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004062 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004063 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004064 vim_free(di);
4065 PyErr_SetVim(_("failed to add key to dictionary"));
4066 return -1;
4067 }
4068 }
4069 return 0;
4070}
4071
4072 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004073pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004074{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004075 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004076 char_u *key;
4077 dictitem_T *di;
4078 PyObject *list;
4079 PyObject *litem;
4080 PyObject *keyObject;
4081 PyObject *valObject;
4082 Py_ssize_t lsize;
4083
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004084 dict = dict_alloc();
4085 if (dict == NULL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004086 {
4087 PyErr_NoMemory();
4088 return -1;
4089 }
4090
4091 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004092 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004093
4094 list = PyMapping_Items(obj);
4095 if (list == NULL)
4096 return -1;
4097 lsize = PyList_Size(list);
4098 while (lsize--)
4099 {
4100 DICTKEY_DECL
4101
4102 litem = PyList_GetItem(list, lsize);
4103 if (litem == NULL)
4104 {
4105 Py_DECREF(list);
4106 return -1;
4107 }
4108
Bram Moolenaara03e6312013-05-29 22:49:26 +02004109 if (!(keyObject = PyTuple_GetItem(litem, 0)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004110 {
4111 Py_DECREF(list);
4112 Py_DECREF(litem);
4113 return -1;
4114 }
4115
Bram Moolenaara03e6312013-05-29 22:49:26 +02004116 DICTKEY_GET(-1, 1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004117
Bram Moolenaara03e6312013-05-29 22:49:26 +02004118 if (!(valObject = PyTuple_GetItem(litem, 1)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004119 {
4120 Py_DECREF(list);
4121 Py_DECREF(litem);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004122 DICTKEY_UNREF
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004123 return -1;
4124 }
4125
Bram Moolenaara03e6312013-05-29 22:49:26 +02004126 Py_DECREF(litem);
4127
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004128 di = dictitem_alloc(key);
4129
4130 DICTKEY_UNREF
4131
4132 if (di == NULL)
4133 {
4134 Py_DECREF(list);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004135 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004136 PyErr_NoMemory();
4137 return -1;
4138 }
4139 di->di_tv.v_lock = 0;
4140
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004141 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004142 {
4143 vim_free(di);
4144 Py_DECREF(list);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004145 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004146 return -1;
4147 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004148
4149 Py_DECREF(valObject);
4150
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004151 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004152 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004153 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004154 vim_free(di);
4155 Py_DECREF(list);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004156 PyErr_SetVim(_("failed to add key to dictionary"));
4157 return -1;
4158 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004159 }
4160 Py_DECREF(list);
4161 return 0;
4162}
4163
4164 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004165pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004166{
4167 list_T *l;
4168
4169 l = list_alloc();
4170 if (l == NULL)
4171 {
4172 PyErr_NoMemory();
4173 return -1;
4174 }
4175
4176 tv->v_type = VAR_LIST;
4177 tv->vval.v_list = l;
4178
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004179 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004180 return -1;
4181
4182 return 0;
4183}
4184
4185 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004186pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004187{
4188 PyObject *iterator = PyObject_GetIter(obj);
4189 PyObject *item;
4190 list_T *l;
4191 listitem_T *li;
4192
4193 l = list_alloc();
4194
4195 if (l == NULL)
4196 {
4197 PyErr_NoMemory();
4198 return -1;
4199 }
4200
4201 tv->vval.v_list = l;
4202 tv->v_type = VAR_LIST;
4203
4204
4205 if (iterator == NULL)
4206 return -1;
4207
Bram Moolenaarc8366792013-05-29 22:46:26 +02004208 while ((item = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004209 {
4210 li = listitem_alloc();
4211 if (li == NULL)
4212 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004213 Py_DECREF(iterator);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004214 PyErr_NoMemory();
4215 return -1;
4216 }
4217 li->li_tv.v_lock = 0;
4218
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004219 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
Bram Moolenaara03e6312013-05-29 22:49:26 +02004220 {
4221 Py_DECREF(item);
4222 Py_DECREF(iterator);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004223 return -1;
Bram Moolenaara03e6312013-05-29 22:49:26 +02004224 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004225
4226 list_append(l, li);
4227
4228 Py_DECREF(item);
4229 }
4230
4231 Py_DECREF(iterator);
4232 return 0;
4233}
4234
Bram Moolenaardb913952012-06-29 12:54:53 +02004235typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4236
4237 static int
4238convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004239 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004240{
4241 PyObject *capsule;
4242 char hexBuf[sizeof(void *) * 2 + 3];
4243
4244 sprintf(hexBuf, "%p", obj);
4245
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004246# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004247 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004248# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004249 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004250# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004251 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004252 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004253# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004254 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004255# else
4256 capsule = PyCObject_FromVoidPtr(tv, NULL);
4257# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004258 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4259 {
4260 Py_DECREF(capsule);
4261 tv->v_type = VAR_UNKNOWN;
4262 return -1;
4263 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004264 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004265 {
4266 tv->v_type = VAR_UNKNOWN;
4267 return -1;
4268 }
4269 /* As we are not using copy_tv which increments reference count we must
4270 * do it ourself. */
4271 switch(tv->v_type)
4272 {
4273 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4274 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4275 }
4276 }
4277 else
4278 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004279 typval_T *v;
4280
4281# ifdef PY_USE_CAPSULE
4282 v = PyCapsule_GetPointer(capsule, NULL);
4283# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004284 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004285# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004286 copy_tv(v, tv);
4287 }
4288 return 0;
4289}
4290
4291 static int
4292ConvertFromPyObject(PyObject *obj, typval_T *tv)
4293{
4294 PyObject *lookup_dict;
4295 int r;
4296
4297 lookup_dict = PyDict_New();
4298 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4299 Py_DECREF(lookup_dict);
4300 return r;
4301}
4302
4303 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004304_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004305{
4306 if (obj->ob_type == &DictionaryType)
4307 {
4308 tv->v_type = VAR_DICT;
4309 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4310 ++tv->vval.v_dict->dv_refcount;
4311 }
4312 else if (obj->ob_type == &ListType)
4313 {
4314 tv->v_type = VAR_LIST;
4315 tv->vval.v_list = (((ListObject *)(obj))->list);
4316 ++tv->vval.v_list->lv_refcount;
4317 }
4318 else if (obj->ob_type == &FunctionType)
4319 {
4320 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4321 return -1;
4322
4323 tv->v_type = VAR_FUNC;
4324 func_ref(tv->vval.v_string);
4325 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004326 else if (PyBytes_Check(obj))
4327 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004328 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004329
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004330 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4331 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004332 if (result == NULL)
4333 return -1;
4334
4335 if (set_string_copy(result, tv) == -1)
4336 return -1;
4337
4338 tv->v_type = VAR_STRING;
4339 }
4340 else if (PyUnicode_Check(obj))
4341 {
4342 PyObject *bytes;
4343 char_u *result;
4344
Bram Moolenaardb913952012-06-29 12:54:53 +02004345 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4346 if (bytes == NULL)
4347 return -1;
4348
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004349 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4350 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004351 if (result == NULL)
4352 return -1;
4353
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004354 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004355 {
4356 Py_XDECREF(bytes);
4357 return -1;
4358 }
4359 Py_XDECREF(bytes);
4360
4361 tv->v_type = VAR_STRING;
4362 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004363#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004364 else if (PyInt_Check(obj))
4365 {
4366 tv->v_type = VAR_NUMBER;
4367 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4368 }
4369#endif
4370 else if (PyLong_Check(obj))
4371 {
4372 tv->v_type = VAR_NUMBER;
4373 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4374 }
4375 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004376 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004377#ifdef FEAT_FLOAT
4378 else if (PyFloat_Check(obj))
4379 {
4380 tv->v_type = VAR_FLOAT;
4381 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4382 }
4383#endif
4384 else if (PyIter_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004385 return convert_dl(obj, tv, pyiter_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004386 else if (PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004387 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004388 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004389 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004390 else
4391 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004392 PyErr_SetString(PyExc_TypeError,
4393 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004394 return -1;
4395 }
4396 return 0;
4397}
4398
4399 static PyObject *
4400ConvertToPyObject(typval_T *tv)
4401{
4402 if (tv == NULL)
4403 {
4404 PyErr_SetVim(_("NULL reference passed"));
4405 return NULL;
4406 }
4407 switch (tv->v_type)
4408 {
4409 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004410 return PyBytes_FromString(tv->vval.v_string == NULL
4411 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004412 case VAR_NUMBER:
4413 return PyLong_FromLong((long) tv->vval.v_number);
4414#ifdef FEAT_FLOAT
4415 case VAR_FLOAT:
4416 return PyFloat_FromDouble((double) tv->vval.v_float);
4417#endif
4418 case VAR_LIST:
4419 return ListNew(tv->vval.v_list);
4420 case VAR_DICT:
4421 return DictionaryNew(tv->vval.v_dict);
4422 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004423 return FunctionNew(tv->vval.v_string == NULL
4424 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004425 case VAR_UNKNOWN:
4426 Py_INCREF(Py_None);
4427 return Py_None;
4428 default:
4429 PyErr_SetVim(_("internal error: invalid value type"));
4430 return NULL;
4431 }
4432}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004433
4434typedef struct
4435{
4436 PyObject_HEAD
4437} CurrentObject;
4438static PyTypeObject CurrentType;
4439
4440 static void
4441init_structs(void)
4442{
4443 vim_memset(&OutputType, 0, sizeof(OutputType));
4444 OutputType.tp_name = "vim.message";
4445 OutputType.tp_basicsize = sizeof(OutputObject);
4446 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4447 OutputType.tp_doc = "vim message object";
4448 OutputType.tp_methods = OutputMethods;
4449#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004450 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4451 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004452 OutputType.tp_alloc = call_PyType_GenericAlloc;
4453 OutputType.tp_new = call_PyType_GenericNew;
4454 OutputType.tp_free = call_PyObject_Free;
4455#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004456 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4457 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004458#endif
4459
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004460 vim_memset(&IterType, 0, sizeof(IterType));
4461 IterType.tp_name = "vim.iter";
4462 IterType.tp_basicsize = sizeof(IterObject);
4463 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
4464 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004465 IterType.tp_iter = (getiterfunc)IterIter;
4466 IterType.tp_iternext = (iternextfunc)IterNext;
4467 IterType.tp_dealloc = (destructor)IterDestructor;
4468 IterType.tp_traverse = (traverseproc)IterTraverse;
4469 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004470
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004471 vim_memset(&BufferType, 0, sizeof(BufferType));
4472 BufferType.tp_name = "vim.buffer";
4473 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004474 BufferType.tp_dealloc = (destructor)BufferDestructor;
4475 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004476 BufferType.tp_as_sequence = &BufferAsSeq;
4477 BufferType.tp_as_mapping = &BufferAsMapping;
4478 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4479 BufferType.tp_doc = "vim buffer object";
4480 BufferType.tp_methods = BufferMethods;
4481#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004482 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004483 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004484 BufferType.tp_alloc = call_PyType_GenericAlloc;
4485 BufferType.tp_new = call_PyType_GenericNew;
4486 BufferType.tp_free = call_PyObject_Free;
4487#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004488 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004489 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004490#endif
4491
4492 vim_memset(&WindowType, 0, sizeof(WindowType));
4493 WindowType.tp_name = "vim.window";
4494 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004495 WindowType.tp_dealloc = (destructor)WindowDestructor;
4496 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004497 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
4498 WindowType.tp_doc = "vim Window object";
4499 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004500 WindowType.tp_traverse = (traverseproc)WindowTraverse;
4501 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004502#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004503 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
4504 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004505 WindowType.tp_alloc = call_PyType_GenericAlloc;
4506 WindowType.tp_new = call_PyType_GenericNew;
4507 WindowType.tp_free = call_PyObject_Free;
4508#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004509 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
4510 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004511#endif
4512
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004513 vim_memset(&TabPageType, 0, sizeof(TabPageType));
4514 TabPageType.tp_name = "vim.tabpage";
4515 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004516 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
4517 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004518 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
4519 TabPageType.tp_doc = "vim tab page object";
4520 TabPageType.tp_methods = TabPageMethods;
4521#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004522 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004523 TabPageType.tp_alloc = call_PyType_GenericAlloc;
4524 TabPageType.tp_new = call_PyType_GenericNew;
4525 TabPageType.tp_free = call_PyObject_Free;
4526#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004527 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004528#endif
4529
Bram Moolenaardfa38d42013-05-15 13:38:47 +02004530 vim_memset(&BufMapType, 0, sizeof(BufMapType));
4531 BufMapType.tp_name = "vim.bufferlist";
4532 BufMapType.tp_basicsize = sizeof(BufMapObject);
4533 BufMapType.tp_as_mapping = &BufMapAsMapping;
4534 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004535 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004536 BufferType.tp_doc = "vim buffer list";
4537
4538 vim_memset(&WinListType, 0, sizeof(WinListType));
4539 WinListType.tp_name = "vim.windowlist";
4540 WinListType.tp_basicsize = sizeof(WinListType);
4541 WinListType.tp_as_sequence = &WinListAsSeq;
4542 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
4543 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004544 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004545
4546 vim_memset(&TabListType, 0, sizeof(TabListType));
4547 TabListType.tp_name = "vim.tabpagelist";
4548 TabListType.tp_basicsize = sizeof(TabListType);
4549 TabListType.tp_as_sequence = &TabListAsSeq;
4550 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
4551 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004552
4553 vim_memset(&RangeType, 0, sizeof(RangeType));
4554 RangeType.tp_name = "vim.range";
4555 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004556 RangeType.tp_dealloc = (destructor)RangeDestructor;
4557 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004558 RangeType.tp_as_sequence = &RangeAsSeq;
4559 RangeType.tp_as_mapping = &RangeAsMapping;
4560 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
4561 RangeType.tp_doc = "vim Range object";
4562 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004563 RangeType.tp_traverse = (traverseproc)RangeTraverse;
4564 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004565#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004566 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004567 RangeType.tp_alloc = call_PyType_GenericAlloc;
4568 RangeType.tp_new = call_PyType_GenericNew;
4569 RangeType.tp_free = call_PyObject_Free;
4570#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004571 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004572#endif
4573
4574 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4575 CurrentType.tp_name = "vim.currentdata";
4576 CurrentType.tp_basicsize = sizeof(CurrentObject);
4577 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4578 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004579 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004580#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004581 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
4582 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004583#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004584 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
4585 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004586#endif
4587
4588 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4589 DictionaryType.tp_name = "vim.dictionary";
4590 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004591 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004592 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4593 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4594 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4595 DictionaryType.tp_methods = DictionaryMethods;
4596#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004597 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
4598 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004599#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004600 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
4601 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004602#endif
4603
4604 vim_memset(&ListType, 0, sizeof(ListType));
4605 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004606 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004607 ListType.tp_basicsize = sizeof(ListObject);
4608 ListType.tp_as_sequence = &ListAsSeq;
4609 ListType.tp_as_mapping = &ListAsMapping;
4610 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4611 ListType.tp_doc = "list pushing modifications to vim structure";
4612 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004613 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004614#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004615 ListType.tp_getattro = (getattrofunc)ListGetattro;
4616 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004617#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004618 ListType.tp_getattr = (getattrfunc)ListGetattr;
4619 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004620#endif
4621
4622 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004623 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004624 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004625 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
4626 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004627 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4628 FunctionType.tp_doc = "object that calls vim function";
4629 FunctionType.tp_methods = FunctionMethods;
4630#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004631 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004632#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004633 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004634#endif
4635
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004636 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4637 OptionsType.tp_name = "vim.options";
4638 OptionsType.tp_basicsize = sizeof(OptionsObject);
4639 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4640 OptionsType.tp_doc = "object for manipulating options";
4641 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004642 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
4643 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
4644 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004645
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004646#if PY_MAJOR_VERSION >= 3
4647 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4648 vimmodule.m_name = "vim";
4649 vimmodule.m_doc = "Vim Python interface\n";
4650 vimmodule.m_size = -1;
4651 vimmodule.m_methods = VimMethods;
4652#endif
4653}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004654
4655#define PYTYPE_READY(type) \
4656 if (PyType_Ready(&type)) \
4657 return -1;
4658
4659 static int
4660init_types()
4661{
4662 PYTYPE_READY(IterType);
4663 PYTYPE_READY(BufferType);
4664 PYTYPE_READY(RangeType);
4665 PYTYPE_READY(WindowType);
4666 PYTYPE_READY(TabPageType);
4667 PYTYPE_READY(BufMapType);
4668 PYTYPE_READY(WinListType);
4669 PYTYPE_READY(TabListType);
4670 PYTYPE_READY(CurrentType);
4671 PYTYPE_READY(DictionaryType);
4672 PYTYPE_READY(ListType);
4673 PYTYPE_READY(FunctionType);
4674 PYTYPE_READY(OptionsType);
4675 PYTYPE_READY(OutputType);
4676 return 0;
4677}
4678
4679static BufMapObject TheBufferMap =
4680{
4681 PyObject_HEAD_INIT(&BufMapType)
4682};
4683
4684static WinListObject TheWindowList =
4685{
4686 PyObject_HEAD_INIT(&WinListType)
4687 NULL
4688};
4689
4690static CurrentObject TheCurrent =
4691{
4692 PyObject_HEAD_INIT(&CurrentType)
4693};
4694
4695static TabListObject TheTabPageList =
4696{
4697 PyObject_HEAD_INIT(&TabListType)
4698};
4699
4700static struct numeric_constant {
4701 char *name;
4702 int value;
4703} numeric_constants[] = {
4704 {"VAR_LOCKED", VAR_LOCKED},
4705 {"VAR_FIXED", VAR_FIXED},
4706 {"VAR_SCOPE", VAR_SCOPE},
4707 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
4708};
4709
4710static struct object_constant {
4711 char *name;
4712 PyObject *value;
4713} object_constants[] = {
4714 {"buffers", (PyObject *)(void *)&TheBufferMap},
4715 {"windows", (PyObject *)(void *)&TheWindowList},
4716 {"tabpages", (PyObject *)(void *)&TheTabPageList},
4717 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02004718
4719 {"Buffer", (PyObject *)&BufferType},
4720 {"Range", (PyObject *)&RangeType},
4721 {"Window", (PyObject *)&WindowType},
4722 {"TabPage", (PyObject *)&TabPageType},
4723 {"Dictionary", (PyObject *)&DictionaryType},
4724 {"List", (PyObject *)&ListType},
4725 {"Function", (PyObject *)&FunctionType},
4726 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004727};
4728
4729typedef int (*object_adder)(PyObject *, const char *, PyObject *);
4730
4731#define ADD_OBJECT(m, name, obj) \
4732 if (add_object(m, name, obj)) \
4733 return -1;
4734
4735#define ADD_CHECKED_OBJECT(m, name, obj) \
4736 { \
4737 PyObject *value = obj; \
4738 if (!value) \
4739 return -1; \
4740 ADD_OBJECT(m, name, value); \
4741 }
4742
4743 static int
4744populate_module(PyObject *m, object_adder add_object)
4745{
4746 int i;
4747
4748 for (i = 0; i < (int)(sizeof(numeric_constants)
4749 / sizeof(struct numeric_constant));
4750 ++i)
4751 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
4752 PyInt_FromLong(numeric_constants[i].value));
4753
4754 for (i = 0; i < (int)(sizeof(object_constants)
4755 / sizeof(struct object_constant));
4756 ++i)
4757 {
4758 PyObject *value;
4759
4760 value = object_constants[i].value;
4761 Py_INCREF(value);
4762 ADD_OBJECT(m, object_constants[i].name, value);
4763 }
4764
4765 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
4766 return -1;
4767 ADD_OBJECT(m, "error", VimError);
4768
4769 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict));
4770 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
4771 ADD_CHECKED_OBJECT(m, "options",
4772 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
4773 return 0;
4774}