blob: ad6e97008aa50a327da1b477de9dc0609073d23a [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;
35#define DICTKEY_GET(err) \
36 if (!(key = StringToChars(keyObject, &dictkey_todecref))) \
37 return err;
38#define DICTKEY_UNREF \
39 Py_XDECREF(dictkey_todecref);
40
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020041typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020042typedef void (*runner)(const char *, void *
43#ifdef PY_CAN_RECURSE
44 , PyGILState_STATE *
45#endif
46 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020047
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020048static int ConvertFromPyObject(PyObject *, typval_T *);
49static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020050static PyObject *WindowNew(win_T *, tabpage_T *);
51static PyObject *BufferNew (buf_T *);
52static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020053
54static PyInt RangeStart;
55static PyInt RangeEnd;
56
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020057static PyObject *globals;
58
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020059/*
60 * obtain a lock on the Vim data structures
61 */
62 static void
63Python_Lock_Vim(void)
64{
65}
66
67/*
68 * release a lock on the Vim data structures
69 */
70 static void
71Python_Release_Vim(void)
72{
73}
74
Bram Moolenaare9ba5162013-05-29 22:02:22 +020075/*
76 * The "todecref" argument holds a pointer to PyObject * that must be
77 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
78 * was needed to generate returned value is object.
79 *
80 * Use Py_XDECREF to decrement reference count.
81 */
82 static char_u *
83StringToChars(PyObject *object, PyObject **todecref)
84{
85 char_u *p;
86 PyObject *bytes = NULL;
87
88 if (PyBytes_Check(object))
89 {
90
91 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
92 return NULL;
93 if (p == NULL)
94 return NULL;
95
96 *todecref = NULL;
97 }
98 else if (PyUnicode_Check(object))
99 {
100 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
101 if (bytes == NULL)
102 return NULL;
103
104 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
105 return NULL;
106 if (p == NULL)
107 return NULL;
108
109 *todecref = bytes;
110 }
111 else
112 {
113 PyErr_SetString(PyExc_TypeError, _("object must be string"));
114 return NULL;
115 }
116
117 return (char_u *) p;
118}
119
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200120/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200121 */
122
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200123/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200124typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200125
126static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200127
128typedef struct
129{
130 PyObject_HEAD
131 long softspace;
132 long error;
133} OutputObject;
134
Bram Moolenaar77045652012-09-21 13:46:06 +0200135 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200136OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200137{
138 if (val == NULL)
139 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200140 PyErr_SetString(PyExc_AttributeError,
141 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200142 return -1;
143 }
144
145 if (strcmp(name, "softspace") == 0)
146 {
147 if (!PyInt_Check(val))
148 {
149 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
150 return -1;
151 }
152
Bram Moolenaard6e39182013-05-21 18:30:34 +0200153 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200154 return 0;
155 }
156
157 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
158 return -1;
159}
160
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200161/* Buffer IO, we write one whole line at a time. */
162static garray_T io_ga = {0, 0, 1, 80, NULL};
163static writefn old_fn = NULL;
164
165 static void
166PythonIO_Flush(void)
167{
168 if (old_fn != NULL && io_ga.ga_len > 0)
169 {
170 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
171 old_fn((char_u *)io_ga.ga_data);
172 }
173 io_ga.ga_len = 0;
174}
175
176 static void
177writer(writefn fn, char_u *str, PyInt n)
178{
179 char_u *ptr;
180
181 /* Flush when switching output function. */
182 if (fn != old_fn)
183 PythonIO_Flush();
184 old_fn = fn;
185
186 /* Write each NL separated line. Text after the last NL is kept for
187 * writing later. */
188 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
189 {
190 PyInt len = ptr - str;
191
192 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
193 break;
194
195 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
196 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
197 fn((char_u *)io_ga.ga_data);
198 str = ptr + 1;
199 n -= len + 1;
200 io_ga.ga_len = 0;
201 }
202
203 /* Put the remaining text into io_ga for later printing. */
204 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
205 {
206 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
207 io_ga.ga_len += (int)n;
208 }
209}
210
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200211 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200212OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200213{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200214 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200215 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200216 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200217
Bram Moolenaar27564802011-09-07 19:30:21 +0200218 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200219 return NULL;
220
221 Py_BEGIN_ALLOW_THREADS
222 Python_Lock_Vim();
223 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
224 Python_Release_Vim();
225 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200226 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200227
228 Py_INCREF(Py_None);
229 return Py_None;
230}
231
232 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200233OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200234{
235 PyInt n;
236 PyInt i;
237 PyObject *list;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200238 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200239
240 if (!PyArg_ParseTuple(args, "O", &list))
241 return NULL;
242 Py_INCREF(list);
243
Bram Moolenaardb913952012-06-29 12:54:53 +0200244 if (!PyList_Check(list))
245 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200246 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
247 Py_DECREF(list);
248 return NULL;
249 }
250
251 n = PyList_Size(list);
252
253 for (i = 0; i < n; ++i)
254 {
255 PyObject *line = PyList_GetItem(list, i);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200256 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200257 PyInt len;
258
Bram Moolenaardb913952012-06-29 12:54:53 +0200259 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
260 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200261 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
262 Py_DECREF(list);
263 return NULL;
264 }
265
266 Py_BEGIN_ALLOW_THREADS
267 Python_Lock_Vim();
268 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
269 Python_Release_Vim();
270 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200271 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200272 }
273
274 Py_DECREF(list);
275 Py_INCREF(Py_None);
276 return Py_None;
277}
278
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100279 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200280OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100281{
282 /* do nothing */
283 Py_INCREF(Py_None);
284 return Py_None;
285}
286
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200287/***************/
288
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200289static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200290 /* name, function, calling, doc */
291 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
292 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
293 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
294 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200295};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200296
297static OutputObject Output =
298{
299 PyObject_HEAD_INIT(&OutputType)
300 0,
301 0
302};
303
304static OutputObject Error =
305{
306 PyObject_HEAD_INIT(&OutputType)
307 0,
308 1
309};
310
311 static int
312PythonIO_Init_io(void)
313{
314 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
315 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
316
317 if (PyErr_Occurred())
318 {
319 EMSG(_("E264: Python: Error initialising I/O objects"));
320 return -1;
321 }
322
323 return 0;
324}
325
326
327static PyObject *VimError;
328
329/* Check to see whether a Vim error has been reported, or a keyboard
330 * interrupt has been detected.
331 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200332
333 static void
334VimTryStart(void)
335{
336 ++trylevel;
337}
338
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200339 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200340VimTryEnd(void)
341{
342 --trylevel;
343 if (got_int)
344 {
345 PyErr_SetNone(PyExc_KeyboardInterrupt);
346 return 1;
347 }
348 else if (!did_throw)
349 return 0;
350 else if (PyErr_Occurred())
351 return 1;
352 else
353 {
354 PyErr_SetVim((char *) current_exception->value);
355 discard_current_exception();
356 return 1;
357 }
358}
359
360 static int
361VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200362{
363 if (got_int)
364 {
365 PyErr_SetNone(PyExc_KeyboardInterrupt);
366 return 1;
367 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200368 return 0;
369}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200370
371/* Vim module - Implementation
372 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200373
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200374 static PyObject *
375VimCommand(PyObject *self UNUSED, PyObject *args)
376{
377 char *cmd;
378 PyObject *result;
379
380 if (!PyArg_ParseTuple(args, "s", &cmd))
381 return NULL;
382
383 PyErr_Clear();
384
385 Py_BEGIN_ALLOW_THREADS
386 Python_Lock_Vim();
387
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200388 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200389 do_cmdline_cmd((char_u *)cmd);
390 update_screen(VALID);
391
392 Python_Release_Vim();
393 Py_END_ALLOW_THREADS
394
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200395 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200396 result = NULL;
397 else
398 result = Py_None;
399
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200400
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200401 Py_XINCREF(result);
402 return result;
403}
404
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200405/*
406 * Function to translate a typval_T into a PyObject; this will recursively
407 * translate lists/dictionaries into their Python equivalents.
408 *
409 * The depth parameter is to avoid infinite recursion, set it to 1 when
410 * you call VimToPython.
411 */
412 static PyObject *
413VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
414{
415 PyObject *result;
416 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200417 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200418
419 /* Avoid infinite recursion */
420 if (depth > 100)
421 {
422 Py_INCREF(Py_None);
423 result = Py_None;
424 return result;
425 }
426
427 /* Check if we run into a recursive loop. The item must be in lookupDict
428 * then and we can use it again. */
429 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
430 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
431 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200432 sprintf(ptrBuf, "%p",
433 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
434 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200435
436 if ((result = PyDict_GetItemString(lookupDict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200437 {
438 Py_INCREF(result);
439 return result;
440 }
441 }
442
443 if (our_tv->v_type == VAR_STRING)
444 {
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200445 result = Py_BuildValue("s", our_tv->vval.v_string == NULL
446 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200447 }
448 else if (our_tv->v_type == VAR_NUMBER)
449 {
450 char buf[NUMBUFLEN];
451
452 /* For backwards compatibility numbers are stored as strings. */
453 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
454 result = Py_BuildValue("s", buf);
455 }
456# ifdef FEAT_FLOAT
457 else if (our_tv->v_type == VAR_FLOAT)
458 {
459 char buf[NUMBUFLEN];
460
461 sprintf(buf, "%f", our_tv->vval.v_float);
462 result = Py_BuildValue("s", buf);
463 }
464# endif
465 else if (our_tv->v_type == VAR_LIST)
466 {
467 list_T *list = our_tv->vval.v_list;
468 listitem_T *curr;
469
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200470 if (list == NULL)
471 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200472
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200473 if (!(result = PyList_New(0)))
474 return NULL;
475
476 if (PyDict_SetItemString(lookupDict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200477 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200478 Py_DECREF(result);
479 return NULL;
480 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200481
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200482 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
483 {
484 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200485 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200486 Py_DECREF(result);
487 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200488 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200489 if (PyList_Append(result, newObj))
490 {
491 Py_DECREF(newObj);
492 Py_DECREF(result);
493 return NULL;
494 }
495 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200496 }
497 }
498 else if (our_tv->v_type == VAR_DICT)
499 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200500
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200501 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
502 long_u todo = ht->ht_used;
503 hashitem_T *hi;
504 dictitem_T *di;
505 if (our_tv->vval.v_dict == NULL)
506 return NULL;
507
508 if (!(result = PyDict_New()))
509 return NULL;
510
511 if (PyDict_SetItemString(lookupDict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200512 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200513 Py_DECREF(result);
514 return NULL;
515 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200516
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200517 for (hi = ht->ht_array; todo > 0; ++hi)
518 {
519 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200520 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200521 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200522
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200523 di = dict_lookup(hi);
524 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookupDict)))
525 {
526 Py_DECREF(result);
527 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200528 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200529 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
530 {
531 Py_DECREF(result);
532 Py_DECREF(newObj);
533 return NULL;
534 }
535 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200536 }
537 }
538 }
539 else
540 {
541 Py_INCREF(Py_None);
542 result = Py_None;
543 }
544
545 return result;
546}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200547
548 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200549VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200550{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200551 char *expr;
552 typval_T *our_tv;
553 PyObject *result;
554 PyObject *lookup_dict;
555
556 if (!PyArg_ParseTuple(args, "s", &expr))
557 return NULL;
558
559 Py_BEGIN_ALLOW_THREADS
560 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200561 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200562 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200563 Python_Release_Vim();
564 Py_END_ALLOW_THREADS
565
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200566 if (VimTryEnd())
567 return NULL;
568
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200569 if (our_tv == NULL)
570 {
571 PyErr_SetVim(_("invalid expression"));
572 return NULL;
573 }
574
575 /* Convert the Vim type into a Python type. Create a dictionary that's
576 * used to check for recursive loops. */
577 lookup_dict = PyDict_New();
578 result = VimToPython(our_tv, 1, lookup_dict);
579 Py_DECREF(lookup_dict);
580
581
582 Py_BEGIN_ALLOW_THREADS
583 Python_Lock_Vim();
584 free_tv(our_tv);
585 Python_Release_Vim();
586 Py_END_ALLOW_THREADS
587
588 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200589}
590
Bram Moolenaardb913952012-06-29 12:54:53 +0200591static PyObject *ConvertToPyObject(typval_T *);
592
593 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200594VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200595{
Bram Moolenaardb913952012-06-29 12:54:53 +0200596 char *expr;
597 typval_T *our_tv;
598 PyObject *result;
599
600 if (!PyArg_ParseTuple(args, "s", &expr))
601 return NULL;
602
603 Py_BEGIN_ALLOW_THREADS
604 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200605 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200606 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200607 Python_Release_Vim();
608 Py_END_ALLOW_THREADS
609
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200610 if (VimTryEnd())
611 return NULL;
612
Bram Moolenaardb913952012-06-29 12:54:53 +0200613 if (our_tv == NULL)
614 {
615 PyErr_SetVim(_("invalid expression"));
616 return NULL;
617 }
618
619 result = ConvertToPyObject(our_tv);
620 Py_BEGIN_ALLOW_THREADS
621 Python_Lock_Vim();
622 free_tv(our_tv);
623 Python_Release_Vim();
624 Py_END_ALLOW_THREADS
625
626 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200627}
628
629 static PyObject *
630VimStrwidth(PyObject *self UNUSED, PyObject *args)
631{
632 char *expr;
633
634 if (!PyArg_ParseTuple(args, "s", &expr))
635 return NULL;
636
Bram Moolenaara54bf402012-12-05 16:30:07 +0100637 return PyLong_FromLong(
638#ifdef FEAT_MBYTE
639 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
640#else
641 STRLEN(expr)
642#endif
643 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200644}
645
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646/*
647 * Vim module - Definitions
648 */
649
650static struct PyMethodDef VimMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200651 /* name, function, calling, documentation */
652 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
653 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
654 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
655 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
656 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200657};
658
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200659/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200660 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200661 */
662
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200663static PyTypeObject IterType;
664
665typedef PyObject *(*nextfun)(void **);
666typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200667typedef int (*traversefun)(void *, visitproc, void *);
668typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200669
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200670/* Main purpose of this object is removing the need for do python
671 * initialization (i.e. PyType_Ready and setting type attributes) for a big
672 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200673
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200674typedef struct
675{
676 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200677 void *cur;
678 nextfun next;
679 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200680 traversefun traverse;
681 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200682} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200683
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200684 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200685IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
686 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200687{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200688 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689
Bram Moolenaar774267b2013-05-21 20:51:59 +0200690 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200691 self->cur = start;
692 self->next = next;
693 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200694 self->traverse = traverse;
695 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200696
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200697 return (PyObject *)(self);
698}
699
700 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200701IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200702{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200703 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200704 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200705 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200706}
707
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200708 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200709IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200710{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200711 if (self->traverse != NULL)
712 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200713 else
714 return 0;
715}
716
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200717/* Mac OSX defines clear() somewhere. */
718#ifdef clear
719# undef clear
720#endif
721
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200722 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200723IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200724{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200725 if (self->clear != NULL)
726 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200727 else
728 return 0;
729}
730
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200731 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200732IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200733{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200734 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200735}
736
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200737 static PyObject *
738IterIter(PyObject *self)
739{
740 return self;
741}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200742
Bram Moolenaardb913952012-06-29 12:54:53 +0200743typedef struct pylinkedlist_S {
744 struct pylinkedlist_S *pll_next;
745 struct pylinkedlist_S *pll_prev;
746 PyObject *pll_obj;
747} pylinkedlist_T;
748
749static pylinkedlist_T *lastdict = NULL;
750static pylinkedlist_T *lastlist = NULL;
751
752 static void
753pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
754{
755 if (ref->pll_prev == NULL)
756 {
757 if (ref->pll_next == NULL)
758 {
759 *last = NULL;
760 return;
761 }
762 }
763 else
764 ref->pll_prev->pll_next = ref->pll_next;
765
766 if (ref->pll_next == NULL)
767 *last = ref->pll_prev;
768 else
769 ref->pll_next->pll_prev = ref->pll_prev;
770}
771
772 static void
773pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
774{
775 if (*last == NULL)
776 ref->pll_prev = NULL;
777 else
778 {
779 (*last)->pll_next = ref;
780 ref->pll_prev = *last;
781 }
782 ref->pll_next = NULL;
783 ref->pll_obj = self;
784 *last = ref;
785}
786
787static PyTypeObject DictionaryType;
788
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200789#define DICTKEY_GET_NOTEMPTY(err) \
790 DICTKEY_GET(err) \
791 if (*key == NUL) \
792 { \
793 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
794 return err; \
795 }
796
Bram Moolenaardb913952012-06-29 12:54:53 +0200797typedef struct
798{
799 PyObject_HEAD
800 dict_T *dict;
801 pylinkedlist_T ref;
802} DictionaryObject;
803
804 static PyObject *
805DictionaryNew(dict_T *dict)
806{
807 DictionaryObject *self;
808
809 self = PyObject_NEW(DictionaryObject, &DictionaryType);
810 if (self == NULL)
811 return NULL;
812 self->dict = dict;
813 ++dict->dv_refcount;
814
815 pyll_add((PyObject *)(self), &self->ref, &lastdict);
816
817 return (PyObject *)(self);
818}
819
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200820 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200821DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200822{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200823 pyll_remove(&self->ref, &lastdict);
824 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200825
826 DESTRUCTOR_FINISH(self);
827}
828
Bram Moolenaardb913952012-06-29 12:54:53 +0200829 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200830DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200831{
832 if (val == NULL)
833 {
834 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
835 return -1;
836 }
837
838 if (strcmp(name, "locked") == 0)
839 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200840 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200841 {
842 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
843 return -1;
844 }
845 else
846 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200847 int istrue = PyObject_IsTrue(val);
848 if (istrue == -1)
849 return -1;
850 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200851 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200852 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200853 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200854 }
855 return 0;
856 }
857 else
858 {
859 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
860 return -1;
861 }
862}
863
864 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200865DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200866{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200867 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +0200868}
869
870 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200871DictionaryItem(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200872{
873 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200874 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200875 DICTKEY_DECL
876
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200877 DICTKEY_GET_NOTEMPTY(NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200878
Bram Moolenaard6e39182013-05-21 18:30:34 +0200879 di = dict_find(self->dict, key, -1);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200880
Bram Moolenaar696c2112012-09-21 13:43:14 +0200881 DICTKEY_UNREF
882
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200883 if (di == NULL)
884 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200885 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200886 return NULL;
887 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200888
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200889 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200890}
891
892 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200893DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200894{
895 char_u *key;
896 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200897 dict_T *d = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +0200898 dictitem_T *di;
899 DICTKEY_DECL
900
901 if (d->dv_lock)
902 {
903 PyErr_SetVim(_("dict is locked"));
904 return -1;
905 }
906
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200907 DICTKEY_GET_NOTEMPTY(-1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200908
909 di = dict_find(d, key, -1);
910
911 if (valObject == NULL)
912 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200913 hashitem_T *hi;
914
Bram Moolenaardb913952012-06-29 12:54:53 +0200915 if (di == NULL)
916 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200917 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200918 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +0200919 return -1;
920 }
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200921 hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaardb913952012-06-29 12:54:53 +0200922 hash_remove(&d->dv_hashtab, hi);
923 dictitem_free(di);
924 return 0;
925 }
926
927 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200928 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +0200929
930 if (di == NULL)
931 {
932 di = dictitem_alloc(key);
933 if (di == NULL)
934 {
935 PyErr_NoMemory();
936 return -1;
937 }
938 di->di_tv.v_lock = 0;
939
940 if (dict_add(d, di) == FAIL)
941 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200942 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +0200943 vim_free(di);
944 PyErr_SetVim(_("failed to add key to dictionary"));
945 return -1;
946 }
947 }
948 else
949 clear_tv(&di->di_tv);
950
951 DICTKEY_UNREF
952
953 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +0200954 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200955 return 0;
956}
957
958 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200959DictionaryListKeys(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200960{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200961 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +0200962 long_u todo = dict->dv_hashtab.ht_used;
963 Py_ssize_t i = 0;
964 PyObject *r;
965 hashitem_T *hi;
966
967 r = PyList_New(todo);
968 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
969 {
970 if (!HASHITEM_EMPTY(hi))
971 {
972 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
973 --todo;
974 ++i;
975 }
976 }
977 return r;
978}
979
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200980static PyMappingMethods DictionaryAsMapping = {
981 (lenfunc) DictionaryLength,
982 (binaryfunc) DictionaryItem,
983 (objobjargproc) DictionaryAssItem,
984};
985
Bram Moolenaardb913952012-06-29 12:54:53 +0200986static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200987 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
988 { NULL, NULL, 0, NULL }
Bram Moolenaardb913952012-06-29 12:54:53 +0200989};
990
991static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200992static PySequenceMethods ListAsSeq;
993static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +0200994
995typedef struct
996{
997 PyObject_HEAD
998 list_T *list;
999 pylinkedlist_T ref;
1000} ListObject;
1001
1002 static PyObject *
1003ListNew(list_T *list)
1004{
1005 ListObject *self;
1006
1007 self = PyObject_NEW(ListObject, &ListType);
1008 if (self == NULL)
1009 return NULL;
1010 self->list = list;
1011 ++list->lv_refcount;
1012
1013 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1014
1015 return (PyObject *)(self);
1016}
1017
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001018 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001019ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001020{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001021 pyll_remove(&self->ref, &lastlist);
1022 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001023
1024 DESTRUCTOR_FINISH(self);
1025}
1026
Bram Moolenaardb913952012-06-29 12:54:53 +02001027 static int
1028list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict)
1029{
1030 Py_ssize_t i;
1031 Py_ssize_t lsize = PySequence_Size(obj);
1032 PyObject *litem;
1033 listitem_T *li;
1034
1035 for(i=0; i<lsize; i++)
1036 {
1037 li = listitem_alloc();
1038 if (li == NULL)
1039 {
1040 PyErr_NoMemory();
1041 return -1;
1042 }
1043 li->li_tv.v_lock = 0;
1044
1045 litem = PySequence_GetItem(obj, i);
1046 if (litem == NULL)
1047 return -1;
1048 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1)
1049 return -1;
1050
1051 list_append(l, li);
1052 }
1053 return 0;
1054}
1055
Bram Moolenaardb913952012-06-29 12:54:53 +02001056 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001057ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001058{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001059 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001060}
1061
1062 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001063ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001064{
1065 listitem_T *li;
1066
Bram Moolenaard6e39182013-05-21 18:30:34 +02001067 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001068 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001069 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001070 return NULL;
1071 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001072 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001073 if (li == NULL)
1074 {
1075 PyErr_SetVim(_("internal error: failed to get vim list item"));
1076 return NULL;
1077 }
1078 return ConvertToPyObject(&li->li_tv);
1079}
1080
1081#define PROC_RANGE \
1082 if (last < 0) {\
1083 if (last < -size) \
1084 last = 0; \
1085 else \
1086 last += size; \
1087 } \
1088 if (first < 0) \
1089 first = 0; \
1090 if (first > size) \
1091 first = size; \
1092 if (last > size) \
1093 last = size;
1094
1095 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001096ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001097{
1098 PyInt i;
1099 PyInt size = ListLength(self);
1100 PyInt n;
1101 PyObject *list;
1102 int reversed = 0;
1103
1104 PROC_RANGE
1105 if (first >= last)
1106 first = last;
1107
1108 n = last-first;
1109 list = PyList_New(n);
1110 if (list == NULL)
1111 return NULL;
1112
1113 for (i = 0; i < n; ++i)
1114 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001115 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001116 if (item == NULL)
1117 {
1118 Py_DECREF(list);
1119 return NULL;
1120 }
1121
1122 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1123 {
1124 Py_DECREF(item);
1125 Py_DECREF(list);
1126 return NULL;
1127 }
1128 }
1129
1130 return list;
1131}
1132
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001133typedef struct
1134{
1135 listwatch_T lw;
1136 list_T *list;
1137} listiterinfo_T;
1138
1139 static void
1140ListIterDestruct(listiterinfo_T *lii)
1141{
1142 list_rem_watch(lii->list, &lii->lw);
1143 PyMem_Free(lii);
1144}
1145
1146 static PyObject *
1147ListIterNext(listiterinfo_T **lii)
1148{
1149 PyObject *r;
1150
1151 if (!((*lii)->lw.lw_item))
1152 return NULL;
1153
1154 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1155 return NULL;
1156
1157 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1158
1159 return r;
1160}
1161
1162 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001163ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001164{
1165 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001166 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001167
1168 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1169 {
1170 PyErr_NoMemory();
1171 return NULL;
1172 }
1173
1174 list_add_watch(l, &lii->lw);
1175 lii->lw.lw_item = l->lv_first;
1176 lii->list = l;
1177
1178 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001179 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1180 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001181}
1182
Bram Moolenaardb913952012-06-29 12:54:53 +02001183 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001184ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001185{
1186 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001187 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001188 listitem_T *li;
1189 Py_ssize_t length = ListLength(self);
1190
1191 if (l->lv_lock)
1192 {
1193 PyErr_SetVim(_("list is locked"));
1194 return -1;
1195 }
1196 if (index>length || (index==length && obj==NULL))
1197 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001198 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001199 return -1;
1200 }
1201
1202 if (obj == NULL)
1203 {
1204 li = list_find(l, (long) index);
1205 list_remove(l, li, li);
1206 clear_tv(&li->li_tv);
1207 vim_free(li);
1208 return 0;
1209 }
1210
1211 if (ConvertFromPyObject(obj, &tv) == -1)
1212 return -1;
1213
1214 if (index == length)
1215 {
1216 if (list_append_tv(l, &tv) == FAIL)
1217 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001218 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001219 PyErr_SetVim(_("Failed to add item to list"));
1220 return -1;
1221 }
1222 }
1223 else
1224 {
1225 li = list_find(l, (long) index);
1226 clear_tv(&li->li_tv);
1227 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001228 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001229 }
1230 return 0;
1231}
1232
1233 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001234ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001235{
1236 PyInt size = ListLength(self);
1237 Py_ssize_t i;
1238 Py_ssize_t lsize;
1239 PyObject *litem;
1240 listitem_T *li;
1241 listitem_T *next;
1242 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001243 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001244
1245 if (l->lv_lock)
1246 {
1247 PyErr_SetVim(_("list is locked"));
1248 return -1;
1249 }
1250
1251 PROC_RANGE
1252
1253 if (first == size)
1254 li = NULL;
1255 else
1256 {
1257 li = list_find(l, (long) first);
1258 if (li == NULL)
1259 {
1260 PyErr_SetVim(_("internal error: no vim list item"));
1261 return -1;
1262 }
1263 if (last > first)
1264 {
1265 i = last - first;
1266 while (i-- && li != NULL)
1267 {
1268 next = li->li_next;
1269 listitem_remove(l, li);
1270 li = next;
1271 }
1272 }
1273 }
1274
1275 if (obj == NULL)
1276 return 0;
1277
1278 if (!PyList_Check(obj))
1279 {
1280 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1281 return -1;
1282 }
1283
1284 lsize = PyList_Size(obj);
1285
1286 for(i=0; i<lsize; i++)
1287 {
1288 litem = PyList_GetItem(obj, i);
1289 if (litem == NULL)
1290 return -1;
1291 if (ConvertFromPyObject(litem, &v) == -1)
1292 return -1;
1293 if (list_insert_tv(l, &v, li) == FAIL)
1294 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001295 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001296 PyErr_SetVim(_("internal error: failed to add item to list"));
1297 return -1;
1298 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001299 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001300 }
1301 return 0;
1302}
1303
1304 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001305ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001306{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001307 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001308 PyObject *lookup_dict;
1309
1310 if (l->lv_lock)
1311 {
1312 PyErr_SetVim(_("list is locked"));
1313 return NULL;
1314 }
1315
1316 if (!PySequence_Check(obj))
1317 {
1318 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1319 return NULL;
1320 }
1321
1322 lookup_dict = PyDict_New();
1323 if (list_py_concat(l, obj, lookup_dict) == -1)
1324 {
1325 Py_DECREF(lookup_dict);
1326 return NULL;
1327 }
1328 Py_DECREF(lookup_dict);
1329
1330 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001331 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001332}
1333
Bram Moolenaar66b79852012-09-21 14:00:35 +02001334 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001335ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001336{
1337 if (val == NULL)
1338 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001339 PyErr_SetString(PyExc_AttributeError,
1340 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001341 return -1;
1342 }
1343
1344 if (strcmp(name, "locked") == 0)
1345 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001346 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001347 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001348 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001349 return -1;
1350 }
1351 else
1352 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001353 int istrue = PyObject_IsTrue(val);
1354 if (istrue == -1)
1355 return -1;
1356 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001357 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001358 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001359 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001360 }
1361 return 0;
1362 }
1363 else
1364 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001365 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001366 return -1;
1367 }
1368}
1369
Bram Moolenaardb913952012-06-29 12:54:53 +02001370static struct PyMethodDef ListMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001371 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1372 { NULL, NULL, 0, NULL }
Bram Moolenaardb913952012-06-29 12:54:53 +02001373};
1374
1375typedef struct
1376{
1377 PyObject_HEAD
1378 char_u *name;
1379} FunctionObject;
1380
1381static PyTypeObject FunctionType;
1382
1383 static PyObject *
1384FunctionNew(char_u *name)
1385{
1386 FunctionObject *self;
1387
1388 self = PyObject_NEW(FunctionObject, &FunctionType);
1389 if (self == NULL)
1390 return NULL;
1391 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1392 if (self->name == NULL)
1393 {
1394 PyErr_NoMemory();
1395 return NULL;
1396 }
1397 STRCPY(self->name, name);
1398 func_ref(name);
1399 return (PyObject *)(self);
1400}
1401
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001402 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001403FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001404{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001405 func_unref(self->name);
1406 PyMem_Free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001407
1408 DESTRUCTOR_FINISH(self);
1409}
1410
Bram Moolenaardb913952012-06-29 12:54:53 +02001411 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001412FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02001413{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001414 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02001415 typval_T args;
1416 typval_T selfdicttv;
1417 typval_T rettv;
1418 dict_T *selfdict = NULL;
1419 PyObject *selfdictObject;
1420 PyObject *result;
1421 int error;
1422
1423 if (ConvertFromPyObject(argsObject, &args) == -1)
1424 return NULL;
1425
1426 if (kwargs != NULL)
1427 {
1428 selfdictObject = PyDict_GetItemString(kwargs, "self");
1429 if (selfdictObject != NULL)
1430 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001431 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001432 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001433 PyErr_SetString(PyExc_TypeError,
1434 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001435 clear_tv(&args);
1436 return NULL;
1437 }
1438 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001439 {
1440 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02001441 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001442 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001443 selfdict = selfdicttv.vval.v_dict;
1444 }
1445 }
1446
Bram Moolenaar71700b82013-05-15 17:49:05 +02001447 Py_BEGIN_ALLOW_THREADS
1448 Python_Lock_Vim();
1449
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001450 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02001451 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001452
1453 Python_Release_Vim();
1454 Py_END_ALLOW_THREADS
1455
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001456 if (VimTryEnd())
1457 result = NULL;
1458 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02001459 {
1460 result = NULL;
1461 PyErr_SetVim(_("failed to run function"));
1462 }
1463 else
1464 result = ConvertToPyObject(&rettv);
1465
Bram Moolenaardb913952012-06-29 12:54:53 +02001466 clear_tv(&args);
1467 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001468 if (selfdict != NULL)
1469 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001470
1471 return result;
1472}
1473
1474static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001475 {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1476 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001477};
1478
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001479/*
1480 * Options object
1481 */
1482
1483static PyTypeObject OptionsType;
1484
1485typedef int (*checkfun)(void *);
1486
1487typedef struct
1488{
1489 PyObject_HEAD
1490 int opt_type;
1491 void *from;
1492 checkfun Check;
1493 PyObject *fromObj;
1494} OptionsObject;
1495
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001496 static int
1497dummy_check(void *arg UNUSED)
1498{
1499 return 0;
1500}
1501
1502 static PyObject *
1503OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1504{
1505 OptionsObject *self;
1506
Bram Moolenaar774267b2013-05-21 20:51:59 +02001507 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001508 if (self == NULL)
1509 return NULL;
1510
1511 self->opt_type = opt_type;
1512 self->from = from;
1513 self->Check = Check;
1514 self->fromObj = fromObj;
1515 if (fromObj)
1516 Py_INCREF(fromObj);
1517
1518 return (PyObject *)(self);
1519}
1520
1521 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001522OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001523{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001524 PyObject_GC_UnTrack((void *)(self));
1525 Py_XDECREF(self->fromObj);
1526 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001527}
1528
1529 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001530OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001531{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001532 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001533 return 0;
1534}
1535
1536 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001537OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001538{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001539 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001540 return 0;
1541}
1542
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001543 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001544OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001545{
1546 char_u *key;
1547 int flags;
1548 long numval;
1549 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001550 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001551
Bram Moolenaard6e39182013-05-21 18:30:34 +02001552 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001553 return NULL;
1554
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001555 DICTKEY_GET_NOTEMPTY(NULL)
1556
1557 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001558 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001559
1560 DICTKEY_UNREF
1561
1562 if (flags == 0)
1563 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001564 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001565 return NULL;
1566 }
1567
1568 if (flags & SOPT_UNSET)
1569 {
1570 Py_INCREF(Py_None);
1571 return Py_None;
1572 }
1573 else if (flags & SOPT_BOOL)
1574 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001575 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001576 r = numval ? Py_True : Py_False;
1577 Py_INCREF(r);
1578 return r;
1579 }
1580 else if (flags & SOPT_NUM)
1581 return PyInt_FromLong(numval);
1582 else if (flags & SOPT_STRING)
1583 {
1584 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001585 {
1586 PyObject *r = PyBytes_FromString((char *) stringval);
1587 vim_free(stringval);
1588 return r;
1589 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001590 else
1591 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001592 PyErr_SetString(PyExc_RuntimeError,
1593 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001594 return NULL;
1595 }
1596 }
1597 else
1598 {
1599 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1600 return NULL;
1601 }
1602}
1603
1604 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001605set_option_value_err(key, numval, stringval, opt_flags)
1606 char_u *key;
1607 int numval;
1608 char_u *stringval;
1609 int opt_flags;
1610{
1611 char_u *errmsg;
1612
1613 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
1614 {
1615 if (VimTryEnd())
1616 return FAIL;
1617 PyErr_SetVim((char *)errmsg);
1618 return FAIL;
1619 }
1620 return OK;
1621}
1622
1623 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001624set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1625 char_u *key;
1626 int numval;
1627 char_u *stringval;
1628 int opt_flags;
1629 int opt_type;
1630 void *from;
1631{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001632 win_T *save_curwin = NULL;
1633 tabpage_T *save_curtab = NULL;
1634 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001635 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001636
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001637 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001638 switch (opt_type)
1639 {
1640 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001641 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1642 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001643 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001644 if (VimTryEnd())
1645 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001646 PyErr_SetVim("Problem while switching windows.");
1647 return -1;
1648 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001649 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001650 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001651 if (r == FAIL)
1652 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001653 break;
1654 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001655 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001656 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001657 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001658 if (r == FAIL)
1659 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001660 break;
1661 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001662 r = set_option_value_err(key, numval, stringval, opt_flags);
1663 if (r == FAIL)
1664 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001665 break;
1666 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001667 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001668}
1669
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001670 static void *
1671py_memsave(void *p, size_t len)
1672{
1673 void *r;
1674 if (!(r = PyMem_Malloc(len)))
1675 return NULL;
1676 mch_memmove(r, p, len);
1677 return r;
1678}
1679
1680#define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1))
1681
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001682 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001683OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001684{
1685 char_u *key;
1686 int flags;
1687 int opt_flags;
1688 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001689 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001690
Bram Moolenaard6e39182013-05-21 18:30:34 +02001691 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001692 return -1;
1693
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001694 DICTKEY_GET_NOTEMPTY(-1)
1695
1696 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001697 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001698
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001699 if (flags == 0)
1700 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001701 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001702 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001703 return -1;
1704 }
1705
1706 if (valObject == NULL)
1707 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001708 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001709 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001710 PyErr_SetString(PyExc_ValueError,
1711 _("unable to unset global option"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001712 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001713 return -1;
1714 }
1715 else if (!(flags & SOPT_GLOBAL))
1716 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001717 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1718 "without global value"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001719 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001720 return -1;
1721 }
1722 else
1723 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001724 unset_global_local_option(key, self->from);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001725 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001726 return 0;
1727 }
1728 }
1729
Bram Moolenaard6e39182013-05-21 18:30:34 +02001730 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001731
1732 if (flags & SOPT_BOOL)
1733 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001734 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001735
Bram Moolenaarb983f752013-05-15 16:11:50 +02001736 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001737 r = -1;
1738 else
1739 r = set_option_value_for(key, istrue, NULL,
1740 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001741 }
1742 else if (flags & SOPT_NUM)
1743 {
1744 int val;
1745
1746#if PY_MAJOR_VERSION < 3
1747 if (PyInt_Check(valObject))
1748 val = PyInt_AsLong(valObject);
1749 else
1750#endif
1751 if (PyLong_Check(valObject))
1752 val = PyLong_AsLong(valObject);
1753 else
1754 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001755 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001756 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001757 return -1;
1758 }
1759
1760 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001761 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001762 }
1763 else
1764 {
1765 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001766 PyObject *todecref;
1767
1768 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001769 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001770 r = set_option_value_for(key, 0, val, opt_flags,
1771 self->opt_type, self->from);
1772 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001773 }
1774 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001775 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001776 }
1777
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001778 DICTKEY_UNREF
1779
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001780 return r;
1781}
1782
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001783static PyMappingMethods OptionsAsMapping = {
1784 (lenfunc) NULL,
1785 (binaryfunc) OptionsItem,
1786 (objobjargproc) OptionsAssItem,
1787};
1788
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001789/* Tabpage object
1790 */
1791
1792typedef struct
1793{
1794 PyObject_HEAD
1795 tabpage_T *tab;
1796} TabPageObject;
1797
1798static PyObject *WinListNew(TabPageObject *tabObject);
1799
1800static PyTypeObject TabPageType;
1801
1802 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001803CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001804{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001805 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001806 {
1807 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1808 return -1;
1809 }
1810
1811 return 0;
1812}
1813
1814 static PyObject *
1815TabPageNew(tabpage_T *tab)
1816{
1817 TabPageObject *self;
1818
1819 if (TAB_PYTHON_REF(tab))
1820 {
1821 self = TAB_PYTHON_REF(tab);
1822 Py_INCREF(self);
1823 }
1824 else
1825 {
1826 self = PyObject_NEW(TabPageObject, &TabPageType);
1827 if (self == NULL)
1828 return NULL;
1829 self->tab = tab;
1830 TAB_PYTHON_REF(tab) = self;
1831 }
1832
1833 return (PyObject *)(self);
1834}
1835
1836 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001837TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001838{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001839 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
1840 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001841
1842 DESTRUCTOR_FINISH(self);
1843}
1844
1845 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001846TabPageAttrValid(TabPageObject *self, char *name)
1847{
1848 PyObject *r;
1849
1850 if (strcmp(name, "valid") != 0)
1851 return NULL;
1852
1853 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
1854 Py_INCREF(r);
1855 return r;
1856}
1857
1858 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001859TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001860{
1861 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001862 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001863 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001864 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001865 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001866 return DictionaryNew(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001867 else if (strcmp(name, "window") == 0)
1868 {
1869 /* For current tab window.c does not bother to set or update tp_curwin
1870 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02001871 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001872 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001873 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001874 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001875 }
1876 return NULL;
1877}
1878
1879 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001880TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001881{
1882 static char repr[100];
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001883
Bram Moolenaard6e39182013-05-21 18:30:34 +02001884 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001885 {
1886 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
1887 return PyString_FromString(repr);
1888 }
1889 else
1890 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001891 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001892
1893 if (t == 0)
1894 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
1895 (self));
1896 else
1897 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
1898
1899 return PyString_FromString(repr);
1900 }
1901}
1902
1903static struct PyMethodDef TabPageMethods[] = {
1904 /* name, function, calling, documentation */
1905 { NULL, NULL, 0, NULL }
1906};
1907
1908/*
1909 * Window list object
1910 */
1911
1912static PyTypeObject TabListType;
1913static PySequenceMethods TabListAsSeq;
1914
1915typedef struct
1916{
1917 PyObject_HEAD
1918} TabListObject;
1919
1920 static PyInt
1921TabListLength(PyObject *self UNUSED)
1922{
1923 tabpage_T *tp = first_tabpage;
1924 PyInt n = 0;
1925
1926 while (tp != NULL)
1927 {
1928 ++n;
1929 tp = tp->tp_next;
1930 }
1931
1932 return n;
1933}
1934
1935 static PyObject *
1936TabListItem(PyObject *self UNUSED, PyInt n)
1937{
1938 tabpage_T *tp;
1939
1940 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
1941 if (n == 0)
1942 return TabPageNew(tp);
1943
1944 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
1945 return NULL;
1946}
1947
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001948/* Window object
1949 */
1950
1951typedef struct
1952{
1953 PyObject_HEAD
1954 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001955 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001956} WindowObject;
1957
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001958static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001959
1960 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001961CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001962{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001963 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001964 {
1965 PyErr_SetVim(_("attempt to refer to deleted window"));
1966 return -1;
1967 }
1968
1969 return 0;
1970}
1971
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001972 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001973WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02001974{
1975 /* We need to handle deletion of windows underneath us.
1976 * If we add a "w_python*_ref" field to the win_T structure,
1977 * then we can get at it in win_free() in vim. We then
1978 * need to create only ONE Python object per window - if
1979 * we try to create a second, just INCREF the existing one
1980 * and return it. The (single) Python object referring to
1981 * the window is stored in "w_python*_ref".
1982 * On a win_free() we set the Python object's win_T* field
1983 * to an invalid value. We trap all uses of a window
1984 * object, and reject them if the win_T* field is invalid.
1985 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001986 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02001987 * w_python_ref and w_python3_ref fields respectively.
1988 */
1989
1990 WindowObject *self;
1991
1992 if (WIN_PYTHON_REF(win))
1993 {
1994 self = WIN_PYTHON_REF(win);
1995 Py_INCREF(self);
1996 }
1997 else
1998 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02001999 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002000 if (self == NULL)
2001 return NULL;
2002 self->win = win;
2003 WIN_PYTHON_REF(win) = self;
2004 }
2005
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002006 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2007
Bram Moolenaar971db462013-05-12 18:44:48 +02002008 return (PyObject *)(self);
2009}
2010
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002011 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002012WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002013{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002014 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002015 if (self->win && self->win != INVALID_WINDOW_VALUE)
2016 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002017 Py_XDECREF(((PyObject *)(self->tabObject)));
2018 PyObject_GC_Del((void *)(self));
2019}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002020
Bram Moolenaar774267b2013-05-21 20:51:59 +02002021 static int
2022WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2023{
2024 Py_VISIT(((PyObject *)(self->tabObject)));
2025 return 0;
2026}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002027
Bram Moolenaar774267b2013-05-21 20:51:59 +02002028 static int
2029WindowClear(WindowObject *self)
2030{
2031 Py_CLEAR(self->tabObject);
2032 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002033}
2034
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002035 static win_T *
2036get_firstwin(TabPageObject *tabObject)
2037{
2038 if (tabObject)
2039 {
2040 if (CheckTabPage(tabObject))
2041 return NULL;
2042 /* For current tab window.c does not bother to set or update tp_firstwin
2043 */
2044 else if (tabObject->tab == curtab)
2045 return firstwin;
2046 else
2047 return tabObject->tab->tp_firstwin;
2048 }
2049 else
2050 return firstwin;
2051}
2052
Bram Moolenaar971db462013-05-12 18:44:48 +02002053 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002054WindowAttrValid(WindowObject *self, char *name)
2055{
2056 PyObject *r;
2057
2058 if (strcmp(name, "valid") != 0)
2059 return NULL;
2060
2061 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2062 Py_INCREF(r);
2063 return r;
2064}
2065
2066 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002067WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002068{
2069 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002070 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002071 else if (strcmp(name, "cursor") == 0)
2072 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002073 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002074
2075 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2076 }
2077 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002078 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002079#ifdef FEAT_WINDOWS
2080 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002081 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002082#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002083#ifdef FEAT_VERTSPLIT
2084 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002085 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002086 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002087 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002088#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002089 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002090 return DictionaryNew(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002091 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002092 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2093 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002094 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002095 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002096 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002097 return NULL;
2098 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002099 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002100 }
2101 else if (strcmp(name, "tabpage") == 0)
2102 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002103 Py_INCREF(self->tabObject);
2104 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002105 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002106 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002107 return Py_BuildValue("[ssssssssss]", "buffer", "cursor", "height",
2108 "vars", "options", "number", "row", "col", "tabpage", "valid");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002109 else
2110 return NULL;
2111}
2112
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002113 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002114WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002115{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002116 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002117 return -1;
2118
2119 if (strcmp(name, "buffer") == 0)
2120 {
2121 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2122 return -1;
2123 }
2124 else if (strcmp(name, "cursor") == 0)
2125 {
2126 long lnum;
2127 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002128
2129 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2130 return -1;
2131
Bram Moolenaard6e39182013-05-21 18:30:34 +02002132 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002133 {
2134 PyErr_SetVim(_("cursor position outside buffer"));
2135 return -1;
2136 }
2137
2138 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002139 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002140 return -1;
2141
Bram Moolenaard6e39182013-05-21 18:30:34 +02002142 self->win->w_cursor.lnum = lnum;
2143 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002144#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002145 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002146#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002147 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002148 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002149
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002150 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002151 return 0;
2152 }
2153 else if (strcmp(name, "height") == 0)
2154 {
2155 int height;
2156 win_T *savewin;
2157
2158 if (!PyArg_Parse(val, "i", &height))
2159 return -1;
2160
2161#ifdef FEAT_GUI
2162 need_mouse_correct = TRUE;
2163#endif
2164 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002165 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002166
2167 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002168 win_setheight(height);
2169 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002170 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002171 return -1;
2172
2173 return 0;
2174 }
2175#ifdef FEAT_VERTSPLIT
2176 else if (strcmp(name, "width") == 0)
2177 {
2178 int width;
2179 win_T *savewin;
2180
2181 if (!PyArg_Parse(val, "i", &width))
2182 return -1;
2183
2184#ifdef FEAT_GUI
2185 need_mouse_correct = TRUE;
2186#endif
2187 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002188 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002189
2190 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002191 win_setwidth(width);
2192 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002193 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002194 return -1;
2195
2196 return 0;
2197 }
2198#endif
2199 else
2200 {
2201 PyErr_SetString(PyExc_AttributeError, name);
2202 return -1;
2203 }
2204}
2205
2206 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002207WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002208{
2209 static char repr[100];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002210
Bram Moolenaard6e39182013-05-21 18:30:34 +02002211 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002212 {
2213 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2214 return PyString_FromString(repr);
2215 }
2216 else
2217 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002218 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002219
Bram Moolenaar6d216452013-05-12 19:00:41 +02002220 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002221 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2222 (self));
2223 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002224 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002225
2226 return PyString_FromString(repr);
2227 }
2228}
2229
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002230static struct PyMethodDef WindowMethods[] = {
2231 /* name, function, calling, documentation */
2232 { NULL, NULL, 0, NULL }
2233};
2234
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002235/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002236 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002237 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002238
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002239static PyTypeObject WinListType;
2240static PySequenceMethods WinListAsSeq;
2241
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002242typedef struct
2243{
2244 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002245 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002246} WinListObject;
2247
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002248 static PyObject *
2249WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002250{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002251 WinListObject *self;
2252
2253 self = PyObject_NEW(WinListObject, &WinListType);
2254 self->tabObject = tabObject;
2255 Py_INCREF(tabObject);
2256
2257 return (PyObject *)(self);
2258}
2259
2260 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002261WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002262{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002263 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002264
2265 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02002266 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002267 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02002268 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002269
2270 DESTRUCTOR_FINISH(self);
2271}
2272
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002273 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002274WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002275{
2276 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002277 PyInt n = 0;
2278
Bram Moolenaard6e39182013-05-21 18:30:34 +02002279 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002280 return -1;
2281
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002282 while (w != NULL)
2283 {
2284 ++n;
2285 w = W_NEXT(w);
2286 }
2287
2288 return n;
2289}
2290
2291 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002292WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002293{
2294 win_T *w;
2295
Bram Moolenaard6e39182013-05-21 18:30:34 +02002296 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002297 return NULL;
2298
2299 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002300 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002301 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002302
2303 PyErr_SetString(PyExc_IndexError, _("no such window"));
2304 return NULL;
2305}
2306
2307/* Convert a Python string into a Vim line.
2308 *
2309 * The result is in allocated memory. All internal nulls are replaced by
2310 * newline characters. It is an error for the string to contain newline
2311 * characters.
2312 *
2313 * On errors, the Python exception data is set, and NULL is returned.
2314 */
2315 static char *
2316StringToLine(PyObject *obj)
2317{
2318 const char *str;
2319 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002320 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002321 PyInt len;
2322 PyInt i;
2323 char *p;
2324
2325 if (obj == NULL || !PyString_Check(obj))
2326 {
2327 PyErr_BadArgument();
2328 return NULL;
2329 }
2330
Bram Moolenaar19e60942011-06-19 00:27:51 +02002331 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2332 str = PyString_AsString(bytes);
2333 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002334
2335 /*
2336 * Error checking: String must not contain newlines, as we
2337 * are replacing a single line, and we must replace it with
2338 * a single line.
2339 * A trailing newline is removed, so that append(f.readlines()) works.
2340 */
2341 p = memchr(str, '\n', len);
2342 if (p != NULL)
2343 {
2344 if (p == str + len - 1)
2345 --len;
2346 else
2347 {
2348 PyErr_SetVim(_("string cannot contain newlines"));
2349 return NULL;
2350 }
2351 }
2352
2353 /* Create a copy of the string, with internal nulls replaced by
2354 * newline characters, as is the vim convention.
2355 */
2356 save = (char *)alloc((unsigned)(len+1));
2357 if (save == NULL)
2358 {
2359 PyErr_NoMemory();
2360 return NULL;
2361 }
2362
2363 for (i = 0; i < len; ++i)
2364 {
2365 if (str[i] == '\0')
2366 save[i] = '\n';
2367 else
2368 save[i] = str[i];
2369 }
2370
2371 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002372 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002373
2374 return save;
2375}
2376
2377/* Get a line from the specified buffer. The line number is
2378 * in Vim format (1-based). The line is returned as a Python
2379 * string object.
2380 */
2381 static PyObject *
2382GetBufferLine(buf_T *buf, PyInt n)
2383{
2384 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2385}
2386
2387
2388/* Get a list of lines from the specified buffer. The line numbers
2389 * are in Vim format (1-based). The range is from lo up to, but not
2390 * including, hi. The list is returned as a Python list of string objects.
2391 */
2392 static PyObject *
2393GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2394{
2395 PyInt i;
2396 PyInt n = hi - lo;
2397 PyObject *list = PyList_New(n);
2398
2399 if (list == NULL)
2400 return NULL;
2401
2402 for (i = 0; i < n; ++i)
2403 {
2404 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2405
2406 /* Error check - was the Python string creation OK? */
2407 if (str == NULL)
2408 {
2409 Py_DECREF(list);
2410 return NULL;
2411 }
2412
2413 /* Set the list item */
2414 if (PyList_SetItem(list, i, str))
2415 {
2416 Py_DECREF(str);
2417 Py_DECREF(list);
2418 return NULL;
2419 }
2420 }
2421
2422 /* The ownership of the Python list is passed to the caller (ie,
2423 * the caller should Py_DECREF() the object when it is finished
2424 * with it).
2425 */
2426
2427 return list;
2428}
2429
2430/*
2431 * Check if deleting lines made the cursor position invalid.
2432 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2433 * deleted).
2434 */
2435 static void
2436py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2437{
2438 if (curwin->w_cursor.lnum >= lo)
2439 {
2440 /* Adjust the cursor position if it's in/after the changed
2441 * lines. */
2442 if (curwin->w_cursor.lnum >= hi)
2443 {
2444 curwin->w_cursor.lnum += extra;
2445 check_cursor_col();
2446 }
2447 else if (extra < 0)
2448 {
2449 curwin->w_cursor.lnum = lo;
2450 check_cursor();
2451 }
2452 else
2453 check_cursor_col();
2454 changed_cline_bef_curs();
2455 }
2456 invalidate_botline();
2457}
2458
Bram Moolenaar19e60942011-06-19 00:27:51 +02002459/*
2460 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002461 * in Vim format (1-based). The replacement line is given as
2462 * a Python string object. The object is checked for validity
2463 * and correct format. Errors are returned as a value of FAIL.
2464 * The return value is OK on success.
2465 * If OK is returned and len_change is not NULL, *len_change
2466 * is set to the change in the buffer length.
2467 */
2468 static int
2469SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2470{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002471 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002472 * There are three cases:
2473 * 1. NULL, or None - this is a deletion.
2474 * 2. A string - this is a replacement.
2475 * 3. Anything else - this is an error.
2476 */
2477 if (line == Py_None || line == NULL)
2478 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002479 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002480
2481 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002482 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002483
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002484 VimTryStart();
2485
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002486 if (u_savedel((linenr_T)n, 1L) == FAIL)
2487 PyErr_SetVim(_("cannot save undo information"));
2488 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2489 PyErr_SetVim(_("cannot delete line"));
2490 else
2491 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002492 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002493 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2494 deleted_lines_mark((linenr_T)n, 1L);
2495 }
2496
Bram Moolenaar105bc352013-05-17 16:03:57 +02002497 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002498
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002499 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002500 return FAIL;
2501
2502 if (len_change)
2503 *len_change = -1;
2504
2505 return OK;
2506 }
2507 else if (PyString_Check(line))
2508 {
2509 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002510 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002511
2512 if (save == NULL)
2513 return FAIL;
2514
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002515 VimTryStart();
2516
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002517 /* We do not need to free "save" if ml_replace() consumes it. */
2518 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002519 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002520
2521 if (u_savesub((linenr_T)n) == FAIL)
2522 {
2523 PyErr_SetVim(_("cannot save undo information"));
2524 vim_free(save);
2525 }
2526 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2527 {
2528 PyErr_SetVim(_("cannot replace line"));
2529 vim_free(save);
2530 }
2531 else
2532 changed_bytes((linenr_T)n, 0);
2533
Bram Moolenaar105bc352013-05-17 16:03:57 +02002534 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002535
2536 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002537 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002538 check_cursor_col();
2539
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002540 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002541 return FAIL;
2542
2543 if (len_change)
2544 *len_change = 0;
2545
2546 return OK;
2547 }
2548 else
2549 {
2550 PyErr_BadArgument();
2551 return FAIL;
2552 }
2553}
2554
Bram Moolenaar19e60942011-06-19 00:27:51 +02002555/* Replace a range of lines in the specified buffer. The line numbers are in
2556 * Vim format (1-based). The range is from lo up to, but not including, hi.
2557 * The replacement lines are given as a Python list of string objects. The
2558 * list is checked for validity and correct format. Errors are returned as a
2559 * value of FAIL. The return value is OK on success.
2560 * If OK is returned and len_change is not NULL, *len_change
2561 * is set to the change in the buffer length.
2562 */
2563 static int
2564SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2565{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002566 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002567 * There are three cases:
2568 * 1. NULL, or None - this is a deletion.
2569 * 2. A list - this is a replacement.
2570 * 3. Anything else - this is an error.
2571 */
2572 if (list == Py_None || list == NULL)
2573 {
2574 PyInt i;
2575 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002576 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002577
2578 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002579 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002580 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002581
2582 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2583 PyErr_SetVim(_("cannot save undo information"));
2584 else
2585 {
2586 for (i = 0; i < n; ++i)
2587 {
2588 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2589 {
2590 PyErr_SetVim(_("cannot delete line"));
2591 break;
2592 }
2593 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002594 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002595 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2596 deleted_lines_mark((linenr_T)lo, (long)i);
2597 }
2598
Bram Moolenaar105bc352013-05-17 16:03:57 +02002599 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002600
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002601 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002602 return FAIL;
2603
2604 if (len_change)
2605 *len_change = -n;
2606
2607 return OK;
2608 }
2609 else if (PyList_Check(list))
2610 {
2611 PyInt i;
2612 PyInt new_len = PyList_Size(list);
2613 PyInt old_len = hi - lo;
2614 PyInt extra = 0; /* lines added to text, can be negative */
2615 char **array;
2616 buf_T *savebuf;
2617
2618 if (new_len == 0) /* avoid allocating zero bytes */
2619 array = NULL;
2620 else
2621 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002622 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002623 if (array == NULL)
2624 {
2625 PyErr_NoMemory();
2626 return FAIL;
2627 }
2628 }
2629
2630 for (i = 0; i < new_len; ++i)
2631 {
2632 PyObject *line = PyList_GetItem(list, i);
2633
2634 array[i] = StringToLine(line);
2635 if (array[i] == NULL)
2636 {
2637 while (i)
2638 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002639 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002640 return FAIL;
2641 }
2642 }
2643
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002644 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02002645 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002646
2647 // START of region without "return". Must call restore_buffer()!
2648 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002649
2650 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2651 PyErr_SetVim(_("cannot save undo information"));
2652
2653 /* If the size of the range is reducing (ie, new_len < old_len) we
2654 * need to delete some old_len. We do this at the start, by
2655 * repeatedly deleting line "lo".
2656 */
2657 if (!PyErr_Occurred())
2658 {
2659 for (i = 0; i < old_len - new_len; ++i)
2660 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2661 {
2662 PyErr_SetVim(_("cannot delete line"));
2663 break;
2664 }
2665 extra -= i;
2666 }
2667
2668 /* For as long as possible, replace the existing old_len with the
2669 * new old_len. This is a more efficient operation, as it requires
2670 * less memory allocation and freeing.
2671 */
2672 if (!PyErr_Occurred())
2673 {
2674 for (i = 0; i < old_len && i < new_len; ++i)
2675 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2676 == FAIL)
2677 {
2678 PyErr_SetVim(_("cannot replace line"));
2679 break;
2680 }
2681 }
2682 else
2683 i = 0;
2684
2685 /* Now we may need to insert the remaining new old_len. If we do, we
2686 * must free the strings as we finish with them (we can't pass the
2687 * responsibility to vim in this case).
2688 */
2689 if (!PyErr_Occurred())
2690 {
2691 while (i < new_len)
2692 {
2693 if (ml_append((linenr_T)(lo + i - 1),
2694 (char_u *)array[i], 0, FALSE) == FAIL)
2695 {
2696 PyErr_SetVim(_("cannot insert line"));
2697 break;
2698 }
2699 vim_free(array[i]);
2700 ++i;
2701 ++extra;
2702 }
2703 }
2704
2705 /* Free any left-over old_len, as a result of an error */
2706 while (i < new_len)
2707 {
2708 vim_free(array[i]);
2709 ++i;
2710 }
2711
2712 /* Free the array of old_len. All of its contents have now
2713 * been dealt with (either freed, or the responsibility passed
2714 * to vim.
2715 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002716 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002717
2718 /* Adjust marks. Invalidate any which lie in the
2719 * changed range, and move any in the remainder of the buffer.
2720 */
2721 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2722 (long)MAXLNUM, (long)extra);
2723 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2724
Bram Moolenaar105bc352013-05-17 16:03:57 +02002725 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002726 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2727
Bram Moolenaar105bc352013-05-17 16:03:57 +02002728 // END of region without "return".
2729 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 = new_len - old_len;
2736
2737 return OK;
2738 }
2739 else
2740 {
2741 PyErr_BadArgument();
2742 return FAIL;
2743 }
2744}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002745
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002746/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002747 * The line number is in Vim format (1-based). The lines to be inserted are
2748 * given as a Python list of string objects or as a single string. The lines
2749 * to be added are checked for validity and correct format. Errors are
2750 * returned as a value of FAIL. The return value is OK on success.
2751 * If OK is returned and len_change is not NULL, *len_change
2752 * is set to the change in the buffer length.
2753 */
2754 static int
2755InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2756{
2757 /* First of all, we check the type of the supplied Python object.
2758 * It must be a string or a list, or the call is in error.
2759 */
2760 if (PyString_Check(lines))
2761 {
2762 char *str = StringToLine(lines);
2763 buf_T *savebuf;
2764
2765 if (str == NULL)
2766 return FAIL;
2767
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002768 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002769 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002770 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002771
2772 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2773 PyErr_SetVim(_("cannot save undo information"));
2774 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2775 PyErr_SetVim(_("cannot insert line"));
2776 else
2777 appended_lines_mark((linenr_T)n, 1L);
2778
2779 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002780 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002781 update_screen(VALID);
2782
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002783 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002784 return FAIL;
2785
2786 if (len_change)
2787 *len_change = 1;
2788
2789 return OK;
2790 }
2791 else if (PyList_Check(lines))
2792 {
2793 PyInt i;
2794 PyInt size = PyList_Size(lines);
2795 char **array;
2796 buf_T *savebuf;
2797
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002798 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002799 if (array == NULL)
2800 {
2801 PyErr_NoMemory();
2802 return FAIL;
2803 }
2804
2805 for (i = 0; i < size; ++i)
2806 {
2807 PyObject *line = PyList_GetItem(lines, i);
2808 array[i] = StringToLine(line);
2809
2810 if (array[i] == NULL)
2811 {
2812 while (i)
2813 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002814 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002815 return FAIL;
2816 }
2817 }
2818
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002819 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002820 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002821 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002822
2823 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2824 PyErr_SetVim(_("cannot save undo information"));
2825 else
2826 {
2827 for (i = 0; i < size; ++i)
2828 {
2829 if (ml_append((linenr_T)(n + i),
2830 (char_u *)array[i], 0, FALSE) == FAIL)
2831 {
2832 PyErr_SetVim(_("cannot insert line"));
2833
2834 /* Free the rest of the lines */
2835 while (i < size)
2836 vim_free(array[i++]);
2837
2838 break;
2839 }
2840 vim_free(array[i]);
2841 }
2842 if (i > 0)
2843 appended_lines_mark((linenr_T)n, (long)i);
2844 }
2845
2846 /* Free the array of lines. All of its contents have now
2847 * been freed.
2848 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002849 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002850
Bram Moolenaar105bc352013-05-17 16:03:57 +02002851 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002852 update_screen(VALID);
2853
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002854 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002855 return FAIL;
2856
2857 if (len_change)
2858 *len_change = size;
2859
2860 return OK;
2861 }
2862 else
2863 {
2864 PyErr_BadArgument();
2865 return FAIL;
2866 }
2867}
2868
2869/*
2870 * Common routines for buffers and line ranges
2871 * -------------------------------------------
2872 */
2873
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002874typedef struct
2875{
2876 PyObject_HEAD
2877 buf_T *buf;
2878} BufferObject;
2879
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002880 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002881CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002882{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002883 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002884 {
2885 PyErr_SetVim(_("attempt to refer to deleted buffer"));
2886 return -1;
2887 }
2888
2889 return 0;
2890}
2891
2892 static PyObject *
2893RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
2894{
2895 if (CheckBuffer(self))
2896 return NULL;
2897
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002898 if (end == -1)
2899 end = self->buf->b_ml.ml_line_count;
2900
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002901 if (n < 0)
2902 n += end - start + 1;
2903
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002904 if (n < 0 || n > end - start)
2905 {
2906 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2907 return NULL;
2908 }
2909
2910 return GetBufferLine(self->buf, n+start);
2911}
2912
2913 static PyObject *
2914RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
2915{
2916 PyInt size;
2917
2918 if (CheckBuffer(self))
2919 return NULL;
2920
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002921 if (end == -1)
2922 end = self->buf->b_ml.ml_line_count;
2923
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002924 size = end - start + 1;
2925
2926 if (lo < 0)
2927 lo = 0;
2928 else if (lo > size)
2929 lo = size;
2930 if (hi < 0)
2931 hi = 0;
2932 if (hi < lo)
2933 hi = lo;
2934 else if (hi > size)
2935 hi = size;
2936
2937 return GetBufferLineList(self->buf, lo+start, hi+start);
2938}
2939
2940 static PyInt
2941RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2942{
2943 PyInt len_change;
2944
2945 if (CheckBuffer(self))
2946 return -1;
2947
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002948 if (end == -1)
2949 end = self->buf->b_ml.ml_line_count;
2950
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002951 if (n < 0)
2952 n += end - start + 1;
2953
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002954 if (n < 0 || n > end - start)
2955 {
2956 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2957 return -1;
2958 }
2959
2960 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
2961 return -1;
2962
2963 if (new_end)
2964 *new_end = end + len_change;
2965
2966 return 0;
2967}
2968
Bram Moolenaar19e60942011-06-19 00:27:51 +02002969 static PyInt
2970RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2971{
2972 PyInt size;
2973 PyInt len_change;
2974
2975 /* Self must be a valid buffer */
2976 if (CheckBuffer(self))
2977 return -1;
2978
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002979 if (end == -1)
2980 end = self->buf->b_ml.ml_line_count;
2981
Bram Moolenaar19e60942011-06-19 00:27:51 +02002982 /* Sort out the slice range */
2983 size = end - start + 1;
2984
2985 if (lo < 0)
2986 lo = 0;
2987 else if (lo > size)
2988 lo = size;
2989 if (hi < 0)
2990 hi = 0;
2991 if (hi < lo)
2992 hi = lo;
2993 else if (hi > size)
2994 hi = size;
2995
2996 if (SetBufferLineList(self->buf, lo + start, hi + start,
2997 val, &len_change) == FAIL)
2998 return -1;
2999
3000 if (new_end)
3001 *new_end = end + len_change;
3002
3003 return 0;
3004}
3005
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003006
3007 static PyObject *
3008RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3009{
3010 PyObject *lines;
3011 PyInt len_change;
3012 PyInt max;
3013 PyInt n;
3014
3015 if (CheckBuffer(self))
3016 return NULL;
3017
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003018 if (end == -1)
3019 end = self->buf->b_ml.ml_line_count;
3020
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003021 max = n = end - start + 1;
3022
3023 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3024 return NULL;
3025
3026 if (n < 0 || n > max)
3027 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003028 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003029 return NULL;
3030 }
3031
3032 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3033 return NULL;
3034
3035 if (new_end)
3036 *new_end = end + len_change;
3037
3038 Py_INCREF(Py_None);
3039 return Py_None;
3040}
3041
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003042/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003043 */
3044
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003045static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003046static PySequenceMethods RangeAsSeq;
3047static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003048
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003049typedef struct
3050{
3051 PyObject_HEAD
3052 BufferObject *buf;
3053 PyInt start;
3054 PyInt end;
3055} RangeObject;
3056
3057 static PyObject *
3058RangeNew(buf_T *buf, PyInt start, PyInt end)
3059{
3060 BufferObject *bufr;
3061 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003062 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003063 if (self == NULL)
3064 return NULL;
3065
3066 bufr = (BufferObject *)BufferNew(buf);
3067 if (bufr == NULL)
3068 {
3069 Py_DECREF(self);
3070 return NULL;
3071 }
3072 Py_INCREF(bufr);
3073
3074 self->buf = bufr;
3075 self->start = start;
3076 self->end = end;
3077
3078 return (PyObject *)(self);
3079}
3080
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003081 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003082RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003083{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003084 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003085 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003086 PyObject_GC_Del((void *)(self));
3087}
3088
3089 static int
3090RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3091{
3092 Py_VISIT(((PyObject *)(self->buf)));
3093 return 0;
3094}
3095
3096 static int
3097RangeClear(RangeObject *self)
3098{
3099 Py_CLEAR(self->buf);
3100 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003101}
3102
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003103 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003104RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003105{
3106 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003107 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003108 return -1; /* ??? */
3109
Bram Moolenaard6e39182013-05-21 18:30:34 +02003110 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003111}
3112
3113 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003114RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003115{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003116 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003117}
3118
3119 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003120RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003121{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003122 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003123}
3124
3125 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003126RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003127{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003128 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003129}
3130
3131 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003132RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003133{
3134 static char repr[100];
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003135
Bram Moolenaard6e39182013-05-21 18:30:34 +02003136 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003137 {
3138 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
3139 (self));
3140 return PyString_FromString(repr);
3141 }
3142 else
3143 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003144 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003145 int len;
3146
3147 if (name == NULL)
3148 name = "";
3149 len = (int)strlen(name);
3150
3151 if (len > 45)
3152 name = name + (45 - len);
3153
3154 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
3155 len > 45 ? "..." : "", name,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003156 self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003157
3158 return PyString_FromString(repr);
3159 }
3160}
3161
3162static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003163 /* name, function, calling, documentation */
3164 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
3165 { NULL, NULL, 0, NULL }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003166};
3167
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003168static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003169static PySequenceMethods BufferAsSeq;
3170static PyMappingMethods BufferAsMapping;
3171
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003172 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003173BufferNew(buf_T *buf)
3174{
3175 /* We need to handle deletion of buffers underneath us.
3176 * If we add a "b_python*_ref" field to the buf_T structure,
3177 * then we can get at it in buf_freeall() in vim. We then
3178 * need to create only ONE Python object per buffer - if
3179 * we try to create a second, just INCREF the existing one
3180 * and return it. The (single) Python object referring to
3181 * the buffer is stored in "b_python*_ref".
3182 * Question: what to do on a buf_freeall(). We'll probably
3183 * have to either delete the Python object (DECREF it to
3184 * zero - a bad idea, as it leaves dangling refs!) or
3185 * set the buf_T * value to an invalid value (-1?), which
3186 * means we need checks in all access functions... Bah.
3187 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003188 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003189 * b_python_ref and b_python3_ref fields respectively.
3190 */
3191
3192 BufferObject *self;
3193
3194 if (BUF_PYTHON_REF(buf) != NULL)
3195 {
3196 self = BUF_PYTHON_REF(buf);
3197 Py_INCREF(self);
3198 }
3199 else
3200 {
3201 self = PyObject_NEW(BufferObject, &BufferType);
3202 if (self == NULL)
3203 return NULL;
3204 self->buf = buf;
3205 BUF_PYTHON_REF(buf) = self;
3206 }
3207
3208 return (PyObject *)(self);
3209}
3210
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003211 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003212BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003213{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003214 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3215 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003216
3217 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003218}
3219
Bram Moolenaar971db462013-05-12 18:44:48 +02003220 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003221BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003222{
3223 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003224 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003225 return -1; /* ??? */
3226
Bram Moolenaard6e39182013-05-21 18:30:34 +02003227 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003228}
3229
3230 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003231BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003232{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003233 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003234}
3235
3236 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003237BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003238{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003239 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003240}
3241
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003242 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003243BufferAttrValid(BufferObject *self, char *name)
3244{
3245 PyObject *r;
3246
3247 if (strcmp(name, "valid") != 0)
3248 return NULL;
3249
3250 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
3251 Py_INCREF(r);
3252 return r;
3253}
3254
3255 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003256BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003257{
3258 if (strcmp(name, "name") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003259 return Py_BuildValue("s", self->buf->b_ffname);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003260 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003261 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003262 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003263 return DictionaryNew(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003264 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003265 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3266 (PyObject *) self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003267 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003268 return Py_BuildValue("[sssss]", "name", "number", "vars", "options",
3269 "valid");
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003270 else
3271 return NULL;
3272}
3273
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003274 static int
3275BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3276{
3277 if (CheckBuffer(self))
3278 return -1;
3279
3280 if (strcmp(name, "name") == 0)
3281 {
3282 char_u *val;
3283 aco_save_T aco;
3284 int r;
3285 PyObject *todecref;
3286
3287 if (!(val = StringToChars(valObject, &todecref)))
3288 return -1;
3289
3290 VimTryStart();
3291 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3292 aucmd_prepbuf(&aco, self->buf);
3293 r = rename_buffer(val);
3294 aucmd_restbuf(&aco);
3295 Py_XDECREF(todecref);
3296 if (VimTryEnd())
3297 return -1;
3298
3299 if (r == FAIL)
3300 {
3301 PyErr_SetVim(_("failed to rename buffer"));
3302 return -1;
3303 }
3304 return 0;
3305 }
3306 else
3307 {
3308 PyErr_SetString(PyExc_AttributeError, name);
3309 return -1;
3310 }
3311}
3312
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003313 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003314BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003315{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003316 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003317}
3318
3319 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003320BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003321{
3322 pos_T *posp;
3323 char *pmark;
3324 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003325 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003326
Bram Moolenaard6e39182013-05-21 18:30:34 +02003327 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003328 return NULL;
3329
3330 if (!PyArg_ParseTuple(args, "s", &pmark))
3331 return NULL;
3332 mark = *pmark;
3333
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003334 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02003335 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003336 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003337 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003338 if (VimTryEnd())
3339 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003340
3341 if (posp == NULL)
3342 {
3343 PyErr_SetVim(_("invalid mark name"));
3344 return NULL;
3345 }
3346
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003347 if (posp->lnum <= 0)
3348 {
3349 /* Or raise an error? */
3350 Py_INCREF(Py_None);
3351 return Py_None;
3352 }
3353
3354 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3355}
3356
3357 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003358BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003359{
3360 PyInt start;
3361 PyInt end;
3362
Bram Moolenaard6e39182013-05-21 18:30:34 +02003363 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003364 return NULL;
3365
3366 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3367 return NULL;
3368
Bram Moolenaard6e39182013-05-21 18:30:34 +02003369 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003370}
3371
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003372 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003373BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003374{
3375 static char repr[100];
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003376
Bram Moolenaard6e39182013-05-21 18:30:34 +02003377 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003378 {
3379 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3380 return PyString_FromString(repr);
3381 }
3382 else
3383 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003384 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003385 PyInt len;
3386
3387 if (name == NULL)
3388 name = "";
3389 len = strlen(name);
3390
3391 if (len > 35)
3392 name = name + (35 - len);
3393
3394 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3395
3396 return PyString_FromString(repr);
3397 }
3398}
3399
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003400static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003401 /* name, function, calling, documentation */
3402 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
3403 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
3404 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaar7f85d292012-02-04 20:17:26 +01003405#if PY_VERSION_HEX >= 0x03000000
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003406 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, "List buffer attributes" },
Bram Moolenaar7f85d292012-02-04 20:17:26 +01003407#endif
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003408 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003409};
3410
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003411/*
3412 * Buffer list object - Implementation
3413 */
3414
3415static PyTypeObject BufMapType;
3416
3417typedef struct
3418{
3419 PyObject_HEAD
3420} BufMapObject;
3421
3422 static PyInt
3423BufMapLength(PyObject *self UNUSED)
3424{
3425 buf_T *b = firstbuf;
3426 PyInt n = 0;
3427
3428 while (b)
3429 {
3430 ++n;
3431 b = b->b_next;
3432 }
3433
3434 return n;
3435}
3436
3437 static PyObject *
3438BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3439{
3440 buf_T *b;
3441 int bnr;
3442
3443#if PY_MAJOR_VERSION < 3
3444 if (PyInt_Check(keyObject))
3445 bnr = PyInt_AsLong(keyObject);
3446 else
3447#endif
3448 if (PyLong_Check(keyObject))
3449 bnr = PyLong_AsLong(keyObject);
3450 else
3451 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003452 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003453 return NULL;
3454 }
3455
3456 b = buflist_findnr(bnr);
3457
3458 if (b)
3459 return BufferNew(b);
3460 else
3461 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003462 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003463 return NULL;
3464 }
3465}
3466
3467 static void
3468BufMapIterDestruct(PyObject *buffer)
3469{
3470 /* Iteration was stopped before all buffers were processed */
3471 if (buffer)
3472 {
3473 Py_DECREF(buffer);
3474 }
3475}
3476
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003477 static int
3478BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3479{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003480 if (buffer)
3481 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003482 return 0;
3483}
3484
3485 static int
3486BufMapIterClear(PyObject **buffer)
3487{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003488 if (*buffer)
3489 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003490 return 0;
3491}
3492
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003493 static PyObject *
3494BufMapIterNext(PyObject **buffer)
3495{
3496 PyObject *next;
3497 PyObject *r;
3498
3499 if (!*buffer)
3500 return NULL;
3501
3502 r = *buffer;
3503
3504 if (CheckBuffer((BufferObject *)(r)))
3505 {
3506 *buffer = NULL;
3507 return NULL;
3508 }
3509
3510 if (!((BufferObject *)(r))->buf->b_next)
3511 next = NULL;
3512 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3513 return NULL;
3514 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02003515 /* Do not increment reference: we no longer hold it (decref), but whoever
3516 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003517 return r;
3518}
3519
3520 static PyObject *
3521BufMapIter(PyObject *self UNUSED)
3522{
3523 PyObject *buffer;
3524
3525 buffer = BufferNew(firstbuf);
3526 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003527 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3528 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003529}
3530
3531static PyMappingMethods BufMapAsMapping = {
3532 (lenfunc) BufMapLength,
3533 (binaryfunc) BufMapItem,
3534 (objobjargproc) 0,
3535};
3536
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003537/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003538 */
3539
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003540 static PyObject *
3541CurrentGetattr(PyObject *self UNUSED, char *name)
3542{
3543 if (strcmp(name, "buffer") == 0)
3544 return (PyObject *)BufferNew(curbuf);
3545 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003546 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003547 else if (strcmp(name, "tabpage") == 0)
3548 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003549 else if (strcmp(name, "line") == 0)
3550 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3551 else if (strcmp(name, "range") == 0)
3552 return RangeNew(curbuf, RangeStart, RangeEnd);
3553 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003554 return Py_BuildValue("[sssss]", "buffer", "window", "line", "range",
3555 "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003556 else
3557 {
3558 PyErr_SetString(PyExc_AttributeError, name);
3559 return NULL;
3560 }
3561}
3562
3563 static int
3564CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3565{
3566 if (strcmp(name, "line") == 0)
3567 {
3568 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3569 return -1;
3570
3571 return 0;
3572 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003573 else if (strcmp(name, "buffer") == 0)
3574 {
3575 int count;
3576
3577 if (value->ob_type != &BufferType)
3578 {
3579 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3580 return -1;
3581 }
3582
3583 if (CheckBuffer((BufferObject *)(value)))
3584 return -1;
3585 count = ((BufferObject *)(value))->buf->b_fnum;
3586
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003587 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003588 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3589 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003590 if (VimTryEnd())
3591 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003592 PyErr_SetVim(_("failed to switch to given buffer"));
3593 return -1;
3594 }
3595
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003596 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003597 }
3598 else if (strcmp(name, "window") == 0)
3599 {
3600 int count;
3601
3602 if (value->ob_type != &WindowType)
3603 {
3604 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3605 return -1;
3606 }
3607
3608 if (CheckWindow((WindowObject *)(value)))
3609 return -1;
3610 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3611
3612 if (!count)
3613 {
3614 PyErr_SetString(PyExc_ValueError,
3615 _("failed to find window in the current tab page"));
3616 return -1;
3617 }
3618
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003619 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003620 win_goto(((WindowObject *)(value))->win);
3621 if (((WindowObject *)(value))->win != curwin)
3622 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003623 if (VimTryEnd())
3624 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003625 PyErr_SetString(PyExc_RuntimeError,
3626 _("did not switch to the specified window"));
3627 return -1;
3628 }
3629
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003630 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003631 }
3632 else if (strcmp(name, "tabpage") == 0)
3633 {
3634 if (value->ob_type != &TabPageType)
3635 {
3636 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3637 return -1;
3638 }
3639
3640 if (CheckTabPage((TabPageObject *)(value)))
3641 return -1;
3642
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003643 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003644 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3645 if (((TabPageObject *)(value))->tab != curtab)
3646 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003647 if (VimTryEnd())
3648 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003649 PyErr_SetString(PyExc_RuntimeError,
3650 _("did not switch to the specified tab page"));
3651 return -1;
3652 }
3653
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003654 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003655 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003656 else
3657 {
3658 PyErr_SetString(PyExc_AttributeError, name);
3659 return -1;
3660 }
3661}
3662
Bram Moolenaardb913952012-06-29 12:54:53 +02003663 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003664init_range_cmd(exarg_T *eap)
3665{
3666 RangeStart = eap->line1;
3667 RangeEnd = eap->line2;
3668}
3669
3670 static void
3671init_range_eval(typval_T *rettv UNUSED)
3672{
3673 RangeStart = (PyInt) curwin->w_cursor.lnum;
3674 RangeEnd = RangeStart;
3675}
3676
3677 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003678run_cmd(const char *cmd, void *arg UNUSED
3679#ifdef PY_CAN_RECURSE
3680 , PyGILState_STATE *pygilstate UNUSED
3681#endif
3682 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003683{
3684 PyRun_SimpleString((char *) cmd);
3685}
3686
3687static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
3688static int code_hdr_len = 30;
3689
3690 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003691run_do(const char *cmd, void *arg UNUSED
3692#ifdef PY_CAN_RECURSE
3693 , PyGILState_STATE *pygilstate
3694#endif
3695 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003696{
3697 PyInt lnum;
3698 size_t len;
3699 char *code;
3700 int status;
3701 PyObject *pyfunc, *pymain;
3702
Bram Moolenaar4ac66762013-05-28 22:31:46 +02003703 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003704 {
3705 EMSG(_("cannot save undo information"));
3706 return;
3707 }
3708
3709 len = code_hdr_len + STRLEN(cmd);
3710 code = PyMem_New(char, len + 1);
3711 memcpy(code, code_hdr, code_hdr_len);
3712 STRCPY(code + code_hdr_len, cmd);
3713 status = PyRun_SimpleString(code);
3714 PyMem_Free(code);
3715
3716 if (status)
3717 {
3718 EMSG(_("failed to run the code"));
3719 return;
3720 }
3721
3722 status = 0;
3723 pymain = PyImport_AddModule("__main__");
3724 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003725#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003726 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003727#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003728
3729 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
3730 {
3731 PyObject *line, *linenr, *ret;
3732
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003733#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003734 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003735#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003736 if (!(line = GetBufferLine(curbuf, lnum)))
3737 goto err;
3738 if (!(linenr = PyInt_FromLong((long) lnum)))
3739 {
3740 Py_DECREF(line);
3741 goto err;
3742 }
3743 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
3744 Py_DECREF(line);
3745 Py_DECREF(linenr);
3746 if (!ret)
3747 goto err;
3748
3749 if (ret != Py_None)
3750 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
3751 goto err;
3752
3753 Py_XDECREF(ret);
3754 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003755#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003756 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003757#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003758 }
3759 goto out;
3760err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003761#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003762 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003763#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003764 PyErr_PrintEx(0);
3765 PythonIO_Flush();
3766 status = 1;
3767out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003768#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003769 if (!status)
3770 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003771#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003772 Py_DECREF(pyfunc);
3773 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
3774 if (status)
3775 return;
3776 check_cursor();
3777 update_curbuf(NOT_VALID);
3778}
3779
3780 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003781run_eval(const char *cmd, typval_T *rettv
3782#ifdef PY_CAN_RECURSE
3783 , PyGILState_STATE *pygilstate UNUSED
3784#endif
3785 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003786{
3787 PyObject *r;
3788
3789 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
3790 if (r == NULL)
3791 {
3792 if (PyErr_Occurred() && !msg_silent)
3793 PyErr_PrintEx(0);
3794 EMSG(_("E858: Eval did not return a valid python object"));
3795 }
3796 else
3797 {
3798 if (ConvertFromPyObject(r, rettv) == -1)
3799 EMSG(_("E859: Failed to convert returned python object to vim value"));
3800 Py_DECREF(r);
3801 }
3802 PyErr_Clear();
3803}
3804
3805 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02003806set_ref_in_py(const int copyID)
3807{
3808 pylinkedlist_T *cur;
3809 dict_T *dd;
3810 list_T *ll;
3811
3812 if (lastdict != NULL)
3813 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3814 {
3815 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3816 if (dd->dv_copyID != copyID)
3817 {
3818 dd->dv_copyID = copyID;
3819 set_ref_in_ht(&dd->dv_hashtab, copyID);
3820 }
3821 }
3822
3823 if (lastlist != NULL)
3824 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3825 {
3826 ll = ((ListObject *) (cur->pll_obj))->list;
3827 if (ll->lv_copyID != copyID)
3828 {
3829 ll->lv_copyID = copyID;
3830 set_ref_in_list(ll, copyID);
3831 }
3832 }
3833}
3834
3835 static int
3836set_string_copy(char_u *str, typval_T *tv)
3837{
3838 tv->vval.v_string = vim_strsave(str);
3839 if (tv->vval.v_string == NULL)
3840 {
3841 PyErr_NoMemory();
3842 return -1;
3843 }
3844 return 0;
3845}
3846
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003847 static int
3848pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3849{
3850 dict_T *d;
3851 char_u *key;
3852 dictitem_T *di;
3853 PyObject *keyObject;
3854 PyObject *valObject;
3855 Py_ssize_t iter = 0;
3856
3857 d = dict_alloc();
3858 if (d == NULL)
3859 {
3860 PyErr_NoMemory();
3861 return -1;
3862 }
3863
3864 tv->v_type = VAR_DICT;
3865 tv->vval.v_dict = d;
3866
3867 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
3868 {
3869 DICTKEY_DECL
3870
3871 if (keyObject == NULL)
3872 return -1;
3873 if (valObject == NULL)
3874 return -1;
3875
3876 DICTKEY_GET_NOTEMPTY(-1)
3877
3878 di = dictitem_alloc(key);
3879
3880 DICTKEY_UNREF
3881
3882 if (di == NULL)
3883 {
3884 PyErr_NoMemory();
3885 return -1;
3886 }
3887 di->di_tv.v_lock = 0;
3888
3889 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3890 {
3891 vim_free(di);
3892 return -1;
3893 }
3894 if (dict_add(d, di) == FAIL)
3895 {
3896 vim_free(di);
3897 PyErr_SetVim(_("failed to add key to dictionary"));
3898 return -1;
3899 }
3900 }
3901 return 0;
3902}
3903
3904 static int
3905pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3906{
3907 dict_T *d;
3908 char_u *key;
3909 dictitem_T *di;
3910 PyObject *list;
3911 PyObject *litem;
3912 PyObject *keyObject;
3913 PyObject *valObject;
3914 Py_ssize_t lsize;
3915
3916 d = dict_alloc();
3917 if (d == NULL)
3918 {
3919 PyErr_NoMemory();
3920 return -1;
3921 }
3922
3923 tv->v_type = VAR_DICT;
3924 tv->vval.v_dict = d;
3925
3926 list = PyMapping_Items(obj);
3927 if (list == NULL)
3928 return -1;
3929 lsize = PyList_Size(list);
3930 while (lsize--)
3931 {
3932 DICTKEY_DECL
3933
3934 litem = PyList_GetItem(list, lsize);
3935 if (litem == NULL)
3936 {
3937 Py_DECREF(list);
3938 return -1;
3939 }
3940
3941 keyObject = PyTuple_GetItem(litem, 0);
3942 if (keyObject == NULL)
3943 {
3944 Py_DECREF(list);
3945 Py_DECREF(litem);
3946 return -1;
3947 }
3948
3949 DICTKEY_GET_NOTEMPTY(-1)
3950
3951 valObject = PyTuple_GetItem(litem, 1);
3952 if (valObject == NULL)
3953 {
3954 Py_DECREF(list);
3955 Py_DECREF(litem);
3956 return -1;
3957 }
3958
3959 di = dictitem_alloc(key);
3960
3961 DICTKEY_UNREF
3962
3963 if (di == NULL)
3964 {
3965 Py_DECREF(list);
3966 Py_DECREF(litem);
3967 PyErr_NoMemory();
3968 return -1;
3969 }
3970 di->di_tv.v_lock = 0;
3971
3972 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3973 {
3974 vim_free(di);
3975 Py_DECREF(list);
3976 Py_DECREF(litem);
3977 return -1;
3978 }
3979 if (dict_add(d, di) == FAIL)
3980 {
3981 vim_free(di);
3982 Py_DECREF(list);
3983 Py_DECREF(litem);
3984 PyErr_SetVim(_("failed to add key to dictionary"));
3985 return -1;
3986 }
3987 Py_DECREF(litem);
3988 }
3989 Py_DECREF(list);
3990 return 0;
3991}
3992
3993 static int
3994pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3995{
3996 list_T *l;
3997
3998 l = list_alloc();
3999 if (l == NULL)
4000 {
4001 PyErr_NoMemory();
4002 return -1;
4003 }
4004
4005 tv->v_type = VAR_LIST;
4006 tv->vval.v_list = l;
4007
4008 if (list_py_concat(l, obj, lookupDict) == -1)
4009 return -1;
4010
4011 return 0;
4012}
4013
4014 static int
4015pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
4016{
4017 PyObject *iterator = PyObject_GetIter(obj);
4018 PyObject *item;
4019 list_T *l;
4020 listitem_T *li;
4021
4022 l = list_alloc();
4023
4024 if (l == NULL)
4025 {
4026 PyErr_NoMemory();
4027 return -1;
4028 }
4029
4030 tv->vval.v_list = l;
4031 tv->v_type = VAR_LIST;
4032
4033
4034 if (iterator == NULL)
4035 return -1;
4036
4037 while ((item = PyIter_Next(obj)))
4038 {
4039 li = listitem_alloc();
4040 if (li == NULL)
4041 {
4042 PyErr_NoMemory();
4043 return -1;
4044 }
4045 li->li_tv.v_lock = 0;
4046
4047 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
4048 return -1;
4049
4050 list_append(l, li);
4051
4052 Py_DECREF(item);
4053 }
4054
4055 Py_DECREF(iterator);
4056 return 0;
4057}
4058
Bram Moolenaardb913952012-06-29 12:54:53 +02004059typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4060
4061 static int
4062convert_dl(PyObject *obj, typval_T *tv,
4063 pytotvfunc py_to_tv, PyObject *lookupDict)
4064{
4065 PyObject *capsule;
4066 char hexBuf[sizeof(void *) * 2 + 3];
4067
4068 sprintf(hexBuf, "%p", obj);
4069
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004070# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004071 capsule = PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004072# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004073 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004074# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004075 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004076 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004077# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004078 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004079# else
4080 capsule = PyCObject_FromVoidPtr(tv, NULL);
4081# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004082 PyDict_SetItemString(lookupDict, hexBuf, capsule);
4083 Py_DECREF(capsule);
4084 if (py_to_tv(obj, tv, lookupDict) == -1)
4085 {
4086 tv->v_type = VAR_UNKNOWN;
4087 return -1;
4088 }
4089 /* As we are not using copy_tv which increments reference count we must
4090 * do it ourself. */
4091 switch(tv->v_type)
4092 {
4093 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4094 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4095 }
4096 }
4097 else
4098 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004099 typval_T *v;
4100
4101# ifdef PY_USE_CAPSULE
4102 v = PyCapsule_GetPointer(capsule, NULL);
4103# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004104 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004105# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004106 copy_tv(v, tv);
4107 }
4108 return 0;
4109}
4110
4111 static int
4112ConvertFromPyObject(PyObject *obj, typval_T *tv)
4113{
4114 PyObject *lookup_dict;
4115 int r;
4116
4117 lookup_dict = PyDict_New();
4118 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4119 Py_DECREF(lookup_dict);
4120 return r;
4121}
4122
4123 static int
4124_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
4125{
4126 if (obj->ob_type == &DictionaryType)
4127 {
4128 tv->v_type = VAR_DICT;
4129 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4130 ++tv->vval.v_dict->dv_refcount;
4131 }
4132 else if (obj->ob_type == &ListType)
4133 {
4134 tv->v_type = VAR_LIST;
4135 tv->vval.v_list = (((ListObject *)(obj))->list);
4136 ++tv->vval.v_list->lv_refcount;
4137 }
4138 else if (obj->ob_type == &FunctionType)
4139 {
4140 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4141 return -1;
4142
4143 tv->v_type = VAR_FUNC;
4144 func_ref(tv->vval.v_string);
4145 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004146 else if (PyBytes_Check(obj))
4147 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004148 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004149
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004150 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4151 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004152 if (result == NULL)
4153 return -1;
4154
4155 if (set_string_copy(result, tv) == -1)
4156 return -1;
4157
4158 tv->v_type = VAR_STRING;
4159 }
4160 else if (PyUnicode_Check(obj))
4161 {
4162 PyObject *bytes;
4163 char_u *result;
4164
Bram Moolenaardb913952012-06-29 12:54:53 +02004165 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4166 if (bytes == NULL)
4167 return -1;
4168
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004169 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4170 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004171 if (result == NULL)
4172 return -1;
4173
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004174 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004175 {
4176 Py_XDECREF(bytes);
4177 return -1;
4178 }
4179 Py_XDECREF(bytes);
4180
4181 tv->v_type = VAR_STRING;
4182 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004183#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004184 else if (PyInt_Check(obj))
4185 {
4186 tv->v_type = VAR_NUMBER;
4187 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4188 }
4189#endif
4190 else if (PyLong_Check(obj))
4191 {
4192 tv->v_type = VAR_NUMBER;
4193 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4194 }
4195 else if (PyDict_Check(obj))
4196 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
4197#ifdef FEAT_FLOAT
4198 else if (PyFloat_Check(obj))
4199 {
4200 tv->v_type = VAR_FLOAT;
4201 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4202 }
4203#endif
4204 else if (PyIter_Check(obj))
4205 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
4206 else if (PySequence_Check(obj))
4207 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
4208 else if (PyMapping_Check(obj))
4209 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
4210 else
4211 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004212 PyErr_SetString(PyExc_TypeError,
4213 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004214 return -1;
4215 }
4216 return 0;
4217}
4218
4219 static PyObject *
4220ConvertToPyObject(typval_T *tv)
4221{
4222 if (tv == NULL)
4223 {
4224 PyErr_SetVim(_("NULL reference passed"));
4225 return NULL;
4226 }
4227 switch (tv->v_type)
4228 {
4229 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004230 return PyBytes_FromString(tv->vval.v_string == NULL
4231 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004232 case VAR_NUMBER:
4233 return PyLong_FromLong((long) tv->vval.v_number);
4234#ifdef FEAT_FLOAT
4235 case VAR_FLOAT:
4236 return PyFloat_FromDouble((double) tv->vval.v_float);
4237#endif
4238 case VAR_LIST:
4239 return ListNew(tv->vval.v_list);
4240 case VAR_DICT:
4241 return DictionaryNew(tv->vval.v_dict);
4242 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004243 return FunctionNew(tv->vval.v_string == NULL
4244 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004245 case VAR_UNKNOWN:
4246 Py_INCREF(Py_None);
4247 return Py_None;
4248 default:
4249 PyErr_SetVim(_("internal error: invalid value type"));
4250 return NULL;
4251 }
4252}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004253
4254typedef struct
4255{
4256 PyObject_HEAD
4257} CurrentObject;
4258static PyTypeObject CurrentType;
4259
4260 static void
4261init_structs(void)
4262{
4263 vim_memset(&OutputType, 0, sizeof(OutputType));
4264 OutputType.tp_name = "vim.message";
4265 OutputType.tp_basicsize = sizeof(OutputObject);
4266 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4267 OutputType.tp_doc = "vim message object";
4268 OutputType.tp_methods = OutputMethods;
4269#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004270 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4271 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004272 OutputType.tp_alloc = call_PyType_GenericAlloc;
4273 OutputType.tp_new = call_PyType_GenericNew;
4274 OutputType.tp_free = call_PyObject_Free;
4275#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004276 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4277 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004278#endif
4279
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004280 vim_memset(&IterType, 0, sizeof(IterType));
4281 IterType.tp_name = "vim.iter";
4282 IterType.tp_basicsize = sizeof(IterObject);
4283 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
4284 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004285 IterType.tp_iter = (getiterfunc)IterIter;
4286 IterType.tp_iternext = (iternextfunc)IterNext;
4287 IterType.tp_dealloc = (destructor)IterDestructor;
4288 IterType.tp_traverse = (traverseproc)IterTraverse;
4289 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004290
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004291 vim_memset(&BufferType, 0, sizeof(BufferType));
4292 BufferType.tp_name = "vim.buffer";
4293 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004294 BufferType.tp_dealloc = (destructor)BufferDestructor;
4295 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004296 BufferType.tp_as_sequence = &BufferAsSeq;
4297 BufferType.tp_as_mapping = &BufferAsMapping;
4298 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4299 BufferType.tp_doc = "vim buffer object";
4300 BufferType.tp_methods = BufferMethods;
4301#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004302 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004303 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004304 BufferType.tp_alloc = call_PyType_GenericAlloc;
4305 BufferType.tp_new = call_PyType_GenericNew;
4306 BufferType.tp_free = call_PyObject_Free;
4307#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004308 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004309 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004310#endif
4311
4312 vim_memset(&WindowType, 0, sizeof(WindowType));
4313 WindowType.tp_name = "vim.window";
4314 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004315 WindowType.tp_dealloc = (destructor)WindowDestructor;
4316 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004317 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
4318 WindowType.tp_doc = "vim Window object";
4319 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004320 WindowType.tp_traverse = (traverseproc)WindowTraverse;
4321 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004322#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004323 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
4324 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004325 WindowType.tp_alloc = call_PyType_GenericAlloc;
4326 WindowType.tp_new = call_PyType_GenericNew;
4327 WindowType.tp_free = call_PyObject_Free;
4328#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004329 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
4330 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004331#endif
4332
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004333 vim_memset(&TabPageType, 0, sizeof(TabPageType));
4334 TabPageType.tp_name = "vim.tabpage";
4335 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004336 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
4337 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004338 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
4339 TabPageType.tp_doc = "vim tab page object";
4340 TabPageType.tp_methods = TabPageMethods;
4341#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004342 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004343 TabPageType.tp_alloc = call_PyType_GenericAlloc;
4344 TabPageType.tp_new = call_PyType_GenericNew;
4345 TabPageType.tp_free = call_PyObject_Free;
4346#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004347 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004348#endif
4349
Bram Moolenaardfa38d42013-05-15 13:38:47 +02004350 vim_memset(&BufMapType, 0, sizeof(BufMapType));
4351 BufMapType.tp_name = "vim.bufferlist";
4352 BufMapType.tp_basicsize = sizeof(BufMapObject);
4353 BufMapType.tp_as_mapping = &BufMapAsMapping;
4354 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004355 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004356 BufferType.tp_doc = "vim buffer list";
4357
4358 vim_memset(&WinListType, 0, sizeof(WinListType));
4359 WinListType.tp_name = "vim.windowlist";
4360 WinListType.tp_basicsize = sizeof(WinListType);
4361 WinListType.tp_as_sequence = &WinListAsSeq;
4362 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
4363 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004364 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004365
4366 vim_memset(&TabListType, 0, sizeof(TabListType));
4367 TabListType.tp_name = "vim.tabpagelist";
4368 TabListType.tp_basicsize = sizeof(TabListType);
4369 TabListType.tp_as_sequence = &TabListAsSeq;
4370 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
4371 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004372
4373 vim_memset(&RangeType, 0, sizeof(RangeType));
4374 RangeType.tp_name = "vim.range";
4375 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004376 RangeType.tp_dealloc = (destructor)RangeDestructor;
4377 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004378 RangeType.tp_as_sequence = &RangeAsSeq;
4379 RangeType.tp_as_mapping = &RangeAsMapping;
4380 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
4381 RangeType.tp_doc = "vim Range object";
4382 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004383 RangeType.tp_traverse = (traverseproc)RangeTraverse;
4384 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004385#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004386 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004387 RangeType.tp_alloc = call_PyType_GenericAlloc;
4388 RangeType.tp_new = call_PyType_GenericNew;
4389 RangeType.tp_free = call_PyObject_Free;
4390#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004391 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004392#endif
4393
4394 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4395 CurrentType.tp_name = "vim.currentdata";
4396 CurrentType.tp_basicsize = sizeof(CurrentObject);
4397 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4398 CurrentType.tp_doc = "vim current object";
4399#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004400 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
4401 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004402#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004403 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
4404 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004405#endif
4406
4407 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4408 DictionaryType.tp_name = "vim.dictionary";
4409 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004410 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004411 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4412 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4413 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4414 DictionaryType.tp_methods = DictionaryMethods;
4415#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004416 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
4417 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004418#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004419 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
4420 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004421#endif
4422
4423 vim_memset(&ListType, 0, sizeof(ListType));
4424 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004425 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004426 ListType.tp_basicsize = sizeof(ListObject);
4427 ListType.tp_as_sequence = &ListAsSeq;
4428 ListType.tp_as_mapping = &ListAsMapping;
4429 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4430 ListType.tp_doc = "list pushing modifications to vim structure";
4431 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004432 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004433#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004434 ListType.tp_getattro = (getattrofunc)ListGetattro;
4435 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004436#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004437 ListType.tp_getattr = (getattrfunc)ListGetattr;
4438 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004439#endif
4440
4441 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004442 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004443 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004444 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
4445 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004446 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4447 FunctionType.tp_doc = "object that calls vim function";
4448 FunctionType.tp_methods = FunctionMethods;
4449#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004450 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004451#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004452 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004453#endif
4454
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004455 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4456 OptionsType.tp_name = "vim.options";
4457 OptionsType.tp_basicsize = sizeof(OptionsObject);
4458 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4459 OptionsType.tp_doc = "object for manipulating options";
4460 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004461 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
4462 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
4463 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004464
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004465#if PY_MAJOR_VERSION >= 3
4466 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4467 vimmodule.m_name = "vim";
4468 vimmodule.m_doc = "Vim Python interface\n";
4469 vimmodule.m_size = -1;
4470 vimmodule.m_methods = VimMethods;
4471#endif
4472}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004473
4474#define PYTYPE_READY(type) \
4475 if (PyType_Ready(&type)) \
4476 return -1;
4477
4478 static int
4479init_types()
4480{
4481 PYTYPE_READY(IterType);
4482 PYTYPE_READY(BufferType);
4483 PYTYPE_READY(RangeType);
4484 PYTYPE_READY(WindowType);
4485 PYTYPE_READY(TabPageType);
4486 PYTYPE_READY(BufMapType);
4487 PYTYPE_READY(WinListType);
4488 PYTYPE_READY(TabListType);
4489 PYTYPE_READY(CurrentType);
4490 PYTYPE_READY(DictionaryType);
4491 PYTYPE_READY(ListType);
4492 PYTYPE_READY(FunctionType);
4493 PYTYPE_READY(OptionsType);
4494 PYTYPE_READY(OutputType);
4495 return 0;
4496}
4497
4498static BufMapObject TheBufferMap =
4499{
4500 PyObject_HEAD_INIT(&BufMapType)
4501};
4502
4503static WinListObject TheWindowList =
4504{
4505 PyObject_HEAD_INIT(&WinListType)
4506 NULL
4507};
4508
4509static CurrentObject TheCurrent =
4510{
4511 PyObject_HEAD_INIT(&CurrentType)
4512};
4513
4514static TabListObject TheTabPageList =
4515{
4516 PyObject_HEAD_INIT(&TabListType)
4517};
4518
4519static struct numeric_constant {
4520 char *name;
4521 int value;
4522} numeric_constants[] = {
4523 {"VAR_LOCKED", VAR_LOCKED},
4524 {"VAR_FIXED", VAR_FIXED},
4525 {"VAR_SCOPE", VAR_SCOPE},
4526 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
4527};
4528
4529static struct object_constant {
4530 char *name;
4531 PyObject *value;
4532} object_constants[] = {
4533 {"buffers", (PyObject *)(void *)&TheBufferMap},
4534 {"windows", (PyObject *)(void *)&TheWindowList},
4535 {"tabpages", (PyObject *)(void *)&TheTabPageList},
4536 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02004537
4538 {"Buffer", (PyObject *)&BufferType},
4539 {"Range", (PyObject *)&RangeType},
4540 {"Window", (PyObject *)&WindowType},
4541 {"TabPage", (PyObject *)&TabPageType},
4542 {"Dictionary", (PyObject *)&DictionaryType},
4543 {"List", (PyObject *)&ListType},
4544 {"Function", (PyObject *)&FunctionType},
4545 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004546};
4547
4548typedef int (*object_adder)(PyObject *, const char *, PyObject *);
4549
4550#define ADD_OBJECT(m, name, obj) \
4551 if (add_object(m, name, obj)) \
4552 return -1;
4553
4554#define ADD_CHECKED_OBJECT(m, name, obj) \
4555 { \
4556 PyObject *value = obj; \
4557 if (!value) \
4558 return -1; \
4559 ADD_OBJECT(m, name, value); \
4560 }
4561
4562 static int
4563populate_module(PyObject *m, object_adder add_object)
4564{
4565 int i;
4566
4567 for (i = 0; i < (int)(sizeof(numeric_constants)
4568 / sizeof(struct numeric_constant));
4569 ++i)
4570 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
4571 PyInt_FromLong(numeric_constants[i].value));
4572
4573 for (i = 0; i < (int)(sizeof(object_constants)
4574 / sizeof(struct object_constant));
4575 ++i)
4576 {
4577 PyObject *value;
4578
4579 value = object_constants[i].value;
4580 Py_INCREF(value);
4581 ADD_OBJECT(m, object_constants[i].name, value);
4582 }
4583
4584 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
4585 return -1;
4586 ADD_OBJECT(m, "error", VimError);
4587
4588 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict));
4589 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
4590 ADD_CHECKED_OBJECT(m, "options",
4591 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
4592 return 0;
4593}