blob: 5b457420797abd340a09809bd8324813053588e8 [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 Moolenaar432b09c2013-05-29 22:26:18 +0200445 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200446 ? "" : (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);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200454 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200455 }
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);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200462 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200463 }
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 Moolenaar432b09c2013-05-29 22:26:18 +02003259 return PyString_FromString((self->buf->b_ffname == NULL
3260 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003261 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003262 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003263 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003264 return DictionaryNew(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003265 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003266 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3267 (PyObject *) self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003268 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003269 return Py_BuildValue("[sssss]", "name", "number", "vars", "options",
3270 "valid");
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003271 else
3272 return NULL;
3273}
3274
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003275 static int
3276BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3277{
3278 if (CheckBuffer(self))
3279 return -1;
3280
3281 if (strcmp(name, "name") == 0)
3282 {
3283 char_u *val;
3284 aco_save_T aco;
3285 int r;
3286 PyObject *todecref;
3287
3288 if (!(val = StringToChars(valObject, &todecref)))
3289 return -1;
3290
3291 VimTryStart();
3292 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3293 aucmd_prepbuf(&aco, self->buf);
3294 r = rename_buffer(val);
3295 aucmd_restbuf(&aco);
3296 Py_XDECREF(todecref);
3297 if (VimTryEnd())
3298 return -1;
3299
3300 if (r == FAIL)
3301 {
3302 PyErr_SetVim(_("failed to rename buffer"));
3303 return -1;
3304 }
3305 return 0;
3306 }
3307 else
3308 {
3309 PyErr_SetString(PyExc_AttributeError, name);
3310 return -1;
3311 }
3312}
3313
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003314 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003315BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003316{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003317 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003318}
3319
3320 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003321BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003322{
3323 pos_T *posp;
3324 char *pmark;
3325 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003326 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003327
Bram Moolenaard6e39182013-05-21 18:30:34 +02003328 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003329 return NULL;
3330
3331 if (!PyArg_ParseTuple(args, "s", &pmark))
3332 return NULL;
3333 mark = *pmark;
3334
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003335 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02003336 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003337 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003338 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003339 if (VimTryEnd())
3340 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003341
3342 if (posp == NULL)
3343 {
3344 PyErr_SetVim(_("invalid mark name"));
3345 return NULL;
3346 }
3347
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003348 if (posp->lnum <= 0)
3349 {
3350 /* Or raise an error? */
3351 Py_INCREF(Py_None);
3352 return Py_None;
3353 }
3354
3355 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3356}
3357
3358 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003359BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003360{
3361 PyInt start;
3362 PyInt end;
3363
Bram Moolenaard6e39182013-05-21 18:30:34 +02003364 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003365 return NULL;
3366
3367 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3368 return NULL;
3369
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003371}
3372
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003373 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003374BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003375{
3376 static char repr[100];
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003377
Bram Moolenaard6e39182013-05-21 18:30:34 +02003378 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003379 {
3380 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3381 return PyString_FromString(repr);
3382 }
3383 else
3384 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003385 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003386 PyInt len;
3387
3388 if (name == NULL)
3389 name = "";
3390 len = strlen(name);
3391
3392 if (len > 35)
3393 name = name + (35 - len);
3394
3395 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3396
3397 return PyString_FromString(repr);
3398 }
3399}
3400
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003401static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003402 /* name, function, calling, documentation */
3403 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
3404 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
3405 {"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 +01003406#if PY_VERSION_HEX >= 0x03000000
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003407 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, "List buffer attributes" },
Bram Moolenaar7f85d292012-02-04 20:17:26 +01003408#endif
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003409 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003410};
3411
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003412/*
3413 * Buffer list object - Implementation
3414 */
3415
3416static PyTypeObject BufMapType;
3417
3418typedef struct
3419{
3420 PyObject_HEAD
3421} BufMapObject;
3422
3423 static PyInt
3424BufMapLength(PyObject *self UNUSED)
3425{
3426 buf_T *b = firstbuf;
3427 PyInt n = 0;
3428
3429 while (b)
3430 {
3431 ++n;
3432 b = b->b_next;
3433 }
3434
3435 return n;
3436}
3437
3438 static PyObject *
3439BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3440{
3441 buf_T *b;
3442 int bnr;
3443
3444#if PY_MAJOR_VERSION < 3
3445 if (PyInt_Check(keyObject))
3446 bnr = PyInt_AsLong(keyObject);
3447 else
3448#endif
3449 if (PyLong_Check(keyObject))
3450 bnr = PyLong_AsLong(keyObject);
3451 else
3452 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003453 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003454 return NULL;
3455 }
3456
3457 b = buflist_findnr(bnr);
3458
3459 if (b)
3460 return BufferNew(b);
3461 else
3462 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003463 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003464 return NULL;
3465 }
3466}
3467
3468 static void
3469BufMapIterDestruct(PyObject *buffer)
3470{
3471 /* Iteration was stopped before all buffers were processed */
3472 if (buffer)
3473 {
3474 Py_DECREF(buffer);
3475 }
3476}
3477
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003478 static int
3479BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3480{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003481 if (buffer)
3482 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003483 return 0;
3484}
3485
3486 static int
3487BufMapIterClear(PyObject **buffer)
3488{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003489 if (*buffer)
3490 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003491 return 0;
3492}
3493
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003494 static PyObject *
3495BufMapIterNext(PyObject **buffer)
3496{
3497 PyObject *next;
3498 PyObject *r;
3499
3500 if (!*buffer)
3501 return NULL;
3502
3503 r = *buffer;
3504
3505 if (CheckBuffer((BufferObject *)(r)))
3506 {
3507 *buffer = NULL;
3508 return NULL;
3509 }
3510
3511 if (!((BufferObject *)(r))->buf->b_next)
3512 next = NULL;
3513 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3514 return NULL;
3515 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02003516 /* Do not increment reference: we no longer hold it (decref), but whoever
3517 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003518 return r;
3519}
3520
3521 static PyObject *
3522BufMapIter(PyObject *self UNUSED)
3523{
3524 PyObject *buffer;
3525
3526 buffer = BufferNew(firstbuf);
3527 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003528 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3529 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003530}
3531
3532static PyMappingMethods BufMapAsMapping = {
3533 (lenfunc) BufMapLength,
3534 (binaryfunc) BufMapItem,
3535 (objobjargproc) 0,
3536};
3537
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003538/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003539 */
3540
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003541 static PyObject *
3542CurrentGetattr(PyObject *self UNUSED, char *name)
3543{
3544 if (strcmp(name, "buffer") == 0)
3545 return (PyObject *)BufferNew(curbuf);
3546 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003547 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003548 else if (strcmp(name, "tabpage") == 0)
3549 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003550 else if (strcmp(name, "line") == 0)
3551 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3552 else if (strcmp(name, "range") == 0)
3553 return RangeNew(curbuf, RangeStart, RangeEnd);
3554 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003555 return Py_BuildValue("[sssss]", "buffer", "window", "line", "range",
3556 "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003557 else
3558 {
3559 PyErr_SetString(PyExc_AttributeError, name);
3560 return NULL;
3561 }
3562}
3563
3564 static int
3565CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3566{
3567 if (strcmp(name, "line") == 0)
3568 {
3569 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3570 return -1;
3571
3572 return 0;
3573 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003574 else if (strcmp(name, "buffer") == 0)
3575 {
3576 int count;
3577
3578 if (value->ob_type != &BufferType)
3579 {
3580 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3581 return -1;
3582 }
3583
3584 if (CheckBuffer((BufferObject *)(value)))
3585 return -1;
3586 count = ((BufferObject *)(value))->buf->b_fnum;
3587
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003588 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003589 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3590 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003591 if (VimTryEnd())
3592 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003593 PyErr_SetVim(_("failed to switch to given buffer"));
3594 return -1;
3595 }
3596
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003597 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003598 }
3599 else if (strcmp(name, "window") == 0)
3600 {
3601 int count;
3602
3603 if (value->ob_type != &WindowType)
3604 {
3605 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3606 return -1;
3607 }
3608
3609 if (CheckWindow((WindowObject *)(value)))
3610 return -1;
3611 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3612
3613 if (!count)
3614 {
3615 PyErr_SetString(PyExc_ValueError,
3616 _("failed to find window in the current tab page"));
3617 return -1;
3618 }
3619
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003620 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003621 win_goto(((WindowObject *)(value))->win);
3622 if (((WindowObject *)(value))->win != curwin)
3623 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003624 if (VimTryEnd())
3625 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003626 PyErr_SetString(PyExc_RuntimeError,
3627 _("did not switch to the specified window"));
3628 return -1;
3629 }
3630
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003631 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003632 }
3633 else if (strcmp(name, "tabpage") == 0)
3634 {
3635 if (value->ob_type != &TabPageType)
3636 {
3637 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3638 return -1;
3639 }
3640
3641 if (CheckTabPage((TabPageObject *)(value)))
3642 return -1;
3643
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003644 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003645 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3646 if (((TabPageObject *)(value))->tab != curtab)
3647 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003648 if (VimTryEnd())
3649 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003650 PyErr_SetString(PyExc_RuntimeError,
3651 _("did not switch to the specified tab page"));
3652 return -1;
3653 }
3654
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003655 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003656 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003657 else
3658 {
3659 PyErr_SetString(PyExc_AttributeError, name);
3660 return -1;
3661 }
3662}
3663
Bram Moolenaardb913952012-06-29 12:54:53 +02003664 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003665init_range_cmd(exarg_T *eap)
3666{
3667 RangeStart = eap->line1;
3668 RangeEnd = eap->line2;
3669}
3670
3671 static void
3672init_range_eval(typval_T *rettv UNUSED)
3673{
3674 RangeStart = (PyInt) curwin->w_cursor.lnum;
3675 RangeEnd = RangeStart;
3676}
3677
3678 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003679run_cmd(const char *cmd, void *arg UNUSED
3680#ifdef PY_CAN_RECURSE
3681 , PyGILState_STATE *pygilstate UNUSED
3682#endif
3683 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003684{
3685 PyRun_SimpleString((char *) cmd);
3686}
3687
3688static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
3689static int code_hdr_len = 30;
3690
3691 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003692run_do(const char *cmd, void *arg UNUSED
3693#ifdef PY_CAN_RECURSE
3694 , PyGILState_STATE *pygilstate
3695#endif
3696 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003697{
3698 PyInt lnum;
3699 size_t len;
3700 char *code;
3701 int status;
3702 PyObject *pyfunc, *pymain;
3703
Bram Moolenaar4ac66762013-05-28 22:31:46 +02003704 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003705 {
3706 EMSG(_("cannot save undo information"));
3707 return;
3708 }
3709
3710 len = code_hdr_len + STRLEN(cmd);
3711 code = PyMem_New(char, len + 1);
3712 memcpy(code, code_hdr, code_hdr_len);
3713 STRCPY(code + code_hdr_len, cmd);
3714 status = PyRun_SimpleString(code);
3715 PyMem_Free(code);
3716
3717 if (status)
3718 {
3719 EMSG(_("failed to run the code"));
3720 return;
3721 }
3722
3723 status = 0;
3724 pymain = PyImport_AddModule("__main__");
3725 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003726#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003727 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003728#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003729
3730 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
3731 {
3732 PyObject *line, *linenr, *ret;
3733
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003734#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003735 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003736#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003737 if (!(line = GetBufferLine(curbuf, lnum)))
3738 goto err;
3739 if (!(linenr = PyInt_FromLong((long) lnum)))
3740 {
3741 Py_DECREF(line);
3742 goto err;
3743 }
3744 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
3745 Py_DECREF(line);
3746 Py_DECREF(linenr);
3747 if (!ret)
3748 goto err;
3749
3750 if (ret != Py_None)
3751 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
3752 goto err;
3753
3754 Py_XDECREF(ret);
3755 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003756#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003757 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003758#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003759 }
3760 goto out;
3761err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003762#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003763 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003764#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003765 PyErr_PrintEx(0);
3766 PythonIO_Flush();
3767 status = 1;
3768out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003769#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003770 if (!status)
3771 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003772#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003773 Py_DECREF(pyfunc);
3774 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
3775 if (status)
3776 return;
3777 check_cursor();
3778 update_curbuf(NOT_VALID);
3779}
3780
3781 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003782run_eval(const char *cmd, typval_T *rettv
3783#ifdef PY_CAN_RECURSE
3784 , PyGILState_STATE *pygilstate UNUSED
3785#endif
3786 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003787{
3788 PyObject *r;
3789
3790 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
3791 if (r == NULL)
3792 {
3793 if (PyErr_Occurred() && !msg_silent)
3794 PyErr_PrintEx(0);
3795 EMSG(_("E858: Eval did not return a valid python object"));
3796 }
3797 else
3798 {
3799 if (ConvertFromPyObject(r, rettv) == -1)
3800 EMSG(_("E859: Failed to convert returned python object to vim value"));
3801 Py_DECREF(r);
3802 }
3803 PyErr_Clear();
3804}
3805
3806 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02003807set_ref_in_py(const int copyID)
3808{
3809 pylinkedlist_T *cur;
3810 dict_T *dd;
3811 list_T *ll;
3812
3813 if (lastdict != NULL)
3814 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3815 {
3816 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3817 if (dd->dv_copyID != copyID)
3818 {
3819 dd->dv_copyID = copyID;
3820 set_ref_in_ht(&dd->dv_hashtab, copyID);
3821 }
3822 }
3823
3824 if (lastlist != NULL)
3825 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3826 {
3827 ll = ((ListObject *) (cur->pll_obj))->list;
3828 if (ll->lv_copyID != copyID)
3829 {
3830 ll->lv_copyID = copyID;
3831 set_ref_in_list(ll, copyID);
3832 }
3833 }
3834}
3835
3836 static int
3837set_string_copy(char_u *str, typval_T *tv)
3838{
3839 tv->vval.v_string = vim_strsave(str);
3840 if (tv->vval.v_string == NULL)
3841 {
3842 PyErr_NoMemory();
3843 return -1;
3844 }
3845 return 0;
3846}
3847
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003848 static int
3849pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3850{
3851 dict_T *d;
3852 char_u *key;
3853 dictitem_T *di;
3854 PyObject *keyObject;
3855 PyObject *valObject;
3856 Py_ssize_t iter = 0;
3857
3858 d = dict_alloc();
3859 if (d == NULL)
3860 {
3861 PyErr_NoMemory();
3862 return -1;
3863 }
3864
3865 tv->v_type = VAR_DICT;
3866 tv->vval.v_dict = d;
3867
3868 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
3869 {
3870 DICTKEY_DECL
3871
3872 if (keyObject == NULL)
3873 return -1;
3874 if (valObject == NULL)
3875 return -1;
3876
3877 DICTKEY_GET_NOTEMPTY(-1)
3878
3879 di = dictitem_alloc(key);
3880
3881 DICTKEY_UNREF
3882
3883 if (di == NULL)
3884 {
3885 PyErr_NoMemory();
3886 return -1;
3887 }
3888 di->di_tv.v_lock = 0;
3889
3890 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3891 {
3892 vim_free(di);
3893 return -1;
3894 }
3895 if (dict_add(d, di) == FAIL)
3896 {
3897 vim_free(di);
3898 PyErr_SetVim(_("failed to add key to dictionary"));
3899 return -1;
3900 }
3901 }
3902 return 0;
3903}
3904
3905 static int
3906pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3907{
3908 dict_T *d;
3909 char_u *key;
3910 dictitem_T *di;
3911 PyObject *list;
3912 PyObject *litem;
3913 PyObject *keyObject;
3914 PyObject *valObject;
3915 Py_ssize_t lsize;
3916
3917 d = dict_alloc();
3918 if (d == NULL)
3919 {
3920 PyErr_NoMemory();
3921 return -1;
3922 }
3923
3924 tv->v_type = VAR_DICT;
3925 tv->vval.v_dict = d;
3926
3927 list = PyMapping_Items(obj);
3928 if (list == NULL)
3929 return -1;
3930 lsize = PyList_Size(list);
3931 while (lsize--)
3932 {
3933 DICTKEY_DECL
3934
3935 litem = PyList_GetItem(list, lsize);
3936 if (litem == NULL)
3937 {
3938 Py_DECREF(list);
3939 return -1;
3940 }
3941
3942 keyObject = PyTuple_GetItem(litem, 0);
3943 if (keyObject == NULL)
3944 {
3945 Py_DECREF(list);
3946 Py_DECREF(litem);
3947 return -1;
3948 }
3949
3950 DICTKEY_GET_NOTEMPTY(-1)
3951
3952 valObject = PyTuple_GetItem(litem, 1);
3953 if (valObject == NULL)
3954 {
3955 Py_DECREF(list);
3956 Py_DECREF(litem);
3957 return -1;
3958 }
3959
3960 di = dictitem_alloc(key);
3961
3962 DICTKEY_UNREF
3963
3964 if (di == NULL)
3965 {
3966 Py_DECREF(list);
3967 Py_DECREF(litem);
3968 PyErr_NoMemory();
3969 return -1;
3970 }
3971 di->di_tv.v_lock = 0;
3972
3973 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3974 {
3975 vim_free(di);
3976 Py_DECREF(list);
3977 Py_DECREF(litem);
3978 return -1;
3979 }
3980 if (dict_add(d, di) == FAIL)
3981 {
3982 vim_free(di);
3983 Py_DECREF(list);
3984 Py_DECREF(litem);
3985 PyErr_SetVim(_("failed to add key to dictionary"));
3986 return -1;
3987 }
3988 Py_DECREF(litem);
3989 }
3990 Py_DECREF(list);
3991 return 0;
3992}
3993
3994 static int
3995pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3996{
3997 list_T *l;
3998
3999 l = list_alloc();
4000 if (l == NULL)
4001 {
4002 PyErr_NoMemory();
4003 return -1;
4004 }
4005
4006 tv->v_type = VAR_LIST;
4007 tv->vval.v_list = l;
4008
4009 if (list_py_concat(l, obj, lookupDict) == -1)
4010 return -1;
4011
4012 return 0;
4013}
4014
4015 static int
4016pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
4017{
4018 PyObject *iterator = PyObject_GetIter(obj);
4019 PyObject *item;
4020 list_T *l;
4021 listitem_T *li;
4022
4023 l = list_alloc();
4024
4025 if (l == NULL)
4026 {
4027 PyErr_NoMemory();
4028 return -1;
4029 }
4030
4031 tv->vval.v_list = l;
4032 tv->v_type = VAR_LIST;
4033
4034
4035 if (iterator == NULL)
4036 return -1;
4037
4038 while ((item = PyIter_Next(obj)))
4039 {
4040 li = listitem_alloc();
4041 if (li == NULL)
4042 {
4043 PyErr_NoMemory();
4044 return -1;
4045 }
4046 li->li_tv.v_lock = 0;
4047
4048 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
4049 return -1;
4050
4051 list_append(l, li);
4052
4053 Py_DECREF(item);
4054 }
4055
4056 Py_DECREF(iterator);
4057 return 0;
4058}
4059
Bram Moolenaardb913952012-06-29 12:54:53 +02004060typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4061
4062 static int
4063convert_dl(PyObject *obj, typval_T *tv,
4064 pytotvfunc py_to_tv, PyObject *lookupDict)
4065{
4066 PyObject *capsule;
4067 char hexBuf[sizeof(void *) * 2 + 3];
4068
4069 sprintf(hexBuf, "%p", obj);
4070
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004071# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004072 capsule = PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004073# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004074 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004075# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004076 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004077 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004078# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004079 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004080# else
4081 capsule = PyCObject_FromVoidPtr(tv, NULL);
4082# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004083 PyDict_SetItemString(lookupDict, hexBuf, capsule);
4084 Py_DECREF(capsule);
4085 if (py_to_tv(obj, tv, lookupDict) == -1)
4086 {
4087 tv->v_type = VAR_UNKNOWN;
4088 return -1;
4089 }
4090 /* As we are not using copy_tv which increments reference count we must
4091 * do it ourself. */
4092 switch(tv->v_type)
4093 {
4094 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4095 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4096 }
4097 }
4098 else
4099 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004100 typval_T *v;
4101
4102# ifdef PY_USE_CAPSULE
4103 v = PyCapsule_GetPointer(capsule, NULL);
4104# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004105 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004106# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004107 copy_tv(v, tv);
4108 }
4109 return 0;
4110}
4111
4112 static int
4113ConvertFromPyObject(PyObject *obj, typval_T *tv)
4114{
4115 PyObject *lookup_dict;
4116 int r;
4117
4118 lookup_dict = PyDict_New();
4119 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4120 Py_DECREF(lookup_dict);
4121 return r;
4122}
4123
4124 static int
4125_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
4126{
4127 if (obj->ob_type == &DictionaryType)
4128 {
4129 tv->v_type = VAR_DICT;
4130 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4131 ++tv->vval.v_dict->dv_refcount;
4132 }
4133 else if (obj->ob_type == &ListType)
4134 {
4135 tv->v_type = VAR_LIST;
4136 tv->vval.v_list = (((ListObject *)(obj))->list);
4137 ++tv->vval.v_list->lv_refcount;
4138 }
4139 else if (obj->ob_type == &FunctionType)
4140 {
4141 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4142 return -1;
4143
4144 tv->v_type = VAR_FUNC;
4145 func_ref(tv->vval.v_string);
4146 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004147 else if (PyBytes_Check(obj))
4148 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004149 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004150
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004151 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4152 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004153 if (result == NULL)
4154 return -1;
4155
4156 if (set_string_copy(result, tv) == -1)
4157 return -1;
4158
4159 tv->v_type = VAR_STRING;
4160 }
4161 else if (PyUnicode_Check(obj))
4162 {
4163 PyObject *bytes;
4164 char_u *result;
4165
Bram Moolenaardb913952012-06-29 12:54:53 +02004166 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4167 if (bytes == NULL)
4168 return -1;
4169
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004170 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4171 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004172 if (result == NULL)
4173 return -1;
4174
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004175 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004176 {
4177 Py_XDECREF(bytes);
4178 return -1;
4179 }
4180 Py_XDECREF(bytes);
4181
4182 tv->v_type = VAR_STRING;
4183 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004184#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004185 else if (PyInt_Check(obj))
4186 {
4187 tv->v_type = VAR_NUMBER;
4188 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4189 }
4190#endif
4191 else if (PyLong_Check(obj))
4192 {
4193 tv->v_type = VAR_NUMBER;
4194 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4195 }
4196 else if (PyDict_Check(obj))
4197 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
4198#ifdef FEAT_FLOAT
4199 else if (PyFloat_Check(obj))
4200 {
4201 tv->v_type = VAR_FLOAT;
4202 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4203 }
4204#endif
4205 else if (PyIter_Check(obj))
4206 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
4207 else if (PySequence_Check(obj))
4208 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
4209 else if (PyMapping_Check(obj))
4210 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
4211 else
4212 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004213 PyErr_SetString(PyExc_TypeError,
4214 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004215 return -1;
4216 }
4217 return 0;
4218}
4219
4220 static PyObject *
4221ConvertToPyObject(typval_T *tv)
4222{
4223 if (tv == NULL)
4224 {
4225 PyErr_SetVim(_("NULL reference passed"));
4226 return NULL;
4227 }
4228 switch (tv->v_type)
4229 {
4230 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004231 return PyBytes_FromString(tv->vval.v_string == NULL
4232 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004233 case VAR_NUMBER:
4234 return PyLong_FromLong((long) tv->vval.v_number);
4235#ifdef FEAT_FLOAT
4236 case VAR_FLOAT:
4237 return PyFloat_FromDouble((double) tv->vval.v_float);
4238#endif
4239 case VAR_LIST:
4240 return ListNew(tv->vval.v_list);
4241 case VAR_DICT:
4242 return DictionaryNew(tv->vval.v_dict);
4243 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004244 return FunctionNew(tv->vval.v_string == NULL
4245 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004246 case VAR_UNKNOWN:
4247 Py_INCREF(Py_None);
4248 return Py_None;
4249 default:
4250 PyErr_SetVim(_("internal error: invalid value type"));
4251 return NULL;
4252 }
4253}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004254
4255typedef struct
4256{
4257 PyObject_HEAD
4258} CurrentObject;
4259static PyTypeObject CurrentType;
4260
4261 static void
4262init_structs(void)
4263{
4264 vim_memset(&OutputType, 0, sizeof(OutputType));
4265 OutputType.tp_name = "vim.message";
4266 OutputType.tp_basicsize = sizeof(OutputObject);
4267 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4268 OutputType.tp_doc = "vim message object";
4269 OutputType.tp_methods = OutputMethods;
4270#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004271 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4272 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004273 OutputType.tp_alloc = call_PyType_GenericAlloc;
4274 OutputType.tp_new = call_PyType_GenericNew;
4275 OutputType.tp_free = call_PyObject_Free;
4276#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004277 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4278 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004279#endif
4280
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004281 vim_memset(&IterType, 0, sizeof(IterType));
4282 IterType.tp_name = "vim.iter";
4283 IterType.tp_basicsize = sizeof(IterObject);
4284 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
4285 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004286 IterType.tp_iter = (getiterfunc)IterIter;
4287 IterType.tp_iternext = (iternextfunc)IterNext;
4288 IterType.tp_dealloc = (destructor)IterDestructor;
4289 IterType.tp_traverse = (traverseproc)IterTraverse;
4290 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004291
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004292 vim_memset(&BufferType, 0, sizeof(BufferType));
4293 BufferType.tp_name = "vim.buffer";
4294 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004295 BufferType.tp_dealloc = (destructor)BufferDestructor;
4296 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004297 BufferType.tp_as_sequence = &BufferAsSeq;
4298 BufferType.tp_as_mapping = &BufferAsMapping;
4299 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4300 BufferType.tp_doc = "vim buffer object";
4301 BufferType.tp_methods = BufferMethods;
4302#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004303 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004304 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004305 BufferType.tp_alloc = call_PyType_GenericAlloc;
4306 BufferType.tp_new = call_PyType_GenericNew;
4307 BufferType.tp_free = call_PyObject_Free;
4308#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004309 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004310 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004311#endif
4312
4313 vim_memset(&WindowType, 0, sizeof(WindowType));
4314 WindowType.tp_name = "vim.window";
4315 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004316 WindowType.tp_dealloc = (destructor)WindowDestructor;
4317 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004318 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
4319 WindowType.tp_doc = "vim Window object";
4320 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004321 WindowType.tp_traverse = (traverseproc)WindowTraverse;
4322 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004323#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004324 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
4325 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004326 WindowType.tp_alloc = call_PyType_GenericAlloc;
4327 WindowType.tp_new = call_PyType_GenericNew;
4328 WindowType.tp_free = call_PyObject_Free;
4329#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004330 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
4331 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004332#endif
4333
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004334 vim_memset(&TabPageType, 0, sizeof(TabPageType));
4335 TabPageType.tp_name = "vim.tabpage";
4336 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004337 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
4338 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004339 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
4340 TabPageType.tp_doc = "vim tab page object";
4341 TabPageType.tp_methods = TabPageMethods;
4342#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004343 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004344 TabPageType.tp_alloc = call_PyType_GenericAlloc;
4345 TabPageType.tp_new = call_PyType_GenericNew;
4346 TabPageType.tp_free = call_PyObject_Free;
4347#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004348 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004349#endif
4350
Bram Moolenaardfa38d42013-05-15 13:38:47 +02004351 vim_memset(&BufMapType, 0, sizeof(BufMapType));
4352 BufMapType.tp_name = "vim.bufferlist";
4353 BufMapType.tp_basicsize = sizeof(BufMapObject);
4354 BufMapType.tp_as_mapping = &BufMapAsMapping;
4355 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004356 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004357 BufferType.tp_doc = "vim buffer list";
4358
4359 vim_memset(&WinListType, 0, sizeof(WinListType));
4360 WinListType.tp_name = "vim.windowlist";
4361 WinListType.tp_basicsize = sizeof(WinListType);
4362 WinListType.tp_as_sequence = &WinListAsSeq;
4363 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
4364 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004365 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004366
4367 vim_memset(&TabListType, 0, sizeof(TabListType));
4368 TabListType.tp_name = "vim.tabpagelist";
4369 TabListType.tp_basicsize = sizeof(TabListType);
4370 TabListType.tp_as_sequence = &TabListAsSeq;
4371 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
4372 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004373
4374 vim_memset(&RangeType, 0, sizeof(RangeType));
4375 RangeType.tp_name = "vim.range";
4376 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004377 RangeType.tp_dealloc = (destructor)RangeDestructor;
4378 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004379 RangeType.tp_as_sequence = &RangeAsSeq;
4380 RangeType.tp_as_mapping = &RangeAsMapping;
4381 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
4382 RangeType.tp_doc = "vim Range object";
4383 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004384 RangeType.tp_traverse = (traverseproc)RangeTraverse;
4385 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004386#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004387 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004388 RangeType.tp_alloc = call_PyType_GenericAlloc;
4389 RangeType.tp_new = call_PyType_GenericNew;
4390 RangeType.tp_free = call_PyObject_Free;
4391#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004392 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004393#endif
4394
4395 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4396 CurrentType.tp_name = "vim.currentdata";
4397 CurrentType.tp_basicsize = sizeof(CurrentObject);
4398 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4399 CurrentType.tp_doc = "vim current object";
4400#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004401 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
4402 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004403#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004404 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
4405 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004406#endif
4407
4408 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4409 DictionaryType.tp_name = "vim.dictionary";
4410 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004411 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004412 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4413 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4414 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4415 DictionaryType.tp_methods = DictionaryMethods;
4416#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004417 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
4418 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004419#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004420 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
4421 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004422#endif
4423
4424 vim_memset(&ListType, 0, sizeof(ListType));
4425 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004426 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004427 ListType.tp_basicsize = sizeof(ListObject);
4428 ListType.tp_as_sequence = &ListAsSeq;
4429 ListType.tp_as_mapping = &ListAsMapping;
4430 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4431 ListType.tp_doc = "list pushing modifications to vim structure";
4432 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004433 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004434#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004435 ListType.tp_getattro = (getattrofunc)ListGetattro;
4436 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004437#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004438 ListType.tp_getattr = (getattrfunc)ListGetattr;
4439 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004440#endif
4441
4442 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004443 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004444 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004445 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
4446 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004447 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4448 FunctionType.tp_doc = "object that calls vim function";
4449 FunctionType.tp_methods = FunctionMethods;
4450#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004451 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004452#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004453 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004454#endif
4455
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004456 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4457 OptionsType.tp_name = "vim.options";
4458 OptionsType.tp_basicsize = sizeof(OptionsObject);
4459 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4460 OptionsType.tp_doc = "object for manipulating options";
4461 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004462 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
4463 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
4464 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004465
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004466#if PY_MAJOR_VERSION >= 3
4467 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4468 vimmodule.m_name = "vim";
4469 vimmodule.m_doc = "Vim Python interface\n";
4470 vimmodule.m_size = -1;
4471 vimmodule.m_methods = VimMethods;
4472#endif
4473}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004474
4475#define PYTYPE_READY(type) \
4476 if (PyType_Ready(&type)) \
4477 return -1;
4478
4479 static int
4480init_types()
4481{
4482 PYTYPE_READY(IterType);
4483 PYTYPE_READY(BufferType);
4484 PYTYPE_READY(RangeType);
4485 PYTYPE_READY(WindowType);
4486 PYTYPE_READY(TabPageType);
4487 PYTYPE_READY(BufMapType);
4488 PYTYPE_READY(WinListType);
4489 PYTYPE_READY(TabListType);
4490 PYTYPE_READY(CurrentType);
4491 PYTYPE_READY(DictionaryType);
4492 PYTYPE_READY(ListType);
4493 PYTYPE_READY(FunctionType);
4494 PYTYPE_READY(OptionsType);
4495 PYTYPE_READY(OutputType);
4496 return 0;
4497}
4498
4499static BufMapObject TheBufferMap =
4500{
4501 PyObject_HEAD_INIT(&BufMapType)
4502};
4503
4504static WinListObject TheWindowList =
4505{
4506 PyObject_HEAD_INIT(&WinListType)
4507 NULL
4508};
4509
4510static CurrentObject TheCurrent =
4511{
4512 PyObject_HEAD_INIT(&CurrentType)
4513};
4514
4515static TabListObject TheTabPageList =
4516{
4517 PyObject_HEAD_INIT(&TabListType)
4518};
4519
4520static struct numeric_constant {
4521 char *name;
4522 int value;
4523} numeric_constants[] = {
4524 {"VAR_LOCKED", VAR_LOCKED},
4525 {"VAR_FIXED", VAR_FIXED},
4526 {"VAR_SCOPE", VAR_SCOPE},
4527 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
4528};
4529
4530static struct object_constant {
4531 char *name;
4532 PyObject *value;
4533} object_constants[] = {
4534 {"buffers", (PyObject *)(void *)&TheBufferMap},
4535 {"windows", (PyObject *)(void *)&TheWindowList},
4536 {"tabpages", (PyObject *)(void *)&TheTabPageList},
4537 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02004538
4539 {"Buffer", (PyObject *)&BufferType},
4540 {"Range", (PyObject *)&RangeType},
4541 {"Window", (PyObject *)&WindowType},
4542 {"TabPage", (PyObject *)&TabPageType},
4543 {"Dictionary", (PyObject *)&DictionaryType},
4544 {"List", (PyObject *)&ListType},
4545 {"Function", (PyObject *)&FunctionType},
4546 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004547};
4548
4549typedef int (*object_adder)(PyObject *, const char *, PyObject *);
4550
4551#define ADD_OBJECT(m, name, obj) \
4552 if (add_object(m, name, obj)) \
4553 return -1;
4554
4555#define ADD_CHECKED_OBJECT(m, name, obj) \
4556 { \
4557 PyObject *value = obj; \
4558 if (!value) \
4559 return -1; \
4560 ADD_OBJECT(m, name, value); \
4561 }
4562
4563 static int
4564populate_module(PyObject *m, object_adder add_object)
4565{
4566 int i;
4567
4568 for (i = 0; i < (int)(sizeof(numeric_constants)
4569 / sizeof(struct numeric_constant));
4570 ++i)
4571 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
4572 PyInt_FromLong(numeric_constants[i].value));
4573
4574 for (i = 0; i < (int)(sizeof(object_constants)
4575 / sizeof(struct object_constant));
4576 ++i)
4577 {
4578 PyObject *value;
4579
4580 value = object_constants[i].value;
4581 Py_INCREF(value);
4582 ADD_OBJECT(m, object_constants[i].name, value);
4583 }
4584
4585 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
4586 return -1;
4587 ADD_OBJECT(m, "error", VimError);
4588
4589 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict));
4590 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
4591 ADD_CHECKED_OBJECT(m, "options",
4592 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
4593 return 0;
4594}