blob: 93470fac06c2f30a669f3ebc5a9a6740cab8dfe4 [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
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020021# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020022#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 Moolenaarc09a6d62013-06-10 21:27:29 +020027static const char *vim_special_path = "_vim_path_";
28
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020029#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020030#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020031#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaarc476e522013-06-23 13:46:40 +020032#define PyErr_FORMAT(exc, str, tail) PyErr_Format(exc, _(str), tail)
33#define PyErr_VIM_FORMAT(str, tail) PyErr_FORMAT(VimError, str, tail)
34
35#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
36 ? "(NULL)" \
37 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020038
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020039#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
40 "empty keys are not allowed")
Bram Moolenaarc476e522013-06-23 13:46:40 +020041#define RAISE_LOCKED(type) PyErr_SET_VIM(_(type " is locked"))
42#define RAISE_LOCKED_DICTIONARY RAISE_LOCKED("dictionary")
43#define RAISE_LOCKED_LIST RAISE_LOCKED("list")
44#define RAISE_UNDO_FAIL PyErr_SET_VIM("cannot save undo information")
45#define RAISE_LINE_FAIL(act) PyErr_SET_VIM("cannot " act " line")
46#define RAISE_KEY_ADD_FAIL(key) \
47 PyErr_VIM_FORMAT("failed to add key '%s' to dictionary", key)
48#define RAISE_INVALID_INDEX_TYPE(idx) \
49 PyErr_FORMAT(PyExc_TypeError, "index must be int or slice, not %s", \
50 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020051
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020052#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
53#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020054#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020055
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020056typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020057typedef void (*runner)(const char *, void *
58#ifdef PY_CAN_RECURSE
59 , PyGILState_STATE *
60#endif
61 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020062
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020063static int ConvertFromPyObject(PyObject *, typval_T *);
64static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020065static int ConvertFromPyMapping(PyObject *, typval_T *);
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 Moolenaarf4258302013-06-02 18:20:17 +020075static PyObject *py_chdir;
76static PyObject *py_fchdir;
77static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020078static PyObject *vim_module;
79static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020080
Bram Moolenaar81c40c52013-06-12 14:41:04 +020081static PyObject *py_find_module;
82static PyObject *py_load_module;
83
84static PyObject *VimError;
85
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020086/*
87 * obtain a lock on the Vim data structures
88 */
89 static void
90Python_Lock_Vim(void)
91{
92}
93
94/*
95 * release a lock on the Vim data structures
96 */
97 static void
98Python_Release_Vim(void)
99{
100}
101
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200102/*
103 * The "todecref" argument holds a pointer to PyObject * that must be
104 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
105 * was needed to generate returned value is object.
106 *
107 * Use Py_XDECREF to decrement reference count.
108 */
109 static char_u *
110StringToChars(PyObject *object, PyObject **todecref)
111{
112 char_u *p;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200113
114 if (PyBytes_Check(object))
115 {
116
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200117 if (PyBytes_AsStringAndSize(object, (char **) &p, NULL) == -1
118 || p == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200119 return NULL;
120
121 *todecref = NULL;
122 }
123 else if (PyUnicode_Check(object))
124 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200125 PyObject *bytes;
126
127 if (!(bytes = PyUnicode_AsEncodedString(object, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200128 return NULL;
129
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200130 if(PyBytes_AsStringAndSize(bytes, (char **) &p, NULL) == -1
131 || p == NULL)
132 {
133 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200134 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200135 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200136
137 *todecref = bytes;
138 }
139 else
140 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200141 PyErr_FORMAT(PyExc_TypeError,
142#if PY_MAJOR_VERSION < 3
143 "expected str() or unicode() instance, but got %s"
144#else
145 "expected bytes() or str() instance, but got %s"
146#endif
147 , Py_TYPE_NAME(object));
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200148 return NULL;
149 }
150
151 return (char_u *) p;
152}
153
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200154#define NUMBER_LONG 1
155#define NUMBER_INT 2
156#define NUMBER_NATURAL 4
157#define NUMBER_UNSIGNED 8
158
159 static int
160NumberToLong(PyObject *obj, long *result, int flags)
161{
162#if PY_MAJOR_VERSION < 3
163 if (PyInt_Check(obj))
164 {
165 *result = PyInt_AsLong(obj);
166 if (PyErr_Occurred())
167 return -1;
168 }
169 else
170#endif
171 if (PyLong_Check(obj))
172 {
173 *result = PyLong_AsLong(obj);
174 if (PyErr_Occurred())
175 return -1;
176 }
177 else if (PyNumber_Check(obj))
178 {
179 PyObject *num;
180
181 if (!(num = PyNumber_Long(obj)))
182 return -1;
183
184 *result = PyLong_AsLong(num);
185
186 Py_DECREF(num);
187
188 if (PyErr_Occurred())
189 return -1;
190 }
191 else
192 {
193 PyErr_FORMAT(PyExc_TypeError,
194#if PY_MAJOR_VERSION < 3
195 "expected int(), long() or something supporting "
196 "coercing to long(), but got %s"
197#else
198 "expected int() or something supporting coercing to int(), "
199 "but got %s"
200#endif
201 , Py_TYPE_NAME(obj));
202 return -1;
203 }
204
205 if (flags & NUMBER_INT)
206 {
207 if (*result > INT_MAX)
208 {
209 PyErr_SET_STRING(PyExc_OverflowError,
210 "value is too large to fit into C int type");
211 return -1;
212 }
213 else if (*result < INT_MIN)
214 {
215 PyErr_SET_STRING(PyExc_OverflowError,
216 "value is too small to fit into C int type");
217 return -1;
218 }
219 }
220
221 if (flags & NUMBER_NATURAL)
222 {
223 if (*result <= 0)
224 {
225 PyErr_SET_STRING(PyExc_ValueError,
226 "number must be greater then zero");
227 return -1;
228 }
229 }
230 else if (flags & NUMBER_UNSIGNED)
231 {
232 if (*result < 0)
233 {
234 PyErr_SET_STRING(PyExc_ValueError,
235 "number must be greater or equal to zero");
236 return -1;
237 }
238 }
239
240 return 0;
241}
242
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200243 static int
244add_string(PyObject *list, char *s)
245{
246 PyObject *string;
247
248 if (!(string = PyString_FromString(s)))
249 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200250
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200251 if (PyList_Append(list, string))
252 {
253 Py_DECREF(string);
254 return -1;
255 }
256
257 Py_DECREF(string);
258 return 0;
259}
260
261 static PyObject *
262ObjectDir(PyObject *self, char **attributes)
263{
264 PyMethodDef *method;
265 char **attr;
266 PyObject *r;
267
268 if (!(r = PyList_New(0)))
269 return NULL;
270
271 if (self)
272 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
273 if (add_string(r, (char *) method->ml_name))
274 {
275 Py_DECREF(r);
276 return NULL;
277 }
278
279 for (attr = attributes ; *attr ; ++attr)
280 if (add_string(r, *attr))
281 {
282 Py_DECREF(r);
283 return NULL;
284 }
285
286#if PY_MAJOR_VERSION < 3
287 if (add_string(r, "__members__"))
288 {
289 Py_DECREF(r);
290 return NULL;
291 }
292#endif
293
294 return r;
295}
296
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200297/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200298 */
299
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200300/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200301typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200302
303static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200304
305typedef struct
306{
307 PyObject_HEAD
308 long softspace;
309 long error;
310} OutputObject;
311
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200312static char *OutputAttrs[] = {
313 "softspace",
314 NULL
315};
316
317 static PyObject *
318OutputDir(PyObject *self)
319{
320 return ObjectDir(self, OutputAttrs);
321}
322
Bram Moolenaar77045652012-09-21 13:46:06 +0200323 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200324OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200325{
326 if (val == NULL)
327 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200328 PyErr_SET_STRING(PyExc_AttributeError,
329 "can't delete OutputObject attributes");
Bram Moolenaar77045652012-09-21 13:46:06 +0200330 return -1;
331 }
332
333 if (strcmp(name, "softspace") == 0)
334 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200335 if (NumberToLong(val, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200337 return 0;
338 }
339
Bram Moolenaarc476e522013-06-23 13:46:40 +0200340 PyErr_FORMAT(PyExc_AttributeError, "invalid attribute: %s", name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200341 return -1;
342}
343
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200344/* Buffer IO, we write one whole line at a time. */
345static garray_T io_ga = {0, 0, 1, 80, NULL};
346static writefn old_fn = NULL;
347
348 static void
349PythonIO_Flush(void)
350{
351 if (old_fn != NULL && io_ga.ga_len > 0)
352 {
353 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
354 old_fn((char_u *)io_ga.ga_data);
355 }
356 io_ga.ga_len = 0;
357}
358
359 static void
360writer(writefn fn, char_u *str, PyInt n)
361{
362 char_u *ptr;
363
364 /* Flush when switching output function. */
365 if (fn != old_fn)
366 PythonIO_Flush();
367 old_fn = fn;
368
369 /* Write each NL separated line. Text after the last NL is kept for
370 * writing later. */
371 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
372 {
373 PyInt len = ptr - str;
374
375 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
376 break;
377
378 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
379 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
380 fn((char_u *)io_ga.ga_data);
381 str = ptr + 1;
382 n -= len + 1;
383 io_ga.ga_len = 0;
384 }
385
386 /* Put the remaining text into io_ga for later printing. */
387 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
388 {
389 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
390 io_ga.ga_len += (int)n;
391 }
392}
393
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200394 static int
395write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200396{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200397 Py_ssize_t len = 0;
398 char *str = NULL;
399 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200400
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200401 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
402 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200403
404 Py_BEGIN_ALLOW_THREADS
405 Python_Lock_Vim();
406 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
407 Python_Release_Vim();
408 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200409 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200411 return 0;
412}
413
414 static PyObject *
415OutputWrite(OutputObject *self, PyObject *string)
416{
417 if (write_output(self, string))
418 return NULL;
419
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200420 Py_INCREF(Py_None);
421 return Py_None;
422}
423
424 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200425OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200426{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200427 PyObject *iterator;
428 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200429
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200430 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200431 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200432
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200433 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200434 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200435 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200436 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200437 Py_DECREF(iterator);
438 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200439 return NULL;
440 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200441 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442 }
443
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200444 Py_DECREF(iterator);
445
446 /* Iterator may have finished due to an exception */
447 if (PyErr_Occurred())
448 return NULL;
449
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200450 Py_INCREF(Py_None);
451 return Py_None;
452}
453
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100454 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200455OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100456{
457 /* do nothing */
458 Py_INCREF(Py_None);
459 return Py_None;
460}
461
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200462/***************/
463
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200464static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200465 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200466 {"write", (PyCFunction)OutputWrite, METH_O, ""},
467 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200468 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200469 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200470 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200471};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200472
473static OutputObject Output =
474{
475 PyObject_HEAD_INIT(&OutputType)
476 0,
477 0
478};
479
480static OutputObject Error =
481{
482 PyObject_HEAD_INIT(&OutputType)
483 0,
484 1
485};
486
487 static int
488PythonIO_Init_io(void)
489{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200490 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
491 return -1;
492 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
493 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200494
495 if (PyErr_Occurred())
496 {
497 EMSG(_("E264: Python: Error initialising I/O objects"));
498 return -1;
499 }
500
501 return 0;
502}
503
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200504typedef struct
505{
506 PyObject_HEAD
507 PyObject *module;
508} LoaderObject;
509static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200510
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200511 static void
512LoaderDestructor(LoaderObject *self)
513{
514 Py_DECREF(self->module);
515 DESTRUCTOR_FINISH(self);
516}
517
518 static PyObject *
519LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
520{
521 PyObject *r = self->module;
522
523 Py_INCREF(r);
524 return r;
525}
526
527static struct PyMethodDef LoaderMethods[] = {
528 /* name, function, calling, doc */
529 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
530 { NULL, NULL, 0, NULL}
531};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200532
533/* Check to see whether a Vim error has been reported, or a keyboard
534 * interrupt has been detected.
535 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200536
537 static void
538VimTryStart(void)
539{
540 ++trylevel;
541}
542
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200543 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200544VimTryEnd(void)
545{
546 --trylevel;
547 if (got_int)
548 {
549 PyErr_SetNone(PyExc_KeyboardInterrupt);
550 return 1;
551 }
552 else if (!did_throw)
553 return 0;
554 else if (PyErr_Occurred())
555 return 1;
556 else
557 {
558 PyErr_SetVim((char *) current_exception->value);
559 discard_current_exception();
560 return 1;
561 }
562}
563
564 static int
565VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200566{
567 if (got_int)
568 {
569 PyErr_SetNone(PyExc_KeyboardInterrupt);
570 return 1;
571 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200572 return 0;
573}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200574
575/* Vim module - Implementation
576 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200577
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200578 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200579VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200580{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200581 char_u *cmd;
582 PyObject *result;
583 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200584
Bram Moolenaar389a1792013-06-23 13:00:44 +0200585 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200586 return NULL;
587
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200588 Py_BEGIN_ALLOW_THREADS
589 Python_Lock_Vim();
590
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200591 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200592 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200593 update_screen(VALID);
594
595 Python_Release_Vim();
596 Py_END_ALLOW_THREADS
597
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200598 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200599 result = NULL;
600 else
601 result = Py_None;
602
603 Py_XINCREF(result);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200604 Py_XDECREF(todecref);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200605 return result;
606}
607
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200608/*
609 * Function to translate a typval_T into a PyObject; this will recursively
610 * translate lists/dictionaries into their Python equivalents.
611 *
612 * The depth parameter is to avoid infinite recursion, set it to 1 when
613 * you call VimToPython.
614 */
615 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200616VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200617{
618 PyObject *result;
619 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200620 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200621
622 /* Avoid infinite recursion */
623 if (depth > 100)
624 {
625 Py_INCREF(Py_None);
626 result = Py_None;
627 return result;
628 }
629
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200630 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200631 * then and we can use it again. */
632 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
633 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
634 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200635 sprintf(ptrBuf, "%p",
636 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
637 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200638
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200639 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200640 {
641 Py_INCREF(result);
642 return result;
643 }
644 }
645
646 if (our_tv->v_type == VAR_STRING)
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200647 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200648 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200649 else if (our_tv->v_type == VAR_NUMBER)
650 {
651 char buf[NUMBUFLEN];
652
653 /* For backwards compatibility numbers are stored as strings. */
654 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200655 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200656 }
657# ifdef FEAT_FLOAT
658 else if (our_tv->v_type == VAR_FLOAT)
659 {
660 char buf[NUMBUFLEN];
661
662 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200663 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200664 }
665# endif
666 else if (our_tv->v_type == VAR_LIST)
667 {
668 list_T *list = our_tv->vval.v_list;
669 listitem_T *curr;
670
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200671 if (list == NULL)
672 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200673
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200674 if (!(result = PyList_New(0)))
675 return NULL;
676
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200677 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200678 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200679 Py_DECREF(result);
680 return NULL;
681 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200682
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200683 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
684 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200685 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200686 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200687 Py_DECREF(result);
688 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200690 if (PyList_Append(result, newObj))
691 {
692 Py_DECREF(newObj);
693 Py_DECREF(result);
694 return NULL;
695 }
696 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697 }
698 }
699 else if (our_tv->v_type == VAR_DICT)
700 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200701
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200702 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
703 long_u todo = ht->ht_used;
704 hashitem_T *hi;
705 dictitem_T *di;
706 if (our_tv->vval.v_dict == NULL)
707 return NULL;
708
709 if (!(result = PyDict_New()))
710 return NULL;
711
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200712 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200713 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200714 Py_DECREF(result);
715 return NULL;
716 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200717
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200718 for (hi = ht->ht_array; todo > 0; ++hi)
719 {
720 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200721 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200722 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200723
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200724 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200725 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200726 {
727 Py_DECREF(result);
728 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200729 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200730 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
731 {
732 Py_DECREF(result);
733 Py_DECREF(newObj);
734 return NULL;
735 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200736 }
737 }
738 }
739 else
740 {
741 Py_INCREF(Py_None);
742 result = Py_None;
743 }
744
745 return result;
746}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200747
748 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200749VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200750{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200751 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200752 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200753 PyObject *string;
754 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200755 PyObject *result;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200756 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200757
Bram Moolenaar389a1792013-06-23 13:00:44 +0200758 if (!PyArg_ParseTuple(args, "O", &string))
759 return NULL;
760
761 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200762 return NULL;
763
764 Py_BEGIN_ALLOW_THREADS
765 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200766 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200767 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200768 Python_Release_Vim();
769 Py_END_ALLOW_THREADS
770
Bram Moolenaar389a1792013-06-23 13:00:44 +0200771 Py_XDECREF(todecref);
772
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200773 if (VimTryEnd())
774 return NULL;
775
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200776 if (our_tv == NULL)
777 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200778 PyErr_SET_VIM("invalid expression");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200779 return NULL;
780 }
781
782 /* Convert the Vim type into a Python type. Create a dictionary that's
783 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200784 if (!(lookup_dict = PyDict_New()))
785 result = NULL;
786 else
787 {
788 result = VimToPython(our_tv, 1, lookup_dict);
789 Py_DECREF(lookup_dict);
790 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791
792
793 Py_BEGIN_ALLOW_THREADS
794 Python_Lock_Vim();
795 free_tv(our_tv);
796 Python_Release_Vim();
797 Py_END_ALLOW_THREADS
798
799 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200800}
801
Bram Moolenaardb913952012-06-29 12:54:53 +0200802static PyObject *ConvertToPyObject(typval_T *);
803
804 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200805VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200806{
Bram Moolenaardb913952012-06-29 12:54:53 +0200807 typval_T *our_tv;
808 PyObject *result;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200809 char_u *expr;
810 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200811
Bram Moolenaar389a1792013-06-23 13:00:44 +0200812 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200813 return NULL;
814
815 Py_BEGIN_ALLOW_THREADS
816 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200817 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200818 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200819 Python_Release_Vim();
820 Py_END_ALLOW_THREADS
821
Bram Moolenaar389a1792013-06-23 13:00:44 +0200822 Py_XDECREF(todecref);
823
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200824 if (VimTryEnd())
825 return NULL;
826
Bram Moolenaardb913952012-06-29 12:54:53 +0200827 if (our_tv == NULL)
828 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200829 PyErr_SET_VIM("invalid expression");
Bram Moolenaardb913952012-06-29 12:54:53 +0200830 return NULL;
831 }
832
833 result = ConvertToPyObject(our_tv);
834 Py_BEGIN_ALLOW_THREADS
835 Python_Lock_Vim();
836 free_tv(our_tv);
837 Python_Release_Vim();
838 Py_END_ALLOW_THREADS
839
840 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200841}
842
843 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200844VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200845{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200846 char_u *str;
847 PyObject *todecref;
848 int result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200849
Bram Moolenaar389a1792013-06-23 13:00:44 +0200850 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200851 return NULL;
852
Bram Moolenaara54bf402012-12-05 16:30:07 +0100853#ifdef FEAT_MBYTE
Bram Moolenaar389a1792013-06-23 13:00:44 +0200854 result = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100855#else
Bram Moolenaar389a1792013-06-23 13:00:44 +0200856 result = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100857#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200858
859 Py_XDECREF(todecref);
860
861 return PyLong_FromLong(result);
Bram Moolenaardb913952012-06-29 12:54:53 +0200862}
863
Bram Moolenaarf4258302013-06-02 18:20:17 +0200864 static PyObject *
865_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
866{
867 PyObject *r;
868 PyObject *newwd;
869 PyObject *todecref;
870 char_u *new_dir;
871
Bram Moolenaard4209d22013-06-05 20:34:15 +0200872 if (_chdir == NULL)
873 return NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200874 if (!(r = PyObject_Call(_chdir, args, kwargs)))
875 return NULL;
876
877 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
878 {
879 Py_DECREF(r);
880 return NULL;
881 }
882
883 if (!(new_dir = StringToChars(newwd, &todecref)))
884 {
885 Py_DECREF(r);
886 Py_DECREF(newwd);
887 return NULL;
888 }
889
890 VimTryStart();
891
892 if (vim_chdir(new_dir))
893 {
894 Py_DECREF(r);
895 Py_DECREF(newwd);
896 Py_XDECREF(todecref);
897
898 if (VimTryEnd())
899 return NULL;
900
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200901 PyErr_SET_VIM("failed to change directory");
Bram Moolenaarf4258302013-06-02 18:20:17 +0200902 return NULL;
903 }
904
905 Py_DECREF(newwd);
906 Py_XDECREF(todecref);
907
908 post_chdir(FALSE);
909
910 if (VimTryEnd())
911 {
912 Py_DECREF(r);
913 return NULL;
914 }
915
916 return r;
917}
918
919 static PyObject *
920VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
921{
922 return _VimChdir(py_chdir, args, kwargs);
923}
924
925 static PyObject *
926VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
927{
928 return _VimChdir(py_fchdir, args, kwargs);
929}
930
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200931typedef struct {
932 PyObject *callable;
933 PyObject *result;
934} map_rtp_data;
935
936 static void
937map_rtp_callback(char_u *path, void *_data)
938{
939 void **data = (void **) _data;
940 PyObject *pathObject;
941 map_rtp_data *mr_data = *((map_rtp_data **) data);
942
943 if (!(pathObject = PyString_FromString((char *) path)))
944 {
945 *data = NULL;
946 return;
947 }
948
949 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
950 pathObject, NULL);
951
952 Py_DECREF(pathObject);
953
954 if (!mr_data->result || mr_data->result != Py_None)
955 *data = NULL;
956 else
957 {
958 Py_DECREF(mr_data->result);
959 mr_data->result = NULL;
960 }
961}
962
963 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200964VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200965{
966 map_rtp_data data;
967
Bram Moolenaar389a1792013-06-23 13:00:44 +0200968 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200969 data.result = NULL;
970
971 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
972
973 if (data.result == NULL)
974 {
975 if (PyErr_Occurred())
976 return NULL;
977 else
978 {
979 Py_INCREF(Py_None);
980 return Py_None;
981 }
982 }
983 return data.result;
984}
985
986/*
987 * _vim_runtimepath_ special path implementation.
988 */
989
990 static void
991map_finder_callback(char_u *path, void *_data)
992{
993 void **data = (void **) _data;
994 PyObject *list = *((PyObject **) data);
995 PyObject *pathObject1, *pathObject2;
996 char *pathbuf;
997 size_t pathlen;
998
999 pathlen = STRLEN(path);
1000
1001#if PY_MAJOR_VERSION < 3
1002# define PY_MAIN_DIR_STRING "python2"
1003#else
1004# define PY_MAIN_DIR_STRING "python3"
1005#endif
1006#define PY_ALTERNATE_DIR_STRING "pythonx"
1007
1008#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1009 if (!(pathbuf = PyMem_New(char,
1010 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1011 {
1012 PyErr_NoMemory();
1013 *data = NULL;
1014 return;
1015 }
1016
1017 mch_memmove(pathbuf, path, pathlen + 1);
1018 add_pathsep((char_u *) pathbuf);
1019
1020 pathlen = STRLEN(pathbuf);
1021 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1022 PYTHONX_STRING_LENGTH + 1);
1023
1024 if (!(pathObject1 = PyString_FromString(pathbuf)))
1025 {
1026 *data = NULL;
1027 PyMem_Free(pathbuf);
1028 return;
1029 }
1030
1031 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1032 PYTHONX_STRING_LENGTH + 1);
1033
1034 if (!(pathObject2 = PyString_FromString(pathbuf)))
1035 {
1036 Py_DECREF(pathObject1);
1037 PyMem_Free(pathbuf);
1038 *data = NULL;
1039 return;
1040 }
1041
1042 PyMem_Free(pathbuf);
1043
1044 if (PyList_Append(list, pathObject1)
1045 || PyList_Append(list, pathObject2))
1046 *data = NULL;
1047
1048 Py_DECREF(pathObject1);
1049 Py_DECREF(pathObject2);
1050}
1051
1052 static PyObject *
1053Vim_GetPaths(PyObject *self UNUSED)
1054{
1055 PyObject *r;
1056
1057 if (!(r = PyList_New(0)))
1058 return NULL;
1059
1060 do_in_runtimepath(NULL, FALSE, &map_finder_callback, r);
1061
1062 if (PyErr_Occurred())
1063 {
1064 Py_DECREF(r);
1065 return NULL;
1066 }
1067
1068 return r;
1069}
1070
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001071 static PyObject *
1072call_load_module(char *name, int len, PyObject *find_module_result)
1073{
1074 PyObject *fd, *pathname, *description;
1075
Bram Moolenaarc476e522013-06-23 13:46:40 +02001076 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001077 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001078 PyErr_FORMAT(PyExc_TypeError,
1079 "expected 3-tuple as imp.find_module() result, but got %s",
1080 Py_TYPE_NAME(find_module_result));
1081 return NULL;
1082 }
1083 if (PyTuple_GET_SIZE(find_module_result) != 3)
1084 {
1085 PyErr_FORMAT(PyExc_TypeError,
1086 "expected 3-tuple as imp.find_module() result, but got "
1087 "tuple of size %d",
1088 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001089 return NULL;
1090 }
1091
1092 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1093 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1094 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1095 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001096 PyErr_SET_STRING(PyExc_RuntimeError,
1097 "internal error: imp.find_module returned tuple with NULL");
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001098 return NULL;
1099 }
1100
1101 return PyObject_CallFunction(py_load_module,
1102 "s#OOO", name, len, fd, pathname, description);
1103}
1104
1105 static PyObject *
1106find_module(char *fullname, char *tail, PyObject *new_path)
1107{
1108 PyObject *find_module_result;
1109 PyObject *module;
1110 char *dot;
1111
1112 if ((dot = (char *) vim_strchr((char_u *) tail, '.')))
1113 {
1114 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001115 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001116 * first component
1117 */
1118 PyObject *newest_path;
1119 int partlen = (int) (dot - 1 - tail);
1120
1121 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1122 "s#O", tail, partlen, new_path)))
1123 return NULL;
1124
1125 if (!(module = call_load_module(
1126 fullname,
1127 ((int) (tail - fullname)) + partlen,
1128 find_module_result)))
1129 {
1130 Py_DECREF(find_module_result);
1131 return NULL;
1132 }
1133
1134 Py_DECREF(find_module_result);
1135
1136 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1137 {
1138 Py_DECREF(module);
1139 return NULL;
1140 }
1141
1142 Py_DECREF(module);
1143
1144 module = find_module(fullname, dot + 1, newest_path);
1145
1146 Py_DECREF(newest_path);
1147
1148 return module;
1149 }
1150 else
1151 {
1152 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1153 "sO", tail, new_path)))
1154 return NULL;
1155
1156 if (!(module = call_load_module(
1157 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001158 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001159 find_module_result)))
1160 {
1161 Py_DECREF(find_module_result);
1162 return NULL;
1163 }
1164
1165 Py_DECREF(find_module_result);
1166
1167 return module;
1168 }
1169}
1170
1171 static PyObject *
1172FinderFindModule(PyObject *self, PyObject *args)
1173{
1174 char *fullname;
1175 PyObject *module;
1176 PyObject *new_path;
1177 LoaderObject *loader;
1178
1179 if (!PyArg_ParseTuple(args, "s", &fullname))
1180 return NULL;
1181
1182 if (!(new_path = Vim_GetPaths(self)))
1183 return NULL;
1184
1185 module = find_module(fullname, fullname, new_path);
1186
1187 Py_DECREF(new_path);
1188
1189 if (!module)
1190 {
1191 Py_INCREF(Py_None);
1192 return Py_None;
1193 }
1194
1195 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1196 {
1197 Py_DECREF(module);
1198 return NULL;
1199 }
1200
1201 loader->module = module;
1202
1203 return (PyObject *) loader;
1204}
1205
1206 static PyObject *
1207VimPathHook(PyObject *self UNUSED, PyObject *args)
1208{
1209 char *path;
1210
1211 if (PyArg_ParseTuple(args, "s", &path)
1212 && STRCMP(path, vim_special_path) == 0)
1213 {
1214 Py_INCREF(vim_module);
1215 return vim_module;
1216 }
1217
1218 PyErr_Clear();
1219 PyErr_SetNone(PyExc_ImportError);
1220 return NULL;
1221}
1222
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001223/*
1224 * Vim module - Definitions
1225 */
1226
1227static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001228 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001229 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001230 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001231 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1232 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001233 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1234 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001235 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001236 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001237 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1238 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1239 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001240};
1241
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001242/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001243 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001244 */
1245
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001246static PyTypeObject IterType;
1247
1248typedef PyObject *(*nextfun)(void **);
1249typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001250typedef int (*traversefun)(void *, visitproc, void *);
1251typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001252
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001253/* Main purpose of this object is removing the need for do python
1254 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1255 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001256
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001257typedef struct
1258{
1259 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001260 void *cur;
1261 nextfun next;
1262 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001263 traversefun traverse;
1264 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001265} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001266
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001267 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001268IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1269 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001270{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001271 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001272
Bram Moolenaar774267b2013-05-21 20:51:59 +02001273 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001274 self->cur = start;
1275 self->next = next;
1276 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001277 self->traverse = traverse;
1278 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001279
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001280 return (PyObject *)(self);
1281}
1282
1283 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001284IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001285{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001286 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001287 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001288 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001289}
1290
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001291 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001292IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001293{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001294 if (self->traverse != NULL)
1295 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001296 else
1297 return 0;
1298}
1299
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001300/* Mac OSX defines clear() somewhere. */
1301#ifdef clear
1302# undef clear
1303#endif
1304
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001305 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001306IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001307{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001308 if (self->clear != NULL)
1309 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001310 else
1311 return 0;
1312}
1313
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001314 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001315IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001316{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001317 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001318}
1319
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001320 static PyObject *
1321IterIter(PyObject *self)
1322{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001323 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001324 return self;
1325}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001326
Bram Moolenaardb913952012-06-29 12:54:53 +02001327typedef struct pylinkedlist_S {
1328 struct pylinkedlist_S *pll_next;
1329 struct pylinkedlist_S *pll_prev;
1330 PyObject *pll_obj;
1331} pylinkedlist_T;
1332
1333static pylinkedlist_T *lastdict = NULL;
1334static pylinkedlist_T *lastlist = NULL;
1335
1336 static void
1337pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1338{
1339 if (ref->pll_prev == NULL)
1340 {
1341 if (ref->pll_next == NULL)
1342 {
1343 *last = NULL;
1344 return;
1345 }
1346 }
1347 else
1348 ref->pll_prev->pll_next = ref->pll_next;
1349
1350 if (ref->pll_next == NULL)
1351 *last = ref->pll_prev;
1352 else
1353 ref->pll_next->pll_prev = ref->pll_prev;
1354}
1355
1356 static void
1357pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1358{
1359 if (*last == NULL)
1360 ref->pll_prev = NULL;
1361 else
1362 {
1363 (*last)->pll_next = ref;
1364 ref->pll_prev = *last;
1365 }
1366 ref->pll_next = NULL;
1367 ref->pll_obj = self;
1368 *last = ref;
1369}
1370
1371static PyTypeObject DictionaryType;
1372
1373typedef struct
1374{
1375 PyObject_HEAD
1376 dict_T *dict;
1377 pylinkedlist_T ref;
1378} DictionaryObject;
1379
Bram Moolenaara9922d62013-05-30 13:01:18 +02001380static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1381
1382#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1383
Bram Moolenaardb913952012-06-29 12:54:53 +02001384 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001385DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001386{
1387 DictionaryObject *self;
1388
Bram Moolenaara9922d62013-05-30 13:01:18 +02001389 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001390 if (self == NULL)
1391 return NULL;
1392 self->dict = dict;
1393 ++dict->dv_refcount;
1394
1395 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1396
1397 return (PyObject *)(self);
1398}
1399
Bram Moolenaara9922d62013-05-30 13:01:18 +02001400 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001401py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001402{
1403 dict_T *r;
1404
1405 if (!(r = dict_alloc()))
1406 {
1407 PyErr_NoMemory();
1408 return NULL;
1409 }
1410 ++r->dv_refcount;
1411
1412 return r;
1413}
1414
1415 static PyObject *
1416DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1417{
1418 DictionaryObject *self;
1419 dict_T *dict;
1420
1421 if (!(dict = py_dict_alloc()))
1422 return NULL;
1423
1424 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1425
1426 --dict->dv_refcount;
1427
1428 if (kwargs || PyTuple_Size(args))
1429 {
1430 PyObject *tmp;
1431 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1432 {
1433 Py_DECREF(self);
1434 return NULL;
1435 }
1436
1437 Py_DECREF(tmp);
1438 }
1439
1440 return (PyObject *)(self);
1441}
1442
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001443 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001444DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001445{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001446 pyll_remove(&self->ref, &lastdict);
1447 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001448
1449 DESTRUCTOR_FINISH(self);
1450}
1451
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001452static char *DictionaryAttrs[] = {
1453 "locked", "scope",
1454 NULL
1455};
1456
1457 static PyObject *
1458DictionaryDir(PyObject *self)
1459{
1460 return ObjectDir(self, DictionaryAttrs);
1461}
1462
Bram Moolenaardb913952012-06-29 12:54:53 +02001463 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001464DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001465{
1466 if (val == NULL)
1467 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001468 PyErr_SET_STRING(PyExc_AttributeError,
1469 "cannot delete vim.Dictionary attributes");
Bram Moolenaar66b79852012-09-21 14:00:35 +02001470 return -1;
1471 }
1472
1473 if (strcmp(name, "locked") == 0)
1474 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001475 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001476 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001477 PyErr_SET_STRING(PyExc_TypeError, "cannot modify fixed dictionary");
Bram Moolenaar66b79852012-09-21 14:00:35 +02001478 return -1;
1479 }
1480 else
1481 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001482 int istrue = PyObject_IsTrue(val);
1483 if (istrue == -1)
1484 return -1;
1485 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001486 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001487 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001488 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001489 }
1490 return 0;
1491 }
1492 else
1493 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001494 PyErr_FORMAT(PyExc_AttributeError, "cannot set attribute %s", name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001495 return -1;
1496 }
1497}
1498
1499 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001500DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001501{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001502 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001503}
1504
Bram Moolenaara9922d62013-05-30 13:01:18 +02001505#define DICT_FLAG_HAS_DEFAULT 0x01
1506#define DICT_FLAG_POP 0x02
1507#define DICT_FLAG_NONE_DEFAULT 0x04
1508#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1509#define DICT_FLAG_RETURN_PAIR 0x10
1510
Bram Moolenaardb913952012-06-29 12:54:53 +02001511 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001512_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001513{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001514 PyObject *keyObject;
1515 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1516 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001517 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001518 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001519 dict_T *dict = self->dict;
1520 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001521 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001522
Bram Moolenaara9922d62013-05-30 13:01:18 +02001523 if (flags & DICT_FLAG_HAS_DEFAULT)
1524 {
1525 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1526 return NULL;
1527 }
1528 else
1529 keyObject = args;
1530
1531 if (flags & DICT_FLAG_RETURN_BOOL)
1532 defObject = Py_False;
1533
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001534 if (!(key = StringToChars(keyObject, &todecref)))
1535 return NULL;
1536
1537 if (*key == NUL)
1538 {
1539 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001540 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001541 return NULL;
1542 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001543
Bram Moolenaara9922d62013-05-30 13:01:18 +02001544 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001545
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001546 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001547
Bram Moolenaara9922d62013-05-30 13:01:18 +02001548 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001549 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001550 if (defObject)
1551 {
1552 Py_INCREF(defObject);
1553 return defObject;
1554 }
1555 else
1556 {
1557 PyErr_SetObject(PyExc_KeyError, keyObject);
1558 return NULL;
1559 }
1560 }
1561 else if (flags & DICT_FLAG_RETURN_BOOL)
1562 {
1563 Py_INCREF(Py_True);
1564 return Py_True;
1565 }
1566
1567 di = dict_lookup(hi);
1568
1569 if (!(r = ConvertToPyObject(&di->di_tv)))
1570 return NULL;
1571
1572 if (flags & DICT_FLAG_POP)
1573 {
1574 if (dict->dv_lock)
1575 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001576 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001577 Py_DECREF(r);
1578 return NULL;
1579 }
1580
1581 hash_remove(&dict->dv_hashtab, hi);
1582 dictitem_free(di);
1583 }
1584
Bram Moolenaara9922d62013-05-30 13:01:18 +02001585 return r;
1586}
1587
1588 static PyObject *
1589DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1590{
1591 return _DictionaryItem(self, keyObject, 0);
1592}
1593
1594 static int
1595DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1596{
1597 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1598 int r;
1599
1600 r = (rObj == Py_True);
1601
1602 Py_DECREF(Py_True);
1603
1604 return r;
1605}
1606
1607typedef struct
1608{
1609 hashitem_T *ht_array;
1610 long_u ht_used;
1611 hashtab_T *ht;
1612 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001613 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001614} dictiterinfo_T;
1615
1616 static PyObject *
1617DictionaryIterNext(dictiterinfo_T **dii)
1618{
1619 PyObject *r;
1620
1621 if (!(*dii)->todo)
1622 return NULL;
1623
1624 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1625 (*dii)->ht->ht_used != (*dii)->ht_used)
1626 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001627 PyErr_SET_STRING(PyExc_RuntimeError,
1628 "hashtab changed during iteration");
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001629 return NULL;
1630 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001631
Bram Moolenaara9922d62013-05-30 13:01:18 +02001632 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1633 ++((*dii)->hi);
1634
1635 --((*dii)->todo);
1636
1637 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1638 return NULL;
1639
1640 return r;
1641}
1642
1643 static PyObject *
1644DictionaryIter(DictionaryObject *self)
1645{
1646 dictiterinfo_T *dii;
1647 hashtab_T *ht;
1648
1649 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1650 {
1651 PyErr_NoMemory();
1652 return NULL;
1653 }
1654
1655 ht = &self->dict->dv_hashtab;
1656 dii->ht_array = ht->ht_array;
1657 dii->ht_used = ht->ht_used;
1658 dii->ht = ht;
1659 dii->hi = dii->ht_array;
1660 dii->todo = dii->ht_used;
1661
1662 return IterNew(dii,
1663 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1664 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001665}
1666
1667 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001668DictionaryAssItem(
1669 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001670{
1671 char_u *key;
1672 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001673 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001674 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001675 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001676
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001677 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001678 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001679 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001680 return -1;
1681 }
1682
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001683 if (!(key = StringToChars(keyObject, &todecref)))
1684 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001685
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001686 if (*key == NUL)
1687 {
1688 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001689 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001690 return -1;
1691 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001692
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001693 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001694
1695 if (valObject == NULL)
1696 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001697 hashitem_T *hi;
1698
Bram Moolenaardb913952012-06-29 12:54:53 +02001699 if (di == NULL)
1700 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001701 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001702 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001703 return -1;
1704 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001705 hi = hash_find(&dict->dv_hashtab, di->di_key);
1706 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001707 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001708 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001709 return 0;
1710 }
1711
1712 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001713 {
1714 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001715 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001716 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001717
1718 if (di == NULL)
1719 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001720 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001721 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001722 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001723 PyErr_NoMemory();
1724 return -1;
1725 }
1726 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001727 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001728
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001729 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001730 {
1731 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001732 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001733 RAISE_KEY_ADD_FAIL(key);
1734 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001735 return -1;
1736 }
1737 }
1738 else
1739 clear_tv(&di->di_tv);
1740
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001741 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001742
1743 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001744 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001745 return 0;
1746}
1747
Bram Moolenaara9922d62013-05-30 13:01:18 +02001748typedef PyObject *(*hi_to_py)(hashitem_T *);
1749
Bram Moolenaardb913952012-06-29 12:54:53 +02001750 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001751DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001752{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001753 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001754 long_u todo = dict->dv_hashtab.ht_used;
1755 Py_ssize_t i = 0;
1756 PyObject *r;
1757 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001758 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001759
1760 r = PyList_New(todo);
1761 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1762 {
1763 if (!HASHITEM_EMPTY(hi))
1764 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001765 if (!(newObj = hiconvert(hi)))
1766 {
1767 Py_DECREF(r);
1768 return NULL;
1769 }
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02001770 PyList_SET_ITEM(r, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001771 --todo;
1772 ++i;
1773 }
1774 }
1775 return r;
1776}
1777
Bram Moolenaara9922d62013-05-30 13:01:18 +02001778 static PyObject *
1779dict_key(hashitem_T *hi)
1780{
1781 return PyBytes_FromString((char *)(hi->hi_key));
1782}
1783
1784 static PyObject *
1785DictionaryListKeys(DictionaryObject *self)
1786{
1787 return DictionaryListObjects(self, dict_key);
1788}
1789
1790 static PyObject *
1791dict_val(hashitem_T *hi)
1792{
1793 dictitem_T *di;
1794
1795 di = dict_lookup(hi);
1796 return ConvertToPyObject(&di->di_tv);
1797}
1798
1799 static PyObject *
1800DictionaryListValues(DictionaryObject *self)
1801{
1802 return DictionaryListObjects(self, dict_val);
1803}
1804
1805 static PyObject *
1806dict_item(hashitem_T *hi)
1807{
1808 PyObject *keyObject;
1809 PyObject *valObject;
1810 PyObject *r;
1811
1812 if (!(keyObject = dict_key(hi)))
1813 return NULL;
1814
1815 if (!(valObject = dict_val(hi)))
1816 {
1817 Py_DECREF(keyObject);
1818 return NULL;
1819 }
1820
1821 r = Py_BuildValue("(OO)", keyObject, valObject);
1822
1823 Py_DECREF(keyObject);
1824 Py_DECREF(valObject);
1825
1826 return r;
1827}
1828
1829 static PyObject *
1830DictionaryListItems(DictionaryObject *self)
1831{
1832 return DictionaryListObjects(self, dict_item);
1833}
1834
1835 static PyObject *
1836DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1837{
1838 dict_T *dict = self->dict;
1839
1840 if (dict->dv_lock)
1841 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001842 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001843 return NULL;
1844 }
1845
1846 if (kwargs)
1847 {
1848 typval_T tv;
1849
1850 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1851 return NULL;
1852
1853 VimTryStart();
1854 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1855 clear_tv(&tv);
1856 if (VimTryEnd())
1857 return NULL;
1858 }
1859 else
1860 {
1861 PyObject *object;
1862
Bram Moolenaar389a1792013-06-23 13:00:44 +02001863 if (!PyArg_ParseTuple(args, "O", &object))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001864 return NULL;
1865
1866 if (PyObject_HasAttrString(object, "keys"))
1867 return DictionaryUpdate(self, NULL, object);
1868 else
1869 {
1870 PyObject *iterator;
1871 PyObject *item;
1872
1873 if (!(iterator = PyObject_GetIter(object)))
1874 return NULL;
1875
1876 while ((item = PyIter_Next(iterator)))
1877 {
1878 PyObject *fast;
1879 PyObject *keyObject;
1880 PyObject *valObject;
1881 PyObject *todecref;
1882 char_u *key;
1883 dictitem_T *di;
1884
1885 if (!(fast = PySequence_Fast(item, "")))
1886 {
1887 Py_DECREF(iterator);
1888 Py_DECREF(item);
1889 return NULL;
1890 }
1891
1892 Py_DECREF(item);
1893
1894 if (PySequence_Fast_GET_SIZE(fast) != 2)
1895 {
1896 Py_DECREF(iterator);
1897 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001898 PyErr_FORMAT(PyExc_ValueError,
1899 "expected sequence element of size 2, "
1900 "but got sequence of size %d",
1901 PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001902 return NULL;
1903 }
1904
1905 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1906
1907 if (!(key = StringToChars(keyObject, &todecref)))
1908 {
1909 Py_DECREF(iterator);
1910 Py_DECREF(fast);
1911 return NULL;
1912 }
1913
1914 di = dictitem_alloc(key);
1915
1916 Py_XDECREF(todecref);
1917
1918 if (di == NULL)
1919 {
1920 Py_DECREF(fast);
1921 Py_DECREF(iterator);
1922 PyErr_NoMemory();
1923 return NULL;
1924 }
1925 di->di_tv.v_lock = 0;
1926 di->di_tv.v_type = VAR_UNKNOWN;
1927
1928 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1929
1930 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1931 {
1932 Py_DECREF(iterator);
1933 Py_DECREF(fast);
1934 dictitem_free(di);
1935 return NULL;
1936 }
1937
1938 Py_DECREF(fast);
1939
1940 if (dict_add(dict, di) == FAIL)
1941 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001942 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001943 Py_DECREF(iterator);
1944 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001945 return NULL;
1946 }
1947 }
1948
1949 Py_DECREF(iterator);
1950
1951 /* Iterator may have finished due to an exception */
1952 if (PyErr_Occurred())
1953 return NULL;
1954 }
1955 }
1956 Py_INCREF(Py_None);
1957 return Py_None;
1958}
1959
1960 static PyObject *
1961DictionaryGet(DictionaryObject *self, PyObject *args)
1962{
1963 return _DictionaryItem(self, args,
1964 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1965}
1966
1967 static PyObject *
1968DictionaryPop(DictionaryObject *self, PyObject *args)
1969{
1970 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1971}
1972
1973 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001974DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001975{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001976 hashitem_T *hi;
1977 PyObject *r;
1978 PyObject *valObject;
1979 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001980
Bram Moolenaarde71b562013-06-02 17:41:54 +02001981 if (self->dict->dv_hashtab.ht_used == 0)
1982 {
1983 PyErr_SetNone(PyExc_KeyError);
1984 return NULL;
1985 }
1986
1987 hi = self->dict->dv_hashtab.ht_array;
1988 while (HASHITEM_EMPTY(hi))
1989 ++hi;
1990
1991 di = dict_lookup(hi);
1992
1993 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001994 return NULL;
1995
Bram Moolenaarde71b562013-06-02 17:41:54 +02001996 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
1997 {
1998 Py_DECREF(valObject);
1999 return NULL;
2000 }
2001
2002 hash_remove(&self->dict->dv_hashtab, hi);
2003 dictitem_free(di);
2004
2005 return r;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002006}
2007
2008 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002009DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002011 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2012}
2013
2014static PySequenceMethods DictionaryAsSeq = {
2015 0, /* sq_length */
2016 0, /* sq_concat */
2017 0, /* sq_repeat */
2018 0, /* sq_item */
2019 0, /* sq_slice */
2020 0, /* sq_ass_item */
2021 0, /* sq_ass_slice */
2022 (objobjproc) DictionaryContains, /* sq_contains */
2023 0, /* sq_inplace_concat */
2024 0, /* sq_inplace_repeat */
2025};
2026
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002027static PyMappingMethods DictionaryAsMapping = {
2028 (lenfunc) DictionaryLength,
2029 (binaryfunc) DictionaryItem,
2030 (objobjargproc) DictionaryAssItem,
2031};
2032
Bram Moolenaardb913952012-06-29 12:54:53 +02002033static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002034 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002035 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2036 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2037 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2038 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2039 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002040 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002041 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002042 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2043 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002044};
2045
2046static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002047static PySequenceMethods ListAsSeq;
2048static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02002049
2050typedef struct
2051{
2052 PyObject_HEAD
2053 list_T *list;
2054 pylinkedlist_T ref;
2055} ListObject;
2056
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002057#define NEW_LIST(list) ListNew(&ListType, list)
2058
Bram Moolenaardb913952012-06-29 12:54:53 +02002059 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002060ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002061{
2062 ListObject *self;
2063
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002064 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002065 if (self == NULL)
2066 return NULL;
2067 self->list = list;
2068 ++list->lv_refcount;
2069
2070 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2071
2072 return (PyObject *)(self);
2073}
2074
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002075 static list_T *
2076py_list_alloc()
2077{
2078 list_T *r;
2079
2080 if (!(r = list_alloc()))
2081 {
2082 PyErr_NoMemory();
2083 return NULL;
2084 }
2085 ++r->lv_refcount;
2086
2087 return r;
2088}
2089
2090 static int
2091list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2092{
2093 PyObject *iterator;
2094 PyObject *item;
2095 listitem_T *li;
2096
2097 if (!(iterator = PyObject_GetIter(obj)))
2098 return -1;
2099
2100 while ((item = PyIter_Next(iterator)))
2101 {
2102 if (!(li = listitem_alloc()))
2103 {
2104 PyErr_NoMemory();
2105 Py_DECREF(item);
2106 Py_DECREF(iterator);
2107 return -1;
2108 }
2109 li->li_tv.v_lock = 0;
2110 li->li_tv.v_type = VAR_UNKNOWN;
2111
2112 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2113 {
2114 Py_DECREF(item);
2115 Py_DECREF(iterator);
2116 listitem_free(li);
2117 return -1;
2118 }
2119
2120 Py_DECREF(item);
2121
2122 list_append(l, li);
2123 }
2124
2125 Py_DECREF(iterator);
2126
2127 /* Iterator may have finished due to an exception */
2128 if (PyErr_Occurred())
2129 return -1;
2130
2131 return 0;
2132}
2133
2134 static PyObject *
2135ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2136{
2137 list_T *list;
2138 PyObject *obj = NULL;
2139
2140 if (kwargs)
2141 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002142 PyErr_SET_STRING(PyExc_TypeError,
2143 "list constructor does not accept keyword arguments");
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002144 return NULL;
2145 }
2146
2147 if (!PyArg_ParseTuple(args, "|O", &obj))
2148 return NULL;
2149
2150 if (!(list = py_list_alloc()))
2151 return NULL;
2152
2153 if (obj)
2154 {
2155 PyObject *lookup_dict;
2156
2157 if (!(lookup_dict = PyDict_New()))
2158 {
2159 list_unref(list);
2160 return NULL;
2161 }
2162
2163 if (list_py_concat(list, obj, lookup_dict) == -1)
2164 {
2165 Py_DECREF(lookup_dict);
2166 list_unref(list);
2167 return NULL;
2168 }
2169
2170 Py_DECREF(lookup_dict);
2171 }
2172
2173 return ListNew(subtype, list);
2174}
2175
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002176 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002177ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002178{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002179 pyll_remove(&self->ref, &lastlist);
2180 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002181
2182 DESTRUCTOR_FINISH(self);
2183}
2184
Bram Moolenaardb913952012-06-29 12:54:53 +02002185 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002186ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002187{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002188 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002189}
2190
2191 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002192ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002193{
2194 listitem_T *li;
2195
Bram Moolenaard6e39182013-05-21 18:30:34 +02002196 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002197 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002198 PyErr_SET_STRING(PyExc_IndexError, "list index out of range");
Bram Moolenaardb913952012-06-29 12:54:53 +02002199 return NULL;
2200 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002201 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002202 if (li == NULL)
2203 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002204 /* No more suitable format specifications in python-2.3 */
2205 PyErr_VIM_FORMAT("internal error: failed to get vim list item %d",
2206 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002207 return NULL;
2208 }
2209 return ConvertToPyObject(&li->li_tv);
2210}
2211
2212#define PROC_RANGE \
2213 if (last < 0) {\
2214 if (last < -size) \
2215 last = 0; \
2216 else \
2217 last += size; \
2218 } \
2219 if (first < 0) \
2220 first = 0; \
2221 if (first > size) \
2222 first = size; \
2223 if (last > size) \
2224 last = size;
2225
2226 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002227ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002228{
2229 PyInt i;
2230 PyInt size = ListLength(self);
2231 PyInt n;
2232 PyObject *list;
2233 int reversed = 0;
2234
2235 PROC_RANGE
2236 if (first >= last)
2237 first = last;
2238
2239 n = last-first;
2240 list = PyList_New(n);
2241 if (list == NULL)
2242 return NULL;
2243
2244 for (i = 0; i < n; ++i)
2245 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002246 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002247 if (item == NULL)
2248 {
2249 Py_DECREF(list);
2250 return NULL;
2251 }
2252
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002253 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002254 }
2255
2256 return list;
2257}
2258
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002259typedef struct
2260{
2261 listwatch_T lw;
2262 list_T *list;
2263} listiterinfo_T;
2264
2265 static void
2266ListIterDestruct(listiterinfo_T *lii)
2267{
2268 list_rem_watch(lii->list, &lii->lw);
2269 PyMem_Free(lii);
2270}
2271
2272 static PyObject *
2273ListIterNext(listiterinfo_T **lii)
2274{
2275 PyObject *r;
2276
2277 if (!((*lii)->lw.lw_item))
2278 return NULL;
2279
2280 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
2281 return NULL;
2282
2283 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2284
2285 return r;
2286}
2287
2288 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002289ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002290{
2291 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002292 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002293
2294 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2295 {
2296 PyErr_NoMemory();
2297 return NULL;
2298 }
2299
2300 list_add_watch(l, &lii->lw);
2301 lii->lw.lw_item = l->lv_first;
2302 lii->list = l;
2303
2304 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002305 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2306 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002307}
2308
Bram Moolenaardb913952012-06-29 12:54:53 +02002309 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002310ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002311{
2312 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002313 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002314 listitem_T *li;
2315 Py_ssize_t length = ListLength(self);
2316
2317 if (l->lv_lock)
2318 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002319 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002320 return -1;
2321 }
2322 if (index>length || (index==length && obj==NULL))
2323 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002324 PyErr_SET_STRING(PyExc_IndexError, "list index out of range");
Bram Moolenaardb913952012-06-29 12:54:53 +02002325 return -1;
2326 }
2327
2328 if (obj == NULL)
2329 {
2330 li = list_find(l, (long) index);
2331 list_remove(l, li, li);
2332 clear_tv(&li->li_tv);
2333 vim_free(li);
2334 return 0;
2335 }
2336
2337 if (ConvertFromPyObject(obj, &tv) == -1)
2338 return -1;
2339
2340 if (index == length)
2341 {
2342 if (list_append_tv(l, &tv) == FAIL)
2343 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002344 clear_tv(&tv);
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002345 PyErr_SET_VIM("failed to add item to list");
Bram Moolenaardb913952012-06-29 12:54:53 +02002346 return -1;
2347 }
2348 }
2349 else
2350 {
2351 li = list_find(l, (long) index);
2352 clear_tv(&li->li_tv);
2353 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002354 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002355 }
2356 return 0;
2357}
2358
2359 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002360ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002361{
2362 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002363 PyObject *iterator;
2364 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002365 listitem_T *li;
2366 listitem_T *next;
2367 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002368 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002369 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002370
2371 if (l->lv_lock)
2372 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002373 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002374 return -1;
2375 }
2376
2377 PROC_RANGE
2378
2379 if (first == size)
2380 li = NULL;
2381 else
2382 {
2383 li = list_find(l, (long) first);
2384 if (li == NULL)
2385 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002386 PyErr_VIM_FORMAT("internal error: no vim list item %d", (int)first);
Bram Moolenaardb913952012-06-29 12:54:53 +02002387 return -1;
2388 }
2389 if (last > first)
2390 {
2391 i = last - first;
2392 while (i-- && li != NULL)
2393 {
2394 next = li->li_next;
2395 listitem_remove(l, li);
2396 li = next;
2397 }
2398 }
2399 }
2400
2401 if (obj == NULL)
2402 return 0;
2403
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002404 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002405 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002406
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002407 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002408 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002409 if (ConvertFromPyObject(item, &v) == -1)
2410 {
2411 Py_DECREF(iterator);
2412 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002413 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002414 }
2415 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002416 if (list_insert_tv(l, &v, li) == FAIL)
2417 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002418 clear_tv(&v);
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002419 PyErr_SET_VIM("internal error: failed to add item to list");
Bram Moolenaardb913952012-06-29 12:54:53 +02002420 return -1;
2421 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002422 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002423 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002424 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02002425 return 0;
2426}
2427
2428 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002429ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002430{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002431 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002432 PyObject *lookup_dict;
2433
2434 if (l->lv_lock)
2435 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002436 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002437 return NULL;
2438 }
2439
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002440 if (!(lookup_dict = PyDict_New()))
2441 return NULL;
2442
Bram Moolenaardb913952012-06-29 12:54:53 +02002443 if (list_py_concat(l, obj, lookup_dict) == -1)
2444 {
2445 Py_DECREF(lookup_dict);
2446 return NULL;
2447 }
2448 Py_DECREF(lookup_dict);
2449
2450 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002451 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002452}
2453
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002454static char *ListAttrs[] = {
2455 "locked",
2456 NULL
2457};
2458
2459 static PyObject *
2460ListDir(PyObject *self)
2461{
2462 return ObjectDir(self, ListAttrs);
2463}
2464
Bram Moolenaar66b79852012-09-21 14:00:35 +02002465 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002466ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002467{
2468 if (val == NULL)
2469 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002470 PyErr_SET_STRING(PyExc_AttributeError,
2471 "cannot delete vim.List attributes");
Bram Moolenaar66b79852012-09-21 14:00:35 +02002472 return -1;
2473 }
2474
2475 if (strcmp(name, "locked") == 0)
2476 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002477 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002478 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002479 PyErr_SET_STRING(PyExc_TypeError, "cannot modify fixed list");
Bram Moolenaar66b79852012-09-21 14:00:35 +02002480 return -1;
2481 }
2482 else
2483 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002484 int istrue = PyObject_IsTrue(val);
2485 if (istrue == -1)
2486 return -1;
2487 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002488 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002489 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002490 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002491 }
2492 return 0;
2493 }
2494 else
2495 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002496 PyErr_FORMAT(PyExc_AttributeError, "cannot set attribute %s", name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002497 return -1;
2498 }
2499}
2500
Bram Moolenaardb913952012-06-29 12:54:53 +02002501static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002502 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2503 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2504 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002505};
2506
2507typedef struct
2508{
2509 PyObject_HEAD
2510 char_u *name;
2511} FunctionObject;
2512
2513static PyTypeObject FunctionType;
2514
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002515#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2516
Bram Moolenaardb913952012-06-29 12:54:53 +02002517 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002518FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002519{
2520 FunctionObject *self;
2521
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002522 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2523
Bram Moolenaardb913952012-06-29 12:54:53 +02002524 if (self == NULL)
2525 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002526
2527 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002528 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002529 if (!translated_function_exists(name))
2530 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002531 PyErr_FORMAT(PyExc_ValueError,
2532 "unnamed function %s does not exist", name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002533 return NULL;
2534 }
2535 self->name = vim_strsave(name);
2536 func_ref(self->name);
2537 }
2538 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002539 if ((self->name = get_expanded_name(name,
2540 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2541 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002542 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002543 PyErr_FORMAT(PyExc_ValueError, "function %s does not exist", name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002544 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002545 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002546
2547 return (PyObject *)(self);
2548}
2549
2550 static PyObject *
2551FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2552{
2553 PyObject *self;
2554 char_u *name;
2555
2556 if (kwargs)
2557 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002558 PyErr_SET_STRING(PyExc_TypeError,
2559 "function constructor does not accept keyword arguments");
Bram Moolenaardb913952012-06-29 12:54:53 +02002560 return NULL;
2561 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002562
Bram Moolenaar389a1792013-06-23 13:00:44 +02002563 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002564 return NULL;
2565
2566 self = FunctionNew(subtype, name);
2567
Bram Moolenaar389a1792013-06-23 13:00:44 +02002568 PyMem_Free(name);
2569
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002570 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002571}
2572
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002573 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002574FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002575{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002576 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002577 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002578
2579 DESTRUCTOR_FINISH(self);
2580}
2581
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002582static char *FunctionAttrs[] = {
2583 "softspace",
2584 NULL
2585};
2586
2587 static PyObject *
2588FunctionDir(PyObject *self)
2589{
2590 return ObjectDir(self, FunctionAttrs);
2591}
2592
Bram Moolenaardb913952012-06-29 12:54:53 +02002593 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002594FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002595{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002596 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002597 typval_T args;
2598 typval_T selfdicttv;
2599 typval_T rettv;
2600 dict_T *selfdict = NULL;
2601 PyObject *selfdictObject;
2602 PyObject *result;
2603 int error;
2604
2605 if (ConvertFromPyObject(argsObject, &args) == -1)
2606 return NULL;
2607
2608 if (kwargs != NULL)
2609 {
2610 selfdictObject = PyDict_GetItemString(kwargs, "self");
2611 if (selfdictObject != NULL)
2612 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002613 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002614 {
2615 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002616 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002617 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002618 selfdict = selfdicttv.vval.v_dict;
2619 }
2620 }
2621
Bram Moolenaar71700b82013-05-15 17:49:05 +02002622 Py_BEGIN_ALLOW_THREADS
2623 Python_Lock_Vim();
2624
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002625 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002626 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002627
2628 Python_Release_Vim();
2629 Py_END_ALLOW_THREADS
2630
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002631 if (VimTryEnd())
2632 result = NULL;
2633 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002634 {
2635 result = NULL;
Bram Moolenaarc476e522013-06-23 13:46:40 +02002636 PyErr_VIM_FORMAT("failed to run function %s", (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002637 }
2638 else
2639 result = ConvertToPyObject(&rettv);
2640
Bram Moolenaardb913952012-06-29 12:54:53 +02002641 clear_tv(&args);
2642 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002643 if (selfdict != NULL)
2644 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002645
2646 return result;
2647}
2648
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002649 static PyObject *
2650FunctionRepr(FunctionObject *self)
2651{
2652 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2653}
2654
Bram Moolenaardb913952012-06-29 12:54:53 +02002655static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002656 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2657 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002658};
2659
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002660/*
2661 * Options object
2662 */
2663
2664static PyTypeObject OptionsType;
2665
2666typedef int (*checkfun)(void *);
2667
2668typedef struct
2669{
2670 PyObject_HEAD
2671 int opt_type;
2672 void *from;
2673 checkfun Check;
2674 PyObject *fromObj;
2675} OptionsObject;
2676
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002677 static int
2678dummy_check(void *arg UNUSED)
2679{
2680 return 0;
2681}
2682
2683 static PyObject *
2684OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2685{
2686 OptionsObject *self;
2687
Bram Moolenaar774267b2013-05-21 20:51:59 +02002688 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002689 if (self == NULL)
2690 return NULL;
2691
2692 self->opt_type = opt_type;
2693 self->from = from;
2694 self->Check = Check;
2695 self->fromObj = fromObj;
2696 if (fromObj)
2697 Py_INCREF(fromObj);
2698
2699 return (PyObject *)(self);
2700}
2701
2702 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002703OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002704{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002705 PyObject_GC_UnTrack((void *)(self));
2706 Py_XDECREF(self->fromObj);
2707 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002708}
2709
2710 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002711OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002712{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002713 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002714 return 0;
2715}
2716
2717 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002718OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002719{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002720 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002721 return 0;
2722}
2723
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002724 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002725OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002726{
2727 char_u *key;
2728 int flags;
2729 long numval;
2730 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002731 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002732
Bram Moolenaard6e39182013-05-21 18:30:34 +02002733 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002734 return NULL;
2735
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002736 if (!(key = StringToChars(keyObject, &todecref)))
2737 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002738
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002739 if (*key == NUL)
2740 {
2741 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002742 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002743 return NULL;
2744 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002745
2746 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002747 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002748
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002749 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002750
2751 if (flags == 0)
2752 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002753 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002754 return NULL;
2755 }
2756
2757 if (flags & SOPT_UNSET)
2758 {
2759 Py_INCREF(Py_None);
2760 return Py_None;
2761 }
2762 else if (flags & SOPT_BOOL)
2763 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002764 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002765 r = numval ? Py_True : Py_False;
2766 Py_INCREF(r);
2767 return r;
2768 }
2769 else if (flags & SOPT_NUM)
2770 return PyInt_FromLong(numval);
2771 else if (flags & SOPT_STRING)
2772 {
2773 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002774 {
2775 PyObject *r = PyBytes_FromString((char *) stringval);
2776 vim_free(stringval);
2777 return r;
2778 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002779 else
2780 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002781 PyErr_SET_STRING(PyExc_RuntimeError,
2782 "unable to get option value");
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002783 return NULL;
2784 }
2785 }
2786 else
2787 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002788 PyErr_SET_VIM("internal error: unknown option type");
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002789 return NULL;
2790 }
2791}
2792
2793 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002794set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002795{
2796 char_u *errmsg;
2797
2798 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2799 {
2800 if (VimTryEnd())
2801 return FAIL;
2802 PyErr_SetVim((char *)errmsg);
2803 return FAIL;
2804 }
2805 return OK;
2806}
2807
2808 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002809set_option_value_for(
2810 char_u *key,
2811 int numval,
2812 char_u *stringval,
2813 int opt_flags,
2814 int opt_type,
2815 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002816{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002817 win_T *save_curwin = NULL;
2818 tabpage_T *save_curtab = NULL;
2819 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002820 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002821
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002822 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002823 switch (opt_type)
2824 {
2825 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002826 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002827 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002828 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002829 if (VimTryEnd())
2830 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002831 PyErr_SET_VIM("problem while switching windows");
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002832 return -1;
2833 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002834 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaard6949742013-06-16 14:18:28 +02002835 restore_win(save_curwin, save_curtab, FALSE);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002836 if (r == FAIL)
2837 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002838 break;
2839 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002840 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002841 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002842 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002843 if (r == FAIL)
2844 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002845 break;
2846 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002847 r = set_option_value_err(key, numval, stringval, opt_flags);
2848 if (r == FAIL)
2849 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002850 break;
2851 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002852 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002853}
2854
2855 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002856OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002857{
2858 char_u *key;
2859 int flags;
2860 int opt_flags;
2861 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002862 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002863
Bram Moolenaard6e39182013-05-21 18:30:34 +02002864 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002865 return -1;
2866
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002867 if (!(key = StringToChars(keyObject, &todecref)))
2868 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002869
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002870 if (*key == NUL)
2871 {
2872 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002873 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002874 return -1;
2875 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002876
2877 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002878 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002879
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002880 if (flags == 0)
2881 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002882 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002883 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002884 return -1;
2885 }
2886
2887 if (valObject == NULL)
2888 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002889 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002890 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002891 PyErr_FORMAT(PyExc_ValueError,
2892 "unable to unset global option %s", key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002893 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002894 return -1;
2895 }
2896 else if (!(flags & SOPT_GLOBAL))
2897 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002898 PyErr_FORMAT(PyExc_ValueError,
2899 "unable to unset option %s "
2900 "which does not have global value", key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002901 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002902 return -1;
2903 }
2904 else
2905 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002906 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002907 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002908 return 0;
2909 }
2910 }
2911
Bram Moolenaard6e39182013-05-21 18:30:34 +02002912 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002913
2914 if (flags & SOPT_BOOL)
2915 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002916 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002917
Bram Moolenaarb983f752013-05-15 16:11:50 +02002918 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002919 r = -1;
2920 else
2921 r = set_option_value_for(key, istrue, NULL,
2922 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002923 }
2924 else if (flags & SOPT_NUM)
2925 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002926 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002927
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002928 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002929 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002930 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002931 return -1;
2932 }
2933
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002934 r = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002935 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002936 }
2937 else
2938 {
2939 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002940 PyObject *todecref;
2941
2942 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002943 r = set_option_value_for(key, 0, val, opt_flags,
2944 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002945 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002946 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002947 }
2948
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002949 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002950
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002951 return r;
2952}
2953
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002954static PyMappingMethods OptionsAsMapping = {
2955 (lenfunc) NULL,
2956 (binaryfunc) OptionsItem,
2957 (objobjargproc) OptionsAssItem,
2958};
2959
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002960/* Tabpage object
2961 */
2962
2963typedef struct
2964{
2965 PyObject_HEAD
2966 tabpage_T *tab;
2967} TabPageObject;
2968
2969static PyObject *WinListNew(TabPageObject *tabObject);
2970
2971static PyTypeObject TabPageType;
2972
2973 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002974CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002975{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002976 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002977 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002978 PyErr_SET_VIM("attempt to refer to deleted tab page");
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002979 return -1;
2980 }
2981
2982 return 0;
2983}
2984
2985 static PyObject *
2986TabPageNew(tabpage_T *tab)
2987{
2988 TabPageObject *self;
2989
2990 if (TAB_PYTHON_REF(tab))
2991 {
2992 self = TAB_PYTHON_REF(tab);
2993 Py_INCREF(self);
2994 }
2995 else
2996 {
2997 self = PyObject_NEW(TabPageObject, &TabPageType);
2998 if (self == NULL)
2999 return NULL;
3000 self->tab = tab;
3001 TAB_PYTHON_REF(tab) = self;
3002 }
3003
3004 return (PyObject *)(self);
3005}
3006
3007 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003008TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003009{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003010 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3011 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003012
3013 DESTRUCTOR_FINISH(self);
3014}
3015
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003016static char *TabPageAttrs[] = {
3017 "windows", "number", "vars", "window", "valid",
3018 NULL
3019};
3020
3021 static PyObject *
3022TabPageDir(PyObject *self)
3023{
3024 return ObjectDir(self, TabPageAttrs);
3025}
3026
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003027 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003028TabPageAttrValid(TabPageObject *self, char *name)
3029{
3030 PyObject *r;
3031
3032 if (strcmp(name, "valid") != 0)
3033 return NULL;
3034
3035 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3036 Py_INCREF(r);
3037 return r;
3038}
3039
3040 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003041TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003042{
3043 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003044 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003045 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003046 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003047 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003048 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003049 else if (strcmp(name, "window") == 0)
3050 {
3051 /* For current tab window.c does not bother to set or update tp_curwin
3052 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003053 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003054 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003055 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003056 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003057 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003058 else if (strcmp(name, "__members__") == 0)
3059 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003060 return NULL;
3061}
3062
3063 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003064TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003065{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003066 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003067 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003068 else
3069 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003070 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003071
3072 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003073 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3074 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003075 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003076 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003077 }
3078}
3079
3080static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003081 /* name, function, calling, documentation */
3082 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3083 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003084};
3085
3086/*
3087 * Window list object
3088 */
3089
3090static PyTypeObject TabListType;
3091static PySequenceMethods TabListAsSeq;
3092
3093typedef struct
3094{
3095 PyObject_HEAD
3096} TabListObject;
3097
3098 static PyInt
3099TabListLength(PyObject *self UNUSED)
3100{
3101 tabpage_T *tp = first_tabpage;
3102 PyInt n = 0;
3103
3104 while (tp != NULL)
3105 {
3106 ++n;
3107 tp = tp->tp_next;
3108 }
3109
3110 return n;
3111}
3112
3113 static PyObject *
3114TabListItem(PyObject *self UNUSED, PyInt n)
3115{
3116 tabpage_T *tp;
3117
3118 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3119 if (n == 0)
3120 return TabPageNew(tp);
3121
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003122 PyErr_SET_STRING(PyExc_IndexError, "no such tab page");
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003123 return NULL;
3124}
3125
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003126/*
3127 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003128 */
3129
3130typedef struct
3131{
3132 PyObject_HEAD
3133 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003134 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003135} WindowObject;
3136
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003137static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003138
3139 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003140CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003141{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003142 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003143 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003144 PyErr_SET_VIM("attempt to refer to deleted window");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003145 return -1;
3146 }
3147
3148 return 0;
3149}
3150
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003151 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003152WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003153{
3154 /* We need to handle deletion of windows underneath us.
3155 * If we add a "w_python*_ref" field to the win_T structure,
3156 * then we can get at it in win_free() in vim. We then
3157 * need to create only ONE Python object per window - if
3158 * we try to create a second, just INCREF the existing one
3159 * and return it. The (single) Python object referring to
3160 * the window is stored in "w_python*_ref".
3161 * On a win_free() we set the Python object's win_T* field
3162 * to an invalid value. We trap all uses of a window
3163 * object, and reject them if the win_T* field is invalid.
3164 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003165 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003166 * w_python_ref and w_python3_ref fields respectively.
3167 */
3168
3169 WindowObject *self;
3170
3171 if (WIN_PYTHON_REF(win))
3172 {
3173 self = WIN_PYTHON_REF(win);
3174 Py_INCREF(self);
3175 }
3176 else
3177 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003178 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003179 if (self == NULL)
3180 return NULL;
3181 self->win = win;
3182 WIN_PYTHON_REF(win) = self;
3183 }
3184
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003185 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3186
Bram Moolenaar971db462013-05-12 18:44:48 +02003187 return (PyObject *)(self);
3188}
3189
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003190 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003191WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003192{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003193 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003194 if (self->win && self->win != INVALID_WINDOW_VALUE)
3195 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003196 Py_XDECREF(((PyObject *)(self->tabObject)));
3197 PyObject_GC_Del((void *)(self));
3198}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003199
Bram Moolenaar774267b2013-05-21 20:51:59 +02003200 static int
3201WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3202{
3203 Py_VISIT(((PyObject *)(self->tabObject)));
3204 return 0;
3205}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003206
Bram Moolenaar774267b2013-05-21 20:51:59 +02003207 static int
3208WindowClear(WindowObject *self)
3209{
3210 Py_CLEAR(self->tabObject);
3211 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003212}
3213
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003214 static win_T *
3215get_firstwin(TabPageObject *tabObject)
3216{
3217 if (tabObject)
3218 {
3219 if (CheckTabPage(tabObject))
3220 return NULL;
3221 /* For current tab window.c does not bother to set or update tp_firstwin
3222 */
3223 else if (tabObject->tab == curtab)
3224 return firstwin;
3225 else
3226 return tabObject->tab->tp_firstwin;
3227 }
3228 else
3229 return firstwin;
3230}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003231static char *WindowAttrs[] = {
3232 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3233 "tabpage", "valid",
3234 NULL
3235};
3236
3237 static PyObject *
3238WindowDir(PyObject *self)
3239{
3240 return ObjectDir(self, WindowAttrs);
3241}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003242
Bram Moolenaar971db462013-05-12 18:44:48 +02003243 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003244WindowAttrValid(WindowObject *self, char *name)
3245{
3246 PyObject *r;
3247
3248 if (strcmp(name, "valid") != 0)
3249 return NULL;
3250
3251 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3252 Py_INCREF(r);
3253 return r;
3254}
3255
3256 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003257WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003258{
3259 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003260 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003261 else if (strcmp(name, "cursor") == 0)
3262 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003263 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003264
3265 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3266 }
3267 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003268 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003269#ifdef FEAT_WINDOWS
3270 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003271 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003272#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003273#ifdef FEAT_VERTSPLIT
3274 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003275 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003276 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003277 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003278#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003279 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003280 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003281 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003282 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3283 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003284 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003285 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003286 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003287 return NULL;
3288 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003289 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003290 }
3291 else if (strcmp(name, "tabpage") == 0)
3292 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003293 Py_INCREF(self->tabObject);
3294 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003295 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003296 else if (strcmp(name, "__members__") == 0)
3297 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003298 else
3299 return NULL;
3300}
3301
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003302 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003303WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003304{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003305 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003306 return -1;
3307
3308 if (strcmp(name, "buffer") == 0)
3309 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003310 PyErr_SET_STRING(PyExc_TypeError, "readonly attribute: buffer");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003311 return -1;
3312 }
3313 else if (strcmp(name, "cursor") == 0)
3314 {
3315 long lnum;
3316 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003317
3318 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
3319 return -1;
3320
Bram Moolenaard6e39182013-05-21 18:30:34 +02003321 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003322 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003323 PyErr_SET_VIM("cursor position outside buffer");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003324 return -1;
3325 }
3326
3327 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003328 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003329 return -1;
3330
Bram Moolenaard6e39182013-05-21 18:30:34 +02003331 self->win->w_cursor.lnum = lnum;
3332 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003333#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003334 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003335#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003336 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003337 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003338
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003339 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003340 return 0;
3341 }
3342 else if (strcmp(name, "height") == 0)
3343 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003344 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003345 win_T *savewin;
3346
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003347 if (NumberToLong(val, &height, NUMBER_INT))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003348 return -1;
3349
3350#ifdef FEAT_GUI
3351 need_mouse_correct = TRUE;
3352#endif
3353 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003354 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003355
3356 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003357 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003358 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003359 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003360 return -1;
3361
3362 return 0;
3363 }
3364#ifdef FEAT_VERTSPLIT
3365 else if (strcmp(name, "width") == 0)
3366 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003367 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003368 win_T *savewin;
3369
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003370 if (NumberToLong(val, &width, NUMBER_INT))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003371 return -1;
3372
3373#ifdef FEAT_GUI
3374 need_mouse_correct = TRUE;
3375#endif
3376 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003377 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003378
3379 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003380 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003381 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003382 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003383 return -1;
3384
3385 return 0;
3386 }
3387#endif
3388 else
3389 {
3390 PyErr_SetString(PyExc_AttributeError, name);
3391 return -1;
3392 }
3393}
3394
3395 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003396WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003397{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003398 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003399 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003400 else
3401 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003402 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003403
Bram Moolenaar6d216452013-05-12 19:00:41 +02003404 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003405 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003406 (self));
3407 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003408 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003409 }
3410}
3411
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003412static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003413 /* name, function, calling, documentation */
3414 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3415 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003416};
3417
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003418/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003419 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003420 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003421
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003422static PyTypeObject WinListType;
3423static PySequenceMethods WinListAsSeq;
3424
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003425typedef struct
3426{
3427 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003428 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003429} WinListObject;
3430
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003431 static PyObject *
3432WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003433{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003434 WinListObject *self;
3435
3436 self = PyObject_NEW(WinListObject, &WinListType);
3437 self->tabObject = tabObject;
3438 Py_INCREF(tabObject);
3439
3440 return (PyObject *)(self);
3441}
3442
3443 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003444WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003445{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003446 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003447
3448 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003449 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003450 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003451 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003452
3453 DESTRUCTOR_FINISH(self);
3454}
3455
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003456 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003457WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003458{
3459 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003460 PyInt n = 0;
3461
Bram Moolenaard6e39182013-05-21 18:30:34 +02003462 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003463 return -1;
3464
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003465 while (w != NULL)
3466 {
3467 ++n;
3468 w = W_NEXT(w);
3469 }
3470
3471 return n;
3472}
3473
3474 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003475WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003476{
3477 win_T *w;
3478
Bram Moolenaard6e39182013-05-21 18:30:34 +02003479 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003480 return NULL;
3481
3482 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003483 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003484 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003485
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003486 PyErr_SET_STRING(PyExc_IndexError, "no such window");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003487 return NULL;
3488}
3489
3490/* Convert a Python string into a Vim line.
3491 *
3492 * The result is in allocated memory. All internal nulls are replaced by
3493 * newline characters. It is an error for the string to contain newline
3494 * characters.
3495 *
3496 * On errors, the Python exception data is set, and NULL is returned.
3497 */
3498 static char *
3499StringToLine(PyObject *obj)
3500{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003501 char *str;
3502 char *save;
3503 PyObject *bytes = NULL;
3504 Py_ssize_t len;
3505 PyInt i;
3506 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003507
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003508 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003509 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003510 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3511 || str == NULL)
3512 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003513 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003514 else if (PyUnicode_Check(obj))
3515 {
3516 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3517 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003518
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003519 if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
3520 || str == NULL)
3521 {
3522 Py_DECREF(bytes);
3523 return NULL;
3524 }
3525 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003526
3527 /*
3528 * Error checking: String must not contain newlines, as we
3529 * are replacing a single line, and we must replace it with
3530 * a single line.
3531 * A trailing newline is removed, so that append(f.readlines()) works.
3532 */
3533 p = memchr(str, '\n', len);
3534 if (p != NULL)
3535 {
3536 if (p == str + len - 1)
3537 --len;
3538 else
3539 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003540 PyErr_SET_VIM("string cannot contain newlines");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003541 return NULL;
3542 }
3543 }
3544
3545 /* Create a copy of the string, with internal nulls replaced by
3546 * newline characters, as is the vim convention.
3547 */
3548 save = (char *)alloc((unsigned)(len+1));
3549 if (save == NULL)
3550 {
3551 PyErr_NoMemory();
3552 return NULL;
3553 }
3554
3555 for (i = 0; i < len; ++i)
3556 {
3557 if (str[i] == '\0')
3558 save[i] = '\n';
3559 else
3560 save[i] = str[i];
3561 }
3562
3563 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003564 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003565
3566 return save;
3567}
3568
3569/* Get a line from the specified buffer. The line number is
3570 * in Vim format (1-based). The line is returned as a Python
3571 * string object.
3572 */
3573 static PyObject *
3574GetBufferLine(buf_T *buf, PyInt n)
3575{
3576 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3577}
3578
3579
3580/* Get a list of lines from the specified buffer. The line numbers
3581 * are in Vim format (1-based). The range is from lo up to, but not
3582 * including, hi. The list is returned as a Python list of string objects.
3583 */
3584 static PyObject *
3585GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3586{
3587 PyInt i;
3588 PyInt n = hi - lo;
3589 PyObject *list = PyList_New(n);
3590
3591 if (list == NULL)
3592 return NULL;
3593
3594 for (i = 0; i < n; ++i)
3595 {
3596 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3597
3598 /* Error check - was the Python string creation OK? */
3599 if (str == NULL)
3600 {
3601 Py_DECREF(list);
3602 return NULL;
3603 }
3604
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02003605 PyList_SET_ITEM(list, i, str);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003606 }
3607
3608 /* The ownership of the Python list is passed to the caller (ie,
3609 * the caller should Py_DECREF() the object when it is finished
3610 * with it).
3611 */
3612
3613 return list;
3614}
3615
3616/*
3617 * Check if deleting lines made the cursor position invalid.
3618 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3619 * deleted).
3620 */
3621 static void
3622py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3623{
3624 if (curwin->w_cursor.lnum >= lo)
3625 {
3626 /* Adjust the cursor position if it's in/after the changed
3627 * lines. */
3628 if (curwin->w_cursor.lnum >= hi)
3629 {
3630 curwin->w_cursor.lnum += extra;
3631 check_cursor_col();
3632 }
3633 else if (extra < 0)
3634 {
3635 curwin->w_cursor.lnum = lo;
3636 check_cursor();
3637 }
3638 else
3639 check_cursor_col();
3640 changed_cline_bef_curs();
3641 }
3642 invalidate_botline();
3643}
3644
Bram Moolenaar19e60942011-06-19 00:27:51 +02003645/*
3646 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003647 * in Vim format (1-based). The replacement line is given as
3648 * a Python string object. The object is checked for validity
3649 * and correct format. Errors are returned as a value of FAIL.
3650 * The return value is OK on success.
3651 * If OK is returned and len_change is not NULL, *len_change
3652 * is set to the change in the buffer length.
3653 */
3654 static int
3655SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3656{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003657 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003658 * There are three cases:
3659 * 1. NULL, or None - this is a deletion.
3660 * 2. A string - this is a replacement.
3661 * 3. Anything else - this is an error.
3662 */
3663 if (line == Py_None || line == NULL)
3664 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003665 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003666
3667 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003668 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003669
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003670 VimTryStart();
3671
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003672 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003673 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003674 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003675 RAISE_LINE_FAIL("delete");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003676 else
3677 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003678 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003679 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3680 deleted_lines_mark((linenr_T)n, 1L);
3681 }
3682
Bram Moolenaar105bc352013-05-17 16:03:57 +02003683 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003684
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003685 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003686 return FAIL;
3687
3688 if (len_change)
3689 *len_change = -1;
3690
3691 return OK;
3692 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003693 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003694 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003695 char *save = StringToLine(line);
3696 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003697
3698 if (save == NULL)
3699 return FAIL;
3700
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003701 VimTryStart();
3702
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003703 /* We do not need to free "save" if ml_replace() consumes it. */
3704 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003705 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003706
3707 if (u_savesub((linenr_T)n) == FAIL)
3708 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003709 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003710 vim_free(save);
3711 }
3712 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3713 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003714 RAISE_LINE_FAIL("replace");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003715 vim_free(save);
3716 }
3717 else
3718 changed_bytes((linenr_T)n, 0);
3719
Bram Moolenaar105bc352013-05-17 16:03:57 +02003720 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003721
3722 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003723 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003724 check_cursor_col();
3725
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003726 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003727 return FAIL;
3728
3729 if (len_change)
3730 *len_change = 0;
3731
3732 return OK;
3733 }
3734 else
3735 {
3736 PyErr_BadArgument();
3737 return FAIL;
3738 }
3739}
3740
Bram Moolenaar19e60942011-06-19 00:27:51 +02003741/* Replace a range of lines in the specified buffer. The line numbers are in
3742 * Vim format (1-based). The range is from lo up to, but not including, hi.
3743 * The replacement lines are given as a Python list of string objects. The
3744 * list is checked for validity and correct format. Errors are returned as a
3745 * value of FAIL. The return value is OK on success.
3746 * If OK is returned and len_change is not NULL, *len_change
3747 * is set to the change in the buffer length.
3748 */
3749 static int
3750SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3751{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003752 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003753 * There are three cases:
3754 * 1. NULL, or None - this is a deletion.
3755 * 2. A list - this is a replacement.
3756 * 3. Anything else - this is an error.
3757 */
3758 if (list == Py_None || list == NULL)
3759 {
3760 PyInt i;
3761 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003762 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003763
3764 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003765 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003766 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003767
3768 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003769 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003770 else
3771 {
3772 for (i = 0; i < n; ++i)
3773 {
3774 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3775 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003776 RAISE_LINE_FAIL("delete");
Bram Moolenaar19e60942011-06-19 00:27:51 +02003777 break;
3778 }
3779 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003780 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003781 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3782 deleted_lines_mark((linenr_T)lo, (long)i);
3783 }
3784
Bram Moolenaar105bc352013-05-17 16:03:57 +02003785 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003786
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003787 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003788 return FAIL;
3789
3790 if (len_change)
3791 *len_change = -n;
3792
3793 return OK;
3794 }
3795 else if (PyList_Check(list))
3796 {
3797 PyInt i;
3798 PyInt new_len = PyList_Size(list);
3799 PyInt old_len = hi - lo;
3800 PyInt extra = 0; /* lines added to text, can be negative */
3801 char **array;
3802 buf_T *savebuf;
3803
3804 if (new_len == 0) /* avoid allocating zero bytes */
3805 array = NULL;
3806 else
3807 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003808 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003809 if (array == NULL)
3810 {
3811 PyErr_NoMemory();
3812 return FAIL;
3813 }
3814 }
3815
3816 for (i = 0; i < new_len; ++i)
3817 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003818 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003819
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003820 if (!(line = PyList_GetItem(list, i)) ||
3821 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003822 {
3823 while (i)
3824 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003825 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003826 return FAIL;
3827 }
3828 }
3829
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003830 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003831 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003832
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003833 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003834 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003835
3836 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003837 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003838
3839 /* If the size of the range is reducing (ie, new_len < old_len) we
3840 * need to delete some old_len. We do this at the start, by
3841 * repeatedly deleting line "lo".
3842 */
3843 if (!PyErr_Occurred())
3844 {
3845 for (i = 0; i < old_len - new_len; ++i)
3846 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3847 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003848 RAISE_LINE_FAIL("delete");
Bram Moolenaar19e60942011-06-19 00:27:51 +02003849 break;
3850 }
3851 extra -= i;
3852 }
3853
3854 /* For as long as possible, replace the existing old_len with the
3855 * new old_len. This is a more efficient operation, as it requires
3856 * less memory allocation and freeing.
3857 */
3858 if (!PyErr_Occurred())
3859 {
3860 for (i = 0; i < old_len && i < new_len; ++i)
3861 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3862 == FAIL)
3863 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003864 RAISE_LINE_FAIL("replace");
Bram Moolenaar19e60942011-06-19 00:27:51 +02003865 break;
3866 }
3867 }
3868 else
3869 i = 0;
3870
3871 /* Now we may need to insert the remaining new old_len. If we do, we
3872 * must free the strings as we finish with them (we can't pass the
3873 * responsibility to vim in this case).
3874 */
3875 if (!PyErr_Occurred())
3876 {
3877 while (i < new_len)
3878 {
3879 if (ml_append((linenr_T)(lo + i - 1),
3880 (char_u *)array[i], 0, FALSE) == FAIL)
3881 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003882 RAISE_LINE_FAIL("insert");
Bram Moolenaar19e60942011-06-19 00:27:51 +02003883 break;
3884 }
3885 vim_free(array[i]);
3886 ++i;
3887 ++extra;
3888 }
3889 }
3890
3891 /* Free any left-over old_len, as a result of an error */
3892 while (i < new_len)
3893 {
3894 vim_free(array[i]);
3895 ++i;
3896 }
3897
3898 /* Free the array of old_len. All of its contents have now
3899 * been dealt with (either freed, or the responsibility passed
3900 * to vim.
3901 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003902 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003903
3904 /* Adjust marks. Invalidate any which lie in the
3905 * changed range, and move any in the remainder of the buffer.
3906 */
3907 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3908 (long)MAXLNUM, (long)extra);
3909 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3910
Bram Moolenaar105bc352013-05-17 16:03:57 +02003911 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003912 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3913
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003914 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003915 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003916
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003917 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003918 return FAIL;
3919
3920 if (len_change)
3921 *len_change = new_len - old_len;
3922
3923 return OK;
3924 }
3925 else
3926 {
3927 PyErr_BadArgument();
3928 return FAIL;
3929 }
3930}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003931
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003932/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003933 * The line number is in Vim format (1-based). The lines to be inserted are
3934 * given as a Python list of string objects or as a single string. The lines
3935 * to be added are checked for validity and correct format. Errors are
3936 * returned as a value of FAIL. The return value is OK on success.
3937 * If OK is returned and len_change is not NULL, *len_change
3938 * is set to the change in the buffer length.
3939 */
3940 static int
3941InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3942{
3943 /* First of all, we check the type of the supplied Python object.
3944 * It must be a string or a list, or the call is in error.
3945 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003946 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003947 {
3948 char *str = StringToLine(lines);
3949 buf_T *savebuf;
3950
3951 if (str == NULL)
3952 return FAIL;
3953
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003954 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003955 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003956 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003957
3958 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003959 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003960 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003961 RAISE_LINE_FAIL("insert");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003962 else
3963 appended_lines_mark((linenr_T)n, 1L);
3964
3965 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003966 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003967 update_screen(VALID);
3968
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003969 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003970 return FAIL;
3971
3972 if (len_change)
3973 *len_change = 1;
3974
3975 return OK;
3976 }
3977 else if (PyList_Check(lines))
3978 {
3979 PyInt i;
3980 PyInt size = PyList_Size(lines);
3981 char **array;
3982 buf_T *savebuf;
3983
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003984 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003985 if (array == NULL)
3986 {
3987 PyErr_NoMemory();
3988 return FAIL;
3989 }
3990
3991 for (i = 0; i < size; ++i)
3992 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003993 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003994
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003995 if (!(line = PyList_GetItem(lines, i)) ||
3996 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003997 {
3998 while (i)
3999 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004000 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004001 return FAIL;
4002 }
4003 }
4004
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004005 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004006 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004007 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004008
4009 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004010 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004011 else
4012 {
4013 for (i = 0; i < size; ++i)
4014 {
4015 if (ml_append((linenr_T)(n + i),
4016 (char_u *)array[i], 0, FALSE) == FAIL)
4017 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004018 RAISE_LINE_FAIL("insert");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004019
4020 /* Free the rest of the lines */
4021 while (i < size)
4022 vim_free(array[i++]);
4023
4024 break;
4025 }
4026 vim_free(array[i]);
4027 }
4028 if (i > 0)
4029 appended_lines_mark((linenr_T)n, (long)i);
4030 }
4031
4032 /* Free the array of lines. All of its contents have now
4033 * been freed.
4034 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004035 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036
Bram Moolenaar105bc352013-05-17 16:03:57 +02004037 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004038 update_screen(VALID);
4039
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004040 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004041 return FAIL;
4042
4043 if (len_change)
4044 *len_change = size;
4045
4046 return OK;
4047 }
4048 else
4049 {
4050 PyErr_BadArgument();
4051 return FAIL;
4052 }
4053}
4054
4055/*
4056 * Common routines for buffers and line ranges
4057 * -------------------------------------------
4058 */
4059
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004060typedef struct
4061{
4062 PyObject_HEAD
4063 buf_T *buf;
4064} BufferObject;
4065
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004066 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004067CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004068{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004069 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004070 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004071 PyErr_SET_VIM("attempt to refer to deleted buffer");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004072 return -1;
4073 }
4074
4075 return 0;
4076}
4077
4078 static PyObject *
4079RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4080{
4081 if (CheckBuffer(self))
4082 return NULL;
4083
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004084 if (end == -1)
4085 end = self->buf->b_ml.ml_line_count;
4086
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004087 if (n < 0)
4088 n += end - start + 1;
4089
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004090 if (n < 0 || n > end - start)
4091 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004092 PyErr_SET_STRING(PyExc_IndexError, "line number out of range");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093 return NULL;
4094 }
4095
4096 return GetBufferLine(self->buf, n+start);
4097}
4098
4099 static PyObject *
4100RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4101{
4102 PyInt size;
4103
4104 if (CheckBuffer(self))
4105 return NULL;
4106
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004107 if (end == -1)
4108 end = self->buf->b_ml.ml_line_count;
4109
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004110 size = end - start + 1;
4111
4112 if (lo < 0)
4113 lo = 0;
4114 else if (lo > size)
4115 lo = size;
4116 if (hi < 0)
4117 hi = 0;
4118 if (hi < lo)
4119 hi = lo;
4120 else if (hi > size)
4121 hi = size;
4122
4123 return GetBufferLineList(self->buf, lo+start, hi+start);
4124}
4125
4126 static PyInt
4127RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
4128{
4129 PyInt len_change;
4130
4131 if (CheckBuffer(self))
4132 return -1;
4133
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004134 if (end == -1)
4135 end = self->buf->b_ml.ml_line_count;
4136
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004137 if (n < 0)
4138 n += end - start + 1;
4139
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004140 if (n < 0 || n > end - start)
4141 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004142 PyErr_SET_STRING(PyExc_IndexError, "line number out of range");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004143 return -1;
4144 }
4145
4146 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
4147 return -1;
4148
4149 if (new_end)
4150 *new_end = end + len_change;
4151
4152 return 0;
4153}
4154
Bram Moolenaar19e60942011-06-19 00:27:51 +02004155 static PyInt
4156RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
4157{
4158 PyInt size;
4159 PyInt len_change;
4160
4161 /* Self must be a valid buffer */
4162 if (CheckBuffer(self))
4163 return -1;
4164
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004165 if (end == -1)
4166 end = self->buf->b_ml.ml_line_count;
4167
Bram Moolenaar19e60942011-06-19 00:27:51 +02004168 /* Sort out the slice range */
4169 size = end - start + 1;
4170
4171 if (lo < 0)
4172 lo = 0;
4173 else if (lo > size)
4174 lo = size;
4175 if (hi < 0)
4176 hi = 0;
4177 if (hi < lo)
4178 hi = lo;
4179 else if (hi > size)
4180 hi = size;
4181
4182 if (SetBufferLineList(self->buf, lo + start, hi + start,
4183 val, &len_change) == FAIL)
4184 return -1;
4185
4186 if (new_end)
4187 *new_end = end + len_change;
4188
4189 return 0;
4190}
4191
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004192
4193 static PyObject *
4194RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
4195{
4196 PyObject *lines;
4197 PyInt len_change;
4198 PyInt max;
4199 PyInt n;
4200
4201 if (CheckBuffer(self))
4202 return NULL;
4203
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004204 if (end == -1)
4205 end = self->buf->b_ml.ml_line_count;
4206
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004207 max = n = end - start + 1;
4208
4209 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4210 return NULL;
4211
4212 if (n < 0 || n > max)
4213 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004214 PyErr_SET_STRING(PyExc_IndexError, "line number out of range");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004215 return NULL;
4216 }
4217
4218 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4219 return NULL;
4220
4221 if (new_end)
4222 *new_end = end + len_change;
4223
4224 Py_INCREF(Py_None);
4225 return Py_None;
4226}
4227
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004228/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004229 */
4230
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004231static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004232static PySequenceMethods RangeAsSeq;
4233static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004234
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004235typedef struct
4236{
4237 PyObject_HEAD
4238 BufferObject *buf;
4239 PyInt start;
4240 PyInt end;
4241} RangeObject;
4242
4243 static PyObject *
4244RangeNew(buf_T *buf, PyInt start, PyInt end)
4245{
4246 BufferObject *bufr;
4247 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004248 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004249 if (self == NULL)
4250 return NULL;
4251
4252 bufr = (BufferObject *)BufferNew(buf);
4253 if (bufr == NULL)
4254 {
4255 Py_DECREF(self);
4256 return NULL;
4257 }
4258 Py_INCREF(bufr);
4259
4260 self->buf = bufr;
4261 self->start = start;
4262 self->end = end;
4263
4264 return (PyObject *)(self);
4265}
4266
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004267 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004268RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004269{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004270 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004271 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004272 PyObject_GC_Del((void *)(self));
4273}
4274
4275 static int
4276RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4277{
4278 Py_VISIT(((PyObject *)(self->buf)));
4279 return 0;
4280}
4281
4282 static int
4283RangeClear(RangeObject *self)
4284{
4285 Py_CLEAR(self->buf);
4286 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004287}
4288
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004289 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004290RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004291{
4292 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004293 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004294 return -1; /* ??? */
4295
Bram Moolenaard6e39182013-05-21 18:30:34 +02004296 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004297}
4298
4299 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004300RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004301{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004302 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004303}
4304
4305 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004306RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004307{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004308 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004309}
4310
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004311static char *RangeAttrs[] = {
4312 "start", "end",
4313 NULL
4314};
4315
4316 static PyObject *
4317RangeDir(PyObject *self)
4318{
4319 return ObjectDir(self, RangeAttrs);
4320}
4321
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004322 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004323RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004324{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004325 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004326}
4327
4328 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004329RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004330{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004331 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004332 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4333 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004334 else
4335 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004336 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004337
4338 if (name == NULL)
4339 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004340
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004341 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004342 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004343 }
4344}
4345
4346static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004347 /* name, function, calling, documentation */
4348 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004349 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4350 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004351};
4352
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004353static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004354static PySequenceMethods BufferAsSeq;
4355static PyMappingMethods BufferAsMapping;
4356
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004357 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004358BufferNew(buf_T *buf)
4359{
4360 /* We need to handle deletion of buffers underneath us.
4361 * If we add a "b_python*_ref" field to the buf_T structure,
4362 * then we can get at it in buf_freeall() in vim. We then
4363 * need to create only ONE Python object per buffer - if
4364 * we try to create a second, just INCREF the existing one
4365 * and return it. The (single) Python object referring to
4366 * the buffer is stored in "b_python*_ref".
4367 * Question: what to do on a buf_freeall(). We'll probably
4368 * have to either delete the Python object (DECREF it to
4369 * zero - a bad idea, as it leaves dangling refs!) or
4370 * set the buf_T * value to an invalid value (-1?), which
4371 * means we need checks in all access functions... Bah.
4372 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004373 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004374 * b_python_ref and b_python3_ref fields respectively.
4375 */
4376
4377 BufferObject *self;
4378
4379 if (BUF_PYTHON_REF(buf) != NULL)
4380 {
4381 self = BUF_PYTHON_REF(buf);
4382 Py_INCREF(self);
4383 }
4384 else
4385 {
4386 self = PyObject_NEW(BufferObject, &BufferType);
4387 if (self == NULL)
4388 return NULL;
4389 self->buf = buf;
4390 BUF_PYTHON_REF(buf) = self;
4391 }
4392
4393 return (PyObject *)(self);
4394}
4395
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004396 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004397BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004398{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004399 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4400 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004401
4402 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004403}
4404
Bram Moolenaar971db462013-05-12 18:44:48 +02004405 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004406BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004407{
4408 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004409 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004410 return -1; /* ??? */
4411
Bram Moolenaard6e39182013-05-21 18:30:34 +02004412 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004413}
4414
4415 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004416BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004417{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004418 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004419}
4420
4421 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004422BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004423{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004424 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004425}
4426
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004427static char *BufferAttrs[] = {
4428 "name", "number", "vars", "options", "valid",
4429 NULL
4430};
4431
4432 static PyObject *
4433BufferDir(PyObject *self)
4434{
4435 return ObjectDir(self, BufferAttrs);
4436}
4437
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004438 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004439BufferAttrValid(BufferObject *self, char *name)
4440{
4441 PyObject *r;
4442
4443 if (strcmp(name, "valid") != 0)
4444 return NULL;
4445
4446 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4447 Py_INCREF(r);
4448 return r;
4449}
4450
4451 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004452BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004453{
4454 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004455 return PyString_FromString((self->buf->b_ffname == NULL
4456 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004457 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004458 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004459 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004460 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004461 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004462 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4463 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004464 else if (strcmp(name, "__members__") == 0)
4465 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004466 else
4467 return NULL;
4468}
4469
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004470 static int
4471BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4472{
4473 if (CheckBuffer(self))
4474 return -1;
4475
4476 if (strcmp(name, "name") == 0)
4477 {
4478 char_u *val;
4479 aco_save_T aco;
4480 int r;
4481 PyObject *todecref;
4482
4483 if (!(val = StringToChars(valObject, &todecref)))
4484 return -1;
4485
4486 VimTryStart();
4487 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4488 aucmd_prepbuf(&aco, self->buf);
4489 r = rename_buffer(val);
4490 aucmd_restbuf(&aco);
4491 Py_XDECREF(todecref);
4492 if (VimTryEnd())
4493 return -1;
4494
4495 if (r == FAIL)
4496 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004497 PyErr_SET_VIM("failed to rename buffer");
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004498 return -1;
4499 }
4500 return 0;
4501 }
4502 else
4503 {
4504 PyErr_SetString(PyExc_AttributeError, name);
4505 return -1;
4506 }
4507}
4508
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004509 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004510BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004511{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004512 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004513}
4514
4515 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004516BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004517{
4518 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004519 char_u *pmark;
4520 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004521 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004522 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004523
Bram Moolenaard6e39182013-05-21 18:30:34 +02004524 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004525 return NULL;
4526
Bram Moolenaar389a1792013-06-23 13:00:44 +02004527 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004528 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004529
Bram Moolenaar389a1792013-06-23 13:00:44 +02004530 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004531 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004532 PyErr_SET_STRING(PyExc_ValueError,
4533 "mark name must be a single character");
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004534 return NULL;
4535 }
4536
4537 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004538
4539 Py_XDECREF(todecref);
4540
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004541 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004542 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004543 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004544 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004545 if (VimTryEnd())
4546 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004547
4548 if (posp == NULL)
4549 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004550 PyErr_SET_VIM("invalid mark name");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004551 return NULL;
4552 }
4553
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004554 if (posp->lnum <= 0)
4555 {
4556 /* Or raise an error? */
4557 Py_INCREF(Py_None);
4558 return Py_None;
4559 }
4560
4561 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4562}
4563
4564 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004565BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004566{
4567 PyInt start;
4568 PyInt end;
4569
Bram Moolenaard6e39182013-05-21 18:30:34 +02004570 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004571 return NULL;
4572
4573 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4574 return NULL;
4575
Bram Moolenaard6e39182013-05-21 18:30:34 +02004576 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004577}
4578
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004579 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004580BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004581{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004582 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004583 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004584 else
4585 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004586 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004587
4588 if (name == NULL)
4589 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004590
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004591 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004592 }
4593}
4594
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004595static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004596 /* name, function, calling, documentation */
4597 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004598 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004599 {"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 +02004600 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4601 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004602};
4603
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004604/*
4605 * Buffer list object - Implementation
4606 */
4607
4608static PyTypeObject BufMapType;
4609
4610typedef struct
4611{
4612 PyObject_HEAD
4613} BufMapObject;
4614
4615 static PyInt
4616BufMapLength(PyObject *self UNUSED)
4617{
4618 buf_T *b = firstbuf;
4619 PyInt n = 0;
4620
4621 while (b)
4622 {
4623 ++n;
4624 b = b->b_next;
4625 }
4626
4627 return n;
4628}
4629
4630 static PyObject *
4631BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4632{
4633 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004634 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004635
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004636 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004637 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004638
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004639 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004640
4641 if (b)
4642 return BufferNew(b);
4643 else
4644 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004645 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004646 return NULL;
4647 }
4648}
4649
4650 static void
4651BufMapIterDestruct(PyObject *buffer)
4652{
4653 /* Iteration was stopped before all buffers were processed */
4654 if (buffer)
4655 {
4656 Py_DECREF(buffer);
4657 }
4658}
4659
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004660 static int
4661BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4662{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004663 if (buffer)
4664 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004665 return 0;
4666}
4667
4668 static int
4669BufMapIterClear(PyObject **buffer)
4670{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004671 if (*buffer)
4672 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004673 return 0;
4674}
4675
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004676 static PyObject *
4677BufMapIterNext(PyObject **buffer)
4678{
4679 PyObject *next;
4680 PyObject *r;
4681
4682 if (!*buffer)
4683 return NULL;
4684
4685 r = *buffer;
4686
4687 if (CheckBuffer((BufferObject *)(r)))
4688 {
4689 *buffer = NULL;
4690 return NULL;
4691 }
4692
4693 if (!((BufferObject *)(r))->buf->b_next)
4694 next = NULL;
4695 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4696 return NULL;
4697 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004698 /* Do not increment reference: we no longer hold it (decref), but whoever
4699 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004700 return r;
4701}
4702
4703 static PyObject *
4704BufMapIter(PyObject *self UNUSED)
4705{
4706 PyObject *buffer;
4707
4708 buffer = BufferNew(firstbuf);
4709 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004710 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4711 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004712}
4713
4714static PyMappingMethods BufMapAsMapping = {
4715 (lenfunc) BufMapLength,
4716 (binaryfunc) BufMapItem,
4717 (objobjargproc) 0,
4718};
4719
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004720/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004721 */
4722
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004723static char *CurrentAttrs[] = {
4724 "buffer", "window", "line", "range", "tabpage",
4725 NULL
4726};
4727
4728 static PyObject *
4729CurrentDir(PyObject *self)
4730{
4731 return ObjectDir(self, CurrentAttrs);
4732}
4733
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004734 static PyObject *
4735CurrentGetattr(PyObject *self UNUSED, char *name)
4736{
4737 if (strcmp(name, "buffer") == 0)
4738 return (PyObject *)BufferNew(curbuf);
4739 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004740 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004741 else if (strcmp(name, "tabpage") == 0)
4742 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004743 else if (strcmp(name, "line") == 0)
4744 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4745 else if (strcmp(name, "range") == 0)
4746 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004747 else if (strcmp(name, "__members__") == 0)
4748 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004749 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004750#if PY_MAJOR_VERSION < 3
4751 return Py_FindMethod(WindowMethods, self, name);
4752#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004753 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004754#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004755}
4756
4757 static int
4758CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4759{
4760 if (strcmp(name, "line") == 0)
4761 {
4762 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4763 return -1;
4764
4765 return 0;
4766 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004767 else if (strcmp(name, "buffer") == 0)
4768 {
4769 int count;
4770
4771 if (value->ob_type != &BufferType)
4772 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004773 PyErr_FORMAT(PyExc_TypeError,
4774 "expected vim.Buffer object, but got %s",
4775 Py_TYPE_NAME(value));
Bram Moolenaare7614592013-05-15 15:51:08 +02004776 return -1;
4777 }
4778
4779 if (CheckBuffer((BufferObject *)(value)))
4780 return -1;
4781 count = ((BufferObject *)(value))->buf->b_fnum;
4782
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004783 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004784 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4785 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004786 if (VimTryEnd())
4787 return -1;
Bram Moolenaarc476e522013-06-23 13:46:40 +02004788 PyErr_VIM_FORMAT("failed to switch to buffer %d", count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004789 return -1;
4790 }
4791
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004792 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004793 }
4794 else if (strcmp(name, "window") == 0)
4795 {
4796 int count;
4797
4798 if (value->ob_type != &WindowType)
4799 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004800 PyErr_FORMAT(PyExc_TypeError,
4801 "expected vim.Window object, but got %s",
4802 Py_TYPE_NAME(value));
Bram Moolenaare7614592013-05-15 15:51:08 +02004803 return -1;
4804 }
4805
4806 if (CheckWindow((WindowObject *)(value)))
4807 return -1;
4808 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4809
4810 if (!count)
4811 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004812 PyErr_SET_STRING(PyExc_ValueError,
4813 "failed to find window in the current tab page");
Bram Moolenaare7614592013-05-15 15:51:08 +02004814 return -1;
4815 }
4816
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004817 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004818 win_goto(((WindowObject *)(value))->win);
4819 if (((WindowObject *)(value))->win != curwin)
4820 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004821 if (VimTryEnd())
4822 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004823 PyErr_SET_STRING(PyExc_RuntimeError,
4824 "did not switch to the specified window");
Bram Moolenaare7614592013-05-15 15:51:08 +02004825 return -1;
4826 }
4827
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004828 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004829 }
4830 else if (strcmp(name, "tabpage") == 0)
4831 {
4832 if (value->ob_type != &TabPageType)
4833 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004834 PyErr_FORMAT(PyExc_TypeError,
4835 "expected vim.TabPage object, but got %s",
4836 Py_TYPE_NAME(value));
Bram Moolenaare7614592013-05-15 15:51:08 +02004837 return -1;
4838 }
4839
4840 if (CheckTabPage((TabPageObject *)(value)))
4841 return -1;
4842
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004843 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004844 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4845 if (((TabPageObject *)(value))->tab != curtab)
4846 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004847 if (VimTryEnd())
4848 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004849 PyErr_SET_STRING(PyExc_RuntimeError,
4850 "did not switch to the specified tab page");
Bram Moolenaare7614592013-05-15 15:51:08 +02004851 return -1;
4852 }
4853
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004854 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004855 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004856 else
4857 {
4858 PyErr_SetString(PyExc_AttributeError, name);
4859 return -1;
4860 }
4861}
4862
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004863static struct PyMethodDef CurrentMethods[] = {
4864 /* name, function, calling, documentation */
4865 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4866 { NULL, NULL, 0, NULL}
4867};
4868
Bram Moolenaardb913952012-06-29 12:54:53 +02004869 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004870init_range_cmd(exarg_T *eap)
4871{
4872 RangeStart = eap->line1;
4873 RangeEnd = eap->line2;
4874}
4875
4876 static void
4877init_range_eval(typval_T *rettv UNUSED)
4878{
4879 RangeStart = (PyInt) curwin->w_cursor.lnum;
4880 RangeEnd = RangeStart;
4881}
4882
4883 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004884run_cmd(const char *cmd, void *arg UNUSED
4885#ifdef PY_CAN_RECURSE
4886 , PyGILState_STATE *pygilstate UNUSED
4887#endif
4888 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004889{
4890 PyRun_SimpleString((char *) cmd);
4891}
4892
4893static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4894static int code_hdr_len = 30;
4895
4896 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004897run_do(const char *cmd, void *arg UNUSED
4898#ifdef PY_CAN_RECURSE
4899 , PyGILState_STATE *pygilstate
4900#endif
4901 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004902{
4903 PyInt lnum;
4904 size_t len;
4905 char *code;
4906 int status;
4907 PyObject *pyfunc, *pymain;
4908
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004909 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004910 {
4911 EMSG(_("cannot save undo information"));
4912 return;
4913 }
4914
4915 len = code_hdr_len + STRLEN(cmd);
4916 code = PyMem_New(char, len + 1);
4917 memcpy(code, code_hdr, code_hdr_len);
4918 STRCPY(code + code_hdr_len, cmd);
4919 status = PyRun_SimpleString(code);
4920 PyMem_Free(code);
4921
4922 if (status)
4923 {
4924 EMSG(_("failed to run the code"));
4925 return;
4926 }
4927
4928 status = 0;
4929 pymain = PyImport_AddModule("__main__");
4930 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004931#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004932 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004933#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004934
4935 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4936 {
4937 PyObject *line, *linenr, *ret;
4938
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004939#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004940 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004941#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004942 if (!(line = GetBufferLine(curbuf, lnum)))
4943 goto err;
4944 if (!(linenr = PyInt_FromLong((long) lnum)))
4945 {
4946 Py_DECREF(line);
4947 goto err;
4948 }
4949 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4950 Py_DECREF(line);
4951 Py_DECREF(linenr);
4952 if (!ret)
4953 goto err;
4954
4955 if (ret != Py_None)
4956 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4957 goto err;
4958
4959 Py_XDECREF(ret);
4960 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004961#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004962 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004963#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004964 }
4965 goto out;
4966err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004967#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004968 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004969#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004970 PyErr_PrintEx(0);
4971 PythonIO_Flush();
4972 status = 1;
4973out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004974#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004975 if (!status)
4976 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004977#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004978 Py_DECREF(pyfunc);
4979 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4980 if (status)
4981 return;
4982 check_cursor();
4983 update_curbuf(NOT_VALID);
4984}
4985
4986 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004987run_eval(const char *cmd, typval_T *rettv
4988#ifdef PY_CAN_RECURSE
4989 , PyGILState_STATE *pygilstate UNUSED
4990#endif
4991 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004992{
4993 PyObject *r;
4994
4995 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4996 if (r == NULL)
4997 {
4998 if (PyErr_Occurred() && !msg_silent)
4999 PyErr_PrintEx(0);
5000 EMSG(_("E858: Eval did not return a valid python object"));
5001 }
5002 else
5003 {
5004 if (ConvertFromPyObject(r, rettv) == -1)
5005 EMSG(_("E859: Failed to convert returned python object to vim value"));
5006 Py_DECREF(r);
5007 }
5008 PyErr_Clear();
5009}
5010
5011 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005012set_ref_in_py(const int copyID)
5013{
5014 pylinkedlist_T *cur;
5015 dict_T *dd;
5016 list_T *ll;
5017
5018 if (lastdict != NULL)
5019 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5020 {
5021 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5022 if (dd->dv_copyID != copyID)
5023 {
5024 dd->dv_copyID = copyID;
5025 set_ref_in_ht(&dd->dv_hashtab, copyID);
5026 }
5027 }
5028
5029 if (lastlist != NULL)
5030 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5031 {
5032 ll = ((ListObject *) (cur->pll_obj))->list;
5033 if (ll->lv_copyID != copyID)
5034 {
5035 ll->lv_copyID = copyID;
5036 set_ref_in_list(ll, copyID);
5037 }
5038 }
5039}
5040
5041 static int
5042set_string_copy(char_u *str, typval_T *tv)
5043{
5044 tv->vval.v_string = vim_strsave(str);
5045 if (tv->vval.v_string == NULL)
5046 {
5047 PyErr_NoMemory();
5048 return -1;
5049 }
5050 return 0;
5051}
5052
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005053 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005054pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005055{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005056 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005057 char_u *key;
5058 dictitem_T *di;
5059 PyObject *keyObject;
5060 PyObject *valObject;
5061 Py_ssize_t iter = 0;
5062
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005063 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005064 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005065
5066 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005067 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005068
5069 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5070 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005071 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005072
Bram Moolenaara03e6312013-05-29 22:49:26 +02005073 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005074 {
5075 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005076 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005077 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005078
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005079 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005080 {
5081 dict_unref(dict);
5082 return -1;
5083 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005084
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005085 if (*key == NUL)
5086 {
5087 dict_unref(dict);
5088 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005089 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005090 return -1;
5091 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005092
5093 di = dictitem_alloc(key);
5094
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005095 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005096
5097 if (di == NULL)
5098 {
5099 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005100 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101 return -1;
5102 }
5103 di->di_tv.v_lock = 0;
5104
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005105 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005106 {
5107 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005108 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005109 return -1;
5110 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005111
5112 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005113 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005114 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005115 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005117 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005118 return -1;
5119 }
5120 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005121
5122 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005123 return 0;
5124}
5125
5126 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005127pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005128{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005129 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005130 char_u *key;
5131 dictitem_T *di;
5132 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005133 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005134 PyObject *keyObject;
5135 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005136
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005137 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005138 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005139
5140 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005141 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005142
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005143 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005144 {
5145 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005146 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005147 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005148
5149 if (!(iterator = PyObject_GetIter(list)))
5150 {
5151 dict_unref(dict);
5152 Py_DECREF(list);
5153 return -1;
5154 }
5155 Py_DECREF(list);
5156
5157 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005158 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005159 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005160
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005161 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005162 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005163 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005164 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005165 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005166 return -1;
5167 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005168
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005169 if (*key == NUL)
5170 {
5171 Py_DECREF(keyObject);
5172 Py_DECREF(iterator);
5173 Py_XDECREF(todecref);
5174 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005175 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005176 return -1;
5177 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005178
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005179 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005180 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005181 Py_DECREF(keyObject);
5182 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005183 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005184 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005185 return -1;
5186 }
5187
5188 di = dictitem_alloc(key);
5189
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005190 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005191 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005192
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005193 if (di == NULL)
5194 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005195 Py_DECREF(iterator);
5196 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005197 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005198 PyErr_NoMemory();
5199 return -1;
5200 }
5201 di->di_tv.v_lock = 0;
5202
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005203 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005204 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005205 Py_DECREF(iterator);
5206 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005207 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005208 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005209 return -1;
5210 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005211
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005212 Py_DECREF(valObject);
5213
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005214 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005215 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005216 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005217 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005218 dictitem_free(di);
5219 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005220 return -1;
5221 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005222 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005223 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005224 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005225 return 0;
5226}
5227
5228 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005229pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005230{
5231 list_T *l;
5232
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005233 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005234 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005235
5236 tv->v_type = VAR_LIST;
5237 tv->vval.v_list = l;
5238
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005239 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005240 {
5241 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005242 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005243 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005244
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005245 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005246 return 0;
5247}
5248
Bram Moolenaardb913952012-06-29 12:54:53 +02005249typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5250
5251 static int
5252convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005253 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005254{
5255 PyObject *capsule;
5256 char hexBuf[sizeof(void *) * 2 + 3];
5257
5258 sprintf(hexBuf, "%p", obj);
5259
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005260# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005261 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005262# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005263 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005264# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005265 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005266 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005267# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005268 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005269# else
5270 capsule = PyCObject_FromVoidPtr(tv, NULL);
5271# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005272 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5273 {
5274 Py_DECREF(capsule);
5275 tv->v_type = VAR_UNKNOWN;
5276 return -1;
5277 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005278 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005279 {
5280 tv->v_type = VAR_UNKNOWN;
5281 return -1;
5282 }
5283 /* As we are not using copy_tv which increments reference count we must
5284 * do it ourself. */
5285 switch(tv->v_type)
5286 {
5287 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5288 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5289 }
5290 }
5291 else
5292 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005293 typval_T *v;
5294
5295# ifdef PY_USE_CAPSULE
5296 v = PyCapsule_GetPointer(capsule, NULL);
5297# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005298 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005299# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005300 copy_tv(v, tv);
5301 }
5302 return 0;
5303}
5304
5305 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005306ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5307{
5308 PyObject *lookup_dict;
5309 int r;
5310
5311 if (!(lookup_dict = PyDict_New()))
5312 return -1;
5313
5314 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5315 {
5316 tv->v_type = VAR_DICT;
5317 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5318 ++tv->vval.v_dict->dv_refcount;
5319 r = 0;
5320 }
5321 else if (PyDict_Check(obj))
5322 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
5323 else if (PyMapping_Check(obj))
5324 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5325 else
5326 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005327 PyErr_FORMAT(PyExc_TypeError,
5328 "unable to convert %s to vim dictionary",
5329 Py_TYPE_NAME(obj));
Bram Moolenaara9922d62013-05-30 13:01:18 +02005330 r = -1;
5331 }
5332 Py_DECREF(lookup_dict);
5333 return r;
5334}
5335
5336 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005337ConvertFromPyObject(PyObject *obj, typval_T *tv)
5338{
5339 PyObject *lookup_dict;
5340 int r;
5341
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005342 if (!(lookup_dict = PyDict_New()))
5343 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005344 r = _ConvertFromPyObject(obj, tv, lookup_dict);
5345 Py_DECREF(lookup_dict);
5346 return r;
5347}
5348
5349 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005350_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005351{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005352 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005353 {
5354 tv->v_type = VAR_DICT;
5355 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5356 ++tv->vval.v_dict->dv_refcount;
5357 }
5358 else if (obj->ob_type == &ListType)
5359 {
5360 tv->v_type = VAR_LIST;
5361 tv->vval.v_list = (((ListObject *)(obj))->list);
5362 ++tv->vval.v_list->lv_refcount;
5363 }
5364 else if (obj->ob_type == &FunctionType)
5365 {
5366 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5367 return -1;
5368
5369 tv->v_type = VAR_FUNC;
5370 func_ref(tv->vval.v_string);
5371 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005372 else if (PyBytes_Check(obj))
5373 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005374 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02005375
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005376 if (PyBytes_AsStringAndSize(obj, (char **) &result, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005377 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005378 if (result == NULL)
5379 return -1;
5380
5381 if (set_string_copy(result, tv) == -1)
5382 return -1;
5383
5384 tv->v_type = VAR_STRING;
5385 }
5386 else if (PyUnicode_Check(obj))
5387 {
5388 PyObject *bytes;
5389 char_u *result;
5390
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005391 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005392 if (bytes == NULL)
5393 return -1;
5394
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005395 if(PyBytes_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005396 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005397 if (result == NULL)
5398 return -1;
5399
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005400 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005401 {
5402 Py_XDECREF(bytes);
5403 return -1;
5404 }
5405 Py_XDECREF(bytes);
5406
5407 tv->v_type = VAR_STRING;
5408 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005409#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005410 else if (PyInt_Check(obj))
5411 {
5412 tv->v_type = VAR_NUMBER;
5413 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005414 if (PyErr_Occurred())
5415 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005416 }
5417#endif
5418 else if (PyLong_Check(obj))
5419 {
5420 tv->v_type = VAR_NUMBER;
5421 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005422 if (PyErr_Occurred())
5423 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005424 }
5425 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005426 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005427#ifdef FEAT_FLOAT
5428 else if (PyFloat_Check(obj))
5429 {
5430 tv->v_type = VAR_FLOAT;
5431 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5432 }
5433#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005434 else if (PyObject_HasAttrString(obj, "keys"))
5435 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005436 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005437 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005438 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005439 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005440 else if (PyNumber_Check(obj))
5441 {
5442 PyObject *num;
5443
5444 if (!(num = PyNumber_Long(obj)))
5445 return -1;
5446
5447 tv->v_type = VAR_NUMBER;
5448 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5449
5450 Py_DECREF(num);
5451 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005452 else
5453 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005454 PyErr_FORMAT(PyExc_TypeError,
5455 "unable to convert %s to vim structure",
5456 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005457 return -1;
5458 }
5459 return 0;
5460}
5461
5462 static PyObject *
5463ConvertToPyObject(typval_T *tv)
5464{
5465 if (tv == NULL)
5466 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005467 PyErr_SET_VIM("internal error: NULL reference passed");
Bram Moolenaardb913952012-06-29 12:54:53 +02005468 return NULL;
5469 }
5470 switch (tv->v_type)
5471 {
5472 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005473 return PyBytes_FromString(tv->vval.v_string == NULL
5474 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005475 case VAR_NUMBER:
5476 return PyLong_FromLong((long) tv->vval.v_number);
5477#ifdef FEAT_FLOAT
5478 case VAR_FLOAT:
5479 return PyFloat_FromDouble((double) tv->vval.v_float);
5480#endif
5481 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005482 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005483 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005484 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005485 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005486 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005487 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005488 case VAR_UNKNOWN:
5489 Py_INCREF(Py_None);
5490 return Py_None;
5491 default:
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005492 PyErr_SET_VIM("internal error: invalid value type");
Bram Moolenaardb913952012-06-29 12:54:53 +02005493 return NULL;
5494 }
5495}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005496
5497typedef struct
5498{
5499 PyObject_HEAD
5500} CurrentObject;
5501static PyTypeObject CurrentType;
5502
5503 static void
5504init_structs(void)
5505{
5506 vim_memset(&OutputType, 0, sizeof(OutputType));
5507 OutputType.tp_name = "vim.message";
5508 OutputType.tp_basicsize = sizeof(OutputObject);
5509 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5510 OutputType.tp_doc = "vim message object";
5511 OutputType.tp_methods = OutputMethods;
5512#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005513 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5514 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005515 OutputType.tp_alloc = call_PyType_GenericAlloc;
5516 OutputType.tp_new = call_PyType_GenericNew;
5517 OutputType.tp_free = call_PyObject_Free;
5518#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005519 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5520 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005521#endif
5522
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005523 vim_memset(&IterType, 0, sizeof(IterType));
5524 IterType.tp_name = "vim.iter";
5525 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005526 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005527 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005528 IterType.tp_iter = (getiterfunc)IterIter;
5529 IterType.tp_iternext = (iternextfunc)IterNext;
5530 IterType.tp_dealloc = (destructor)IterDestructor;
5531 IterType.tp_traverse = (traverseproc)IterTraverse;
5532 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005533
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005534 vim_memset(&BufferType, 0, sizeof(BufferType));
5535 BufferType.tp_name = "vim.buffer";
5536 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005537 BufferType.tp_dealloc = (destructor)BufferDestructor;
5538 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005539 BufferType.tp_as_sequence = &BufferAsSeq;
5540 BufferType.tp_as_mapping = &BufferAsMapping;
5541 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5542 BufferType.tp_doc = "vim buffer object";
5543 BufferType.tp_methods = BufferMethods;
5544#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005545 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005546 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005547 BufferType.tp_alloc = call_PyType_GenericAlloc;
5548 BufferType.tp_new = call_PyType_GenericNew;
5549 BufferType.tp_free = call_PyObject_Free;
5550#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005551 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005552 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005553#endif
5554
5555 vim_memset(&WindowType, 0, sizeof(WindowType));
5556 WindowType.tp_name = "vim.window";
5557 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005558 WindowType.tp_dealloc = (destructor)WindowDestructor;
5559 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005560 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005561 WindowType.tp_doc = "vim Window object";
5562 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005563 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5564 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005565#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005566 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5567 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005568 WindowType.tp_alloc = call_PyType_GenericAlloc;
5569 WindowType.tp_new = call_PyType_GenericNew;
5570 WindowType.tp_free = call_PyObject_Free;
5571#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005572 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5573 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005574#endif
5575
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005576 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5577 TabPageType.tp_name = "vim.tabpage";
5578 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005579 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5580 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005581 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5582 TabPageType.tp_doc = "vim tab page object";
5583 TabPageType.tp_methods = TabPageMethods;
5584#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005585 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005586 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5587 TabPageType.tp_new = call_PyType_GenericNew;
5588 TabPageType.tp_free = call_PyObject_Free;
5589#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005590 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005591#endif
5592
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005593 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5594 BufMapType.tp_name = "vim.bufferlist";
5595 BufMapType.tp_basicsize = sizeof(BufMapObject);
5596 BufMapType.tp_as_mapping = &BufMapAsMapping;
5597 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005598 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005599 BufferType.tp_doc = "vim buffer list";
5600
5601 vim_memset(&WinListType, 0, sizeof(WinListType));
5602 WinListType.tp_name = "vim.windowlist";
5603 WinListType.tp_basicsize = sizeof(WinListType);
5604 WinListType.tp_as_sequence = &WinListAsSeq;
5605 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5606 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005607 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005608
5609 vim_memset(&TabListType, 0, sizeof(TabListType));
5610 TabListType.tp_name = "vim.tabpagelist";
5611 TabListType.tp_basicsize = sizeof(TabListType);
5612 TabListType.tp_as_sequence = &TabListAsSeq;
5613 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5614 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005615
5616 vim_memset(&RangeType, 0, sizeof(RangeType));
5617 RangeType.tp_name = "vim.range";
5618 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005619 RangeType.tp_dealloc = (destructor)RangeDestructor;
5620 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005621 RangeType.tp_as_sequence = &RangeAsSeq;
5622 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005623 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005624 RangeType.tp_doc = "vim Range object";
5625 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005626 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5627 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005628#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005629 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005630 RangeType.tp_alloc = call_PyType_GenericAlloc;
5631 RangeType.tp_new = call_PyType_GenericNew;
5632 RangeType.tp_free = call_PyObject_Free;
5633#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005634 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005635#endif
5636
5637 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5638 CurrentType.tp_name = "vim.currentdata";
5639 CurrentType.tp_basicsize = sizeof(CurrentObject);
5640 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5641 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005642 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005643#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005644 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5645 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005646#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005647 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5648 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005649#endif
5650
5651 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5652 DictionaryType.tp_name = "vim.dictionary";
5653 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005654 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005655 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005656 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005657 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005658 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5659 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005660 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5661 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5662 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005663#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005664 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5665 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005666#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005667 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5668 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005669#endif
5670
5671 vim_memset(&ListType, 0, sizeof(ListType));
5672 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005673 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005674 ListType.tp_basicsize = sizeof(ListObject);
5675 ListType.tp_as_sequence = &ListAsSeq;
5676 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005677 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005678 ListType.tp_doc = "list pushing modifications to vim structure";
5679 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005680 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005681 ListType.tp_new = (newfunc)ListConstructor;
5682 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005683#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005684 ListType.tp_getattro = (getattrofunc)ListGetattro;
5685 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005686#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005687 ListType.tp_getattr = (getattrfunc)ListGetattr;
5688 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005689#endif
5690
5691 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005692 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005693 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005694 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5695 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005696 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005697 FunctionType.tp_doc = "object that calls vim function";
5698 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005699 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005700 FunctionType.tp_new = (newfunc)FunctionConstructor;
5701 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005702#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005703 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005704#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005705 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005706#endif
5707
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005708 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5709 OptionsType.tp_name = "vim.options";
5710 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005711 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005712 OptionsType.tp_doc = "object for manipulating options";
5713 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005714 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5715 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5716 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005717
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005718 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5719 LoaderType.tp_name = "vim.Loader";
5720 LoaderType.tp_basicsize = sizeof(LoaderObject);
5721 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5722 LoaderType.tp_doc = "vim message object";
5723 LoaderType.tp_methods = LoaderMethods;
5724 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5725
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005726#if PY_MAJOR_VERSION >= 3
5727 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5728 vimmodule.m_name = "vim";
5729 vimmodule.m_doc = "Vim Python interface\n";
5730 vimmodule.m_size = -1;
5731 vimmodule.m_methods = VimMethods;
5732#endif
5733}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005734
5735#define PYTYPE_READY(type) \
5736 if (PyType_Ready(&type)) \
5737 return -1;
5738
5739 static int
5740init_types()
5741{
5742 PYTYPE_READY(IterType);
5743 PYTYPE_READY(BufferType);
5744 PYTYPE_READY(RangeType);
5745 PYTYPE_READY(WindowType);
5746 PYTYPE_READY(TabPageType);
5747 PYTYPE_READY(BufMapType);
5748 PYTYPE_READY(WinListType);
5749 PYTYPE_READY(TabListType);
5750 PYTYPE_READY(CurrentType);
5751 PYTYPE_READY(DictionaryType);
5752 PYTYPE_READY(ListType);
5753 PYTYPE_READY(FunctionType);
5754 PYTYPE_READY(OptionsType);
5755 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005756 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005757 return 0;
5758}
5759
5760 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005761init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005762{
5763 PyObject *path;
5764 PyObject *path_hook;
5765 PyObject *path_hooks;
5766
5767 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5768 return -1;
5769
5770 if (!(path_hooks = PySys_GetObject("path_hooks")))
5771 {
5772 PyErr_Clear();
5773 path_hooks = PyList_New(1);
5774 PyList_SET_ITEM(path_hooks, 0, path_hook);
5775 if (PySys_SetObject("path_hooks", path_hooks))
5776 {
5777 Py_DECREF(path_hooks);
5778 return -1;
5779 }
5780 Py_DECREF(path_hooks);
5781 }
5782 else if (PyList_Check(path_hooks))
5783 {
5784 if (PyList_Append(path_hooks, path_hook))
5785 {
5786 Py_DECREF(path_hook);
5787 return -1;
5788 }
5789 Py_DECREF(path_hook);
5790 }
5791 else
5792 {
5793 VimTryStart();
5794 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5795 "You should now do the following:\n"
5796 "- append vim.path_hook to sys.path_hooks\n"
5797 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5798 VimTryEnd(); /* Discard the error */
5799 Py_DECREF(path_hook);
5800 return 0;
5801 }
5802
5803 if (!(path = PySys_GetObject("path")))
5804 {
5805 PyErr_Clear();
5806 path = PyList_New(1);
5807 Py_INCREF(vim_special_path_object);
5808 PyList_SET_ITEM(path, 0, vim_special_path_object);
5809 if (PySys_SetObject("path", path))
5810 {
5811 Py_DECREF(path);
5812 return -1;
5813 }
5814 Py_DECREF(path);
5815 }
5816 else if (PyList_Check(path))
5817 {
5818 if (PyList_Append(path, vim_special_path_object))
5819 return -1;
5820 }
5821 else
5822 {
5823 VimTryStart();
5824 EMSG(_("Failed to set path: sys.path is not a list\n"
5825 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5826 VimTryEnd(); /* Discard the error */
5827 }
5828
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005829 return 0;
5830}
5831
5832static BufMapObject TheBufferMap =
5833{
5834 PyObject_HEAD_INIT(&BufMapType)
5835};
5836
5837static WinListObject TheWindowList =
5838{
5839 PyObject_HEAD_INIT(&WinListType)
5840 NULL
5841};
5842
5843static CurrentObject TheCurrent =
5844{
5845 PyObject_HEAD_INIT(&CurrentType)
5846};
5847
5848static TabListObject TheTabPageList =
5849{
5850 PyObject_HEAD_INIT(&TabListType)
5851};
5852
5853static struct numeric_constant {
5854 char *name;
5855 int value;
5856} numeric_constants[] = {
5857 {"VAR_LOCKED", VAR_LOCKED},
5858 {"VAR_FIXED", VAR_FIXED},
5859 {"VAR_SCOPE", VAR_SCOPE},
5860 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5861};
5862
5863static struct object_constant {
5864 char *name;
5865 PyObject *value;
5866} object_constants[] = {
5867 {"buffers", (PyObject *)(void *)&TheBufferMap},
5868 {"windows", (PyObject *)(void *)&TheWindowList},
5869 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5870 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005871
5872 {"Buffer", (PyObject *)&BufferType},
5873 {"Range", (PyObject *)&RangeType},
5874 {"Window", (PyObject *)&WindowType},
5875 {"TabPage", (PyObject *)&TabPageType},
5876 {"Dictionary", (PyObject *)&DictionaryType},
5877 {"List", (PyObject *)&ListType},
5878 {"Function", (PyObject *)&FunctionType},
5879 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005880 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005881};
5882
5883typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005884typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005885
5886#define ADD_OBJECT(m, name, obj) \
5887 if (add_object(m, name, obj)) \
5888 return -1;
5889
5890#define ADD_CHECKED_OBJECT(m, name, obj) \
5891 { \
5892 PyObject *value = obj; \
5893 if (!value) \
5894 return -1; \
5895 ADD_OBJECT(m, name, value); \
5896 }
5897
5898 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005899populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005900{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005901 int i;
5902 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005903 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005904 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005905
5906 for (i = 0; i < (int)(sizeof(numeric_constants)
5907 / sizeof(struct numeric_constant));
5908 ++i)
5909 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5910 PyInt_FromLong(numeric_constants[i].value));
5911
5912 for (i = 0; i < (int)(sizeof(object_constants)
5913 / sizeof(struct object_constant));
5914 ++i)
5915 {
5916 PyObject *value;
5917
5918 value = object_constants[i].value;
5919 Py_INCREF(value);
5920 ADD_OBJECT(m, object_constants[i].name, value);
5921 }
5922
5923 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5924 return -1;
5925 ADD_OBJECT(m, "error", VimError);
5926
Bram Moolenaara9922d62013-05-30 13:01:18 +02005927 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5928 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005929 ADD_CHECKED_OBJECT(m, "options",
5930 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005931
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005932 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005933 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005934 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005935
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005936 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005937 return -1;
5938 ADD_OBJECT(m, "_getcwd", py_getcwd)
5939
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005940 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005941 return -1;
5942 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005943 if (!(attr = get_attr(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005944 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005945 if (PyObject_SetAttrString(other_module, "chdir", attr))
5946 {
5947 Py_DECREF(attr);
5948 return -1;
5949 }
5950 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005951
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005952 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005953 {
5954 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005955 if (!(attr = get_attr(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005956 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005957 if (PyObject_SetAttrString(other_module, "fchdir", attr))
5958 {
5959 Py_DECREF(attr);
5960 return -1;
5961 }
5962 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005963 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02005964 else
5965 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02005966
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005967 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
5968 return -1;
5969
5970 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
5971
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005972 if (!(imp = PyImport_ImportModule("imp")))
5973 return -1;
5974
5975 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
5976 {
5977 Py_DECREF(imp);
5978 return -1;
5979 }
5980
5981 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
5982 {
5983 Py_DECREF(py_find_module);
5984 Py_DECREF(imp);
5985 return -1;
5986 }
5987
5988 Py_DECREF(imp);
5989
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005990 ADD_OBJECT(m, "_find_module", py_find_module);
5991 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005992
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005993 return 0;
5994}