blob: 7d3a26ebf743a7cdf8655a25bec5321151339b11 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020016#if PY_VERSION_HEX < 0x02050000
17typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
18#endif
19
Bram Moolenaar91805fc2011-06-26 04:01:44 +020020#ifdef FEAT_MBYTE
21# define ENC_OPT p_enc
22#else
23# define ENC_OPT "latin1"
24#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020025#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020026
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020027#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
28
29#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
30#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020031#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020032
Bram Moolenaare9ba5162013-05-29 22:02:22 +020033#define DICTKEY_DECL \
34 PyObject *dictkey_todecref;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +020035#define DICTKEY_CHECK_EMPTY(err) \
36 if (*key == NUL) \
37 { \
38 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
39 return err; \
40 }
41#define DICTKEY_SET_KEY (key = StringToChars(keyObject, &dictkey_todecref))
Bram Moolenaara03e6312013-05-29 22:49:26 +020042#define DICTKEY_GET(err, decref) \
Bram Moolenaar9bb77d62013-05-30 12:14:49 +020043 if (!DICTKEY_SET_KEY) \
Bram Moolenaara03e6312013-05-29 22:49:26 +020044 { \
45 if (decref) \
46 { \
47 Py_DECREF(keyObject); \
48 } \
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +020049 return err; \
Bram Moolenaara03e6312013-05-29 22:49:26 +020050 } \
51 if (decref && !dictkey_todecref) \
52 dictkey_todecref = keyObject; \
Bram Moolenaar9bb77d62013-05-30 12:14:49 +020053 DICTKEY_CHECK_EMPTY(err)
Bram Moolenaare9ba5162013-05-29 22:02:22 +020054#define DICTKEY_UNREF \
55 Py_XDECREF(dictkey_todecref);
56
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020057typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020058typedef void (*runner)(const char *, void *
59#ifdef PY_CAN_RECURSE
60 , PyGILState_STATE *
61#endif
62 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020063
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064static int ConvertFromPyObject(PyObject *, typval_T *);
65static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020066static PyObject *WindowNew(win_T *, tabpage_T *);
67static PyObject *BufferNew (buf_T *);
68static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020069
70static PyInt RangeStart;
71static PyInt RangeEnd;
72
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020073static PyObject *globals;
74
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020075/*
76 * obtain a lock on the Vim data structures
77 */
78 static void
79Python_Lock_Vim(void)
80{
81}
82
83/*
84 * release a lock on the Vim data structures
85 */
86 static void
87Python_Release_Vim(void)
88{
89}
90
Bram Moolenaare9ba5162013-05-29 22:02:22 +020091/*
92 * The "todecref" argument holds a pointer to PyObject * that must be
93 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
94 * was needed to generate returned value is object.
95 *
96 * Use Py_XDECREF to decrement reference count.
97 */
98 static char_u *
99StringToChars(PyObject *object, PyObject **todecref)
100{
101 char_u *p;
102 PyObject *bytes = NULL;
103
104 if (PyBytes_Check(object))
105 {
106
107 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
108 return NULL;
109 if (p == NULL)
110 return NULL;
111
112 *todecref = NULL;
113 }
114 else if (PyUnicode_Check(object))
115 {
116 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
117 if (bytes == NULL)
118 return NULL;
119
120 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
121 return NULL;
122 if (p == NULL)
123 return NULL;
124
125 *todecref = bytes;
126 }
127 else
128 {
129 PyErr_SetString(PyExc_TypeError, _("object must be string"));
130 return NULL;
131 }
132
133 return (char_u *) p;
134}
135
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200136 static int
137add_string(PyObject *list, char *s)
138{
139 PyObject *string;
140
141 if (!(string = PyString_FromString(s)))
142 return -1;
143 if (PyList_Append(list, string))
144 {
145 Py_DECREF(string);
146 return -1;
147 }
148
149 Py_DECREF(string);
150 return 0;
151}
152
153 static PyObject *
154ObjectDir(PyObject *self, char **attributes)
155{
156 PyMethodDef *method;
157 char **attr;
158 PyObject *r;
159
160 if (!(r = PyList_New(0)))
161 return NULL;
162
163 if (self)
164 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
165 if (add_string(r, (char *) method->ml_name))
166 {
167 Py_DECREF(r);
168 return NULL;
169 }
170
171 for (attr = attributes ; *attr ; ++attr)
172 if (add_string(r, *attr))
173 {
174 Py_DECREF(r);
175 return NULL;
176 }
177
178#if PY_MAJOR_VERSION < 3
179 if (add_string(r, "__members__"))
180 {
181 Py_DECREF(r);
182 return NULL;
183 }
184#endif
185
186 return r;
187}
188
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200189/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200190 */
191
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200192/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200193typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200194
195static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200196
197typedef struct
198{
199 PyObject_HEAD
200 long softspace;
201 long error;
202} OutputObject;
203
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200204static char *OutputAttrs[] = {
205 "softspace",
206 NULL
207};
208
209 static PyObject *
210OutputDir(PyObject *self)
211{
212 return ObjectDir(self, OutputAttrs);
213}
214
Bram Moolenaar77045652012-09-21 13:46:06 +0200215 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200216OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200217{
218 if (val == NULL)
219 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200220 PyErr_SetString(PyExc_AttributeError,
221 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200222 return -1;
223 }
224
225 if (strcmp(name, "softspace") == 0)
226 {
227 if (!PyInt_Check(val))
228 {
229 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
230 return -1;
231 }
232
Bram Moolenaard6e39182013-05-21 18:30:34 +0200233 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200234 return 0;
235 }
236
237 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
238 return -1;
239}
240
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200241/* Buffer IO, we write one whole line at a time. */
242static garray_T io_ga = {0, 0, 1, 80, NULL};
243static writefn old_fn = NULL;
244
245 static void
246PythonIO_Flush(void)
247{
248 if (old_fn != NULL && io_ga.ga_len > 0)
249 {
250 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
251 old_fn((char_u *)io_ga.ga_data);
252 }
253 io_ga.ga_len = 0;
254}
255
256 static void
257writer(writefn fn, char_u *str, PyInt n)
258{
259 char_u *ptr;
260
261 /* Flush when switching output function. */
262 if (fn != old_fn)
263 PythonIO_Flush();
264 old_fn = fn;
265
266 /* Write each NL separated line. Text after the last NL is kept for
267 * writing later. */
268 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
269 {
270 PyInt len = ptr - str;
271
272 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
273 break;
274
275 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
276 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
277 fn((char_u *)io_ga.ga_data);
278 str = ptr + 1;
279 n -= len + 1;
280 io_ga.ga_len = 0;
281 }
282
283 /* Put the remaining text into io_ga for later printing. */
284 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
285 {
286 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
287 io_ga.ga_len += (int)n;
288 }
289}
290
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200291 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200292OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200293{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200294 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200295 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200296 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200297
Bram Moolenaar27564802011-09-07 19:30:21 +0200298 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200299 return NULL;
300
301 Py_BEGIN_ALLOW_THREADS
302 Python_Lock_Vim();
303 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
304 Python_Release_Vim();
305 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200306 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200307
308 Py_INCREF(Py_None);
309 return Py_None;
310}
311
312 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200313OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200314{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200315 PyObject *seq;
316 PyObject *iterator;
317 PyObject *item;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200318 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200319
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200320 if (!PyArg_ParseTuple(args, "O", &seq))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200321 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200322
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200323 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200324 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200325
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200326 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200327 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200328 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200329 PyInt len;
330
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200331 if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
Bram Moolenaardb913952012-06-29 12:54:53 +0200332 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200333 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200334 Py_DECREF(iterator);
335 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200336 return NULL;
337 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200338 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200339
340 Py_BEGIN_ALLOW_THREADS
341 Python_Lock_Vim();
342 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
343 Python_Release_Vim();
344 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200345 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200346 }
347
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200348 Py_DECREF(iterator);
349
350 /* Iterator may have finished due to an exception */
351 if (PyErr_Occurred())
352 return NULL;
353
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200354 Py_INCREF(Py_None);
355 return Py_None;
356}
357
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100358 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200359OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100360{
361 /* do nothing */
362 Py_INCREF(Py_None);
363 return Py_None;
364}
365
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200366/***************/
367
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200368static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200369 /* name, function, calling, doc */
370 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
371 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
372 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200373 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200374 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200375};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200376
377static OutputObject Output =
378{
379 PyObject_HEAD_INIT(&OutputType)
380 0,
381 0
382};
383
384static OutputObject Error =
385{
386 PyObject_HEAD_INIT(&OutputType)
387 0,
388 1
389};
390
391 static int
392PythonIO_Init_io(void)
393{
394 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
395 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
396
397 if (PyErr_Occurred())
398 {
399 EMSG(_("E264: Python: Error initialising I/O objects"));
400 return -1;
401 }
402
403 return 0;
404}
405
406
407static PyObject *VimError;
408
409/* Check to see whether a Vim error has been reported, or a keyboard
410 * interrupt has been detected.
411 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200412
413 static void
414VimTryStart(void)
415{
416 ++trylevel;
417}
418
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200419 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200420VimTryEnd(void)
421{
422 --trylevel;
423 if (got_int)
424 {
425 PyErr_SetNone(PyExc_KeyboardInterrupt);
426 return 1;
427 }
428 else if (!did_throw)
429 return 0;
430 else if (PyErr_Occurred())
431 return 1;
432 else
433 {
434 PyErr_SetVim((char *) current_exception->value);
435 discard_current_exception();
436 return 1;
437 }
438}
439
440 static int
441VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442{
443 if (got_int)
444 {
445 PyErr_SetNone(PyExc_KeyboardInterrupt);
446 return 1;
447 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448 return 0;
449}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200450
451/* Vim module - Implementation
452 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200453
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200454 static PyObject *
455VimCommand(PyObject *self UNUSED, PyObject *args)
456{
457 char *cmd;
458 PyObject *result;
459
460 if (!PyArg_ParseTuple(args, "s", &cmd))
461 return NULL;
462
463 PyErr_Clear();
464
465 Py_BEGIN_ALLOW_THREADS
466 Python_Lock_Vim();
467
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200468 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200469 do_cmdline_cmd((char_u *)cmd);
470 update_screen(VALID);
471
472 Python_Release_Vim();
473 Py_END_ALLOW_THREADS
474
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200475 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200476 result = NULL;
477 else
478 result = Py_None;
479
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200480
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200481 Py_XINCREF(result);
482 return result;
483}
484
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200485/*
486 * Function to translate a typval_T into a PyObject; this will recursively
487 * translate lists/dictionaries into their Python equivalents.
488 *
489 * The depth parameter is to avoid infinite recursion, set it to 1 when
490 * you call VimToPython.
491 */
492 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200493VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200494{
495 PyObject *result;
496 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200497 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200498
499 /* Avoid infinite recursion */
500 if (depth > 100)
501 {
502 Py_INCREF(Py_None);
503 result = Py_None;
504 return result;
505 }
506
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200507 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200508 * then and we can use it again. */
509 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
510 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
511 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200512 sprintf(ptrBuf, "%p",
513 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
514 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200515
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200516 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200517 {
518 Py_INCREF(result);
519 return result;
520 }
521 }
522
523 if (our_tv->v_type == VAR_STRING)
524 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200525 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200526 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200527 }
528 else if (our_tv->v_type == VAR_NUMBER)
529 {
530 char buf[NUMBUFLEN];
531
532 /* For backwards compatibility numbers are stored as strings. */
533 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200534 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200535 }
536# ifdef FEAT_FLOAT
537 else if (our_tv->v_type == VAR_FLOAT)
538 {
539 char buf[NUMBUFLEN];
540
541 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200542 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200543 }
544# endif
545 else if (our_tv->v_type == VAR_LIST)
546 {
547 list_T *list = our_tv->vval.v_list;
548 listitem_T *curr;
549
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200550 if (list == NULL)
551 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200552
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200553 if (!(result = PyList_New(0)))
554 return NULL;
555
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200556 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200557 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200558 Py_DECREF(result);
559 return NULL;
560 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200561
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200562 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
563 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200564 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200565 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200566 Py_DECREF(result);
567 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200568 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200569 if (PyList_Append(result, newObj))
570 {
571 Py_DECREF(newObj);
572 Py_DECREF(result);
573 return NULL;
574 }
575 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200576 }
577 }
578 else if (our_tv->v_type == VAR_DICT)
579 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200580
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200581 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
582 long_u todo = ht->ht_used;
583 hashitem_T *hi;
584 dictitem_T *di;
585 if (our_tv->vval.v_dict == NULL)
586 return NULL;
587
588 if (!(result = PyDict_New()))
589 return NULL;
590
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200591 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200592 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200593 Py_DECREF(result);
594 return NULL;
595 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200596
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200597 for (hi = ht->ht_array; todo > 0; ++hi)
598 {
599 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200600 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200601 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200602
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200603 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200604 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200605 {
606 Py_DECREF(result);
607 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200608 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200609 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
610 {
611 Py_DECREF(result);
612 Py_DECREF(newObj);
613 return NULL;
614 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200615 }
616 }
617 }
618 else
619 {
620 Py_INCREF(Py_None);
621 result = Py_None;
622 }
623
624 return result;
625}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200626
627 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200628VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200629{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200630 char *expr;
631 typval_T *our_tv;
632 PyObject *result;
633 PyObject *lookup_dict;
634
635 if (!PyArg_ParseTuple(args, "s", &expr))
636 return NULL;
637
638 Py_BEGIN_ALLOW_THREADS
639 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200640 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200641 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200642 Python_Release_Vim();
643 Py_END_ALLOW_THREADS
644
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200645 if (VimTryEnd())
646 return NULL;
647
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200648 if (our_tv == NULL)
649 {
650 PyErr_SetVim(_("invalid expression"));
651 return NULL;
652 }
653
654 /* Convert the Vim type into a Python type. Create a dictionary that's
655 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200656 if (!(lookup_dict = PyDict_New()))
657 result = NULL;
658 else
659 {
660 result = VimToPython(our_tv, 1, lookup_dict);
661 Py_DECREF(lookup_dict);
662 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200663
664
665 Py_BEGIN_ALLOW_THREADS
666 Python_Lock_Vim();
667 free_tv(our_tv);
668 Python_Release_Vim();
669 Py_END_ALLOW_THREADS
670
671 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200672}
673
Bram Moolenaardb913952012-06-29 12:54:53 +0200674static PyObject *ConvertToPyObject(typval_T *);
675
676 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200677VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200678{
Bram Moolenaardb913952012-06-29 12:54:53 +0200679 char *expr;
680 typval_T *our_tv;
681 PyObject *result;
682
683 if (!PyArg_ParseTuple(args, "s", &expr))
684 return NULL;
685
686 Py_BEGIN_ALLOW_THREADS
687 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200688 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200689 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200690 Python_Release_Vim();
691 Py_END_ALLOW_THREADS
692
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200693 if (VimTryEnd())
694 return NULL;
695
Bram Moolenaardb913952012-06-29 12:54:53 +0200696 if (our_tv == NULL)
697 {
698 PyErr_SetVim(_("invalid expression"));
699 return NULL;
700 }
701
702 result = ConvertToPyObject(our_tv);
703 Py_BEGIN_ALLOW_THREADS
704 Python_Lock_Vim();
705 free_tv(our_tv);
706 Python_Release_Vim();
707 Py_END_ALLOW_THREADS
708
709 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200710}
711
712 static PyObject *
713VimStrwidth(PyObject *self UNUSED, PyObject *args)
714{
715 char *expr;
716
717 if (!PyArg_ParseTuple(args, "s", &expr))
718 return NULL;
719
Bram Moolenaara54bf402012-12-05 16:30:07 +0100720 return PyLong_FromLong(
721#ifdef FEAT_MBYTE
722 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
723#else
724 STRLEN(expr)
725#endif
726 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200727}
728
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200729/*
730 * Vim module - Definitions
731 */
732
733static struct PyMethodDef VimMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200734 /* name, function, calling, documentation */
735 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
736 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
737 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
738 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
739 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200740};
741
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200742/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200743 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200744 */
745
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200746static PyTypeObject IterType;
747
748typedef PyObject *(*nextfun)(void **);
749typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200750typedef int (*traversefun)(void *, visitproc, void *);
751typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200752
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200753/* Main purpose of this object is removing the need for do python
754 * initialization (i.e. PyType_Ready and setting type attributes) for a big
755 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200756
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200757typedef struct
758{
759 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200760 void *cur;
761 nextfun next;
762 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200763 traversefun traverse;
764 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200765} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200766
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200767 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200768IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
769 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200770{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200771 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200772
Bram Moolenaar774267b2013-05-21 20:51:59 +0200773 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200774 self->cur = start;
775 self->next = next;
776 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200777 self->traverse = traverse;
778 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200779
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200780 return (PyObject *)(self);
781}
782
783 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200784IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200785{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200786 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200787 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200788 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200789}
790
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200791 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200792IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200793{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200794 if (self->traverse != NULL)
795 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200796 else
797 return 0;
798}
799
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200800/* Mac OSX defines clear() somewhere. */
801#ifdef clear
802# undef clear
803#endif
804
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200805 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200806IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200807{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200808 if (self->clear != NULL)
809 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200810 else
811 return 0;
812}
813
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200814 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200815IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200816{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200817 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200818}
819
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200820 static PyObject *
821IterIter(PyObject *self)
822{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +0200823 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200824 return self;
825}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200826
Bram Moolenaardb913952012-06-29 12:54:53 +0200827typedef struct pylinkedlist_S {
828 struct pylinkedlist_S *pll_next;
829 struct pylinkedlist_S *pll_prev;
830 PyObject *pll_obj;
831} pylinkedlist_T;
832
833static pylinkedlist_T *lastdict = NULL;
834static pylinkedlist_T *lastlist = NULL;
835
836 static void
837pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
838{
839 if (ref->pll_prev == NULL)
840 {
841 if (ref->pll_next == NULL)
842 {
843 *last = NULL;
844 return;
845 }
846 }
847 else
848 ref->pll_prev->pll_next = ref->pll_next;
849
850 if (ref->pll_next == NULL)
851 *last = ref->pll_prev;
852 else
853 ref->pll_next->pll_prev = ref->pll_prev;
854}
855
856 static void
857pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
858{
859 if (*last == NULL)
860 ref->pll_prev = NULL;
861 else
862 {
863 (*last)->pll_next = ref;
864 ref->pll_prev = *last;
865 }
866 ref->pll_next = NULL;
867 ref->pll_obj = self;
868 *last = ref;
869}
870
871static PyTypeObject DictionaryType;
872
873typedef struct
874{
875 PyObject_HEAD
876 dict_T *dict;
877 pylinkedlist_T ref;
878} DictionaryObject;
879
880 static PyObject *
881DictionaryNew(dict_T *dict)
882{
883 DictionaryObject *self;
884
885 self = PyObject_NEW(DictionaryObject, &DictionaryType);
886 if (self == NULL)
887 return NULL;
888 self->dict = dict;
889 ++dict->dv_refcount;
890
891 pyll_add((PyObject *)(self), &self->ref, &lastdict);
892
893 return (PyObject *)(self);
894}
895
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200896 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200897DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200898{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200899 pyll_remove(&self->ref, &lastdict);
900 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200901
902 DESTRUCTOR_FINISH(self);
903}
904
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200905static char *DictionaryAttrs[] = {
906 "locked", "scope",
907 NULL
908};
909
910 static PyObject *
911DictionaryDir(PyObject *self)
912{
913 return ObjectDir(self, DictionaryAttrs);
914}
915
Bram Moolenaardb913952012-06-29 12:54:53 +0200916 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200917DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200918{
919 if (val == NULL)
920 {
921 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
922 return -1;
923 }
924
925 if (strcmp(name, "locked") == 0)
926 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200927 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200928 {
929 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
930 return -1;
931 }
932 else
933 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200934 int istrue = PyObject_IsTrue(val);
935 if (istrue == -1)
936 return -1;
937 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200938 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200939 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200940 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200941 }
942 return 0;
943 }
944 else
945 {
946 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
947 return -1;
948 }
949}
950
951 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200952DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200953{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200954 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +0200955}
956
957 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200958DictionaryItem(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200959{
960 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200961 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200962 DICTKEY_DECL
963
Bram Moolenaara03e6312013-05-29 22:49:26 +0200964 DICTKEY_GET(NULL, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +0200965
Bram Moolenaard6e39182013-05-21 18:30:34 +0200966 di = dict_find(self->dict, key, -1);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200967
Bram Moolenaar696c2112012-09-21 13:43:14 +0200968 DICTKEY_UNREF
969
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200970 if (di == NULL)
971 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200972 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200973 return NULL;
974 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200975
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200976 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200977}
978
979 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +0200980DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +0200981{
982 char_u *key;
983 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200984 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +0200985 dictitem_T *di;
986 DICTKEY_DECL
987
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200988 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +0200989 {
990 PyErr_SetVim(_("dict is locked"));
991 return -1;
992 }
993
Bram Moolenaara03e6312013-05-29 22:49:26 +0200994 DICTKEY_GET(-1, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +0200995
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200996 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +0200997
998 if (valObject == NULL)
999 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001000 hashitem_T *hi;
1001
Bram Moolenaardb913952012-06-29 12:54:53 +02001002 if (di == NULL)
1003 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001004 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001005 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001006 return -1;
1007 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001008 hi = hash_find(&dict->dv_hashtab, di->di_key);
1009 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001010 dictitem_free(di);
1011 return 0;
1012 }
1013
1014 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001015 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001016
1017 if (di == NULL)
1018 {
1019 di = dictitem_alloc(key);
1020 if (di == NULL)
1021 {
1022 PyErr_NoMemory();
1023 return -1;
1024 }
1025 di->di_tv.v_lock = 0;
1026
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001027 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001028 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001029 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +02001030 vim_free(di);
1031 PyErr_SetVim(_("failed to add key to dictionary"));
1032 return -1;
1033 }
1034 }
1035 else
1036 clear_tv(&di->di_tv);
1037
1038 DICTKEY_UNREF
1039
1040 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001041 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001042 return 0;
1043}
1044
1045 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001046DictionaryListKeys(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001047{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001048 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001049 long_u todo = dict->dv_hashtab.ht_used;
1050 Py_ssize_t i = 0;
1051 PyObject *r;
1052 hashitem_T *hi;
1053
1054 r = PyList_New(todo);
1055 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1056 {
1057 if (!HASHITEM_EMPTY(hi))
1058 {
1059 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
1060 --todo;
1061 ++i;
1062 }
1063 }
1064 return r;
1065}
1066
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001067static PyMappingMethods DictionaryAsMapping = {
1068 (lenfunc) DictionaryLength,
1069 (binaryfunc) DictionaryItem,
1070 (objobjargproc) DictionaryAssItem,
1071};
1072
Bram Moolenaardb913952012-06-29 12:54:53 +02001073static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001074 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001075 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1076 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001077};
1078
1079static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001080static PySequenceMethods ListAsSeq;
1081static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001082
1083typedef struct
1084{
1085 PyObject_HEAD
1086 list_T *list;
1087 pylinkedlist_T ref;
1088} ListObject;
1089
1090 static PyObject *
1091ListNew(list_T *list)
1092{
1093 ListObject *self;
1094
1095 self = PyObject_NEW(ListObject, &ListType);
1096 if (self == NULL)
1097 return NULL;
1098 self->list = list;
1099 ++list->lv_refcount;
1100
1101 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1102
1103 return (PyObject *)(self);
1104}
1105
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001106 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001107ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001108{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001109 pyll_remove(&self->ref, &lastlist);
1110 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001111
1112 DESTRUCTOR_FINISH(self);
1113}
1114
Bram Moolenaardb913952012-06-29 12:54:53 +02001115 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001116list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001117{
1118 Py_ssize_t i;
1119 Py_ssize_t lsize = PySequence_Size(obj);
1120 PyObject *litem;
1121 listitem_T *li;
1122
1123 for(i=0; i<lsize; i++)
1124 {
1125 li = listitem_alloc();
1126 if (li == NULL)
1127 {
1128 PyErr_NoMemory();
1129 return -1;
1130 }
1131 li->li_tv.v_lock = 0;
1132
1133 litem = PySequence_GetItem(obj, i);
1134 if (litem == NULL)
1135 return -1;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001136 if (_ConvertFromPyObject(litem, &li->li_tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001137 return -1;
1138
1139 list_append(l, li);
1140 }
1141 return 0;
1142}
1143
Bram Moolenaardb913952012-06-29 12:54:53 +02001144 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001145ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001146{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001147 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001148}
1149
1150 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001151ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001152{
1153 listitem_T *li;
1154
Bram Moolenaard6e39182013-05-21 18:30:34 +02001155 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001156 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001157 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001158 return NULL;
1159 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001160 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001161 if (li == NULL)
1162 {
1163 PyErr_SetVim(_("internal error: failed to get vim list item"));
1164 return NULL;
1165 }
1166 return ConvertToPyObject(&li->li_tv);
1167}
1168
1169#define PROC_RANGE \
1170 if (last < 0) {\
1171 if (last < -size) \
1172 last = 0; \
1173 else \
1174 last += size; \
1175 } \
1176 if (first < 0) \
1177 first = 0; \
1178 if (first > size) \
1179 first = size; \
1180 if (last > size) \
1181 last = size;
1182
1183 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001184ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001185{
1186 PyInt i;
1187 PyInt size = ListLength(self);
1188 PyInt n;
1189 PyObject *list;
1190 int reversed = 0;
1191
1192 PROC_RANGE
1193 if (first >= last)
1194 first = last;
1195
1196 n = last-first;
1197 list = PyList_New(n);
1198 if (list == NULL)
1199 return NULL;
1200
1201 for (i = 0; i < n; ++i)
1202 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001203 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001204 if (item == NULL)
1205 {
1206 Py_DECREF(list);
1207 return NULL;
1208 }
1209
1210 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1211 {
1212 Py_DECREF(item);
1213 Py_DECREF(list);
1214 return NULL;
1215 }
1216 }
1217
1218 return list;
1219}
1220
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001221typedef struct
1222{
1223 listwatch_T lw;
1224 list_T *list;
1225} listiterinfo_T;
1226
1227 static void
1228ListIterDestruct(listiterinfo_T *lii)
1229{
1230 list_rem_watch(lii->list, &lii->lw);
1231 PyMem_Free(lii);
1232}
1233
1234 static PyObject *
1235ListIterNext(listiterinfo_T **lii)
1236{
1237 PyObject *r;
1238
1239 if (!((*lii)->lw.lw_item))
1240 return NULL;
1241
1242 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1243 return NULL;
1244
1245 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1246
1247 return r;
1248}
1249
1250 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001251ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001252{
1253 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001254 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001255
1256 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1257 {
1258 PyErr_NoMemory();
1259 return NULL;
1260 }
1261
1262 list_add_watch(l, &lii->lw);
1263 lii->lw.lw_item = l->lv_first;
1264 lii->list = l;
1265
1266 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001267 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1268 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001269}
1270
Bram Moolenaardb913952012-06-29 12:54:53 +02001271 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001272ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001273{
1274 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001275 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001276 listitem_T *li;
1277 Py_ssize_t length = ListLength(self);
1278
1279 if (l->lv_lock)
1280 {
1281 PyErr_SetVim(_("list is locked"));
1282 return -1;
1283 }
1284 if (index>length || (index==length && obj==NULL))
1285 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001286 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001287 return -1;
1288 }
1289
1290 if (obj == NULL)
1291 {
1292 li = list_find(l, (long) index);
1293 list_remove(l, li, li);
1294 clear_tv(&li->li_tv);
1295 vim_free(li);
1296 return 0;
1297 }
1298
1299 if (ConvertFromPyObject(obj, &tv) == -1)
1300 return -1;
1301
1302 if (index == length)
1303 {
1304 if (list_append_tv(l, &tv) == FAIL)
1305 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001306 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001307 PyErr_SetVim(_("Failed to add item to list"));
1308 return -1;
1309 }
1310 }
1311 else
1312 {
1313 li = list_find(l, (long) index);
1314 clear_tv(&li->li_tv);
1315 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001316 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001317 }
1318 return 0;
1319}
1320
1321 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001322ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001323{
1324 PyInt size = ListLength(self);
1325 Py_ssize_t i;
1326 Py_ssize_t lsize;
1327 PyObject *litem;
1328 listitem_T *li;
1329 listitem_T *next;
1330 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001331 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001332
1333 if (l->lv_lock)
1334 {
1335 PyErr_SetVim(_("list is locked"));
1336 return -1;
1337 }
1338
1339 PROC_RANGE
1340
1341 if (first == size)
1342 li = NULL;
1343 else
1344 {
1345 li = list_find(l, (long) first);
1346 if (li == NULL)
1347 {
1348 PyErr_SetVim(_("internal error: no vim list item"));
1349 return -1;
1350 }
1351 if (last > first)
1352 {
1353 i = last - first;
1354 while (i-- && li != NULL)
1355 {
1356 next = li->li_next;
1357 listitem_remove(l, li);
1358 li = next;
1359 }
1360 }
1361 }
1362
1363 if (obj == NULL)
1364 return 0;
1365
1366 if (!PyList_Check(obj))
1367 {
1368 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1369 return -1;
1370 }
1371
1372 lsize = PyList_Size(obj);
1373
1374 for(i=0; i<lsize; i++)
1375 {
1376 litem = PyList_GetItem(obj, i);
1377 if (litem == NULL)
1378 return -1;
1379 if (ConvertFromPyObject(litem, &v) == -1)
1380 return -1;
1381 if (list_insert_tv(l, &v, li) == FAIL)
1382 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001383 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001384 PyErr_SetVim(_("internal error: failed to add item to list"));
1385 return -1;
1386 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001387 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001388 }
1389 return 0;
1390}
1391
1392 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001393ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001394{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001395 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001396 PyObject *lookup_dict;
1397
1398 if (l->lv_lock)
1399 {
1400 PyErr_SetVim(_("list is locked"));
1401 return NULL;
1402 }
1403
1404 if (!PySequence_Check(obj))
1405 {
1406 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1407 return NULL;
1408 }
1409
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001410 if (!(lookup_dict = PyDict_New()))
1411 return NULL;
1412
Bram Moolenaardb913952012-06-29 12:54:53 +02001413 if (list_py_concat(l, obj, lookup_dict) == -1)
1414 {
1415 Py_DECREF(lookup_dict);
1416 return NULL;
1417 }
1418 Py_DECREF(lookup_dict);
1419
1420 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001421 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001422}
1423
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001424static char *ListAttrs[] = {
1425 "locked",
1426 NULL
1427};
1428
1429 static PyObject *
1430ListDir(PyObject *self)
1431{
1432 return ObjectDir(self, ListAttrs);
1433}
1434
Bram Moolenaar66b79852012-09-21 14:00:35 +02001435 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001436ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001437{
1438 if (val == NULL)
1439 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001440 PyErr_SetString(PyExc_AttributeError,
1441 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001442 return -1;
1443 }
1444
1445 if (strcmp(name, "locked") == 0)
1446 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001447 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001448 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001449 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001450 return -1;
1451 }
1452 else
1453 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001454 int istrue = PyObject_IsTrue(val);
1455 if (istrue == -1)
1456 return -1;
1457 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001458 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001459 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001460 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001461 }
1462 return 0;
1463 }
1464 else
1465 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001466 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001467 return -1;
1468 }
1469}
1470
Bram Moolenaardb913952012-06-29 12:54:53 +02001471static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001472 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1473 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
1474 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001475};
1476
1477typedef struct
1478{
1479 PyObject_HEAD
1480 char_u *name;
1481} FunctionObject;
1482
1483static PyTypeObject FunctionType;
1484
1485 static PyObject *
1486FunctionNew(char_u *name)
1487{
1488 FunctionObject *self;
1489
1490 self = PyObject_NEW(FunctionObject, &FunctionType);
1491 if (self == NULL)
1492 return NULL;
1493 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1494 if (self->name == NULL)
1495 {
1496 PyErr_NoMemory();
1497 return NULL;
1498 }
1499 STRCPY(self->name, name);
1500 func_ref(name);
1501 return (PyObject *)(self);
1502}
1503
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001504 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001505FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001506{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001507 func_unref(self->name);
1508 PyMem_Free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001509
1510 DESTRUCTOR_FINISH(self);
1511}
1512
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001513static char *FunctionAttrs[] = {
1514 "softspace",
1515 NULL
1516};
1517
1518 static PyObject *
1519FunctionDir(PyObject *self)
1520{
1521 return ObjectDir(self, FunctionAttrs);
1522}
1523
Bram Moolenaardb913952012-06-29 12:54:53 +02001524 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001525FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02001526{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001527 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02001528 typval_T args;
1529 typval_T selfdicttv;
1530 typval_T rettv;
1531 dict_T *selfdict = NULL;
1532 PyObject *selfdictObject;
1533 PyObject *result;
1534 int error;
1535
1536 if (ConvertFromPyObject(argsObject, &args) == -1)
1537 return NULL;
1538
1539 if (kwargs != NULL)
1540 {
1541 selfdictObject = PyDict_GetItemString(kwargs, "self");
1542 if (selfdictObject != NULL)
1543 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001544 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001545 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001546 PyErr_SetString(PyExc_TypeError,
1547 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001548 clear_tv(&args);
1549 return NULL;
1550 }
1551 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001552 {
1553 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02001554 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001555 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001556 selfdict = selfdicttv.vval.v_dict;
1557 }
1558 }
1559
Bram Moolenaar71700b82013-05-15 17:49:05 +02001560 Py_BEGIN_ALLOW_THREADS
1561 Python_Lock_Vim();
1562
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001563 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02001564 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001565
1566 Python_Release_Vim();
1567 Py_END_ALLOW_THREADS
1568
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001569 if (VimTryEnd())
1570 result = NULL;
1571 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02001572 {
1573 result = NULL;
1574 PyErr_SetVim(_("failed to run function"));
1575 }
1576 else
1577 result = ConvertToPyObject(&rettv);
1578
Bram Moolenaardb913952012-06-29 12:54:53 +02001579 clear_tv(&args);
1580 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001581 if (selfdict != NULL)
1582 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001583
1584 return result;
1585}
1586
1587static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001588 {"__call__",(PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1589 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
1590 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001591};
1592
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001593/*
1594 * Options object
1595 */
1596
1597static PyTypeObject OptionsType;
1598
1599typedef int (*checkfun)(void *);
1600
1601typedef struct
1602{
1603 PyObject_HEAD
1604 int opt_type;
1605 void *from;
1606 checkfun Check;
1607 PyObject *fromObj;
1608} OptionsObject;
1609
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001610 static int
1611dummy_check(void *arg UNUSED)
1612{
1613 return 0;
1614}
1615
1616 static PyObject *
1617OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1618{
1619 OptionsObject *self;
1620
Bram Moolenaar774267b2013-05-21 20:51:59 +02001621 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001622 if (self == NULL)
1623 return NULL;
1624
1625 self->opt_type = opt_type;
1626 self->from = from;
1627 self->Check = Check;
1628 self->fromObj = fromObj;
1629 if (fromObj)
1630 Py_INCREF(fromObj);
1631
1632 return (PyObject *)(self);
1633}
1634
1635 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001636OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001637{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001638 PyObject_GC_UnTrack((void *)(self));
1639 Py_XDECREF(self->fromObj);
1640 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001641}
1642
1643 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001644OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001645{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001646 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001647 return 0;
1648}
1649
1650 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001651OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001652{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001653 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001654 return 0;
1655}
1656
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001657 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001658OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001659{
1660 char_u *key;
1661 int flags;
1662 long numval;
1663 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001664 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001665
Bram Moolenaard6e39182013-05-21 18:30:34 +02001666 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001667 return NULL;
1668
Bram Moolenaara03e6312013-05-29 22:49:26 +02001669 DICTKEY_GET(NULL, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001670
1671 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001672 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001673
1674 DICTKEY_UNREF
1675
1676 if (flags == 0)
1677 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001678 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001679 return NULL;
1680 }
1681
1682 if (flags & SOPT_UNSET)
1683 {
1684 Py_INCREF(Py_None);
1685 return Py_None;
1686 }
1687 else if (flags & SOPT_BOOL)
1688 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001689 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001690 r = numval ? Py_True : Py_False;
1691 Py_INCREF(r);
1692 return r;
1693 }
1694 else if (flags & SOPT_NUM)
1695 return PyInt_FromLong(numval);
1696 else if (flags & SOPT_STRING)
1697 {
1698 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001699 {
1700 PyObject *r = PyBytes_FromString((char *) stringval);
1701 vim_free(stringval);
1702 return r;
1703 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001704 else
1705 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001706 PyErr_SetString(PyExc_RuntimeError,
1707 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001708 return NULL;
1709 }
1710 }
1711 else
1712 {
1713 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1714 return NULL;
1715 }
1716}
1717
1718 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001719set_option_value_err(key, numval, stringval, opt_flags)
1720 char_u *key;
1721 int numval;
1722 char_u *stringval;
1723 int opt_flags;
1724{
1725 char_u *errmsg;
1726
1727 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
1728 {
1729 if (VimTryEnd())
1730 return FAIL;
1731 PyErr_SetVim((char *)errmsg);
1732 return FAIL;
1733 }
1734 return OK;
1735}
1736
1737 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001738set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1739 char_u *key;
1740 int numval;
1741 char_u *stringval;
1742 int opt_flags;
1743 int opt_type;
1744 void *from;
1745{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001746 win_T *save_curwin = NULL;
1747 tabpage_T *save_curtab = NULL;
1748 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001749 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001750
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001751 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001752 switch (opt_type)
1753 {
1754 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001755 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1756 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001757 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001758 if (VimTryEnd())
1759 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001760 PyErr_SetVim("Problem while switching windows.");
1761 return -1;
1762 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001763 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001764 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001765 if (r == FAIL)
1766 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001767 break;
1768 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001769 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001770 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001771 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001772 if (r == FAIL)
1773 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001774 break;
1775 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001776 r = set_option_value_err(key, numval, stringval, opt_flags);
1777 if (r == FAIL)
1778 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001779 break;
1780 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001781 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001782}
1783
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001784 static void *
1785py_memsave(void *p, size_t len)
1786{
1787 void *r;
1788 if (!(r = PyMem_Malloc(len)))
1789 return NULL;
1790 mch_memmove(r, p, len);
1791 return r;
1792}
1793
1794#define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1))
1795
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001796 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001797OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001798{
1799 char_u *key;
1800 int flags;
1801 int opt_flags;
1802 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001803 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001804
Bram Moolenaard6e39182013-05-21 18:30:34 +02001805 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001806 return -1;
1807
Bram Moolenaara03e6312013-05-29 22:49:26 +02001808 DICTKEY_GET(-1, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001809
1810 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001811 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001812
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001813 if (flags == 0)
1814 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001815 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001816 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001817 return -1;
1818 }
1819
1820 if (valObject == NULL)
1821 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001822 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001823 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001824 PyErr_SetString(PyExc_ValueError,
1825 _("unable to unset global option"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001826 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001827 return -1;
1828 }
1829 else if (!(flags & SOPT_GLOBAL))
1830 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001831 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1832 "without global value"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001833 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001834 return -1;
1835 }
1836 else
1837 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001838 unset_global_local_option(key, self->from);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001839 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001840 return 0;
1841 }
1842 }
1843
Bram Moolenaard6e39182013-05-21 18:30:34 +02001844 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001845
1846 if (flags & SOPT_BOOL)
1847 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001848 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02001849
Bram Moolenaarb983f752013-05-15 16:11:50 +02001850 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001851 r = -1;
1852 else
1853 r = set_option_value_for(key, istrue, NULL,
1854 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001855 }
1856 else if (flags & SOPT_NUM)
1857 {
1858 int val;
1859
1860#if PY_MAJOR_VERSION < 3
1861 if (PyInt_Check(valObject))
1862 val = PyInt_AsLong(valObject);
1863 else
1864#endif
1865 if (PyLong_Check(valObject))
1866 val = PyLong_AsLong(valObject);
1867 else
1868 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001869 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001870 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001871 return -1;
1872 }
1873
1874 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02001875 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001876 }
1877 else
1878 {
1879 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001880 PyObject *todecref;
1881
1882 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001883 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001884 r = set_option_value_for(key, 0, val, opt_flags,
1885 self->opt_type, self->from);
1886 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001887 }
1888 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001889 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001890 }
1891
Bram Moolenaar1bc24282013-05-29 21:37:35 +02001892 DICTKEY_UNREF
1893
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001894 return r;
1895}
1896
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001897static PyMappingMethods OptionsAsMapping = {
1898 (lenfunc) NULL,
1899 (binaryfunc) OptionsItem,
1900 (objobjargproc) OptionsAssItem,
1901};
1902
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001903/* Tabpage object
1904 */
1905
1906typedef struct
1907{
1908 PyObject_HEAD
1909 tabpage_T *tab;
1910} TabPageObject;
1911
1912static PyObject *WinListNew(TabPageObject *tabObject);
1913
1914static PyTypeObject TabPageType;
1915
1916 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001917CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001918{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001919 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001920 {
1921 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1922 return -1;
1923 }
1924
1925 return 0;
1926}
1927
1928 static PyObject *
1929TabPageNew(tabpage_T *tab)
1930{
1931 TabPageObject *self;
1932
1933 if (TAB_PYTHON_REF(tab))
1934 {
1935 self = TAB_PYTHON_REF(tab);
1936 Py_INCREF(self);
1937 }
1938 else
1939 {
1940 self = PyObject_NEW(TabPageObject, &TabPageType);
1941 if (self == NULL)
1942 return NULL;
1943 self->tab = tab;
1944 TAB_PYTHON_REF(tab) = self;
1945 }
1946
1947 return (PyObject *)(self);
1948}
1949
1950 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001951TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001952{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001953 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
1954 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001955
1956 DESTRUCTOR_FINISH(self);
1957}
1958
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001959static char *TabPageAttrs[] = {
1960 "windows", "number", "vars", "window", "valid",
1961 NULL
1962};
1963
1964 static PyObject *
1965TabPageDir(PyObject *self)
1966{
1967 return ObjectDir(self, TabPageAttrs);
1968}
1969
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001970 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001971TabPageAttrValid(TabPageObject *self, char *name)
1972{
1973 PyObject *r;
1974
1975 if (strcmp(name, "valid") != 0)
1976 return NULL;
1977
1978 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
1979 Py_INCREF(r);
1980 return r;
1981}
1982
1983 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001984TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001985{
1986 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001987 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001988 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001989 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001990 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001991 return DictionaryNew(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001992 else if (strcmp(name, "window") == 0)
1993 {
1994 /* For current tab window.c does not bother to set or update tp_curwin
1995 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02001996 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001997 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001998 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001999 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002000 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002001 else if (strcmp(name, "__members__") == 0)
2002 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002003 return NULL;
2004}
2005
2006 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002007TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002008{
2009 static char repr[100];
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002010
Bram Moolenaard6e39182013-05-21 18:30:34 +02002011 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002012 {
2013 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
2014 return PyString_FromString(repr);
2015 }
2016 else
2017 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002018 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002019
2020 if (t == 0)
2021 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
2022 (self));
2023 else
2024 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
2025
2026 return PyString_FromString(repr);
2027 }
2028}
2029
2030static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002031 /* name, function, calling, documentation */
2032 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2033 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002034};
2035
2036/*
2037 * Window list object
2038 */
2039
2040static PyTypeObject TabListType;
2041static PySequenceMethods TabListAsSeq;
2042
2043typedef struct
2044{
2045 PyObject_HEAD
2046} TabListObject;
2047
2048 static PyInt
2049TabListLength(PyObject *self UNUSED)
2050{
2051 tabpage_T *tp = first_tabpage;
2052 PyInt n = 0;
2053
2054 while (tp != NULL)
2055 {
2056 ++n;
2057 tp = tp->tp_next;
2058 }
2059
2060 return n;
2061}
2062
2063 static PyObject *
2064TabListItem(PyObject *self UNUSED, PyInt n)
2065{
2066 tabpage_T *tp;
2067
2068 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2069 if (n == 0)
2070 return TabPageNew(tp);
2071
2072 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2073 return NULL;
2074}
2075
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002076/* Window object
2077 */
2078
2079typedef struct
2080{
2081 PyObject_HEAD
2082 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002083 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002084} WindowObject;
2085
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002086static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002087
2088 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002089CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002090{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002091 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002092 {
2093 PyErr_SetVim(_("attempt to refer to deleted window"));
2094 return -1;
2095 }
2096
2097 return 0;
2098}
2099
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002100 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002101WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002102{
2103 /* We need to handle deletion of windows underneath us.
2104 * If we add a "w_python*_ref" field to the win_T structure,
2105 * then we can get at it in win_free() in vim. We then
2106 * need to create only ONE Python object per window - if
2107 * we try to create a second, just INCREF the existing one
2108 * and return it. The (single) Python object referring to
2109 * the window is stored in "w_python*_ref".
2110 * On a win_free() we set the Python object's win_T* field
2111 * to an invalid value. We trap all uses of a window
2112 * object, and reject them if the win_T* field is invalid.
2113 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002114 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002115 * w_python_ref and w_python3_ref fields respectively.
2116 */
2117
2118 WindowObject *self;
2119
2120 if (WIN_PYTHON_REF(win))
2121 {
2122 self = WIN_PYTHON_REF(win);
2123 Py_INCREF(self);
2124 }
2125 else
2126 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002127 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002128 if (self == NULL)
2129 return NULL;
2130 self->win = win;
2131 WIN_PYTHON_REF(win) = self;
2132 }
2133
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002134 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2135
Bram Moolenaar971db462013-05-12 18:44:48 +02002136 return (PyObject *)(self);
2137}
2138
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002139 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002140WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002141{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002142 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002143 if (self->win && self->win != INVALID_WINDOW_VALUE)
2144 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002145 Py_XDECREF(((PyObject *)(self->tabObject)));
2146 PyObject_GC_Del((void *)(self));
2147}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002148
Bram Moolenaar774267b2013-05-21 20:51:59 +02002149 static int
2150WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2151{
2152 Py_VISIT(((PyObject *)(self->tabObject)));
2153 return 0;
2154}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002155
Bram Moolenaar774267b2013-05-21 20:51:59 +02002156 static int
2157WindowClear(WindowObject *self)
2158{
2159 Py_CLEAR(self->tabObject);
2160 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002161}
2162
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002163 static win_T *
2164get_firstwin(TabPageObject *tabObject)
2165{
2166 if (tabObject)
2167 {
2168 if (CheckTabPage(tabObject))
2169 return NULL;
2170 /* For current tab window.c does not bother to set or update tp_firstwin
2171 */
2172 else if (tabObject->tab == curtab)
2173 return firstwin;
2174 else
2175 return tabObject->tab->tp_firstwin;
2176 }
2177 else
2178 return firstwin;
2179}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002180static char *WindowAttrs[] = {
2181 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2182 "tabpage", "valid",
2183 NULL
2184};
2185
2186 static PyObject *
2187WindowDir(PyObject *self)
2188{
2189 return ObjectDir(self, WindowAttrs);
2190}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002191
Bram Moolenaar971db462013-05-12 18:44:48 +02002192 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002193WindowAttrValid(WindowObject *self, char *name)
2194{
2195 PyObject *r;
2196
2197 if (strcmp(name, "valid") != 0)
2198 return NULL;
2199
2200 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2201 Py_INCREF(r);
2202 return r;
2203}
2204
2205 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002206WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002207{
2208 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002209 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002210 else if (strcmp(name, "cursor") == 0)
2211 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002212 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002213
2214 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2215 }
2216 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002217 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002218#ifdef FEAT_WINDOWS
2219 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002220 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002221#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002222#ifdef FEAT_VERTSPLIT
2223 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002224 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002225 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002226 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002227#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002228 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002229 return DictionaryNew(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002230 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002231 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2232 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002233 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002234 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002235 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002236 return NULL;
2237 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002238 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002239 }
2240 else if (strcmp(name, "tabpage") == 0)
2241 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002242 Py_INCREF(self->tabObject);
2243 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002244 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002245 else if (strcmp(name, "__members__") == 0)
2246 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002247 else
2248 return NULL;
2249}
2250
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002251 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002252WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002253{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002254 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002255 return -1;
2256
2257 if (strcmp(name, "buffer") == 0)
2258 {
2259 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2260 return -1;
2261 }
2262 else if (strcmp(name, "cursor") == 0)
2263 {
2264 long lnum;
2265 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002266
2267 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2268 return -1;
2269
Bram Moolenaard6e39182013-05-21 18:30:34 +02002270 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002271 {
2272 PyErr_SetVim(_("cursor position outside buffer"));
2273 return -1;
2274 }
2275
2276 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002277 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002278 return -1;
2279
Bram Moolenaard6e39182013-05-21 18:30:34 +02002280 self->win->w_cursor.lnum = lnum;
2281 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002282#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002283 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002284#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002285 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002286 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002287
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002288 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002289 return 0;
2290 }
2291 else if (strcmp(name, "height") == 0)
2292 {
2293 int height;
2294 win_T *savewin;
2295
2296 if (!PyArg_Parse(val, "i", &height))
2297 return -1;
2298
2299#ifdef FEAT_GUI
2300 need_mouse_correct = TRUE;
2301#endif
2302 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002303 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002304
2305 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002306 win_setheight(height);
2307 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002308 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002309 return -1;
2310
2311 return 0;
2312 }
2313#ifdef FEAT_VERTSPLIT
2314 else if (strcmp(name, "width") == 0)
2315 {
2316 int width;
2317 win_T *savewin;
2318
2319 if (!PyArg_Parse(val, "i", &width))
2320 return -1;
2321
2322#ifdef FEAT_GUI
2323 need_mouse_correct = TRUE;
2324#endif
2325 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002326 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002327
2328 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002329 win_setwidth(width);
2330 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002331 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002332 return -1;
2333
2334 return 0;
2335 }
2336#endif
2337 else
2338 {
2339 PyErr_SetString(PyExc_AttributeError, name);
2340 return -1;
2341 }
2342}
2343
2344 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002345WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002346{
2347 static char repr[100];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002348
Bram Moolenaard6e39182013-05-21 18:30:34 +02002349 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002350 {
2351 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2352 return PyString_FromString(repr);
2353 }
2354 else
2355 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002356 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002357
Bram Moolenaar6d216452013-05-12 19:00:41 +02002358 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002359 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2360 (self));
2361 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002362 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002363
2364 return PyString_FromString(repr);
2365 }
2366}
2367
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002368static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002369 /* name, function, calling, documentation */
2370 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2371 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002372};
2373
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002374/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002375 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002376 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002377
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002378static PyTypeObject WinListType;
2379static PySequenceMethods WinListAsSeq;
2380
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002381typedef struct
2382{
2383 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002384 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002385} WinListObject;
2386
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002387 static PyObject *
2388WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002389{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002390 WinListObject *self;
2391
2392 self = PyObject_NEW(WinListObject, &WinListType);
2393 self->tabObject = tabObject;
2394 Py_INCREF(tabObject);
2395
2396 return (PyObject *)(self);
2397}
2398
2399 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002400WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002401{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002402 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002403
2404 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02002405 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002406 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02002407 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002408
2409 DESTRUCTOR_FINISH(self);
2410}
2411
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002412 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002413WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002414{
2415 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002416 PyInt n = 0;
2417
Bram Moolenaard6e39182013-05-21 18:30:34 +02002418 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002419 return -1;
2420
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002421 while (w != NULL)
2422 {
2423 ++n;
2424 w = W_NEXT(w);
2425 }
2426
2427 return n;
2428}
2429
2430 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002431WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002432{
2433 win_T *w;
2434
Bram Moolenaard6e39182013-05-21 18:30:34 +02002435 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002436 return NULL;
2437
2438 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002439 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002440 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002441
2442 PyErr_SetString(PyExc_IndexError, _("no such window"));
2443 return NULL;
2444}
2445
2446/* Convert a Python string into a Vim line.
2447 *
2448 * The result is in allocated memory. All internal nulls are replaced by
2449 * newline characters. It is an error for the string to contain newline
2450 * characters.
2451 *
2452 * On errors, the Python exception data is set, and NULL is returned.
2453 */
2454 static char *
2455StringToLine(PyObject *obj)
2456{
2457 const char *str;
2458 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002459 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002460 PyInt len;
2461 PyInt i;
2462 char *p;
2463
2464 if (obj == NULL || !PyString_Check(obj))
2465 {
2466 PyErr_BadArgument();
2467 return NULL;
2468 }
2469
Bram Moolenaar19e60942011-06-19 00:27:51 +02002470 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2471 str = PyString_AsString(bytes);
2472 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002473
2474 /*
2475 * Error checking: String must not contain newlines, as we
2476 * are replacing a single line, and we must replace it with
2477 * a single line.
2478 * A trailing newline is removed, so that append(f.readlines()) works.
2479 */
2480 p = memchr(str, '\n', len);
2481 if (p != NULL)
2482 {
2483 if (p == str + len - 1)
2484 --len;
2485 else
2486 {
2487 PyErr_SetVim(_("string cannot contain newlines"));
2488 return NULL;
2489 }
2490 }
2491
2492 /* Create a copy of the string, with internal nulls replaced by
2493 * newline characters, as is the vim convention.
2494 */
2495 save = (char *)alloc((unsigned)(len+1));
2496 if (save == NULL)
2497 {
2498 PyErr_NoMemory();
2499 return NULL;
2500 }
2501
2502 for (i = 0; i < len; ++i)
2503 {
2504 if (str[i] == '\0')
2505 save[i] = '\n';
2506 else
2507 save[i] = str[i];
2508 }
2509
2510 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002511 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002512
2513 return save;
2514}
2515
2516/* Get a line from the specified buffer. The line number is
2517 * in Vim format (1-based). The line is returned as a Python
2518 * string object.
2519 */
2520 static PyObject *
2521GetBufferLine(buf_T *buf, PyInt n)
2522{
2523 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2524}
2525
2526
2527/* Get a list of lines from the specified buffer. The line numbers
2528 * are in Vim format (1-based). The range is from lo up to, but not
2529 * including, hi. The list is returned as a Python list of string objects.
2530 */
2531 static PyObject *
2532GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2533{
2534 PyInt i;
2535 PyInt n = hi - lo;
2536 PyObject *list = PyList_New(n);
2537
2538 if (list == NULL)
2539 return NULL;
2540
2541 for (i = 0; i < n; ++i)
2542 {
2543 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2544
2545 /* Error check - was the Python string creation OK? */
2546 if (str == NULL)
2547 {
2548 Py_DECREF(list);
2549 return NULL;
2550 }
2551
2552 /* Set the list item */
2553 if (PyList_SetItem(list, i, str))
2554 {
2555 Py_DECREF(str);
2556 Py_DECREF(list);
2557 return NULL;
2558 }
2559 }
2560
2561 /* The ownership of the Python list is passed to the caller (ie,
2562 * the caller should Py_DECREF() the object when it is finished
2563 * with it).
2564 */
2565
2566 return list;
2567}
2568
2569/*
2570 * Check if deleting lines made the cursor position invalid.
2571 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2572 * deleted).
2573 */
2574 static void
2575py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2576{
2577 if (curwin->w_cursor.lnum >= lo)
2578 {
2579 /* Adjust the cursor position if it's in/after the changed
2580 * lines. */
2581 if (curwin->w_cursor.lnum >= hi)
2582 {
2583 curwin->w_cursor.lnum += extra;
2584 check_cursor_col();
2585 }
2586 else if (extra < 0)
2587 {
2588 curwin->w_cursor.lnum = lo;
2589 check_cursor();
2590 }
2591 else
2592 check_cursor_col();
2593 changed_cline_bef_curs();
2594 }
2595 invalidate_botline();
2596}
2597
Bram Moolenaar19e60942011-06-19 00:27:51 +02002598/*
2599 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002600 * in Vim format (1-based). The replacement line is given as
2601 * a Python string object. The object is checked for validity
2602 * and correct format. Errors are returned as a value of FAIL.
2603 * The return value is OK on success.
2604 * If OK is returned and len_change is not NULL, *len_change
2605 * is set to the change in the buffer length.
2606 */
2607 static int
2608SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2609{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002610 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002611 * There are three cases:
2612 * 1. NULL, or None - this is a deletion.
2613 * 2. A string - this is a replacement.
2614 * 3. Anything else - this is an error.
2615 */
2616 if (line == Py_None || line == NULL)
2617 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002618 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002619
2620 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002621 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002622
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002623 VimTryStart();
2624
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002625 if (u_savedel((linenr_T)n, 1L) == FAIL)
2626 PyErr_SetVim(_("cannot save undo information"));
2627 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2628 PyErr_SetVim(_("cannot delete line"));
2629 else
2630 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002631 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002632 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2633 deleted_lines_mark((linenr_T)n, 1L);
2634 }
2635
Bram Moolenaar105bc352013-05-17 16:03:57 +02002636 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002637
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002638 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002639 return FAIL;
2640
2641 if (len_change)
2642 *len_change = -1;
2643
2644 return OK;
2645 }
2646 else if (PyString_Check(line))
2647 {
2648 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002649 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002650
2651 if (save == NULL)
2652 return FAIL;
2653
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002654 VimTryStart();
2655
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002656 /* We do not need to free "save" if ml_replace() consumes it. */
2657 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002658 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002659
2660 if (u_savesub((linenr_T)n) == FAIL)
2661 {
2662 PyErr_SetVim(_("cannot save undo information"));
2663 vim_free(save);
2664 }
2665 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2666 {
2667 PyErr_SetVim(_("cannot replace line"));
2668 vim_free(save);
2669 }
2670 else
2671 changed_bytes((linenr_T)n, 0);
2672
Bram Moolenaar105bc352013-05-17 16:03:57 +02002673 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002674
2675 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002676 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002677 check_cursor_col();
2678
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002679 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002680 return FAIL;
2681
2682 if (len_change)
2683 *len_change = 0;
2684
2685 return OK;
2686 }
2687 else
2688 {
2689 PyErr_BadArgument();
2690 return FAIL;
2691 }
2692}
2693
Bram Moolenaar19e60942011-06-19 00:27:51 +02002694/* Replace a range of lines in the specified buffer. The line numbers are in
2695 * Vim format (1-based). The range is from lo up to, but not including, hi.
2696 * The replacement lines are given as a Python list of string objects. The
2697 * list is checked for validity and correct format. Errors are returned as a
2698 * value of FAIL. The return value is OK on success.
2699 * If OK is returned and len_change is not NULL, *len_change
2700 * is set to the change in the buffer length.
2701 */
2702 static int
2703SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2704{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002705 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002706 * There are three cases:
2707 * 1. NULL, or None - this is a deletion.
2708 * 2. A list - this is a replacement.
2709 * 3. Anything else - this is an error.
2710 */
2711 if (list == Py_None || list == NULL)
2712 {
2713 PyInt i;
2714 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002715 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002716
2717 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002718 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002719 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002720
2721 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2722 PyErr_SetVim(_("cannot save undo information"));
2723 else
2724 {
2725 for (i = 0; i < n; ++i)
2726 {
2727 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2728 {
2729 PyErr_SetVim(_("cannot delete line"));
2730 break;
2731 }
2732 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002733 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002734 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2735 deleted_lines_mark((linenr_T)lo, (long)i);
2736 }
2737
Bram Moolenaar105bc352013-05-17 16:03:57 +02002738 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002739
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002740 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002741 return FAIL;
2742
2743 if (len_change)
2744 *len_change = -n;
2745
2746 return OK;
2747 }
2748 else if (PyList_Check(list))
2749 {
2750 PyInt i;
2751 PyInt new_len = PyList_Size(list);
2752 PyInt old_len = hi - lo;
2753 PyInt extra = 0; /* lines added to text, can be negative */
2754 char **array;
2755 buf_T *savebuf;
2756
2757 if (new_len == 0) /* avoid allocating zero bytes */
2758 array = NULL;
2759 else
2760 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002761 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002762 if (array == NULL)
2763 {
2764 PyErr_NoMemory();
2765 return FAIL;
2766 }
2767 }
2768
2769 for (i = 0; i < new_len; ++i)
2770 {
2771 PyObject *line = PyList_GetItem(list, i);
2772
2773 array[i] = StringToLine(line);
2774 if (array[i] == NULL)
2775 {
2776 while (i)
2777 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002778 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002779 return FAIL;
2780 }
2781 }
2782
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002783 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02002784 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002785
2786 // START of region without "return". Must call restore_buffer()!
2787 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002788
2789 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2790 PyErr_SetVim(_("cannot save undo information"));
2791
2792 /* If the size of the range is reducing (ie, new_len < old_len) we
2793 * need to delete some old_len. We do this at the start, by
2794 * repeatedly deleting line "lo".
2795 */
2796 if (!PyErr_Occurred())
2797 {
2798 for (i = 0; i < old_len - new_len; ++i)
2799 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2800 {
2801 PyErr_SetVim(_("cannot delete line"));
2802 break;
2803 }
2804 extra -= i;
2805 }
2806
2807 /* For as long as possible, replace the existing old_len with the
2808 * new old_len. This is a more efficient operation, as it requires
2809 * less memory allocation and freeing.
2810 */
2811 if (!PyErr_Occurred())
2812 {
2813 for (i = 0; i < old_len && i < new_len; ++i)
2814 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2815 == FAIL)
2816 {
2817 PyErr_SetVim(_("cannot replace line"));
2818 break;
2819 }
2820 }
2821 else
2822 i = 0;
2823
2824 /* Now we may need to insert the remaining new old_len. If we do, we
2825 * must free the strings as we finish with them (we can't pass the
2826 * responsibility to vim in this case).
2827 */
2828 if (!PyErr_Occurred())
2829 {
2830 while (i < new_len)
2831 {
2832 if (ml_append((linenr_T)(lo + i - 1),
2833 (char_u *)array[i], 0, FALSE) == FAIL)
2834 {
2835 PyErr_SetVim(_("cannot insert line"));
2836 break;
2837 }
2838 vim_free(array[i]);
2839 ++i;
2840 ++extra;
2841 }
2842 }
2843
2844 /* Free any left-over old_len, as a result of an error */
2845 while (i < new_len)
2846 {
2847 vim_free(array[i]);
2848 ++i;
2849 }
2850
2851 /* Free the array of old_len. All of its contents have now
2852 * been dealt with (either freed, or the responsibility passed
2853 * to vim.
2854 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002855 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002856
2857 /* Adjust marks. Invalidate any which lie in the
2858 * changed range, and move any in the remainder of the buffer.
2859 */
2860 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2861 (long)MAXLNUM, (long)extra);
2862 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2863
Bram Moolenaar105bc352013-05-17 16:03:57 +02002864 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002865 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2866
Bram Moolenaar105bc352013-05-17 16:03:57 +02002867 // END of region without "return".
2868 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002869
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002870 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02002871 return FAIL;
2872
2873 if (len_change)
2874 *len_change = new_len - old_len;
2875
2876 return OK;
2877 }
2878 else
2879 {
2880 PyErr_BadArgument();
2881 return FAIL;
2882 }
2883}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002884
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002885/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002886 * The line number is in Vim format (1-based). The lines to be inserted are
2887 * given as a Python list of string objects or as a single string. The lines
2888 * to be added are checked for validity and correct format. Errors are
2889 * returned as a value of FAIL. The return value is OK on success.
2890 * If OK is returned and len_change is not NULL, *len_change
2891 * is set to the change in the buffer length.
2892 */
2893 static int
2894InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2895{
2896 /* First of all, we check the type of the supplied Python object.
2897 * It must be a string or a list, or the call is in error.
2898 */
2899 if (PyString_Check(lines))
2900 {
2901 char *str = StringToLine(lines);
2902 buf_T *savebuf;
2903
2904 if (str == NULL)
2905 return FAIL;
2906
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002907 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002908 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002909 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002910
2911 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2912 PyErr_SetVim(_("cannot save undo information"));
2913 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2914 PyErr_SetVim(_("cannot insert line"));
2915 else
2916 appended_lines_mark((linenr_T)n, 1L);
2917
2918 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002919 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002920 update_screen(VALID);
2921
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002922 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002923 return FAIL;
2924
2925 if (len_change)
2926 *len_change = 1;
2927
2928 return OK;
2929 }
2930 else if (PyList_Check(lines))
2931 {
2932 PyInt i;
2933 PyInt size = PyList_Size(lines);
2934 char **array;
2935 buf_T *savebuf;
2936
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002937 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002938 if (array == NULL)
2939 {
2940 PyErr_NoMemory();
2941 return FAIL;
2942 }
2943
2944 for (i = 0; i < size; ++i)
2945 {
2946 PyObject *line = PyList_GetItem(lines, i);
2947 array[i] = StringToLine(line);
2948
2949 if (array[i] == NULL)
2950 {
2951 while (i)
2952 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002953 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002954 return FAIL;
2955 }
2956 }
2957
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002958 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002959 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002960 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002961
2962 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2963 PyErr_SetVim(_("cannot save undo information"));
2964 else
2965 {
2966 for (i = 0; i < size; ++i)
2967 {
2968 if (ml_append((linenr_T)(n + i),
2969 (char_u *)array[i], 0, FALSE) == FAIL)
2970 {
2971 PyErr_SetVim(_("cannot insert line"));
2972
2973 /* Free the rest of the lines */
2974 while (i < size)
2975 vim_free(array[i++]);
2976
2977 break;
2978 }
2979 vim_free(array[i]);
2980 }
2981 if (i > 0)
2982 appended_lines_mark((linenr_T)n, (long)i);
2983 }
2984
2985 /* Free the array of lines. All of its contents have now
2986 * been freed.
2987 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002988 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002989
Bram Moolenaar105bc352013-05-17 16:03:57 +02002990 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002991 update_screen(VALID);
2992
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002993 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002994 return FAIL;
2995
2996 if (len_change)
2997 *len_change = size;
2998
2999 return OK;
3000 }
3001 else
3002 {
3003 PyErr_BadArgument();
3004 return FAIL;
3005 }
3006}
3007
3008/*
3009 * Common routines for buffers and line ranges
3010 * -------------------------------------------
3011 */
3012
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003013typedef struct
3014{
3015 PyObject_HEAD
3016 buf_T *buf;
3017} BufferObject;
3018
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003019 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003020CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003021{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003022 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003023 {
3024 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3025 return -1;
3026 }
3027
3028 return 0;
3029}
3030
3031 static PyObject *
3032RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3033{
3034 if (CheckBuffer(self))
3035 return NULL;
3036
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003037 if (end == -1)
3038 end = self->buf->b_ml.ml_line_count;
3039
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003040 if (n < 0)
3041 n += end - start + 1;
3042
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003043 if (n < 0 || n > end - start)
3044 {
3045 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3046 return NULL;
3047 }
3048
3049 return GetBufferLine(self->buf, n+start);
3050}
3051
3052 static PyObject *
3053RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3054{
3055 PyInt size;
3056
3057 if (CheckBuffer(self))
3058 return NULL;
3059
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003060 if (end == -1)
3061 end = self->buf->b_ml.ml_line_count;
3062
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003063 size = end - start + 1;
3064
3065 if (lo < 0)
3066 lo = 0;
3067 else if (lo > size)
3068 lo = size;
3069 if (hi < 0)
3070 hi = 0;
3071 if (hi < lo)
3072 hi = lo;
3073 else if (hi > size)
3074 hi = size;
3075
3076 return GetBufferLineList(self->buf, lo+start, hi+start);
3077}
3078
3079 static PyInt
3080RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3081{
3082 PyInt len_change;
3083
3084 if (CheckBuffer(self))
3085 return -1;
3086
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003087 if (end == -1)
3088 end = self->buf->b_ml.ml_line_count;
3089
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003090 if (n < 0)
3091 n += end - start + 1;
3092
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003093 if (n < 0 || n > end - start)
3094 {
3095 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3096 return -1;
3097 }
3098
3099 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3100 return -1;
3101
3102 if (new_end)
3103 *new_end = end + len_change;
3104
3105 return 0;
3106}
3107
Bram Moolenaar19e60942011-06-19 00:27:51 +02003108 static PyInt
3109RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3110{
3111 PyInt size;
3112 PyInt len_change;
3113
3114 /* Self must be a valid buffer */
3115 if (CheckBuffer(self))
3116 return -1;
3117
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003118 if (end == -1)
3119 end = self->buf->b_ml.ml_line_count;
3120
Bram Moolenaar19e60942011-06-19 00:27:51 +02003121 /* Sort out the slice range */
3122 size = end - start + 1;
3123
3124 if (lo < 0)
3125 lo = 0;
3126 else if (lo > size)
3127 lo = size;
3128 if (hi < 0)
3129 hi = 0;
3130 if (hi < lo)
3131 hi = lo;
3132 else if (hi > size)
3133 hi = size;
3134
3135 if (SetBufferLineList(self->buf, lo + start, hi + start,
3136 val, &len_change) == FAIL)
3137 return -1;
3138
3139 if (new_end)
3140 *new_end = end + len_change;
3141
3142 return 0;
3143}
3144
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003145
3146 static PyObject *
3147RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3148{
3149 PyObject *lines;
3150 PyInt len_change;
3151 PyInt max;
3152 PyInt n;
3153
3154 if (CheckBuffer(self))
3155 return NULL;
3156
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003157 if (end == -1)
3158 end = self->buf->b_ml.ml_line_count;
3159
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003160 max = n = end - start + 1;
3161
3162 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3163 return NULL;
3164
3165 if (n < 0 || n > max)
3166 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003167 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003168 return NULL;
3169 }
3170
3171 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3172 return NULL;
3173
3174 if (new_end)
3175 *new_end = end + len_change;
3176
3177 Py_INCREF(Py_None);
3178 return Py_None;
3179}
3180
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003181/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003182 */
3183
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003184static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003185static PySequenceMethods RangeAsSeq;
3186static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003187
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003188typedef struct
3189{
3190 PyObject_HEAD
3191 BufferObject *buf;
3192 PyInt start;
3193 PyInt end;
3194} RangeObject;
3195
3196 static PyObject *
3197RangeNew(buf_T *buf, PyInt start, PyInt end)
3198{
3199 BufferObject *bufr;
3200 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003201 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003202 if (self == NULL)
3203 return NULL;
3204
3205 bufr = (BufferObject *)BufferNew(buf);
3206 if (bufr == NULL)
3207 {
3208 Py_DECREF(self);
3209 return NULL;
3210 }
3211 Py_INCREF(bufr);
3212
3213 self->buf = bufr;
3214 self->start = start;
3215 self->end = end;
3216
3217 return (PyObject *)(self);
3218}
3219
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003220 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003221RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003222{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003223 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003224 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003225 PyObject_GC_Del((void *)(self));
3226}
3227
3228 static int
3229RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3230{
3231 Py_VISIT(((PyObject *)(self->buf)));
3232 return 0;
3233}
3234
3235 static int
3236RangeClear(RangeObject *self)
3237{
3238 Py_CLEAR(self->buf);
3239 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003240}
3241
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003242 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003243RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003244{
3245 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003246 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003247 return -1; /* ??? */
3248
Bram Moolenaard6e39182013-05-21 18:30:34 +02003249 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003250}
3251
3252 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003253RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003254{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003255 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003256}
3257
3258 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003259RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003260{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003261 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003262}
3263
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003264static char *RangeAttrs[] = {
3265 "start", "end",
3266 NULL
3267};
3268
3269 static PyObject *
3270RangeDir(PyObject *self)
3271{
3272 return ObjectDir(self, RangeAttrs);
3273}
3274
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003275 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003276RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003277{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003278 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003279}
3280
3281 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003282RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003283{
3284 static char repr[100];
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003285
Bram Moolenaard6e39182013-05-21 18:30:34 +02003286 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003287 {
3288 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
3289 (self));
3290 return PyString_FromString(repr);
3291 }
3292 else
3293 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003294 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003295 int len;
3296
3297 if (name == NULL)
3298 name = "";
3299 len = (int)strlen(name);
3300
3301 if (len > 45)
3302 name = name + (45 - len);
3303
3304 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
3305 len > 45 ? "..." : "", name,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003306 self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003307
3308 return PyString_FromString(repr);
3309 }
3310}
3311
3312static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003313 /* name, function, calling, documentation */
3314 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003315 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3316 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003317};
3318
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003319static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003320static PySequenceMethods BufferAsSeq;
3321static PyMappingMethods BufferAsMapping;
3322
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003323 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003324BufferNew(buf_T *buf)
3325{
3326 /* We need to handle deletion of buffers underneath us.
3327 * If we add a "b_python*_ref" field to the buf_T structure,
3328 * then we can get at it in buf_freeall() in vim. We then
3329 * need to create only ONE Python object per buffer - if
3330 * we try to create a second, just INCREF the existing one
3331 * and return it. The (single) Python object referring to
3332 * the buffer is stored in "b_python*_ref".
3333 * Question: what to do on a buf_freeall(). We'll probably
3334 * have to either delete the Python object (DECREF it to
3335 * zero - a bad idea, as it leaves dangling refs!) or
3336 * set the buf_T * value to an invalid value (-1?), which
3337 * means we need checks in all access functions... Bah.
3338 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003339 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003340 * b_python_ref and b_python3_ref fields respectively.
3341 */
3342
3343 BufferObject *self;
3344
3345 if (BUF_PYTHON_REF(buf) != NULL)
3346 {
3347 self = BUF_PYTHON_REF(buf);
3348 Py_INCREF(self);
3349 }
3350 else
3351 {
3352 self = PyObject_NEW(BufferObject, &BufferType);
3353 if (self == NULL)
3354 return NULL;
3355 self->buf = buf;
3356 BUF_PYTHON_REF(buf) = self;
3357 }
3358
3359 return (PyObject *)(self);
3360}
3361
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003362 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003363BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003364{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003365 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3366 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003367
3368 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003369}
3370
Bram Moolenaar971db462013-05-12 18:44:48 +02003371 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003372BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003373{
3374 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003375 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003376 return -1; /* ??? */
3377
Bram Moolenaard6e39182013-05-21 18:30:34 +02003378 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003379}
3380
3381 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003382BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003383{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003384 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003385}
3386
3387 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003388BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003389{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003390 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003391}
3392
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003393static char *BufferAttrs[] = {
3394 "name", "number", "vars", "options", "valid",
3395 NULL
3396};
3397
3398 static PyObject *
3399BufferDir(PyObject *self)
3400{
3401 return ObjectDir(self, BufferAttrs);
3402}
3403
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003404 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003405BufferAttrValid(BufferObject *self, char *name)
3406{
3407 PyObject *r;
3408
3409 if (strcmp(name, "valid") != 0)
3410 return NULL;
3411
3412 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
3413 Py_INCREF(r);
3414 return r;
3415}
3416
3417 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003418BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003419{
3420 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02003421 return PyString_FromString((self->buf->b_ffname == NULL
3422 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003423 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003424 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003425 else if (strcmp(name, "vars") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003426 return DictionaryNew(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003427 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003428 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3429 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003430 else if (strcmp(name, "__members__") == 0)
3431 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003432 else
3433 return NULL;
3434}
3435
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003436 static int
3437BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3438{
3439 if (CheckBuffer(self))
3440 return -1;
3441
3442 if (strcmp(name, "name") == 0)
3443 {
3444 char_u *val;
3445 aco_save_T aco;
3446 int r;
3447 PyObject *todecref;
3448
3449 if (!(val = StringToChars(valObject, &todecref)))
3450 return -1;
3451
3452 VimTryStart();
3453 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3454 aucmd_prepbuf(&aco, self->buf);
3455 r = rename_buffer(val);
3456 aucmd_restbuf(&aco);
3457 Py_XDECREF(todecref);
3458 if (VimTryEnd())
3459 return -1;
3460
3461 if (r == FAIL)
3462 {
3463 PyErr_SetVim(_("failed to rename buffer"));
3464 return -1;
3465 }
3466 return 0;
3467 }
3468 else
3469 {
3470 PyErr_SetString(PyExc_AttributeError, name);
3471 return -1;
3472 }
3473}
3474
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003475 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003476BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003477{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003478 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003479}
3480
3481 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003482BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003483{
3484 pos_T *posp;
3485 char *pmark;
3486 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003487 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003488
Bram Moolenaard6e39182013-05-21 18:30:34 +02003489 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003490 return NULL;
3491
3492 if (!PyArg_ParseTuple(args, "s", &pmark))
3493 return NULL;
3494 mark = *pmark;
3495
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003496 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02003497 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003498 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003499 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003500 if (VimTryEnd())
3501 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003502
3503 if (posp == NULL)
3504 {
3505 PyErr_SetVim(_("invalid mark name"));
3506 return NULL;
3507 }
3508
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003509 if (posp->lnum <= 0)
3510 {
3511 /* Or raise an error? */
3512 Py_INCREF(Py_None);
3513 return Py_None;
3514 }
3515
3516 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3517}
3518
3519 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003520BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003521{
3522 PyInt start;
3523 PyInt end;
3524
Bram Moolenaard6e39182013-05-21 18:30:34 +02003525 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003526 return NULL;
3527
3528 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3529 return NULL;
3530
Bram Moolenaard6e39182013-05-21 18:30:34 +02003531 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003532}
3533
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003534 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003535BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003536{
3537 static char repr[100];
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003538
Bram Moolenaard6e39182013-05-21 18:30:34 +02003539 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003540 {
3541 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3542 return PyString_FromString(repr);
3543 }
3544 else
3545 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003546 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003547 PyInt len;
3548
3549 if (name == NULL)
3550 name = "";
3551 len = strlen(name);
3552
3553 if (len > 35)
3554 name = name + (35 - len);
3555
3556 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3557
3558 return PyString_FromString(repr);
3559 }
3560}
3561
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003562static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003563 /* name, function, calling, documentation */
3564 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
3565 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
3566 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003567 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
3568 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003569};
3570
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003571/*
3572 * Buffer list object - Implementation
3573 */
3574
3575static PyTypeObject BufMapType;
3576
3577typedef struct
3578{
3579 PyObject_HEAD
3580} BufMapObject;
3581
3582 static PyInt
3583BufMapLength(PyObject *self UNUSED)
3584{
3585 buf_T *b = firstbuf;
3586 PyInt n = 0;
3587
3588 while (b)
3589 {
3590 ++n;
3591 b = b->b_next;
3592 }
3593
3594 return n;
3595}
3596
3597 static PyObject *
3598BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3599{
3600 buf_T *b;
3601 int bnr;
3602
3603#if PY_MAJOR_VERSION < 3
3604 if (PyInt_Check(keyObject))
3605 bnr = PyInt_AsLong(keyObject);
3606 else
3607#endif
3608 if (PyLong_Check(keyObject))
3609 bnr = PyLong_AsLong(keyObject);
3610 else
3611 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003612 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003613 return NULL;
3614 }
3615
3616 b = buflist_findnr(bnr);
3617
3618 if (b)
3619 return BufferNew(b);
3620 else
3621 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003622 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003623 return NULL;
3624 }
3625}
3626
3627 static void
3628BufMapIterDestruct(PyObject *buffer)
3629{
3630 /* Iteration was stopped before all buffers were processed */
3631 if (buffer)
3632 {
3633 Py_DECREF(buffer);
3634 }
3635}
3636
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003637 static int
3638BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3639{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003640 if (buffer)
3641 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003642 return 0;
3643}
3644
3645 static int
3646BufMapIterClear(PyObject **buffer)
3647{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003648 if (*buffer)
3649 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003650 return 0;
3651}
3652
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003653 static PyObject *
3654BufMapIterNext(PyObject **buffer)
3655{
3656 PyObject *next;
3657 PyObject *r;
3658
3659 if (!*buffer)
3660 return NULL;
3661
3662 r = *buffer;
3663
3664 if (CheckBuffer((BufferObject *)(r)))
3665 {
3666 *buffer = NULL;
3667 return NULL;
3668 }
3669
3670 if (!((BufferObject *)(r))->buf->b_next)
3671 next = NULL;
3672 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3673 return NULL;
3674 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02003675 /* Do not increment reference: we no longer hold it (decref), but whoever
3676 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003677 return r;
3678}
3679
3680 static PyObject *
3681BufMapIter(PyObject *self UNUSED)
3682{
3683 PyObject *buffer;
3684
3685 buffer = BufferNew(firstbuf);
3686 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003687 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3688 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003689}
3690
3691static PyMappingMethods BufMapAsMapping = {
3692 (lenfunc) BufMapLength,
3693 (binaryfunc) BufMapItem,
3694 (objobjargproc) 0,
3695};
3696
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003697/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003698 */
3699
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003700static char *CurrentAttrs[] = {
3701 "buffer", "window", "line", "range", "tabpage",
3702 NULL
3703};
3704
3705 static PyObject *
3706CurrentDir(PyObject *self)
3707{
3708 return ObjectDir(self, CurrentAttrs);
3709}
3710
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003711 static PyObject *
3712CurrentGetattr(PyObject *self UNUSED, char *name)
3713{
3714 if (strcmp(name, "buffer") == 0)
3715 return (PyObject *)BufferNew(curbuf);
3716 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003717 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003718 else if (strcmp(name, "tabpage") == 0)
3719 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003720 else if (strcmp(name, "line") == 0)
3721 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3722 else if (strcmp(name, "range") == 0)
3723 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003724 else if (strcmp(name, "__members__") == 0)
3725 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003726 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003727#if PY_MAJOR_VERSION < 3
3728 return Py_FindMethod(WindowMethods, self, name);
3729#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003730 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003731#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003732}
3733
3734 static int
3735CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3736{
3737 if (strcmp(name, "line") == 0)
3738 {
3739 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3740 return -1;
3741
3742 return 0;
3743 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003744 else if (strcmp(name, "buffer") == 0)
3745 {
3746 int count;
3747
3748 if (value->ob_type != &BufferType)
3749 {
3750 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3751 return -1;
3752 }
3753
3754 if (CheckBuffer((BufferObject *)(value)))
3755 return -1;
3756 count = ((BufferObject *)(value))->buf->b_fnum;
3757
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003758 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003759 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3760 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003761 if (VimTryEnd())
3762 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003763 PyErr_SetVim(_("failed to switch to given buffer"));
3764 return -1;
3765 }
3766
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003767 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003768 }
3769 else if (strcmp(name, "window") == 0)
3770 {
3771 int count;
3772
3773 if (value->ob_type != &WindowType)
3774 {
3775 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3776 return -1;
3777 }
3778
3779 if (CheckWindow((WindowObject *)(value)))
3780 return -1;
3781 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3782
3783 if (!count)
3784 {
3785 PyErr_SetString(PyExc_ValueError,
3786 _("failed to find window in the current tab page"));
3787 return -1;
3788 }
3789
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003790 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003791 win_goto(((WindowObject *)(value))->win);
3792 if (((WindowObject *)(value))->win != curwin)
3793 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003794 if (VimTryEnd())
3795 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003796 PyErr_SetString(PyExc_RuntimeError,
3797 _("did not switch to the specified window"));
3798 return -1;
3799 }
3800
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003801 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003802 }
3803 else if (strcmp(name, "tabpage") == 0)
3804 {
3805 if (value->ob_type != &TabPageType)
3806 {
3807 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3808 return -1;
3809 }
3810
3811 if (CheckTabPage((TabPageObject *)(value)))
3812 return -1;
3813
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003814 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02003815 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3816 if (((TabPageObject *)(value))->tab != curtab)
3817 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003818 if (VimTryEnd())
3819 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02003820 PyErr_SetString(PyExc_RuntimeError,
3821 _("did not switch to the specified tab page"));
3822 return -1;
3823 }
3824
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003825 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02003826 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003827 else
3828 {
3829 PyErr_SetString(PyExc_AttributeError, name);
3830 return -1;
3831 }
3832}
3833
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003834static struct PyMethodDef CurrentMethods[] = {
3835 /* name, function, calling, documentation */
3836 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
3837 { NULL, NULL, 0, NULL}
3838};
3839
Bram Moolenaardb913952012-06-29 12:54:53 +02003840 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003841init_range_cmd(exarg_T *eap)
3842{
3843 RangeStart = eap->line1;
3844 RangeEnd = eap->line2;
3845}
3846
3847 static void
3848init_range_eval(typval_T *rettv UNUSED)
3849{
3850 RangeStart = (PyInt) curwin->w_cursor.lnum;
3851 RangeEnd = RangeStart;
3852}
3853
3854 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003855run_cmd(const char *cmd, void *arg UNUSED
3856#ifdef PY_CAN_RECURSE
3857 , PyGILState_STATE *pygilstate UNUSED
3858#endif
3859 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003860{
3861 PyRun_SimpleString((char *) cmd);
3862}
3863
3864static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
3865static int code_hdr_len = 30;
3866
3867 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003868run_do(const char *cmd, void *arg UNUSED
3869#ifdef PY_CAN_RECURSE
3870 , PyGILState_STATE *pygilstate
3871#endif
3872 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003873{
3874 PyInt lnum;
3875 size_t len;
3876 char *code;
3877 int status;
3878 PyObject *pyfunc, *pymain;
3879
Bram Moolenaar4ac66762013-05-28 22:31:46 +02003880 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003881 {
3882 EMSG(_("cannot save undo information"));
3883 return;
3884 }
3885
3886 len = code_hdr_len + STRLEN(cmd);
3887 code = PyMem_New(char, len + 1);
3888 memcpy(code, code_hdr, code_hdr_len);
3889 STRCPY(code + code_hdr_len, cmd);
3890 status = PyRun_SimpleString(code);
3891 PyMem_Free(code);
3892
3893 if (status)
3894 {
3895 EMSG(_("failed to run the code"));
3896 return;
3897 }
3898
3899 status = 0;
3900 pymain = PyImport_AddModule("__main__");
3901 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003902#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003903 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003904#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003905
3906 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
3907 {
3908 PyObject *line, *linenr, *ret;
3909
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003910#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003911 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003912#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003913 if (!(line = GetBufferLine(curbuf, lnum)))
3914 goto err;
3915 if (!(linenr = PyInt_FromLong((long) lnum)))
3916 {
3917 Py_DECREF(line);
3918 goto err;
3919 }
3920 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
3921 Py_DECREF(line);
3922 Py_DECREF(linenr);
3923 if (!ret)
3924 goto err;
3925
3926 if (ret != Py_None)
3927 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
3928 goto err;
3929
3930 Py_XDECREF(ret);
3931 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003932#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003933 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003934#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003935 }
3936 goto out;
3937err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003938#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003939 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003940#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003941 PyErr_PrintEx(0);
3942 PythonIO_Flush();
3943 status = 1;
3944out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003945#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003946 if (!status)
3947 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003948#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003949 Py_DECREF(pyfunc);
3950 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
3951 if (status)
3952 return;
3953 check_cursor();
3954 update_curbuf(NOT_VALID);
3955}
3956
3957 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02003958run_eval(const char *cmd, typval_T *rettv
3959#ifdef PY_CAN_RECURSE
3960 , PyGILState_STATE *pygilstate UNUSED
3961#endif
3962 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02003963{
3964 PyObject *r;
3965
3966 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
3967 if (r == NULL)
3968 {
3969 if (PyErr_Occurred() && !msg_silent)
3970 PyErr_PrintEx(0);
3971 EMSG(_("E858: Eval did not return a valid python object"));
3972 }
3973 else
3974 {
3975 if (ConvertFromPyObject(r, rettv) == -1)
3976 EMSG(_("E859: Failed to convert returned python object to vim value"));
3977 Py_DECREF(r);
3978 }
3979 PyErr_Clear();
3980}
3981
3982 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02003983set_ref_in_py(const int copyID)
3984{
3985 pylinkedlist_T *cur;
3986 dict_T *dd;
3987 list_T *ll;
3988
3989 if (lastdict != NULL)
3990 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3991 {
3992 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3993 if (dd->dv_copyID != copyID)
3994 {
3995 dd->dv_copyID = copyID;
3996 set_ref_in_ht(&dd->dv_hashtab, copyID);
3997 }
3998 }
3999
4000 if (lastlist != NULL)
4001 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4002 {
4003 ll = ((ListObject *) (cur->pll_obj))->list;
4004 if (ll->lv_copyID != copyID)
4005 {
4006 ll->lv_copyID = copyID;
4007 set_ref_in_list(ll, copyID);
4008 }
4009 }
4010}
4011
4012 static int
4013set_string_copy(char_u *str, typval_T *tv)
4014{
4015 tv->vval.v_string = vim_strsave(str);
4016 if (tv->vval.v_string == NULL)
4017 {
4018 PyErr_NoMemory();
4019 return -1;
4020 }
4021 return 0;
4022}
4023
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004024 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004025pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004026{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004027 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004028 char_u *key;
4029 dictitem_T *di;
4030 PyObject *keyObject;
4031 PyObject *valObject;
4032 Py_ssize_t iter = 0;
4033
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004034 if (!(dict = dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004035 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004036
4037 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004038 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004039
4040 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4041 {
4042 DICTKEY_DECL
4043
Bram Moolenaara03e6312013-05-29 22:49:26 +02004044 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004045 {
4046 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004047 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004048 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004049
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004050 if (!DICTKEY_SET_KEY)
4051 {
4052 dict_unref(dict);
4053 return -1;
4054 }
4055 DICTKEY_CHECK_EMPTY(-1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004056
4057 di = dictitem_alloc(key);
4058
4059 DICTKEY_UNREF
4060
4061 if (di == NULL)
4062 {
4063 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004064 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004065 return -1;
4066 }
4067 di->di_tv.v_lock = 0;
4068
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004069 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004070 {
4071 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004072 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004073 return -1;
4074 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004075
4076 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004077 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004078 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004079 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004080 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004081 PyErr_SetVim(_("failed to add key to dictionary"));
4082 return -1;
4083 }
4084 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004085
4086 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004087 return 0;
4088}
4089
4090 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004091pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004092{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004093 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004094 char_u *key;
4095 dictitem_T *di;
4096 PyObject *list;
4097 PyObject *litem;
4098 PyObject *keyObject;
4099 PyObject *valObject;
4100 Py_ssize_t lsize;
4101
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004102 if (!(dict = dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004103 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004104
4105 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004106 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004107
4108 list = PyMapping_Items(obj);
4109 if (list == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004110 {
4111 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004112 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004113 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004114 lsize = PyList_Size(list);
4115 while (lsize--)
4116 {
4117 DICTKEY_DECL
4118
4119 litem = PyList_GetItem(list, lsize);
4120 if (litem == NULL)
4121 {
4122 Py_DECREF(list);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004123 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004124 return -1;
4125 }
4126
Bram Moolenaara03e6312013-05-29 22:49:26 +02004127 if (!(keyObject = PyTuple_GetItem(litem, 0)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004128 {
4129 Py_DECREF(list);
4130 Py_DECREF(litem);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004131 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004132 return -1;
4133 }
4134
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004135 if (!DICTKEY_SET_KEY)
4136 {
4137 dict_unref(dict);
4138 Py_DECREF(list);
4139 Py_DECREF(litem);
4140 DICTKEY_UNREF
4141 return -1;
4142 }
4143 DICTKEY_CHECK_EMPTY(-1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004144
Bram Moolenaara03e6312013-05-29 22:49:26 +02004145 if (!(valObject = PyTuple_GetItem(litem, 1)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004146 {
4147 Py_DECREF(list);
4148 Py_DECREF(litem);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004149 dict_unref(dict);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004150 DICTKEY_UNREF
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004151 return -1;
4152 }
4153
Bram Moolenaara03e6312013-05-29 22:49:26 +02004154 Py_DECREF(litem);
4155
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004156 di = dictitem_alloc(key);
4157
4158 DICTKEY_UNREF
4159
4160 if (di == NULL)
4161 {
4162 Py_DECREF(list);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004163 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004164 PyErr_NoMemory();
4165 return -1;
4166 }
4167 di->di_tv.v_lock = 0;
4168
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004169 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004170 {
4171 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004172 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004173 Py_DECREF(list);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004174 return -1;
4175 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004176
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004177 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004178 {
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004179 dictitem_free(di);
4180 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004181 Py_DECREF(list);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004182 PyErr_SetVim(_("failed to add key to dictionary"));
4183 return -1;
4184 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004185 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004186 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004187 Py_DECREF(list);
4188 return 0;
4189}
4190
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004191 static list_T *
4192py_list_alloc()
4193{
4194 list_T *r;
4195
4196 if (!(r = list_alloc()))
4197 {
4198 PyErr_NoMemory();
4199 return NULL;
4200 }
4201 ++r->lv_refcount;
4202
4203 return r;
4204}
4205
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004206 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004207pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004208{
4209 list_T *l;
4210
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004211 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004212 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004213
4214 tv->v_type = VAR_LIST;
4215 tv->vval.v_list = l;
4216
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004217 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004218 {
4219 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004220 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004221 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004222
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004223 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004224 return 0;
4225}
4226
4227 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004228pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004229{
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004230 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004231 PyObject *item;
4232 list_T *l;
4233 listitem_T *li;
4234
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004235 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004236 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004237
4238 tv->vval.v_list = l;
4239 tv->v_type = VAR_LIST;
4240
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004241 if (!(iterator = PyObject_GetIter(obj)))
4242 {
4243 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004244 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004245 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004246
Bram Moolenaarc8366792013-05-29 22:46:26 +02004247 while ((item = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004248 {
4249 li = listitem_alloc();
4250 if (li == NULL)
4251 {
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004252 list_unref(l);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004253 Py_DECREF(iterator);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004254 PyErr_NoMemory();
4255 return -1;
4256 }
4257 li->li_tv.v_lock = 0;
4258
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004259 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
Bram Moolenaara03e6312013-05-29 22:49:26 +02004260 {
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004261 list_unref(l);
4262 listitem_free(li);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004263 Py_DECREF(item);
4264 Py_DECREF(iterator);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004265 return -1;
Bram Moolenaara03e6312013-05-29 22:49:26 +02004266 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004267
4268 list_append(l, li);
4269
4270 Py_DECREF(item);
4271 }
4272
4273 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004274
4275 /* Iterator may have finished due to an exception */
4276 if (PyErr_Occurred())
4277 {
4278 list_unref(l);
4279 return -1;
4280 }
4281
4282 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004283 return 0;
4284}
4285
Bram Moolenaardb913952012-06-29 12:54:53 +02004286typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4287
4288 static int
4289convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004290 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004291{
4292 PyObject *capsule;
4293 char hexBuf[sizeof(void *) * 2 + 3];
4294
4295 sprintf(hexBuf, "%p", obj);
4296
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004297# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004298 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004299# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004300 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004301# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004302 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004303 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004304# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004305 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004306# else
4307 capsule = PyCObject_FromVoidPtr(tv, NULL);
4308# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004309 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4310 {
4311 Py_DECREF(capsule);
4312 tv->v_type = VAR_UNKNOWN;
4313 return -1;
4314 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004315 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004316 {
4317 tv->v_type = VAR_UNKNOWN;
4318 return -1;
4319 }
4320 /* As we are not using copy_tv which increments reference count we must
4321 * do it ourself. */
4322 switch(tv->v_type)
4323 {
4324 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4325 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4326 }
4327 }
4328 else
4329 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004330 typval_T *v;
4331
4332# ifdef PY_USE_CAPSULE
4333 v = PyCapsule_GetPointer(capsule, NULL);
4334# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004335 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004336# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004337 copy_tv(v, tv);
4338 }
4339 return 0;
4340}
4341
4342 static int
4343ConvertFromPyObject(PyObject *obj, typval_T *tv)
4344{
4345 PyObject *lookup_dict;
4346 int r;
4347
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004348 if (!(lookup_dict = PyDict_New()))
4349 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004350 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4351 Py_DECREF(lookup_dict);
4352 return r;
4353}
4354
4355 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004356_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004357{
4358 if (obj->ob_type == &DictionaryType)
4359 {
4360 tv->v_type = VAR_DICT;
4361 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4362 ++tv->vval.v_dict->dv_refcount;
4363 }
4364 else if (obj->ob_type == &ListType)
4365 {
4366 tv->v_type = VAR_LIST;
4367 tv->vval.v_list = (((ListObject *)(obj))->list);
4368 ++tv->vval.v_list->lv_refcount;
4369 }
4370 else if (obj->ob_type == &FunctionType)
4371 {
4372 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4373 return -1;
4374
4375 tv->v_type = VAR_FUNC;
4376 func_ref(tv->vval.v_string);
4377 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004378 else if (PyBytes_Check(obj))
4379 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004380 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004381
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004382 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4383 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004384 if (result == NULL)
4385 return -1;
4386
4387 if (set_string_copy(result, tv) == -1)
4388 return -1;
4389
4390 tv->v_type = VAR_STRING;
4391 }
4392 else if (PyUnicode_Check(obj))
4393 {
4394 PyObject *bytes;
4395 char_u *result;
4396
Bram Moolenaardb913952012-06-29 12:54:53 +02004397 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4398 if (bytes == NULL)
4399 return -1;
4400
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004401 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4402 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004403 if (result == NULL)
4404 return -1;
4405
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004406 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004407 {
4408 Py_XDECREF(bytes);
4409 return -1;
4410 }
4411 Py_XDECREF(bytes);
4412
4413 tv->v_type = VAR_STRING;
4414 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004415#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004416 else if (PyInt_Check(obj))
4417 {
4418 tv->v_type = VAR_NUMBER;
4419 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4420 }
4421#endif
4422 else if (PyLong_Check(obj))
4423 {
4424 tv->v_type = VAR_NUMBER;
4425 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4426 }
4427 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004428 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004429#ifdef FEAT_FLOAT
4430 else if (PyFloat_Check(obj))
4431 {
4432 tv->v_type = VAR_FLOAT;
4433 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4434 }
4435#endif
4436 else if (PyIter_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004437 return convert_dl(obj, tv, pyiter_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004438 else if (PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004439 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004440 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004441 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004442 else
4443 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004444 PyErr_SetString(PyExc_TypeError,
4445 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004446 return -1;
4447 }
4448 return 0;
4449}
4450
4451 static PyObject *
4452ConvertToPyObject(typval_T *tv)
4453{
4454 if (tv == NULL)
4455 {
4456 PyErr_SetVim(_("NULL reference passed"));
4457 return NULL;
4458 }
4459 switch (tv->v_type)
4460 {
4461 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004462 return PyBytes_FromString(tv->vval.v_string == NULL
4463 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004464 case VAR_NUMBER:
4465 return PyLong_FromLong((long) tv->vval.v_number);
4466#ifdef FEAT_FLOAT
4467 case VAR_FLOAT:
4468 return PyFloat_FromDouble((double) tv->vval.v_float);
4469#endif
4470 case VAR_LIST:
4471 return ListNew(tv->vval.v_list);
4472 case VAR_DICT:
4473 return DictionaryNew(tv->vval.v_dict);
4474 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004475 return FunctionNew(tv->vval.v_string == NULL
4476 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004477 case VAR_UNKNOWN:
4478 Py_INCREF(Py_None);
4479 return Py_None;
4480 default:
4481 PyErr_SetVim(_("internal error: invalid value type"));
4482 return NULL;
4483 }
4484}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004485
4486typedef struct
4487{
4488 PyObject_HEAD
4489} CurrentObject;
4490static PyTypeObject CurrentType;
4491
4492 static void
4493init_structs(void)
4494{
4495 vim_memset(&OutputType, 0, sizeof(OutputType));
4496 OutputType.tp_name = "vim.message";
4497 OutputType.tp_basicsize = sizeof(OutputObject);
4498 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4499 OutputType.tp_doc = "vim message object";
4500 OutputType.tp_methods = OutputMethods;
4501#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004502 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4503 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004504 OutputType.tp_alloc = call_PyType_GenericAlloc;
4505 OutputType.tp_new = call_PyType_GenericNew;
4506 OutputType.tp_free = call_PyObject_Free;
4507#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004508 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4509 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004510#endif
4511
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004512 vim_memset(&IterType, 0, sizeof(IterType));
4513 IterType.tp_name = "vim.iter";
4514 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02004515 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004516 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004517 IterType.tp_iter = (getiterfunc)IterIter;
4518 IterType.tp_iternext = (iternextfunc)IterNext;
4519 IterType.tp_dealloc = (destructor)IterDestructor;
4520 IterType.tp_traverse = (traverseproc)IterTraverse;
4521 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004522
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004523 vim_memset(&BufferType, 0, sizeof(BufferType));
4524 BufferType.tp_name = "vim.buffer";
4525 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004526 BufferType.tp_dealloc = (destructor)BufferDestructor;
4527 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004528 BufferType.tp_as_sequence = &BufferAsSeq;
4529 BufferType.tp_as_mapping = &BufferAsMapping;
4530 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4531 BufferType.tp_doc = "vim buffer object";
4532 BufferType.tp_methods = BufferMethods;
4533#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004534 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004535 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004536 BufferType.tp_alloc = call_PyType_GenericAlloc;
4537 BufferType.tp_new = call_PyType_GenericNew;
4538 BufferType.tp_free = call_PyObject_Free;
4539#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004540 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004541 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004542#endif
4543
4544 vim_memset(&WindowType, 0, sizeof(WindowType));
4545 WindowType.tp_name = "vim.window";
4546 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004547 WindowType.tp_dealloc = (destructor)WindowDestructor;
4548 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02004549 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004550 WindowType.tp_doc = "vim Window object";
4551 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004552 WindowType.tp_traverse = (traverseproc)WindowTraverse;
4553 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004554#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004555 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
4556 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004557 WindowType.tp_alloc = call_PyType_GenericAlloc;
4558 WindowType.tp_new = call_PyType_GenericNew;
4559 WindowType.tp_free = call_PyObject_Free;
4560#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004561 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
4562 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004563#endif
4564
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004565 vim_memset(&TabPageType, 0, sizeof(TabPageType));
4566 TabPageType.tp_name = "vim.tabpage";
4567 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004568 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
4569 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004570 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
4571 TabPageType.tp_doc = "vim tab page object";
4572 TabPageType.tp_methods = TabPageMethods;
4573#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004574 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004575 TabPageType.tp_alloc = call_PyType_GenericAlloc;
4576 TabPageType.tp_new = call_PyType_GenericNew;
4577 TabPageType.tp_free = call_PyObject_Free;
4578#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004579 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004580#endif
4581
Bram Moolenaardfa38d42013-05-15 13:38:47 +02004582 vim_memset(&BufMapType, 0, sizeof(BufMapType));
4583 BufMapType.tp_name = "vim.bufferlist";
4584 BufMapType.tp_basicsize = sizeof(BufMapObject);
4585 BufMapType.tp_as_mapping = &BufMapAsMapping;
4586 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004587 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004588 BufferType.tp_doc = "vim buffer list";
4589
4590 vim_memset(&WinListType, 0, sizeof(WinListType));
4591 WinListType.tp_name = "vim.windowlist";
4592 WinListType.tp_basicsize = sizeof(WinListType);
4593 WinListType.tp_as_sequence = &WinListAsSeq;
4594 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
4595 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004596 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004597
4598 vim_memset(&TabListType, 0, sizeof(TabListType));
4599 TabListType.tp_name = "vim.tabpagelist";
4600 TabListType.tp_basicsize = sizeof(TabListType);
4601 TabListType.tp_as_sequence = &TabListAsSeq;
4602 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
4603 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004604
4605 vim_memset(&RangeType, 0, sizeof(RangeType));
4606 RangeType.tp_name = "vim.range";
4607 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004608 RangeType.tp_dealloc = (destructor)RangeDestructor;
4609 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004610 RangeType.tp_as_sequence = &RangeAsSeq;
4611 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02004612 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004613 RangeType.tp_doc = "vim Range object";
4614 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004615 RangeType.tp_traverse = (traverseproc)RangeTraverse;
4616 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004617#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004618 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004619 RangeType.tp_alloc = call_PyType_GenericAlloc;
4620 RangeType.tp_new = call_PyType_GenericNew;
4621 RangeType.tp_free = call_PyObject_Free;
4622#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004623 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004624#endif
4625
4626 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4627 CurrentType.tp_name = "vim.currentdata";
4628 CurrentType.tp_basicsize = sizeof(CurrentObject);
4629 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4630 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004631 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004632#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004633 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
4634 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004635#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004636 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
4637 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004638#endif
4639
4640 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4641 DictionaryType.tp_name = "vim.dictionary";
4642 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004643 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004644 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4645 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4646 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4647 DictionaryType.tp_methods = DictionaryMethods;
4648#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004649 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
4650 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004651#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004652 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
4653 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004654#endif
4655
4656 vim_memset(&ListType, 0, sizeof(ListType));
4657 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004658 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004659 ListType.tp_basicsize = sizeof(ListObject);
4660 ListType.tp_as_sequence = &ListAsSeq;
4661 ListType.tp_as_mapping = &ListAsMapping;
4662 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4663 ListType.tp_doc = "list pushing modifications to vim structure";
4664 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004665 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004666#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004667 ListType.tp_getattro = (getattrofunc)ListGetattro;
4668 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004669#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004670 ListType.tp_getattr = (getattrfunc)ListGetattr;
4671 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004672#endif
4673
4674 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004675 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004676 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004677 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
4678 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004679 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4680 FunctionType.tp_doc = "object that calls vim function";
4681 FunctionType.tp_methods = FunctionMethods;
4682#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004683 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004684#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004685 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004686#endif
4687
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004688 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4689 OptionsType.tp_name = "vim.options";
4690 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02004691 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004692 OptionsType.tp_doc = "object for manipulating options";
4693 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004694 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
4695 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
4696 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004697
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004698#if PY_MAJOR_VERSION >= 3
4699 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4700 vimmodule.m_name = "vim";
4701 vimmodule.m_doc = "Vim Python interface\n";
4702 vimmodule.m_size = -1;
4703 vimmodule.m_methods = VimMethods;
4704#endif
4705}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004706
4707#define PYTYPE_READY(type) \
4708 if (PyType_Ready(&type)) \
4709 return -1;
4710
4711 static int
4712init_types()
4713{
4714 PYTYPE_READY(IterType);
4715 PYTYPE_READY(BufferType);
4716 PYTYPE_READY(RangeType);
4717 PYTYPE_READY(WindowType);
4718 PYTYPE_READY(TabPageType);
4719 PYTYPE_READY(BufMapType);
4720 PYTYPE_READY(WinListType);
4721 PYTYPE_READY(TabListType);
4722 PYTYPE_READY(CurrentType);
4723 PYTYPE_READY(DictionaryType);
4724 PYTYPE_READY(ListType);
4725 PYTYPE_READY(FunctionType);
4726 PYTYPE_READY(OptionsType);
4727 PYTYPE_READY(OutputType);
4728 return 0;
4729}
4730
4731static BufMapObject TheBufferMap =
4732{
4733 PyObject_HEAD_INIT(&BufMapType)
4734};
4735
4736static WinListObject TheWindowList =
4737{
4738 PyObject_HEAD_INIT(&WinListType)
4739 NULL
4740};
4741
4742static CurrentObject TheCurrent =
4743{
4744 PyObject_HEAD_INIT(&CurrentType)
4745};
4746
4747static TabListObject TheTabPageList =
4748{
4749 PyObject_HEAD_INIT(&TabListType)
4750};
4751
4752static struct numeric_constant {
4753 char *name;
4754 int value;
4755} numeric_constants[] = {
4756 {"VAR_LOCKED", VAR_LOCKED},
4757 {"VAR_FIXED", VAR_FIXED},
4758 {"VAR_SCOPE", VAR_SCOPE},
4759 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
4760};
4761
4762static struct object_constant {
4763 char *name;
4764 PyObject *value;
4765} object_constants[] = {
4766 {"buffers", (PyObject *)(void *)&TheBufferMap},
4767 {"windows", (PyObject *)(void *)&TheWindowList},
4768 {"tabpages", (PyObject *)(void *)&TheTabPageList},
4769 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02004770
4771 {"Buffer", (PyObject *)&BufferType},
4772 {"Range", (PyObject *)&RangeType},
4773 {"Window", (PyObject *)&WindowType},
4774 {"TabPage", (PyObject *)&TabPageType},
4775 {"Dictionary", (PyObject *)&DictionaryType},
4776 {"List", (PyObject *)&ListType},
4777 {"Function", (PyObject *)&FunctionType},
4778 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02004779};
4780
4781typedef int (*object_adder)(PyObject *, const char *, PyObject *);
4782
4783#define ADD_OBJECT(m, name, obj) \
4784 if (add_object(m, name, obj)) \
4785 return -1;
4786
4787#define ADD_CHECKED_OBJECT(m, name, obj) \
4788 { \
4789 PyObject *value = obj; \
4790 if (!value) \
4791 return -1; \
4792 ADD_OBJECT(m, name, value); \
4793 }
4794
4795 static int
4796populate_module(PyObject *m, object_adder add_object)
4797{
4798 int i;
4799
4800 for (i = 0; i < (int)(sizeof(numeric_constants)
4801 / sizeof(struct numeric_constant));
4802 ++i)
4803 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
4804 PyInt_FromLong(numeric_constants[i].value));
4805
4806 for (i = 0; i < (int)(sizeof(object_constants)
4807 / sizeof(struct object_constant));
4808 ++i)
4809 {
4810 PyObject *value;
4811
4812 value = object_constants[i].value;
4813 Py_INCREF(value);
4814 ADD_OBJECT(m, object_constants[i].name, value);
4815 }
4816
4817 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
4818 return -1;
4819 ADD_OBJECT(m, "error", VimError);
4820
4821 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict));
4822 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
4823 ADD_CHECKED_OBJECT(m, "options",
4824 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
4825 return 0;
4826}