blob: 71c30af41f1339eaa620d0d6aa826520a752df67 [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 Moolenaar3d0c52d2013-05-12 19:45:35 +020029#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
30
Bram Moolenaar35eacd72013-05-30 22:06:33 +020031#define RAISE_NO_EMPTY_KEYS PyErr_SetString(PyExc_ValueError, \
32 _("empty keys are not allowed"))
33
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020034#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
35#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020036#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020038typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020039typedef void (*runner)(const char *, void *
40#ifdef PY_CAN_RECURSE
41 , PyGILState_STATE *
42#endif
43 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020044
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020045static int ConvertFromPyObject(PyObject *, typval_T *);
46static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020047static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020048static PyObject *WindowNew(win_T *, tabpage_T *);
49static PyObject *BufferNew (buf_T *);
50static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020051
52static PyInt RangeStart;
53static PyInt RangeEnd;
54
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020055static PyObject *globals;
56
Bram Moolenaarf4258302013-06-02 18:20:17 +020057static PyObject *py_chdir;
58static PyObject *py_fchdir;
59static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020060static PyObject *vim_module;
61static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020062
Bram Moolenaar81c40c52013-06-12 14:41:04 +020063static PyObject *py_find_module;
64static PyObject *py_load_module;
65
66static PyObject *VimError;
67
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020068/*
69 * obtain a lock on the Vim data structures
70 */
71 static void
72Python_Lock_Vim(void)
73{
74}
75
76/*
77 * release a lock on the Vim data structures
78 */
79 static void
80Python_Release_Vim(void)
81{
82}
83
Bram Moolenaare9ba5162013-05-29 22:02:22 +020084/*
85 * The "todecref" argument holds a pointer to PyObject * that must be
86 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
87 * was needed to generate returned value is object.
88 *
89 * Use Py_XDECREF to decrement reference count.
90 */
91 static char_u *
92StringToChars(PyObject *object, PyObject **todecref)
93{
94 char_u *p;
Bram Moolenaare9ba5162013-05-29 22:02:22 +020095
96 if (PyBytes_Check(object))
97 {
98
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020099 if (PyBytes_AsStringAndSize(object, (char **) &p, NULL) == -1
100 || p == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200101 return NULL;
102
103 *todecref = NULL;
104 }
105 else if (PyUnicode_Check(object))
106 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200107 PyObject *bytes;
108
109 if (!(bytes = PyUnicode_AsEncodedString(object, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200110 return NULL;
111
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200112 if(PyBytes_AsStringAndSize(bytes, (char **) &p, NULL) == -1
113 || p == NULL)
114 {
115 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200116 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200117 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200118
119 *todecref = bytes;
120 }
121 else
122 {
123 PyErr_SetString(PyExc_TypeError, _("object must be string"));
124 return NULL;
125 }
126
127 return (char_u *) p;
128}
129
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200130 static int
131add_string(PyObject *list, char *s)
132{
133 PyObject *string;
134
135 if (!(string = PyString_FromString(s)))
136 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200137
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200138 if (PyList_Append(list, string))
139 {
140 Py_DECREF(string);
141 return -1;
142 }
143
144 Py_DECREF(string);
145 return 0;
146}
147
148 static PyObject *
149ObjectDir(PyObject *self, char **attributes)
150{
151 PyMethodDef *method;
152 char **attr;
153 PyObject *r;
154
155 if (!(r = PyList_New(0)))
156 return NULL;
157
158 if (self)
159 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
160 if (add_string(r, (char *) method->ml_name))
161 {
162 Py_DECREF(r);
163 return NULL;
164 }
165
166 for (attr = attributes ; *attr ; ++attr)
167 if (add_string(r, *attr))
168 {
169 Py_DECREF(r);
170 return NULL;
171 }
172
173#if PY_MAJOR_VERSION < 3
174 if (add_string(r, "__members__"))
175 {
176 Py_DECREF(r);
177 return NULL;
178 }
179#endif
180
181 return r;
182}
183
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200184/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200185 */
186
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200187/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200188typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200189
190static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200191
192typedef struct
193{
194 PyObject_HEAD
195 long softspace;
196 long error;
197} OutputObject;
198
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200199static char *OutputAttrs[] = {
200 "softspace",
201 NULL
202};
203
204 static PyObject *
205OutputDir(PyObject *self)
206{
207 return ObjectDir(self, OutputAttrs);
208}
209
Bram Moolenaar77045652012-09-21 13:46:06 +0200210 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200211OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200212{
213 if (val == NULL)
214 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200215 PyErr_SetString(PyExc_AttributeError,
216 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200217 return -1;
218 }
219
220 if (strcmp(name, "softspace") == 0)
221 {
222 if (!PyInt_Check(val))
223 {
224 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
225 return -1;
226 }
227
Bram Moolenaard6e39182013-05-21 18:30:34 +0200228 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200229 return 0;
230 }
231
232 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
233 return -1;
234}
235
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200236/* Buffer IO, we write one whole line at a time. */
237static garray_T io_ga = {0, 0, 1, 80, NULL};
238static writefn old_fn = NULL;
239
240 static void
241PythonIO_Flush(void)
242{
243 if (old_fn != NULL && io_ga.ga_len > 0)
244 {
245 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
246 old_fn((char_u *)io_ga.ga_data);
247 }
248 io_ga.ga_len = 0;
249}
250
251 static void
252writer(writefn fn, char_u *str, PyInt n)
253{
254 char_u *ptr;
255
256 /* Flush when switching output function. */
257 if (fn != old_fn)
258 PythonIO_Flush();
259 old_fn = fn;
260
261 /* Write each NL separated line. Text after the last NL is kept for
262 * writing later. */
263 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
264 {
265 PyInt len = ptr - str;
266
267 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
268 break;
269
270 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
271 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
272 fn((char_u *)io_ga.ga_data);
273 str = ptr + 1;
274 n -= len + 1;
275 io_ga.ga_len = 0;
276 }
277
278 /* Put the remaining text into io_ga for later printing. */
279 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
280 {
281 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
282 io_ga.ga_len += (int)n;
283 }
284}
285
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200286 static int
287write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200288{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200289 Py_ssize_t len = 0;
290 char *str = NULL;
291 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200292
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200293 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
294 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200295
296 Py_BEGIN_ALLOW_THREADS
297 Python_Lock_Vim();
298 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
299 Python_Release_Vim();
300 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200301 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200302
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200303 return 0;
304}
305
306 static PyObject *
307OutputWrite(OutputObject *self, PyObject *string)
308{
309 if (write_output(self, string))
310 return NULL;
311
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200312 Py_INCREF(Py_None);
313 return Py_None;
314}
315
316 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200317OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200318{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200319 PyObject *iterator;
320 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200321
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200322 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200323 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200324
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200325 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200326 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200327 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200328 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200329 Py_DECREF(iterator);
330 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200331 return NULL;
332 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200333 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200334 }
335
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200336 Py_DECREF(iterator);
337
338 /* Iterator may have finished due to an exception */
339 if (PyErr_Occurred())
340 return NULL;
341
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200342 Py_INCREF(Py_None);
343 return Py_None;
344}
345
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100346 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200347OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100348{
349 /* do nothing */
350 Py_INCREF(Py_None);
351 return Py_None;
352}
353
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200354/***************/
355
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200356static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200357 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200358 {"write", (PyCFunction)OutputWrite, METH_O, ""},
359 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200360 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200361 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200362 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200363};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200364
365static OutputObject Output =
366{
367 PyObject_HEAD_INIT(&OutputType)
368 0,
369 0
370};
371
372static OutputObject Error =
373{
374 PyObject_HEAD_INIT(&OutputType)
375 0,
376 1
377};
378
379 static int
380PythonIO_Init_io(void)
381{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200382 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
383 return -1;
384 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
385 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200386
387 if (PyErr_Occurred())
388 {
389 EMSG(_("E264: Python: Error initialising I/O objects"));
390 return -1;
391 }
392
393 return 0;
394}
395
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200396typedef struct
397{
398 PyObject_HEAD
399 PyObject *module;
400} LoaderObject;
401static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200402
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200403 static void
404LoaderDestructor(LoaderObject *self)
405{
406 Py_DECREF(self->module);
407 DESTRUCTOR_FINISH(self);
408}
409
410 static PyObject *
411LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
412{
413 PyObject *r = self->module;
414
415 Py_INCREF(r);
416 return r;
417}
418
419static struct PyMethodDef LoaderMethods[] = {
420 /* name, function, calling, doc */
421 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
422 { NULL, NULL, 0, NULL}
423};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200424
425/* Check to see whether a Vim error has been reported, or a keyboard
426 * interrupt has been detected.
427 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200428
429 static void
430VimTryStart(void)
431{
432 ++trylevel;
433}
434
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200435 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200436VimTryEnd(void)
437{
438 --trylevel;
439 if (got_int)
440 {
441 PyErr_SetNone(PyExc_KeyboardInterrupt);
442 return 1;
443 }
444 else if (!did_throw)
445 return 0;
446 else if (PyErr_Occurred())
447 return 1;
448 else
449 {
450 PyErr_SetVim((char *) current_exception->value);
451 discard_current_exception();
452 return 1;
453 }
454}
455
456 static int
457VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200458{
459 if (got_int)
460 {
461 PyErr_SetNone(PyExc_KeyboardInterrupt);
462 return 1;
463 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200464 return 0;
465}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200466
467/* Vim module - Implementation
468 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200469
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200470 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200471VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200472{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200473 char_u *cmd;
474 PyObject *result;
475 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200476
Bram Moolenaar389a1792013-06-23 13:00:44 +0200477 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200478 return NULL;
479
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200480 Py_BEGIN_ALLOW_THREADS
481 Python_Lock_Vim();
482
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200483 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200484 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200485 update_screen(VALID);
486
487 Python_Release_Vim();
488 Py_END_ALLOW_THREADS
489
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200490 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200491 result = NULL;
492 else
493 result = Py_None;
494
495 Py_XINCREF(result);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200496 Py_XDECREF(todecref);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200497 return result;
498}
499
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200500/*
501 * Function to translate a typval_T into a PyObject; this will recursively
502 * translate lists/dictionaries into their Python equivalents.
503 *
504 * The depth parameter is to avoid infinite recursion, set it to 1 when
505 * you call VimToPython.
506 */
507 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200508VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200509{
510 PyObject *result;
511 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200512 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200513
514 /* Avoid infinite recursion */
515 if (depth > 100)
516 {
517 Py_INCREF(Py_None);
518 result = Py_None;
519 return result;
520 }
521
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200522 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200523 * then and we can use it again. */
524 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
525 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
526 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200527 sprintf(ptrBuf, "%p",
528 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
529 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200530
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200531 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200532 {
533 Py_INCREF(result);
534 return result;
535 }
536 }
537
538 if (our_tv->v_type == VAR_STRING)
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200539 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200540 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200541 else if (our_tv->v_type == VAR_NUMBER)
542 {
543 char buf[NUMBUFLEN];
544
545 /* For backwards compatibility numbers are stored as strings. */
546 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200547 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200548 }
549# ifdef FEAT_FLOAT
550 else if (our_tv->v_type == VAR_FLOAT)
551 {
552 char buf[NUMBUFLEN];
553
554 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200555 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200556 }
557# endif
558 else if (our_tv->v_type == VAR_LIST)
559 {
560 list_T *list = our_tv->vval.v_list;
561 listitem_T *curr;
562
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200563 if (list == NULL)
564 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200565
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200566 if (!(result = PyList_New(0)))
567 return NULL;
568
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200569 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200570 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200571 Py_DECREF(result);
572 return NULL;
573 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200574
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200575 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
576 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200577 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200578 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200579 Py_DECREF(result);
580 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200581 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200582 if (PyList_Append(result, newObj))
583 {
584 Py_DECREF(newObj);
585 Py_DECREF(result);
586 return NULL;
587 }
588 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200589 }
590 }
591 else if (our_tv->v_type == VAR_DICT)
592 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200593
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200594 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
595 long_u todo = ht->ht_used;
596 hashitem_T *hi;
597 dictitem_T *di;
598 if (our_tv->vval.v_dict == NULL)
599 return NULL;
600
601 if (!(result = PyDict_New()))
602 return NULL;
603
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200604 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200605 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200606 Py_DECREF(result);
607 return NULL;
608 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200609
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200610 for (hi = ht->ht_array; todo > 0; ++hi)
611 {
612 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200614 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200615
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200616 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200617 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200618 {
619 Py_DECREF(result);
620 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200621 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200622 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
623 {
624 Py_DECREF(result);
625 Py_DECREF(newObj);
626 return NULL;
627 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628 }
629 }
630 }
631 else
632 {
633 Py_INCREF(Py_None);
634 result = Py_None;
635 }
636
637 return result;
638}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200639
640 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200641VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200642{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200643 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200644 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200645 PyObject *string;
646 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200647 PyObject *result;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200648 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200649
Bram Moolenaar389a1792013-06-23 13:00:44 +0200650 if (!PyArg_ParseTuple(args, "O", &string))
651 return NULL;
652
653 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200654 return NULL;
655
656 Py_BEGIN_ALLOW_THREADS
657 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200658 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200659 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200660 Python_Release_Vim();
661 Py_END_ALLOW_THREADS
662
Bram Moolenaar389a1792013-06-23 13:00:44 +0200663 Py_XDECREF(todecref);
664
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200665 if (VimTryEnd())
666 return NULL;
667
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200668 if (our_tv == NULL)
669 {
670 PyErr_SetVim(_("invalid expression"));
671 return NULL;
672 }
673
674 /* Convert the Vim type into a Python type. Create a dictionary that's
675 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200676 if (!(lookup_dict = PyDict_New()))
677 result = NULL;
678 else
679 {
680 result = VimToPython(our_tv, 1, lookup_dict);
681 Py_DECREF(lookup_dict);
682 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200683
684
685 Py_BEGIN_ALLOW_THREADS
686 Python_Lock_Vim();
687 free_tv(our_tv);
688 Python_Release_Vim();
689 Py_END_ALLOW_THREADS
690
691 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200692}
693
Bram Moolenaardb913952012-06-29 12:54:53 +0200694static PyObject *ConvertToPyObject(typval_T *);
695
696 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200697VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200698{
Bram Moolenaardb913952012-06-29 12:54:53 +0200699 typval_T *our_tv;
700 PyObject *result;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200701 char_u *expr;
702 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200703
Bram Moolenaar389a1792013-06-23 13:00:44 +0200704 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200705 return NULL;
706
707 Py_BEGIN_ALLOW_THREADS
708 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200709 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200710 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200711 Python_Release_Vim();
712 Py_END_ALLOW_THREADS
713
Bram Moolenaar389a1792013-06-23 13:00:44 +0200714 Py_XDECREF(todecref);
715
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200716 if (VimTryEnd())
717 return NULL;
718
Bram Moolenaardb913952012-06-29 12:54:53 +0200719 if (our_tv == NULL)
720 {
721 PyErr_SetVim(_("invalid expression"));
722 return NULL;
723 }
724
725 result = ConvertToPyObject(our_tv);
726 Py_BEGIN_ALLOW_THREADS
727 Python_Lock_Vim();
728 free_tv(our_tv);
729 Python_Release_Vim();
730 Py_END_ALLOW_THREADS
731
732 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200733}
734
735 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200736VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200737{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200738 char_u *str;
739 PyObject *todecref;
740 int result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200741
Bram Moolenaar389a1792013-06-23 13:00:44 +0200742 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200743 return NULL;
744
Bram Moolenaara54bf402012-12-05 16:30:07 +0100745#ifdef FEAT_MBYTE
Bram Moolenaar389a1792013-06-23 13:00:44 +0200746 result = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100747#else
Bram Moolenaar389a1792013-06-23 13:00:44 +0200748 result = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100749#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200750
751 Py_XDECREF(todecref);
752
753 return PyLong_FromLong(result);
Bram Moolenaardb913952012-06-29 12:54:53 +0200754}
755
Bram Moolenaarf4258302013-06-02 18:20:17 +0200756 static PyObject *
757_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
758{
759 PyObject *r;
760 PyObject *newwd;
761 PyObject *todecref;
762 char_u *new_dir;
763
Bram Moolenaard4209d22013-06-05 20:34:15 +0200764 if (_chdir == NULL)
765 return NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200766 if (!(r = PyObject_Call(_chdir, args, kwargs)))
767 return NULL;
768
769 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
770 {
771 Py_DECREF(r);
772 return NULL;
773 }
774
775 if (!(new_dir = StringToChars(newwd, &todecref)))
776 {
777 Py_DECREF(r);
778 Py_DECREF(newwd);
779 return NULL;
780 }
781
782 VimTryStart();
783
784 if (vim_chdir(new_dir))
785 {
786 Py_DECREF(r);
787 Py_DECREF(newwd);
788 Py_XDECREF(todecref);
789
790 if (VimTryEnd())
791 return NULL;
792
793 PyErr_SetVim(_("failed to change directory"));
794 return NULL;
795 }
796
797 Py_DECREF(newwd);
798 Py_XDECREF(todecref);
799
800 post_chdir(FALSE);
801
802 if (VimTryEnd())
803 {
804 Py_DECREF(r);
805 return NULL;
806 }
807
808 return r;
809}
810
811 static PyObject *
812VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
813{
814 return _VimChdir(py_chdir, args, kwargs);
815}
816
817 static PyObject *
818VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
819{
820 return _VimChdir(py_fchdir, args, kwargs);
821}
822
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200823typedef struct {
824 PyObject *callable;
825 PyObject *result;
826} map_rtp_data;
827
828 static void
829map_rtp_callback(char_u *path, void *_data)
830{
831 void **data = (void **) _data;
832 PyObject *pathObject;
833 map_rtp_data *mr_data = *((map_rtp_data **) data);
834
835 if (!(pathObject = PyString_FromString((char *) path)))
836 {
837 *data = NULL;
838 return;
839 }
840
841 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
842 pathObject, NULL);
843
844 Py_DECREF(pathObject);
845
846 if (!mr_data->result || mr_data->result != Py_None)
847 *data = NULL;
848 else
849 {
850 Py_DECREF(mr_data->result);
851 mr_data->result = NULL;
852 }
853}
854
855 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200856VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200857{
858 map_rtp_data data;
859
Bram Moolenaar389a1792013-06-23 13:00:44 +0200860 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200861 data.result = NULL;
862
863 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
864
865 if (data.result == NULL)
866 {
867 if (PyErr_Occurred())
868 return NULL;
869 else
870 {
871 Py_INCREF(Py_None);
872 return Py_None;
873 }
874 }
875 return data.result;
876}
877
878/*
879 * _vim_runtimepath_ special path implementation.
880 */
881
882 static void
883map_finder_callback(char_u *path, void *_data)
884{
885 void **data = (void **) _data;
886 PyObject *list = *((PyObject **) data);
887 PyObject *pathObject1, *pathObject2;
888 char *pathbuf;
889 size_t pathlen;
890
891 pathlen = STRLEN(path);
892
893#if PY_MAJOR_VERSION < 3
894# define PY_MAIN_DIR_STRING "python2"
895#else
896# define PY_MAIN_DIR_STRING "python3"
897#endif
898#define PY_ALTERNATE_DIR_STRING "pythonx"
899
900#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
901 if (!(pathbuf = PyMem_New(char,
902 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
903 {
904 PyErr_NoMemory();
905 *data = NULL;
906 return;
907 }
908
909 mch_memmove(pathbuf, path, pathlen + 1);
910 add_pathsep((char_u *) pathbuf);
911
912 pathlen = STRLEN(pathbuf);
913 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
914 PYTHONX_STRING_LENGTH + 1);
915
916 if (!(pathObject1 = PyString_FromString(pathbuf)))
917 {
918 *data = NULL;
919 PyMem_Free(pathbuf);
920 return;
921 }
922
923 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
924 PYTHONX_STRING_LENGTH + 1);
925
926 if (!(pathObject2 = PyString_FromString(pathbuf)))
927 {
928 Py_DECREF(pathObject1);
929 PyMem_Free(pathbuf);
930 *data = NULL;
931 return;
932 }
933
934 PyMem_Free(pathbuf);
935
936 if (PyList_Append(list, pathObject1)
937 || PyList_Append(list, pathObject2))
938 *data = NULL;
939
940 Py_DECREF(pathObject1);
941 Py_DECREF(pathObject2);
942}
943
944 static PyObject *
945Vim_GetPaths(PyObject *self UNUSED)
946{
947 PyObject *r;
948
949 if (!(r = PyList_New(0)))
950 return NULL;
951
952 do_in_runtimepath(NULL, FALSE, &map_finder_callback, r);
953
954 if (PyErr_Occurred())
955 {
956 Py_DECREF(r);
957 return NULL;
958 }
959
960 return r;
961}
962
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200963 static PyObject *
964call_load_module(char *name, int len, PyObject *find_module_result)
965{
966 PyObject *fd, *pathname, *description;
967
968 if (!PyTuple_Check(find_module_result)
969 || PyTuple_GET_SIZE(find_module_result) != 3)
970 {
971 PyErr_SetString(PyExc_TypeError,
972 _("expected 3-tuple as imp.find_module() result"));
973 return NULL;
974 }
975
976 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
977 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
978 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
979 {
980 PyErr_SetString(PyExc_RuntimeError,
981 _("internal error: imp.find_module returned tuple with NULL"));
982 return NULL;
983 }
984
985 return PyObject_CallFunction(py_load_module,
986 "s#OOO", name, len, fd, pathname, description);
987}
988
989 static PyObject *
990find_module(char *fullname, char *tail, PyObject *new_path)
991{
992 PyObject *find_module_result;
993 PyObject *module;
994 char *dot;
995
996 if ((dot = (char *) vim_strchr((char_u *) tail, '.')))
997 {
998 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +0200999 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001000 * first component
1001 */
1002 PyObject *newest_path;
1003 int partlen = (int) (dot - 1 - tail);
1004
1005 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1006 "s#O", tail, partlen, new_path)))
1007 return NULL;
1008
1009 if (!(module = call_load_module(
1010 fullname,
1011 ((int) (tail - fullname)) + partlen,
1012 find_module_result)))
1013 {
1014 Py_DECREF(find_module_result);
1015 return NULL;
1016 }
1017
1018 Py_DECREF(find_module_result);
1019
1020 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1021 {
1022 Py_DECREF(module);
1023 return NULL;
1024 }
1025
1026 Py_DECREF(module);
1027
1028 module = find_module(fullname, dot + 1, newest_path);
1029
1030 Py_DECREF(newest_path);
1031
1032 return module;
1033 }
1034 else
1035 {
1036 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1037 "sO", tail, new_path)))
1038 return NULL;
1039
1040 if (!(module = call_load_module(
1041 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001042 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001043 find_module_result)))
1044 {
1045 Py_DECREF(find_module_result);
1046 return NULL;
1047 }
1048
1049 Py_DECREF(find_module_result);
1050
1051 return module;
1052 }
1053}
1054
1055 static PyObject *
1056FinderFindModule(PyObject *self, PyObject *args)
1057{
1058 char *fullname;
1059 PyObject *module;
1060 PyObject *new_path;
1061 LoaderObject *loader;
1062
1063 if (!PyArg_ParseTuple(args, "s", &fullname))
1064 return NULL;
1065
1066 if (!(new_path = Vim_GetPaths(self)))
1067 return NULL;
1068
1069 module = find_module(fullname, fullname, new_path);
1070
1071 Py_DECREF(new_path);
1072
1073 if (!module)
1074 {
1075 Py_INCREF(Py_None);
1076 return Py_None;
1077 }
1078
1079 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1080 {
1081 Py_DECREF(module);
1082 return NULL;
1083 }
1084
1085 loader->module = module;
1086
1087 return (PyObject *) loader;
1088}
1089
1090 static PyObject *
1091VimPathHook(PyObject *self UNUSED, PyObject *args)
1092{
1093 char *path;
1094
1095 if (PyArg_ParseTuple(args, "s", &path)
1096 && STRCMP(path, vim_special_path) == 0)
1097 {
1098 Py_INCREF(vim_module);
1099 return vim_module;
1100 }
1101
1102 PyErr_Clear();
1103 PyErr_SetNone(PyExc_ImportError);
1104 return NULL;
1105}
1106
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001107/*
1108 * Vim module - Definitions
1109 */
1110
1111static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001112 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001113 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001114 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001115 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1116 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001117 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1118 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001119 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001120 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001121 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1122 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1123 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001124};
1125
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001126/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001127 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001128 */
1129
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001130static PyTypeObject IterType;
1131
1132typedef PyObject *(*nextfun)(void **);
1133typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001134typedef int (*traversefun)(void *, visitproc, void *);
1135typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001136
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001137/* Main purpose of this object is removing the need for do python
1138 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1139 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001140
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001141typedef struct
1142{
1143 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001144 void *cur;
1145 nextfun next;
1146 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001147 traversefun traverse;
1148 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001149} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001150
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001151 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001152IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1153 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001154{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001155 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001156
Bram Moolenaar774267b2013-05-21 20:51:59 +02001157 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001158 self->cur = start;
1159 self->next = next;
1160 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001161 self->traverse = traverse;
1162 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001163
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001164 return (PyObject *)(self);
1165}
1166
1167 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001168IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001169{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001170 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001171 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001172 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001173}
1174
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001175 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001176IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001177{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001178 if (self->traverse != NULL)
1179 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001180 else
1181 return 0;
1182}
1183
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001184/* Mac OSX defines clear() somewhere. */
1185#ifdef clear
1186# undef clear
1187#endif
1188
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001189 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001190IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001191{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001192 if (self->clear != NULL)
1193 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001194 else
1195 return 0;
1196}
1197
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001198 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001199IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001200{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001201 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001202}
1203
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001204 static PyObject *
1205IterIter(PyObject *self)
1206{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001207 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001208 return self;
1209}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001210
Bram Moolenaardb913952012-06-29 12:54:53 +02001211typedef struct pylinkedlist_S {
1212 struct pylinkedlist_S *pll_next;
1213 struct pylinkedlist_S *pll_prev;
1214 PyObject *pll_obj;
1215} pylinkedlist_T;
1216
1217static pylinkedlist_T *lastdict = NULL;
1218static pylinkedlist_T *lastlist = NULL;
1219
1220 static void
1221pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1222{
1223 if (ref->pll_prev == NULL)
1224 {
1225 if (ref->pll_next == NULL)
1226 {
1227 *last = NULL;
1228 return;
1229 }
1230 }
1231 else
1232 ref->pll_prev->pll_next = ref->pll_next;
1233
1234 if (ref->pll_next == NULL)
1235 *last = ref->pll_prev;
1236 else
1237 ref->pll_next->pll_prev = ref->pll_prev;
1238}
1239
1240 static void
1241pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1242{
1243 if (*last == NULL)
1244 ref->pll_prev = NULL;
1245 else
1246 {
1247 (*last)->pll_next = ref;
1248 ref->pll_prev = *last;
1249 }
1250 ref->pll_next = NULL;
1251 ref->pll_obj = self;
1252 *last = ref;
1253}
1254
1255static PyTypeObject DictionaryType;
1256
1257typedef struct
1258{
1259 PyObject_HEAD
1260 dict_T *dict;
1261 pylinkedlist_T ref;
1262} DictionaryObject;
1263
Bram Moolenaara9922d62013-05-30 13:01:18 +02001264static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1265
1266#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1267
Bram Moolenaardb913952012-06-29 12:54:53 +02001268 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001269DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001270{
1271 DictionaryObject *self;
1272
Bram Moolenaara9922d62013-05-30 13:01:18 +02001273 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001274 if (self == NULL)
1275 return NULL;
1276 self->dict = dict;
1277 ++dict->dv_refcount;
1278
1279 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1280
1281 return (PyObject *)(self);
1282}
1283
Bram Moolenaara9922d62013-05-30 13:01:18 +02001284 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001285py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001286{
1287 dict_T *r;
1288
1289 if (!(r = dict_alloc()))
1290 {
1291 PyErr_NoMemory();
1292 return NULL;
1293 }
1294 ++r->dv_refcount;
1295
1296 return r;
1297}
1298
1299 static PyObject *
1300DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1301{
1302 DictionaryObject *self;
1303 dict_T *dict;
1304
1305 if (!(dict = py_dict_alloc()))
1306 return NULL;
1307
1308 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1309
1310 --dict->dv_refcount;
1311
1312 if (kwargs || PyTuple_Size(args))
1313 {
1314 PyObject *tmp;
1315 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1316 {
1317 Py_DECREF(self);
1318 return NULL;
1319 }
1320
1321 Py_DECREF(tmp);
1322 }
1323
1324 return (PyObject *)(self);
1325}
1326
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001327 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001328DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001329{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001330 pyll_remove(&self->ref, &lastdict);
1331 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001332
1333 DESTRUCTOR_FINISH(self);
1334}
1335
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001336static char *DictionaryAttrs[] = {
1337 "locked", "scope",
1338 NULL
1339};
1340
1341 static PyObject *
1342DictionaryDir(PyObject *self)
1343{
1344 return ObjectDir(self, DictionaryAttrs);
1345}
1346
Bram Moolenaardb913952012-06-29 12:54:53 +02001347 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001348DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001349{
1350 if (val == NULL)
1351 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001352 PyErr_SetString(PyExc_AttributeError,
1353 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001354 return -1;
1355 }
1356
1357 if (strcmp(name, "locked") == 0)
1358 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001359 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001360 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001361 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001362 return -1;
1363 }
1364 else
1365 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001366 int istrue = PyObject_IsTrue(val);
1367 if (istrue == -1)
1368 return -1;
1369 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001370 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001371 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001372 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001373 }
1374 return 0;
1375 }
1376 else
1377 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001378 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001379 return -1;
1380 }
1381}
1382
1383 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001384DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001385{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001386 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001387}
1388
Bram Moolenaara9922d62013-05-30 13:01:18 +02001389#define DICT_FLAG_HAS_DEFAULT 0x01
1390#define DICT_FLAG_POP 0x02
1391#define DICT_FLAG_NONE_DEFAULT 0x04
1392#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1393#define DICT_FLAG_RETURN_PAIR 0x10
1394
Bram Moolenaardb913952012-06-29 12:54:53 +02001395 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001396_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001397{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001398 PyObject *keyObject;
1399 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1400 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001401 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001402 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001403 dict_T *dict = self->dict;
1404 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001405 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001406
Bram Moolenaara9922d62013-05-30 13:01:18 +02001407 if (flags & DICT_FLAG_HAS_DEFAULT)
1408 {
1409 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1410 return NULL;
1411 }
1412 else
1413 keyObject = args;
1414
1415 if (flags & DICT_FLAG_RETURN_BOOL)
1416 defObject = Py_False;
1417
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001418 if (!(key = StringToChars(keyObject, &todecref)))
1419 return NULL;
1420
1421 if (*key == NUL)
1422 {
1423 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001424 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001425 return NULL;
1426 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001427
Bram Moolenaara9922d62013-05-30 13:01:18 +02001428 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001429
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001430 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001431
Bram Moolenaara9922d62013-05-30 13:01:18 +02001432 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001433 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001434 if (defObject)
1435 {
1436 Py_INCREF(defObject);
1437 return defObject;
1438 }
1439 else
1440 {
1441 PyErr_SetObject(PyExc_KeyError, keyObject);
1442 return NULL;
1443 }
1444 }
1445 else if (flags & DICT_FLAG_RETURN_BOOL)
1446 {
1447 Py_INCREF(Py_True);
1448 return Py_True;
1449 }
1450
1451 di = dict_lookup(hi);
1452
1453 if (!(r = ConvertToPyObject(&di->di_tv)))
1454 return NULL;
1455
1456 if (flags & DICT_FLAG_POP)
1457 {
1458 if (dict->dv_lock)
1459 {
1460 PyErr_SetVim(_("dict is locked"));
1461 Py_DECREF(r);
1462 return NULL;
1463 }
1464
1465 hash_remove(&dict->dv_hashtab, hi);
1466 dictitem_free(di);
1467 }
1468
Bram Moolenaara9922d62013-05-30 13:01:18 +02001469 return r;
1470}
1471
1472 static PyObject *
1473DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1474{
1475 return _DictionaryItem(self, keyObject, 0);
1476}
1477
1478 static int
1479DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1480{
1481 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1482 int r;
1483
1484 r = (rObj == Py_True);
1485
1486 Py_DECREF(Py_True);
1487
1488 return r;
1489}
1490
1491typedef struct
1492{
1493 hashitem_T *ht_array;
1494 long_u ht_used;
1495 hashtab_T *ht;
1496 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001497 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001498} dictiterinfo_T;
1499
1500 static PyObject *
1501DictionaryIterNext(dictiterinfo_T **dii)
1502{
1503 PyObject *r;
1504
1505 if (!(*dii)->todo)
1506 return NULL;
1507
1508 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1509 (*dii)->ht->ht_used != (*dii)->ht_used)
1510 {
1511 PyErr_SetString(PyExc_RuntimeError,
1512 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001513 return NULL;
1514 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001515
Bram Moolenaara9922d62013-05-30 13:01:18 +02001516 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1517 ++((*dii)->hi);
1518
1519 --((*dii)->todo);
1520
1521 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1522 return NULL;
1523
1524 return r;
1525}
1526
1527 static PyObject *
1528DictionaryIter(DictionaryObject *self)
1529{
1530 dictiterinfo_T *dii;
1531 hashtab_T *ht;
1532
1533 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1534 {
1535 PyErr_NoMemory();
1536 return NULL;
1537 }
1538
1539 ht = &self->dict->dv_hashtab;
1540 dii->ht_array = ht->ht_array;
1541 dii->ht_used = ht->ht_used;
1542 dii->ht = ht;
1543 dii->hi = dii->ht_array;
1544 dii->todo = dii->ht_used;
1545
1546 return IterNew(dii,
1547 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1548 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001549}
1550
1551 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001552DictionaryAssItem(
1553 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001554{
1555 char_u *key;
1556 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001557 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001558 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001559 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001560
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001561 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001562 {
1563 PyErr_SetVim(_("dict is locked"));
1564 return -1;
1565 }
1566
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001567 if (!(key = StringToChars(keyObject, &todecref)))
1568 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001569
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001570 if (*key == NUL)
1571 {
1572 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001573 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001574 return -1;
1575 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001576
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001577 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001578
1579 if (valObject == NULL)
1580 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001581 hashitem_T *hi;
1582
Bram Moolenaardb913952012-06-29 12:54:53 +02001583 if (di == NULL)
1584 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001585 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001586 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001587 return -1;
1588 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001589 hi = hash_find(&dict->dv_hashtab, di->di_key);
1590 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001591 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001592 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001593 return 0;
1594 }
1595
1596 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001597 {
1598 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001599 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001600 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001601
1602 if (di == NULL)
1603 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001604 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001605 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001606 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001607 PyErr_NoMemory();
1608 return -1;
1609 }
1610 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001611 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001612
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001613 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001614 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001615 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001616 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001617 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001618 PyErr_SetVim(_("failed to add key to dictionary"));
1619 return -1;
1620 }
1621 }
1622 else
1623 clear_tv(&di->di_tv);
1624
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001625 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001626
1627 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001628 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001629 return 0;
1630}
1631
Bram Moolenaara9922d62013-05-30 13:01:18 +02001632typedef PyObject *(*hi_to_py)(hashitem_T *);
1633
Bram Moolenaardb913952012-06-29 12:54:53 +02001634 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001635DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001636{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001637 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001638 long_u todo = dict->dv_hashtab.ht_used;
1639 Py_ssize_t i = 0;
1640 PyObject *r;
1641 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001642 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001643
1644 r = PyList_New(todo);
1645 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1646 {
1647 if (!HASHITEM_EMPTY(hi))
1648 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001649 if (!(newObj = hiconvert(hi)))
1650 {
1651 Py_DECREF(r);
1652 return NULL;
1653 }
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02001654 PyList_SET_ITEM(r, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001655 --todo;
1656 ++i;
1657 }
1658 }
1659 return r;
1660}
1661
Bram Moolenaara9922d62013-05-30 13:01:18 +02001662 static PyObject *
1663dict_key(hashitem_T *hi)
1664{
1665 return PyBytes_FromString((char *)(hi->hi_key));
1666}
1667
1668 static PyObject *
1669DictionaryListKeys(DictionaryObject *self)
1670{
1671 return DictionaryListObjects(self, dict_key);
1672}
1673
1674 static PyObject *
1675dict_val(hashitem_T *hi)
1676{
1677 dictitem_T *di;
1678
1679 di = dict_lookup(hi);
1680 return ConvertToPyObject(&di->di_tv);
1681}
1682
1683 static PyObject *
1684DictionaryListValues(DictionaryObject *self)
1685{
1686 return DictionaryListObjects(self, dict_val);
1687}
1688
1689 static PyObject *
1690dict_item(hashitem_T *hi)
1691{
1692 PyObject *keyObject;
1693 PyObject *valObject;
1694 PyObject *r;
1695
1696 if (!(keyObject = dict_key(hi)))
1697 return NULL;
1698
1699 if (!(valObject = dict_val(hi)))
1700 {
1701 Py_DECREF(keyObject);
1702 return NULL;
1703 }
1704
1705 r = Py_BuildValue("(OO)", keyObject, valObject);
1706
1707 Py_DECREF(keyObject);
1708 Py_DECREF(valObject);
1709
1710 return r;
1711}
1712
1713 static PyObject *
1714DictionaryListItems(DictionaryObject *self)
1715{
1716 return DictionaryListObjects(self, dict_item);
1717}
1718
1719 static PyObject *
1720DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1721{
1722 dict_T *dict = self->dict;
1723
1724 if (dict->dv_lock)
1725 {
1726 PyErr_SetVim(_("dict is locked"));
1727 return NULL;
1728 }
1729
1730 if (kwargs)
1731 {
1732 typval_T tv;
1733
1734 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1735 return NULL;
1736
1737 VimTryStart();
1738 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1739 clear_tv(&tv);
1740 if (VimTryEnd())
1741 return NULL;
1742 }
1743 else
1744 {
1745 PyObject *object;
1746
Bram Moolenaar389a1792013-06-23 13:00:44 +02001747 if (!PyArg_ParseTuple(args, "O", &object))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001748 return NULL;
1749
1750 if (PyObject_HasAttrString(object, "keys"))
1751 return DictionaryUpdate(self, NULL, object);
1752 else
1753 {
1754 PyObject *iterator;
1755 PyObject *item;
1756
1757 if (!(iterator = PyObject_GetIter(object)))
1758 return NULL;
1759
1760 while ((item = PyIter_Next(iterator)))
1761 {
1762 PyObject *fast;
1763 PyObject *keyObject;
1764 PyObject *valObject;
1765 PyObject *todecref;
1766 char_u *key;
1767 dictitem_T *di;
1768
1769 if (!(fast = PySequence_Fast(item, "")))
1770 {
1771 Py_DECREF(iterator);
1772 Py_DECREF(item);
1773 return NULL;
1774 }
1775
1776 Py_DECREF(item);
1777
1778 if (PySequence_Fast_GET_SIZE(fast) != 2)
1779 {
1780 Py_DECREF(iterator);
1781 Py_DECREF(fast);
1782 PyErr_SetString(PyExc_ValueError,
1783 _("expected sequence element of size 2"));
1784 return NULL;
1785 }
1786
1787 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1788
1789 if (!(key = StringToChars(keyObject, &todecref)))
1790 {
1791 Py_DECREF(iterator);
1792 Py_DECREF(fast);
1793 return NULL;
1794 }
1795
1796 di = dictitem_alloc(key);
1797
1798 Py_XDECREF(todecref);
1799
1800 if (di == NULL)
1801 {
1802 Py_DECREF(fast);
1803 Py_DECREF(iterator);
1804 PyErr_NoMemory();
1805 return NULL;
1806 }
1807 di->di_tv.v_lock = 0;
1808 di->di_tv.v_type = VAR_UNKNOWN;
1809
1810 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1811
1812 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1813 {
1814 Py_DECREF(iterator);
1815 Py_DECREF(fast);
1816 dictitem_free(di);
1817 return NULL;
1818 }
1819
1820 Py_DECREF(fast);
1821
1822 if (dict_add(dict, di) == FAIL)
1823 {
1824 Py_DECREF(iterator);
1825 dictitem_free(di);
1826 PyErr_SetVim(_("failed to add key to dictionary"));
1827 return NULL;
1828 }
1829 }
1830
1831 Py_DECREF(iterator);
1832
1833 /* Iterator may have finished due to an exception */
1834 if (PyErr_Occurred())
1835 return NULL;
1836 }
1837 }
1838 Py_INCREF(Py_None);
1839 return Py_None;
1840}
1841
1842 static PyObject *
1843DictionaryGet(DictionaryObject *self, PyObject *args)
1844{
1845 return _DictionaryItem(self, args,
1846 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1847}
1848
1849 static PyObject *
1850DictionaryPop(DictionaryObject *self, PyObject *args)
1851{
1852 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1853}
1854
1855 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001856DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001857{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001858 hashitem_T *hi;
1859 PyObject *r;
1860 PyObject *valObject;
1861 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001862
Bram Moolenaarde71b562013-06-02 17:41:54 +02001863 if (self->dict->dv_hashtab.ht_used == 0)
1864 {
1865 PyErr_SetNone(PyExc_KeyError);
1866 return NULL;
1867 }
1868
1869 hi = self->dict->dv_hashtab.ht_array;
1870 while (HASHITEM_EMPTY(hi))
1871 ++hi;
1872
1873 di = dict_lookup(hi);
1874
1875 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001876 return NULL;
1877
Bram Moolenaarde71b562013-06-02 17:41:54 +02001878 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
1879 {
1880 Py_DECREF(valObject);
1881 return NULL;
1882 }
1883
1884 hash_remove(&self->dict->dv_hashtab, hi);
1885 dictitem_free(di);
1886
1887 return r;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001888}
1889
1890 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001891DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001892{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001893 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1894}
1895
1896static PySequenceMethods DictionaryAsSeq = {
1897 0, /* sq_length */
1898 0, /* sq_concat */
1899 0, /* sq_repeat */
1900 0, /* sq_item */
1901 0, /* sq_slice */
1902 0, /* sq_ass_item */
1903 0, /* sq_ass_slice */
1904 (objobjproc) DictionaryContains, /* sq_contains */
1905 0, /* sq_inplace_concat */
1906 0, /* sq_inplace_repeat */
1907};
1908
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001909static PyMappingMethods DictionaryAsMapping = {
1910 (lenfunc) DictionaryLength,
1911 (binaryfunc) DictionaryItem,
1912 (objobjargproc) DictionaryAssItem,
1913};
1914
Bram Moolenaardb913952012-06-29 12:54:53 +02001915static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001916 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001917 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1918 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1919 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1920 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1921 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02001922 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001923 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001924 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1925 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001926};
1927
1928static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001929static PySequenceMethods ListAsSeq;
1930static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001931
1932typedef struct
1933{
1934 PyObject_HEAD
1935 list_T *list;
1936 pylinkedlist_T ref;
1937} ListObject;
1938
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001939#define NEW_LIST(list) ListNew(&ListType, list)
1940
Bram Moolenaardb913952012-06-29 12:54:53 +02001941 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001942ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001943{
1944 ListObject *self;
1945
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001946 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001947 if (self == NULL)
1948 return NULL;
1949 self->list = list;
1950 ++list->lv_refcount;
1951
1952 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1953
1954 return (PyObject *)(self);
1955}
1956
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001957 static list_T *
1958py_list_alloc()
1959{
1960 list_T *r;
1961
1962 if (!(r = list_alloc()))
1963 {
1964 PyErr_NoMemory();
1965 return NULL;
1966 }
1967 ++r->lv_refcount;
1968
1969 return r;
1970}
1971
1972 static int
1973list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1974{
1975 PyObject *iterator;
1976 PyObject *item;
1977 listitem_T *li;
1978
1979 if (!(iterator = PyObject_GetIter(obj)))
1980 return -1;
1981
1982 while ((item = PyIter_Next(iterator)))
1983 {
1984 if (!(li = listitem_alloc()))
1985 {
1986 PyErr_NoMemory();
1987 Py_DECREF(item);
1988 Py_DECREF(iterator);
1989 return -1;
1990 }
1991 li->li_tv.v_lock = 0;
1992 li->li_tv.v_type = VAR_UNKNOWN;
1993
1994 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1995 {
1996 Py_DECREF(item);
1997 Py_DECREF(iterator);
1998 listitem_free(li);
1999 return -1;
2000 }
2001
2002 Py_DECREF(item);
2003
2004 list_append(l, li);
2005 }
2006
2007 Py_DECREF(iterator);
2008
2009 /* Iterator may have finished due to an exception */
2010 if (PyErr_Occurred())
2011 return -1;
2012
2013 return 0;
2014}
2015
2016 static PyObject *
2017ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2018{
2019 list_T *list;
2020 PyObject *obj = NULL;
2021
2022 if (kwargs)
2023 {
2024 PyErr_SetString(PyExc_TypeError,
2025 _("list constructor does not accept keyword arguments"));
2026 return NULL;
2027 }
2028
2029 if (!PyArg_ParseTuple(args, "|O", &obj))
2030 return NULL;
2031
2032 if (!(list = py_list_alloc()))
2033 return NULL;
2034
2035 if (obj)
2036 {
2037 PyObject *lookup_dict;
2038
2039 if (!(lookup_dict = PyDict_New()))
2040 {
2041 list_unref(list);
2042 return NULL;
2043 }
2044
2045 if (list_py_concat(list, obj, lookup_dict) == -1)
2046 {
2047 Py_DECREF(lookup_dict);
2048 list_unref(list);
2049 return NULL;
2050 }
2051
2052 Py_DECREF(lookup_dict);
2053 }
2054
2055 return ListNew(subtype, list);
2056}
2057
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002058 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002059ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002060{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002061 pyll_remove(&self->ref, &lastlist);
2062 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002063
2064 DESTRUCTOR_FINISH(self);
2065}
2066
Bram Moolenaardb913952012-06-29 12:54:53 +02002067 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002068ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002069{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002070 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002071}
2072
2073 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002074ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002075{
2076 listitem_T *li;
2077
Bram Moolenaard6e39182013-05-21 18:30:34 +02002078 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002079 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002080 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002081 return NULL;
2082 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002083 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002084 if (li == NULL)
2085 {
2086 PyErr_SetVim(_("internal error: failed to get vim list item"));
2087 return NULL;
2088 }
2089 return ConvertToPyObject(&li->li_tv);
2090}
2091
2092#define PROC_RANGE \
2093 if (last < 0) {\
2094 if (last < -size) \
2095 last = 0; \
2096 else \
2097 last += size; \
2098 } \
2099 if (first < 0) \
2100 first = 0; \
2101 if (first > size) \
2102 first = size; \
2103 if (last > size) \
2104 last = size;
2105
2106 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002107ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002108{
2109 PyInt i;
2110 PyInt size = ListLength(self);
2111 PyInt n;
2112 PyObject *list;
2113 int reversed = 0;
2114
2115 PROC_RANGE
2116 if (first >= last)
2117 first = last;
2118
2119 n = last-first;
2120 list = PyList_New(n);
2121 if (list == NULL)
2122 return NULL;
2123
2124 for (i = 0; i < n; ++i)
2125 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002126 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002127 if (item == NULL)
2128 {
2129 Py_DECREF(list);
2130 return NULL;
2131 }
2132
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002133 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002134 }
2135
2136 return list;
2137}
2138
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002139typedef struct
2140{
2141 listwatch_T lw;
2142 list_T *list;
2143} listiterinfo_T;
2144
2145 static void
2146ListIterDestruct(listiterinfo_T *lii)
2147{
2148 list_rem_watch(lii->list, &lii->lw);
2149 PyMem_Free(lii);
2150}
2151
2152 static PyObject *
2153ListIterNext(listiterinfo_T **lii)
2154{
2155 PyObject *r;
2156
2157 if (!((*lii)->lw.lw_item))
2158 return NULL;
2159
2160 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
2161 return NULL;
2162
2163 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2164
2165 return r;
2166}
2167
2168 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002169ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002170{
2171 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002172 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002173
2174 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2175 {
2176 PyErr_NoMemory();
2177 return NULL;
2178 }
2179
2180 list_add_watch(l, &lii->lw);
2181 lii->lw.lw_item = l->lv_first;
2182 lii->list = l;
2183
2184 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002185 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2186 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002187}
2188
Bram Moolenaardb913952012-06-29 12:54:53 +02002189 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002190ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002191{
2192 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002193 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002194 listitem_T *li;
2195 Py_ssize_t length = ListLength(self);
2196
2197 if (l->lv_lock)
2198 {
2199 PyErr_SetVim(_("list is locked"));
2200 return -1;
2201 }
2202 if (index>length || (index==length && obj==NULL))
2203 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002204 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002205 return -1;
2206 }
2207
2208 if (obj == NULL)
2209 {
2210 li = list_find(l, (long) index);
2211 list_remove(l, li, li);
2212 clear_tv(&li->li_tv);
2213 vim_free(li);
2214 return 0;
2215 }
2216
2217 if (ConvertFromPyObject(obj, &tv) == -1)
2218 return -1;
2219
2220 if (index == length)
2221 {
2222 if (list_append_tv(l, &tv) == FAIL)
2223 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002224 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002225 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002226 return -1;
2227 }
2228 }
2229 else
2230 {
2231 li = list_find(l, (long) index);
2232 clear_tv(&li->li_tv);
2233 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002234 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002235 }
2236 return 0;
2237}
2238
2239 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002240ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002241{
2242 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002243 PyObject *iterator;
2244 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002245 listitem_T *li;
2246 listitem_T *next;
2247 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002248 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002249 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002250
2251 if (l->lv_lock)
2252 {
2253 PyErr_SetVim(_("list is locked"));
2254 return -1;
2255 }
2256
2257 PROC_RANGE
2258
2259 if (first == size)
2260 li = NULL;
2261 else
2262 {
2263 li = list_find(l, (long) first);
2264 if (li == NULL)
2265 {
2266 PyErr_SetVim(_("internal error: no vim list item"));
2267 return -1;
2268 }
2269 if (last > first)
2270 {
2271 i = last - first;
2272 while (i-- && li != NULL)
2273 {
2274 next = li->li_next;
2275 listitem_remove(l, li);
2276 li = next;
2277 }
2278 }
2279 }
2280
2281 if (obj == NULL)
2282 return 0;
2283
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002284 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002285 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002286
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002287 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002288 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002289 if (ConvertFromPyObject(item, &v) == -1)
2290 {
2291 Py_DECREF(iterator);
2292 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002293 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002294 }
2295 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002296 if (list_insert_tv(l, &v, li) == FAIL)
2297 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002298 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002299 PyErr_SetVim(_("internal error: failed to add item to list"));
2300 return -1;
2301 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002302 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002303 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002304 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02002305 return 0;
2306}
2307
2308 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002309ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002310{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002311 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002312 PyObject *lookup_dict;
2313
2314 if (l->lv_lock)
2315 {
2316 PyErr_SetVim(_("list is locked"));
2317 return NULL;
2318 }
2319
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002320 if (!(lookup_dict = PyDict_New()))
2321 return NULL;
2322
Bram Moolenaardb913952012-06-29 12:54:53 +02002323 if (list_py_concat(l, obj, lookup_dict) == -1)
2324 {
2325 Py_DECREF(lookup_dict);
2326 return NULL;
2327 }
2328 Py_DECREF(lookup_dict);
2329
2330 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002331 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002332}
2333
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002334static char *ListAttrs[] = {
2335 "locked",
2336 NULL
2337};
2338
2339 static PyObject *
2340ListDir(PyObject *self)
2341{
2342 return ObjectDir(self, ListAttrs);
2343}
2344
Bram Moolenaar66b79852012-09-21 14:00:35 +02002345 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002346ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002347{
2348 if (val == NULL)
2349 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002350 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002351 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002352 return -1;
2353 }
2354
2355 if (strcmp(name, "locked") == 0)
2356 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002357 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002358 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002359 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002360 return -1;
2361 }
2362 else
2363 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002364 int istrue = PyObject_IsTrue(val);
2365 if (istrue == -1)
2366 return -1;
2367 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002368 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002369 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002370 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002371 }
2372 return 0;
2373 }
2374 else
2375 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002376 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002377 return -1;
2378 }
2379}
2380
Bram Moolenaardb913952012-06-29 12:54:53 +02002381static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002382 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2383 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2384 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002385};
2386
2387typedef struct
2388{
2389 PyObject_HEAD
2390 char_u *name;
2391} FunctionObject;
2392
2393static PyTypeObject FunctionType;
2394
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002395#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2396
Bram Moolenaardb913952012-06-29 12:54:53 +02002397 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002398FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002399{
2400 FunctionObject *self;
2401
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002402 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2403
Bram Moolenaardb913952012-06-29 12:54:53 +02002404 if (self == NULL)
2405 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002406
2407 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002408 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002409 if (!translated_function_exists(name))
2410 {
2411 PyErr_SetString(PyExc_ValueError,
2412 _("unnamed function does not exist"));
2413 return NULL;
2414 }
2415 self->name = vim_strsave(name);
2416 func_ref(self->name);
2417 }
2418 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002419 if ((self->name = get_expanded_name(name,
2420 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2421 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002422 {
Bram Moolenaar018acca2013-05-30 13:37:28 +02002423 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2424 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002425 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002426
2427 return (PyObject *)(self);
2428}
2429
2430 static PyObject *
2431FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2432{
2433 PyObject *self;
2434 char_u *name;
2435
2436 if (kwargs)
2437 {
2438 PyErr_SetString(PyExc_TypeError,
2439 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002440 return NULL;
2441 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002442
Bram Moolenaar389a1792013-06-23 13:00:44 +02002443 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002444 return NULL;
2445
2446 self = FunctionNew(subtype, name);
2447
Bram Moolenaar389a1792013-06-23 13:00:44 +02002448 PyMem_Free(name);
2449
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002450 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002451}
2452
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002453 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002454FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002455{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002456 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002457 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002458
2459 DESTRUCTOR_FINISH(self);
2460}
2461
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002462static char *FunctionAttrs[] = {
2463 "softspace",
2464 NULL
2465};
2466
2467 static PyObject *
2468FunctionDir(PyObject *self)
2469{
2470 return ObjectDir(self, FunctionAttrs);
2471}
2472
Bram Moolenaardb913952012-06-29 12:54:53 +02002473 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002474FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002475{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002476 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002477 typval_T args;
2478 typval_T selfdicttv;
2479 typval_T rettv;
2480 dict_T *selfdict = NULL;
2481 PyObject *selfdictObject;
2482 PyObject *result;
2483 int error;
2484
2485 if (ConvertFromPyObject(argsObject, &args) == -1)
2486 return NULL;
2487
2488 if (kwargs != NULL)
2489 {
2490 selfdictObject = PyDict_GetItemString(kwargs, "self");
2491 if (selfdictObject != NULL)
2492 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002493 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002494 {
2495 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002496 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002497 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002498 selfdict = selfdicttv.vval.v_dict;
2499 }
2500 }
2501
Bram Moolenaar71700b82013-05-15 17:49:05 +02002502 Py_BEGIN_ALLOW_THREADS
2503 Python_Lock_Vim();
2504
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002505 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002506 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002507
2508 Python_Release_Vim();
2509 Py_END_ALLOW_THREADS
2510
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002511 if (VimTryEnd())
2512 result = NULL;
2513 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002514 {
2515 result = NULL;
2516 PyErr_SetVim(_("failed to run function"));
2517 }
2518 else
2519 result = ConvertToPyObject(&rettv);
2520
Bram Moolenaardb913952012-06-29 12:54:53 +02002521 clear_tv(&args);
2522 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002523 if (selfdict != NULL)
2524 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002525
2526 return result;
2527}
2528
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002529 static PyObject *
2530FunctionRepr(FunctionObject *self)
2531{
2532 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2533}
2534
Bram Moolenaardb913952012-06-29 12:54:53 +02002535static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002536 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2537 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002538};
2539
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002540/*
2541 * Options object
2542 */
2543
2544static PyTypeObject OptionsType;
2545
2546typedef int (*checkfun)(void *);
2547
2548typedef struct
2549{
2550 PyObject_HEAD
2551 int opt_type;
2552 void *from;
2553 checkfun Check;
2554 PyObject *fromObj;
2555} OptionsObject;
2556
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002557 static int
2558dummy_check(void *arg UNUSED)
2559{
2560 return 0;
2561}
2562
2563 static PyObject *
2564OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2565{
2566 OptionsObject *self;
2567
Bram Moolenaar774267b2013-05-21 20:51:59 +02002568 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002569 if (self == NULL)
2570 return NULL;
2571
2572 self->opt_type = opt_type;
2573 self->from = from;
2574 self->Check = Check;
2575 self->fromObj = fromObj;
2576 if (fromObj)
2577 Py_INCREF(fromObj);
2578
2579 return (PyObject *)(self);
2580}
2581
2582 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002583OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002584{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002585 PyObject_GC_UnTrack((void *)(self));
2586 Py_XDECREF(self->fromObj);
2587 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002588}
2589
2590 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002591OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002592{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002593 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002594 return 0;
2595}
2596
2597 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002598OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002599{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002600 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002601 return 0;
2602}
2603
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002604 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002605OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002606{
2607 char_u *key;
2608 int flags;
2609 long numval;
2610 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002611 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002612
Bram Moolenaard6e39182013-05-21 18:30:34 +02002613 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002614 return NULL;
2615
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002616 if (!(key = StringToChars(keyObject, &todecref)))
2617 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002618
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002619 if (*key == NUL)
2620 {
2621 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002622 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002623 return NULL;
2624 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002625
2626 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002627 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002628
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002629 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002630
2631 if (flags == 0)
2632 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002633 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002634 return NULL;
2635 }
2636
2637 if (flags & SOPT_UNSET)
2638 {
2639 Py_INCREF(Py_None);
2640 return Py_None;
2641 }
2642 else if (flags & SOPT_BOOL)
2643 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002644 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002645 r = numval ? Py_True : Py_False;
2646 Py_INCREF(r);
2647 return r;
2648 }
2649 else if (flags & SOPT_NUM)
2650 return PyInt_FromLong(numval);
2651 else if (flags & SOPT_STRING)
2652 {
2653 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002654 {
2655 PyObject *r = PyBytes_FromString((char *) stringval);
2656 vim_free(stringval);
2657 return r;
2658 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002659 else
2660 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002661 PyErr_SetString(PyExc_RuntimeError,
2662 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002663 return NULL;
2664 }
2665 }
2666 else
2667 {
2668 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2669 return NULL;
2670 }
2671}
2672
2673 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002674set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002675{
2676 char_u *errmsg;
2677
2678 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2679 {
2680 if (VimTryEnd())
2681 return FAIL;
2682 PyErr_SetVim((char *)errmsg);
2683 return FAIL;
2684 }
2685 return OK;
2686}
2687
2688 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002689set_option_value_for(
2690 char_u *key,
2691 int numval,
2692 char_u *stringval,
2693 int opt_flags,
2694 int opt_type,
2695 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002696{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002697 win_T *save_curwin = NULL;
2698 tabpage_T *save_curtab = NULL;
2699 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002700 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002701
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002702 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002703 switch (opt_type)
2704 {
2705 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002706 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002707 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002708 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002709 if (VimTryEnd())
2710 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002711 PyErr_SetVim("Problem while switching windows.");
2712 return -1;
2713 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002714 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaard6949742013-06-16 14:18:28 +02002715 restore_win(save_curwin, save_curtab, FALSE);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002716 if (r == FAIL)
2717 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002718 break;
2719 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002720 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002721 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002722 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002723 if (r == FAIL)
2724 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002725 break;
2726 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002727 r = set_option_value_err(key, numval, stringval, opt_flags);
2728 if (r == FAIL)
2729 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002730 break;
2731 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002732 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002733}
2734
2735 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002736OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002737{
2738 char_u *key;
2739 int flags;
2740 int opt_flags;
2741 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002742 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002743
Bram Moolenaard6e39182013-05-21 18:30:34 +02002744 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002745 return -1;
2746
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002747 if (!(key = StringToChars(keyObject, &todecref)))
2748 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002749
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002750 if (*key == NUL)
2751 {
2752 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002753 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002754 return -1;
2755 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002756
2757 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002758 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002759
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002760 if (flags == 0)
2761 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002762 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002763 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002764 return -1;
2765 }
2766
2767 if (valObject == NULL)
2768 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002769 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002770 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002771 PyErr_SetString(PyExc_ValueError,
2772 _("unable to unset global option"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002773 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002774 return -1;
2775 }
2776 else if (!(flags & SOPT_GLOBAL))
2777 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002778 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2779 "without global value"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002780 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002781 return -1;
2782 }
2783 else
2784 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002785 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002786 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002787 return 0;
2788 }
2789 }
2790
Bram Moolenaard6e39182013-05-21 18:30:34 +02002791 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002792
2793 if (flags & SOPT_BOOL)
2794 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002795 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002796
Bram Moolenaarb983f752013-05-15 16:11:50 +02002797 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002798 r = -1;
2799 else
2800 r = set_option_value_for(key, istrue, NULL,
2801 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002802 }
2803 else if (flags & SOPT_NUM)
2804 {
2805 int val;
2806
2807#if PY_MAJOR_VERSION < 3
2808 if (PyInt_Check(valObject))
2809 val = PyInt_AsLong(valObject);
2810 else
2811#endif
2812 if (PyLong_Check(valObject))
2813 val = PyLong_AsLong(valObject);
2814 else
2815 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002816 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002817 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002818 return -1;
2819 }
2820
2821 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002822 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002823 }
2824 else
2825 {
2826 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002827 PyObject *todecref;
2828
2829 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002830 r = set_option_value_for(key, 0, val, opt_flags,
2831 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002832 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002833 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002834 }
2835
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002836 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002837
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002838 return r;
2839}
2840
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002841static PyMappingMethods OptionsAsMapping = {
2842 (lenfunc) NULL,
2843 (binaryfunc) OptionsItem,
2844 (objobjargproc) OptionsAssItem,
2845};
2846
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002847/* Tabpage object
2848 */
2849
2850typedef struct
2851{
2852 PyObject_HEAD
2853 tabpage_T *tab;
2854} TabPageObject;
2855
2856static PyObject *WinListNew(TabPageObject *tabObject);
2857
2858static PyTypeObject TabPageType;
2859
2860 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002861CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002862{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002863 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002864 {
2865 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2866 return -1;
2867 }
2868
2869 return 0;
2870}
2871
2872 static PyObject *
2873TabPageNew(tabpage_T *tab)
2874{
2875 TabPageObject *self;
2876
2877 if (TAB_PYTHON_REF(tab))
2878 {
2879 self = TAB_PYTHON_REF(tab);
2880 Py_INCREF(self);
2881 }
2882 else
2883 {
2884 self = PyObject_NEW(TabPageObject, &TabPageType);
2885 if (self == NULL)
2886 return NULL;
2887 self->tab = tab;
2888 TAB_PYTHON_REF(tab) = self;
2889 }
2890
2891 return (PyObject *)(self);
2892}
2893
2894 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002895TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002896{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002897 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2898 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002899
2900 DESTRUCTOR_FINISH(self);
2901}
2902
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002903static char *TabPageAttrs[] = {
2904 "windows", "number", "vars", "window", "valid",
2905 NULL
2906};
2907
2908 static PyObject *
2909TabPageDir(PyObject *self)
2910{
2911 return ObjectDir(self, TabPageAttrs);
2912}
2913
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002914 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002915TabPageAttrValid(TabPageObject *self, char *name)
2916{
2917 PyObject *r;
2918
2919 if (strcmp(name, "valid") != 0)
2920 return NULL;
2921
2922 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2923 Py_INCREF(r);
2924 return r;
2925}
2926
2927 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002928TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002929{
2930 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002931 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002932 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002933 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002934 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002935 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002936 else if (strcmp(name, "window") == 0)
2937 {
2938 /* For current tab window.c does not bother to set or update tp_curwin
2939 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002940 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002941 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002942 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002943 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002944 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002945 else if (strcmp(name, "__members__") == 0)
2946 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002947 return NULL;
2948}
2949
2950 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002951TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002952{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002953 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002954 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002955 else
2956 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002957 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002958
2959 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002960 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2961 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002962 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002963 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002964 }
2965}
2966
2967static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002968 /* name, function, calling, documentation */
2969 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2970 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002971};
2972
2973/*
2974 * Window list object
2975 */
2976
2977static PyTypeObject TabListType;
2978static PySequenceMethods TabListAsSeq;
2979
2980typedef struct
2981{
2982 PyObject_HEAD
2983} TabListObject;
2984
2985 static PyInt
2986TabListLength(PyObject *self UNUSED)
2987{
2988 tabpage_T *tp = first_tabpage;
2989 PyInt n = 0;
2990
2991 while (tp != NULL)
2992 {
2993 ++n;
2994 tp = tp->tp_next;
2995 }
2996
2997 return n;
2998}
2999
3000 static PyObject *
3001TabListItem(PyObject *self UNUSED, PyInt n)
3002{
3003 tabpage_T *tp;
3004
3005 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3006 if (n == 0)
3007 return TabPageNew(tp);
3008
3009 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
3010 return NULL;
3011}
3012
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003013/*
3014 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003015 */
3016
3017typedef struct
3018{
3019 PyObject_HEAD
3020 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003021 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003022} WindowObject;
3023
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003024static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003025
3026 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003027CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003028{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003029 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003030 {
3031 PyErr_SetVim(_("attempt to refer to deleted window"));
3032 return -1;
3033 }
3034
3035 return 0;
3036}
3037
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003038 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003039WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003040{
3041 /* We need to handle deletion of windows underneath us.
3042 * If we add a "w_python*_ref" field to the win_T structure,
3043 * then we can get at it in win_free() in vim. We then
3044 * need to create only ONE Python object per window - if
3045 * we try to create a second, just INCREF the existing one
3046 * and return it. The (single) Python object referring to
3047 * the window is stored in "w_python*_ref".
3048 * On a win_free() we set the Python object's win_T* field
3049 * to an invalid value. We trap all uses of a window
3050 * object, and reject them if the win_T* field is invalid.
3051 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003052 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003053 * w_python_ref and w_python3_ref fields respectively.
3054 */
3055
3056 WindowObject *self;
3057
3058 if (WIN_PYTHON_REF(win))
3059 {
3060 self = WIN_PYTHON_REF(win);
3061 Py_INCREF(self);
3062 }
3063 else
3064 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003065 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003066 if (self == NULL)
3067 return NULL;
3068 self->win = win;
3069 WIN_PYTHON_REF(win) = self;
3070 }
3071
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003072 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3073
Bram Moolenaar971db462013-05-12 18:44:48 +02003074 return (PyObject *)(self);
3075}
3076
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003077 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003078WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003079{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003080 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003081 if (self->win && self->win != INVALID_WINDOW_VALUE)
3082 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003083 Py_XDECREF(((PyObject *)(self->tabObject)));
3084 PyObject_GC_Del((void *)(self));
3085}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003086
Bram Moolenaar774267b2013-05-21 20:51:59 +02003087 static int
3088WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3089{
3090 Py_VISIT(((PyObject *)(self->tabObject)));
3091 return 0;
3092}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003093
Bram Moolenaar774267b2013-05-21 20:51:59 +02003094 static int
3095WindowClear(WindowObject *self)
3096{
3097 Py_CLEAR(self->tabObject);
3098 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003099}
3100
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003101 static win_T *
3102get_firstwin(TabPageObject *tabObject)
3103{
3104 if (tabObject)
3105 {
3106 if (CheckTabPage(tabObject))
3107 return NULL;
3108 /* For current tab window.c does not bother to set or update tp_firstwin
3109 */
3110 else if (tabObject->tab == curtab)
3111 return firstwin;
3112 else
3113 return tabObject->tab->tp_firstwin;
3114 }
3115 else
3116 return firstwin;
3117}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003118static char *WindowAttrs[] = {
3119 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3120 "tabpage", "valid",
3121 NULL
3122};
3123
3124 static PyObject *
3125WindowDir(PyObject *self)
3126{
3127 return ObjectDir(self, WindowAttrs);
3128}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003129
Bram Moolenaar971db462013-05-12 18:44:48 +02003130 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003131WindowAttrValid(WindowObject *self, char *name)
3132{
3133 PyObject *r;
3134
3135 if (strcmp(name, "valid") != 0)
3136 return NULL;
3137
3138 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3139 Py_INCREF(r);
3140 return r;
3141}
3142
3143 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003144WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003145{
3146 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003147 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003148 else if (strcmp(name, "cursor") == 0)
3149 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003150 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003151
3152 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3153 }
3154 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003155 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003156#ifdef FEAT_WINDOWS
3157 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003158 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003159#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003160#ifdef FEAT_VERTSPLIT
3161 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003162 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003163 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003164 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003165#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003166 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003167 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003168 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003169 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3170 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003171 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003172 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003173 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003174 return NULL;
3175 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003176 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003177 }
3178 else if (strcmp(name, "tabpage") == 0)
3179 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003180 Py_INCREF(self->tabObject);
3181 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003182 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003183 else if (strcmp(name, "__members__") == 0)
3184 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003185 else
3186 return NULL;
3187}
3188
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003189 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003190WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003191{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003192 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003193 return -1;
3194
3195 if (strcmp(name, "buffer") == 0)
3196 {
3197 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
3198 return -1;
3199 }
3200 else if (strcmp(name, "cursor") == 0)
3201 {
3202 long lnum;
3203 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003204
3205 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
3206 return -1;
3207
Bram Moolenaard6e39182013-05-21 18:30:34 +02003208 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003209 {
3210 PyErr_SetVim(_("cursor position outside buffer"));
3211 return -1;
3212 }
3213
3214 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003215 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003216 return -1;
3217
Bram Moolenaard6e39182013-05-21 18:30:34 +02003218 self->win->w_cursor.lnum = lnum;
3219 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003220#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003221 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003222#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003223 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003224 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003225
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003226 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003227 return 0;
3228 }
3229 else if (strcmp(name, "height") == 0)
3230 {
3231 int height;
3232 win_T *savewin;
3233
3234 if (!PyArg_Parse(val, "i", &height))
3235 return -1;
3236
3237#ifdef FEAT_GUI
3238 need_mouse_correct = TRUE;
3239#endif
3240 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003241 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003242
3243 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003244 win_setheight(height);
3245 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003246 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003247 return -1;
3248
3249 return 0;
3250 }
3251#ifdef FEAT_VERTSPLIT
3252 else if (strcmp(name, "width") == 0)
3253 {
3254 int width;
3255 win_T *savewin;
3256
3257 if (!PyArg_Parse(val, "i", &width))
3258 return -1;
3259
3260#ifdef FEAT_GUI
3261 need_mouse_correct = TRUE;
3262#endif
3263 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003264 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003265
3266 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003267 win_setwidth(width);
3268 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003269 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003270 return -1;
3271
3272 return 0;
3273 }
3274#endif
3275 else
3276 {
3277 PyErr_SetString(PyExc_AttributeError, name);
3278 return -1;
3279 }
3280}
3281
3282 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003283WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003284{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003285 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003286 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003287 else
3288 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003289 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003290
Bram Moolenaar6d216452013-05-12 19:00:41 +02003291 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003292 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003293 (self));
3294 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003295 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003296 }
3297}
3298
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003299static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003300 /* name, function, calling, documentation */
3301 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3302 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003303};
3304
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003305/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003306 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003307 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003308
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003309static PyTypeObject WinListType;
3310static PySequenceMethods WinListAsSeq;
3311
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003312typedef struct
3313{
3314 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003315 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003316} WinListObject;
3317
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003318 static PyObject *
3319WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003320{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003321 WinListObject *self;
3322
3323 self = PyObject_NEW(WinListObject, &WinListType);
3324 self->tabObject = tabObject;
3325 Py_INCREF(tabObject);
3326
3327 return (PyObject *)(self);
3328}
3329
3330 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003331WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003332{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003333 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003334
3335 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003336 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003337 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003338 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003339
3340 DESTRUCTOR_FINISH(self);
3341}
3342
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003343 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003344WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003345{
3346 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003347 PyInt n = 0;
3348
Bram Moolenaard6e39182013-05-21 18:30:34 +02003349 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003350 return -1;
3351
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003352 while (w != NULL)
3353 {
3354 ++n;
3355 w = W_NEXT(w);
3356 }
3357
3358 return n;
3359}
3360
3361 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003362WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003363{
3364 win_T *w;
3365
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003367 return NULL;
3368
3369 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003370 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003371 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003372
3373 PyErr_SetString(PyExc_IndexError, _("no such window"));
3374 return NULL;
3375}
3376
3377/* Convert a Python string into a Vim line.
3378 *
3379 * The result is in allocated memory. All internal nulls are replaced by
3380 * newline characters. It is an error for the string to contain newline
3381 * characters.
3382 *
3383 * On errors, the Python exception data is set, and NULL is returned.
3384 */
3385 static char *
3386StringToLine(PyObject *obj)
3387{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003388 char *str;
3389 char *save;
3390 PyObject *bytes = NULL;
3391 Py_ssize_t len;
3392 PyInt i;
3393 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003394
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003395 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003396 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003397 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3398 || str == NULL)
3399 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003400 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003401 else if (PyUnicode_Check(obj))
3402 {
3403 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3404 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003405
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003406 if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
3407 || str == NULL)
3408 {
3409 Py_DECREF(bytes);
3410 return NULL;
3411 }
3412 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003413
3414 /*
3415 * Error checking: String must not contain newlines, as we
3416 * are replacing a single line, and we must replace it with
3417 * a single line.
3418 * A trailing newline is removed, so that append(f.readlines()) works.
3419 */
3420 p = memchr(str, '\n', len);
3421 if (p != NULL)
3422 {
3423 if (p == str + len - 1)
3424 --len;
3425 else
3426 {
3427 PyErr_SetVim(_("string cannot contain newlines"));
3428 return NULL;
3429 }
3430 }
3431
3432 /* Create a copy of the string, with internal nulls replaced by
3433 * newline characters, as is the vim convention.
3434 */
3435 save = (char *)alloc((unsigned)(len+1));
3436 if (save == NULL)
3437 {
3438 PyErr_NoMemory();
3439 return NULL;
3440 }
3441
3442 for (i = 0; i < len; ++i)
3443 {
3444 if (str[i] == '\0')
3445 save[i] = '\n';
3446 else
3447 save[i] = str[i];
3448 }
3449
3450 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003451 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003452
3453 return save;
3454}
3455
3456/* Get a line from the specified buffer. The line number is
3457 * in Vim format (1-based). The line is returned as a Python
3458 * string object.
3459 */
3460 static PyObject *
3461GetBufferLine(buf_T *buf, PyInt n)
3462{
3463 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3464}
3465
3466
3467/* Get a list of lines from the specified buffer. The line numbers
3468 * are in Vim format (1-based). The range is from lo up to, but not
3469 * including, hi. The list is returned as a Python list of string objects.
3470 */
3471 static PyObject *
3472GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3473{
3474 PyInt i;
3475 PyInt n = hi - lo;
3476 PyObject *list = PyList_New(n);
3477
3478 if (list == NULL)
3479 return NULL;
3480
3481 for (i = 0; i < n; ++i)
3482 {
3483 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3484
3485 /* Error check - was the Python string creation OK? */
3486 if (str == NULL)
3487 {
3488 Py_DECREF(list);
3489 return NULL;
3490 }
3491
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02003492 PyList_SET_ITEM(list, i, str);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003493 }
3494
3495 /* The ownership of the Python list is passed to the caller (ie,
3496 * the caller should Py_DECREF() the object when it is finished
3497 * with it).
3498 */
3499
3500 return list;
3501}
3502
3503/*
3504 * Check if deleting lines made the cursor position invalid.
3505 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3506 * deleted).
3507 */
3508 static void
3509py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3510{
3511 if (curwin->w_cursor.lnum >= lo)
3512 {
3513 /* Adjust the cursor position if it's in/after the changed
3514 * lines. */
3515 if (curwin->w_cursor.lnum >= hi)
3516 {
3517 curwin->w_cursor.lnum += extra;
3518 check_cursor_col();
3519 }
3520 else if (extra < 0)
3521 {
3522 curwin->w_cursor.lnum = lo;
3523 check_cursor();
3524 }
3525 else
3526 check_cursor_col();
3527 changed_cline_bef_curs();
3528 }
3529 invalidate_botline();
3530}
3531
Bram Moolenaar19e60942011-06-19 00:27:51 +02003532/*
3533 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003534 * in Vim format (1-based). The replacement line is given as
3535 * a Python string object. The object is checked for validity
3536 * and correct format. Errors are returned as a value of FAIL.
3537 * The return value is OK on success.
3538 * If OK is returned and len_change is not NULL, *len_change
3539 * is set to the change in the buffer length.
3540 */
3541 static int
3542SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3543{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003544 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003545 * There are three cases:
3546 * 1. NULL, or None - this is a deletion.
3547 * 2. A string - this is a replacement.
3548 * 3. Anything else - this is an error.
3549 */
3550 if (line == Py_None || line == NULL)
3551 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003552 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003553
3554 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003555 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003556
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003557 VimTryStart();
3558
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003559 if (u_savedel((linenr_T)n, 1L) == FAIL)
3560 PyErr_SetVim(_("cannot save undo information"));
3561 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3562 PyErr_SetVim(_("cannot delete line"));
3563 else
3564 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003565 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003566 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3567 deleted_lines_mark((linenr_T)n, 1L);
3568 }
3569
Bram Moolenaar105bc352013-05-17 16:03:57 +02003570 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003571
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003572 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003573 return FAIL;
3574
3575 if (len_change)
3576 *len_change = -1;
3577
3578 return OK;
3579 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003580 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003581 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003582 char *save = StringToLine(line);
3583 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003584
3585 if (save == NULL)
3586 return FAIL;
3587
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003588 VimTryStart();
3589
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003590 /* We do not need to free "save" if ml_replace() consumes it. */
3591 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003592 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003593
3594 if (u_savesub((linenr_T)n) == FAIL)
3595 {
3596 PyErr_SetVim(_("cannot save undo information"));
3597 vim_free(save);
3598 }
3599 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3600 {
3601 PyErr_SetVim(_("cannot replace line"));
3602 vim_free(save);
3603 }
3604 else
3605 changed_bytes((linenr_T)n, 0);
3606
Bram Moolenaar105bc352013-05-17 16:03:57 +02003607 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003608
3609 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003610 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003611 check_cursor_col();
3612
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003613 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003614 return FAIL;
3615
3616 if (len_change)
3617 *len_change = 0;
3618
3619 return OK;
3620 }
3621 else
3622 {
3623 PyErr_BadArgument();
3624 return FAIL;
3625 }
3626}
3627
Bram Moolenaar19e60942011-06-19 00:27:51 +02003628/* Replace a range of lines in the specified buffer. The line numbers are in
3629 * Vim format (1-based). The range is from lo up to, but not including, hi.
3630 * The replacement lines are given as a Python list of string objects. The
3631 * list is checked for validity and correct format. Errors are returned as a
3632 * value of FAIL. The return value is OK on success.
3633 * If OK is returned and len_change is not NULL, *len_change
3634 * is set to the change in the buffer length.
3635 */
3636 static int
3637SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3638{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003639 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003640 * There are three cases:
3641 * 1. NULL, or None - this is a deletion.
3642 * 2. A list - this is a replacement.
3643 * 3. Anything else - this is an error.
3644 */
3645 if (list == Py_None || list == NULL)
3646 {
3647 PyInt i;
3648 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003649 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003650
3651 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003652 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003653 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003654
3655 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3656 PyErr_SetVim(_("cannot save undo information"));
3657 else
3658 {
3659 for (i = 0; i < n; ++i)
3660 {
3661 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3662 {
3663 PyErr_SetVim(_("cannot delete line"));
3664 break;
3665 }
3666 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003667 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003668 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3669 deleted_lines_mark((linenr_T)lo, (long)i);
3670 }
3671
Bram Moolenaar105bc352013-05-17 16:03:57 +02003672 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003673
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003674 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003675 return FAIL;
3676
3677 if (len_change)
3678 *len_change = -n;
3679
3680 return OK;
3681 }
3682 else if (PyList_Check(list))
3683 {
3684 PyInt i;
3685 PyInt new_len = PyList_Size(list);
3686 PyInt old_len = hi - lo;
3687 PyInt extra = 0; /* lines added to text, can be negative */
3688 char **array;
3689 buf_T *savebuf;
3690
3691 if (new_len == 0) /* avoid allocating zero bytes */
3692 array = NULL;
3693 else
3694 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003695 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003696 if (array == NULL)
3697 {
3698 PyErr_NoMemory();
3699 return FAIL;
3700 }
3701 }
3702
3703 for (i = 0; i < new_len; ++i)
3704 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003705 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003706
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003707 if (!(line = PyList_GetItem(list, i)) ||
3708 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003709 {
3710 while (i)
3711 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003712 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003713 return FAIL;
3714 }
3715 }
3716
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003717 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003718 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003719
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003720 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003721 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003722
3723 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3724 PyErr_SetVim(_("cannot save undo information"));
3725
3726 /* If the size of the range is reducing (ie, new_len < old_len) we
3727 * need to delete some old_len. We do this at the start, by
3728 * repeatedly deleting line "lo".
3729 */
3730 if (!PyErr_Occurred())
3731 {
3732 for (i = 0; i < old_len - new_len; ++i)
3733 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3734 {
3735 PyErr_SetVim(_("cannot delete line"));
3736 break;
3737 }
3738 extra -= i;
3739 }
3740
3741 /* For as long as possible, replace the existing old_len with the
3742 * new old_len. This is a more efficient operation, as it requires
3743 * less memory allocation and freeing.
3744 */
3745 if (!PyErr_Occurred())
3746 {
3747 for (i = 0; i < old_len && i < new_len; ++i)
3748 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3749 == FAIL)
3750 {
3751 PyErr_SetVim(_("cannot replace line"));
3752 break;
3753 }
3754 }
3755 else
3756 i = 0;
3757
3758 /* Now we may need to insert the remaining new old_len. If we do, we
3759 * must free the strings as we finish with them (we can't pass the
3760 * responsibility to vim in this case).
3761 */
3762 if (!PyErr_Occurred())
3763 {
3764 while (i < new_len)
3765 {
3766 if (ml_append((linenr_T)(lo + i - 1),
3767 (char_u *)array[i], 0, FALSE) == FAIL)
3768 {
3769 PyErr_SetVim(_("cannot insert line"));
3770 break;
3771 }
3772 vim_free(array[i]);
3773 ++i;
3774 ++extra;
3775 }
3776 }
3777
3778 /* Free any left-over old_len, as a result of an error */
3779 while (i < new_len)
3780 {
3781 vim_free(array[i]);
3782 ++i;
3783 }
3784
3785 /* Free the array of old_len. All of its contents have now
3786 * been dealt with (either freed, or the responsibility passed
3787 * to vim.
3788 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003789 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003790
3791 /* Adjust marks. Invalidate any which lie in the
3792 * changed range, and move any in the remainder of the buffer.
3793 */
3794 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3795 (long)MAXLNUM, (long)extra);
3796 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3797
Bram Moolenaar105bc352013-05-17 16:03:57 +02003798 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003799 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3800
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003801 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003802 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003803
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003804 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003805 return FAIL;
3806
3807 if (len_change)
3808 *len_change = new_len - old_len;
3809
3810 return OK;
3811 }
3812 else
3813 {
3814 PyErr_BadArgument();
3815 return FAIL;
3816 }
3817}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003818
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003819/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003820 * The line number is in Vim format (1-based). The lines to be inserted are
3821 * given as a Python list of string objects or as a single string. The lines
3822 * to be added are checked for validity and correct format. Errors are
3823 * returned as a value of FAIL. The return value is OK on success.
3824 * If OK is returned and len_change is not NULL, *len_change
3825 * is set to the change in the buffer length.
3826 */
3827 static int
3828InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3829{
3830 /* First of all, we check the type of the supplied Python object.
3831 * It must be a string or a list, or the call is in error.
3832 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003833 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003834 {
3835 char *str = StringToLine(lines);
3836 buf_T *savebuf;
3837
3838 if (str == NULL)
3839 return FAIL;
3840
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003841 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003842 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003843 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003844
3845 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3846 PyErr_SetVim(_("cannot save undo information"));
3847 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3848 PyErr_SetVim(_("cannot insert line"));
3849 else
3850 appended_lines_mark((linenr_T)n, 1L);
3851
3852 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003853 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003854 update_screen(VALID);
3855
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003856 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003857 return FAIL;
3858
3859 if (len_change)
3860 *len_change = 1;
3861
3862 return OK;
3863 }
3864 else if (PyList_Check(lines))
3865 {
3866 PyInt i;
3867 PyInt size = PyList_Size(lines);
3868 char **array;
3869 buf_T *savebuf;
3870
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003871 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003872 if (array == NULL)
3873 {
3874 PyErr_NoMemory();
3875 return FAIL;
3876 }
3877
3878 for (i = 0; i < size; ++i)
3879 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003880 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003881
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003882 if (!(line = PyList_GetItem(lines, i)) ||
3883 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003884 {
3885 while (i)
3886 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003887 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003888 return FAIL;
3889 }
3890 }
3891
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003892 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003893 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003894 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003895
3896 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3897 PyErr_SetVim(_("cannot save undo information"));
3898 else
3899 {
3900 for (i = 0; i < size; ++i)
3901 {
3902 if (ml_append((linenr_T)(n + i),
3903 (char_u *)array[i], 0, FALSE) == FAIL)
3904 {
3905 PyErr_SetVim(_("cannot insert line"));
3906
3907 /* Free the rest of the lines */
3908 while (i < size)
3909 vim_free(array[i++]);
3910
3911 break;
3912 }
3913 vim_free(array[i]);
3914 }
3915 if (i > 0)
3916 appended_lines_mark((linenr_T)n, (long)i);
3917 }
3918
3919 /* Free the array of lines. All of its contents have now
3920 * been freed.
3921 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003922 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003923
Bram Moolenaar105bc352013-05-17 16:03:57 +02003924 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003925 update_screen(VALID);
3926
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003927 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003928 return FAIL;
3929
3930 if (len_change)
3931 *len_change = size;
3932
3933 return OK;
3934 }
3935 else
3936 {
3937 PyErr_BadArgument();
3938 return FAIL;
3939 }
3940}
3941
3942/*
3943 * Common routines for buffers and line ranges
3944 * -------------------------------------------
3945 */
3946
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003947typedef struct
3948{
3949 PyObject_HEAD
3950 buf_T *buf;
3951} BufferObject;
3952
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003953 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003954CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003955{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003956 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003957 {
3958 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3959 return -1;
3960 }
3961
3962 return 0;
3963}
3964
3965 static PyObject *
3966RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3967{
3968 if (CheckBuffer(self))
3969 return NULL;
3970
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003971 if (end == -1)
3972 end = self->buf->b_ml.ml_line_count;
3973
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003974 if (n < 0)
3975 n += end - start + 1;
3976
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003977 if (n < 0 || n > end - start)
3978 {
3979 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3980 return NULL;
3981 }
3982
3983 return GetBufferLine(self->buf, n+start);
3984}
3985
3986 static PyObject *
3987RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3988{
3989 PyInt size;
3990
3991 if (CheckBuffer(self))
3992 return NULL;
3993
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003994 if (end == -1)
3995 end = self->buf->b_ml.ml_line_count;
3996
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003997 size = end - start + 1;
3998
3999 if (lo < 0)
4000 lo = 0;
4001 else if (lo > size)
4002 lo = size;
4003 if (hi < 0)
4004 hi = 0;
4005 if (hi < lo)
4006 hi = lo;
4007 else if (hi > size)
4008 hi = size;
4009
4010 return GetBufferLineList(self->buf, lo+start, hi+start);
4011}
4012
4013 static PyInt
4014RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
4015{
4016 PyInt len_change;
4017
4018 if (CheckBuffer(self))
4019 return -1;
4020
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004021 if (end == -1)
4022 end = self->buf->b_ml.ml_line_count;
4023
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004024 if (n < 0)
4025 n += end - start + 1;
4026
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004027 if (n < 0 || n > end - start)
4028 {
4029 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
4030 return -1;
4031 }
4032
4033 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
4034 return -1;
4035
4036 if (new_end)
4037 *new_end = end + len_change;
4038
4039 return 0;
4040}
4041
Bram Moolenaar19e60942011-06-19 00:27:51 +02004042 static PyInt
4043RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
4044{
4045 PyInt size;
4046 PyInt len_change;
4047
4048 /* Self must be a valid buffer */
4049 if (CheckBuffer(self))
4050 return -1;
4051
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004052 if (end == -1)
4053 end = self->buf->b_ml.ml_line_count;
4054
Bram Moolenaar19e60942011-06-19 00:27:51 +02004055 /* Sort out the slice range */
4056 size = end - start + 1;
4057
4058 if (lo < 0)
4059 lo = 0;
4060 else if (lo > size)
4061 lo = size;
4062 if (hi < 0)
4063 hi = 0;
4064 if (hi < lo)
4065 hi = lo;
4066 else if (hi > size)
4067 hi = size;
4068
4069 if (SetBufferLineList(self->buf, lo + start, hi + start,
4070 val, &len_change) == FAIL)
4071 return -1;
4072
4073 if (new_end)
4074 *new_end = end + len_change;
4075
4076 return 0;
4077}
4078
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079
4080 static PyObject *
4081RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
4082{
4083 PyObject *lines;
4084 PyInt len_change;
4085 PyInt max;
4086 PyInt n;
4087
4088 if (CheckBuffer(self))
4089 return NULL;
4090
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004091 if (end == -1)
4092 end = self->buf->b_ml.ml_line_count;
4093
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004094 max = n = end - start + 1;
4095
4096 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4097 return NULL;
4098
4099 if (n < 0 || n > max)
4100 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004101 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004102 return NULL;
4103 }
4104
4105 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4106 return NULL;
4107
4108 if (new_end)
4109 *new_end = end + len_change;
4110
4111 Py_INCREF(Py_None);
4112 return Py_None;
4113}
4114
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004115/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116 */
4117
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004118static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004119static PySequenceMethods RangeAsSeq;
4120static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004121
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004122typedef struct
4123{
4124 PyObject_HEAD
4125 BufferObject *buf;
4126 PyInt start;
4127 PyInt end;
4128} RangeObject;
4129
4130 static PyObject *
4131RangeNew(buf_T *buf, PyInt start, PyInt end)
4132{
4133 BufferObject *bufr;
4134 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004135 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136 if (self == NULL)
4137 return NULL;
4138
4139 bufr = (BufferObject *)BufferNew(buf);
4140 if (bufr == NULL)
4141 {
4142 Py_DECREF(self);
4143 return NULL;
4144 }
4145 Py_INCREF(bufr);
4146
4147 self->buf = bufr;
4148 self->start = start;
4149 self->end = end;
4150
4151 return (PyObject *)(self);
4152}
4153
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004154 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004155RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004156{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004157 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004158 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004159 PyObject_GC_Del((void *)(self));
4160}
4161
4162 static int
4163RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4164{
4165 Py_VISIT(((PyObject *)(self->buf)));
4166 return 0;
4167}
4168
4169 static int
4170RangeClear(RangeObject *self)
4171{
4172 Py_CLEAR(self->buf);
4173 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004174}
4175
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004176 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004177RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004178{
4179 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004180 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004181 return -1; /* ??? */
4182
Bram Moolenaard6e39182013-05-21 18:30:34 +02004183 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004184}
4185
4186 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004187RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004188{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004189 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004190}
4191
4192 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004193RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004194{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004195 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004196}
4197
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004198static char *RangeAttrs[] = {
4199 "start", "end",
4200 NULL
4201};
4202
4203 static PyObject *
4204RangeDir(PyObject *self)
4205{
4206 return ObjectDir(self, RangeAttrs);
4207}
4208
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004209 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004210RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004211{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004212 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004213}
4214
4215 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004216RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004217{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004218 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004219 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4220 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004221 else
4222 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004223 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004224
4225 if (name == NULL)
4226 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004227
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004228 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004229 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004230 }
4231}
4232
4233static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004234 /* name, function, calling, documentation */
4235 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004236 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4237 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004238};
4239
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004240static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004241static PySequenceMethods BufferAsSeq;
4242static PyMappingMethods BufferAsMapping;
4243
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004244 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004245BufferNew(buf_T *buf)
4246{
4247 /* We need to handle deletion of buffers underneath us.
4248 * If we add a "b_python*_ref" field to the buf_T structure,
4249 * then we can get at it in buf_freeall() in vim. We then
4250 * need to create only ONE Python object per buffer - if
4251 * we try to create a second, just INCREF the existing one
4252 * and return it. The (single) Python object referring to
4253 * the buffer is stored in "b_python*_ref".
4254 * Question: what to do on a buf_freeall(). We'll probably
4255 * have to either delete the Python object (DECREF it to
4256 * zero - a bad idea, as it leaves dangling refs!) or
4257 * set the buf_T * value to an invalid value (-1?), which
4258 * means we need checks in all access functions... Bah.
4259 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004260 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004261 * b_python_ref and b_python3_ref fields respectively.
4262 */
4263
4264 BufferObject *self;
4265
4266 if (BUF_PYTHON_REF(buf) != NULL)
4267 {
4268 self = BUF_PYTHON_REF(buf);
4269 Py_INCREF(self);
4270 }
4271 else
4272 {
4273 self = PyObject_NEW(BufferObject, &BufferType);
4274 if (self == NULL)
4275 return NULL;
4276 self->buf = buf;
4277 BUF_PYTHON_REF(buf) = self;
4278 }
4279
4280 return (PyObject *)(self);
4281}
4282
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004283 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004284BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004285{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004286 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4287 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004288
4289 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004290}
4291
Bram Moolenaar971db462013-05-12 18:44:48 +02004292 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004293BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004294{
4295 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004296 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004297 return -1; /* ??? */
4298
Bram Moolenaard6e39182013-05-21 18:30:34 +02004299 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004300}
4301
4302 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004303BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004304{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004305 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004306}
4307
4308 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004309BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004310{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004311 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004312}
4313
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004314static char *BufferAttrs[] = {
4315 "name", "number", "vars", "options", "valid",
4316 NULL
4317};
4318
4319 static PyObject *
4320BufferDir(PyObject *self)
4321{
4322 return ObjectDir(self, BufferAttrs);
4323}
4324
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004325 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004326BufferAttrValid(BufferObject *self, char *name)
4327{
4328 PyObject *r;
4329
4330 if (strcmp(name, "valid") != 0)
4331 return NULL;
4332
4333 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4334 Py_INCREF(r);
4335 return r;
4336}
4337
4338 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004339BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004340{
4341 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004342 return PyString_FromString((self->buf->b_ffname == NULL
4343 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004344 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004345 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004346 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004347 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004348 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004349 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4350 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004351 else if (strcmp(name, "__members__") == 0)
4352 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004353 else
4354 return NULL;
4355}
4356
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004357 static int
4358BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4359{
4360 if (CheckBuffer(self))
4361 return -1;
4362
4363 if (strcmp(name, "name") == 0)
4364 {
4365 char_u *val;
4366 aco_save_T aco;
4367 int r;
4368 PyObject *todecref;
4369
4370 if (!(val = StringToChars(valObject, &todecref)))
4371 return -1;
4372
4373 VimTryStart();
4374 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4375 aucmd_prepbuf(&aco, self->buf);
4376 r = rename_buffer(val);
4377 aucmd_restbuf(&aco);
4378 Py_XDECREF(todecref);
4379 if (VimTryEnd())
4380 return -1;
4381
4382 if (r == FAIL)
4383 {
4384 PyErr_SetVim(_("failed to rename buffer"));
4385 return -1;
4386 }
4387 return 0;
4388 }
4389 else
4390 {
4391 PyErr_SetString(PyExc_AttributeError, name);
4392 return -1;
4393 }
4394}
4395
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004396 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004397BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004398{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004399 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004400}
4401
4402 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004403BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004404{
4405 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004406 char_u *pmark;
4407 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004408 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004409 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004410
Bram Moolenaard6e39182013-05-21 18:30:34 +02004411 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004412 return NULL;
4413
Bram Moolenaar389a1792013-06-23 13:00:44 +02004414 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004415 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004416
Bram Moolenaar389a1792013-06-23 13:00:44 +02004417 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004418 {
4419 PyErr_SetString(PyExc_ValueError,
4420 _("mark name must be a single character"));
4421 return NULL;
4422 }
4423
4424 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004425
4426 Py_XDECREF(todecref);
4427
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004428 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004429 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004430 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004431 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004432 if (VimTryEnd())
4433 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004434
4435 if (posp == NULL)
4436 {
4437 PyErr_SetVim(_("invalid mark name"));
4438 return NULL;
4439 }
4440
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004441 if (posp->lnum <= 0)
4442 {
4443 /* Or raise an error? */
4444 Py_INCREF(Py_None);
4445 return Py_None;
4446 }
4447
4448 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4449}
4450
4451 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004452BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004453{
4454 PyInt start;
4455 PyInt end;
4456
Bram Moolenaard6e39182013-05-21 18:30:34 +02004457 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004458 return NULL;
4459
4460 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4461 return NULL;
4462
Bram Moolenaard6e39182013-05-21 18:30:34 +02004463 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004464}
4465
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004466 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004467BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004468{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004469 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004470 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004471 else
4472 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004473 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004474
4475 if (name == NULL)
4476 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004477
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004478 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004479 }
4480}
4481
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004482static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004483 /* name, function, calling, documentation */
4484 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004485 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004486 {"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 +02004487 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4488 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004489};
4490
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004491/*
4492 * Buffer list object - Implementation
4493 */
4494
4495static PyTypeObject BufMapType;
4496
4497typedef struct
4498{
4499 PyObject_HEAD
4500} BufMapObject;
4501
4502 static PyInt
4503BufMapLength(PyObject *self UNUSED)
4504{
4505 buf_T *b = firstbuf;
4506 PyInt n = 0;
4507
4508 while (b)
4509 {
4510 ++n;
4511 b = b->b_next;
4512 }
4513
4514 return n;
4515}
4516
4517 static PyObject *
4518BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4519{
4520 buf_T *b;
4521 int bnr;
4522
4523#if PY_MAJOR_VERSION < 3
4524 if (PyInt_Check(keyObject))
4525 bnr = PyInt_AsLong(keyObject);
4526 else
4527#endif
4528 if (PyLong_Check(keyObject))
4529 bnr = PyLong_AsLong(keyObject);
4530 else
4531 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004532 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004533 return NULL;
4534 }
4535
4536 b = buflist_findnr(bnr);
4537
4538 if (b)
4539 return BufferNew(b);
4540 else
4541 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004542 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004543 return NULL;
4544 }
4545}
4546
4547 static void
4548BufMapIterDestruct(PyObject *buffer)
4549{
4550 /* Iteration was stopped before all buffers were processed */
4551 if (buffer)
4552 {
4553 Py_DECREF(buffer);
4554 }
4555}
4556
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004557 static int
4558BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4559{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004560 if (buffer)
4561 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004562 return 0;
4563}
4564
4565 static int
4566BufMapIterClear(PyObject **buffer)
4567{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004568 if (*buffer)
4569 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004570 return 0;
4571}
4572
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004573 static PyObject *
4574BufMapIterNext(PyObject **buffer)
4575{
4576 PyObject *next;
4577 PyObject *r;
4578
4579 if (!*buffer)
4580 return NULL;
4581
4582 r = *buffer;
4583
4584 if (CheckBuffer((BufferObject *)(r)))
4585 {
4586 *buffer = NULL;
4587 return NULL;
4588 }
4589
4590 if (!((BufferObject *)(r))->buf->b_next)
4591 next = NULL;
4592 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4593 return NULL;
4594 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004595 /* Do not increment reference: we no longer hold it (decref), but whoever
4596 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004597 return r;
4598}
4599
4600 static PyObject *
4601BufMapIter(PyObject *self UNUSED)
4602{
4603 PyObject *buffer;
4604
4605 buffer = BufferNew(firstbuf);
4606 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004607 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4608 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004609}
4610
4611static PyMappingMethods BufMapAsMapping = {
4612 (lenfunc) BufMapLength,
4613 (binaryfunc) BufMapItem,
4614 (objobjargproc) 0,
4615};
4616
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004617/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004618 */
4619
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004620static char *CurrentAttrs[] = {
4621 "buffer", "window", "line", "range", "tabpage",
4622 NULL
4623};
4624
4625 static PyObject *
4626CurrentDir(PyObject *self)
4627{
4628 return ObjectDir(self, CurrentAttrs);
4629}
4630
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004631 static PyObject *
4632CurrentGetattr(PyObject *self UNUSED, char *name)
4633{
4634 if (strcmp(name, "buffer") == 0)
4635 return (PyObject *)BufferNew(curbuf);
4636 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004637 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004638 else if (strcmp(name, "tabpage") == 0)
4639 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004640 else if (strcmp(name, "line") == 0)
4641 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4642 else if (strcmp(name, "range") == 0)
4643 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004644 else if (strcmp(name, "__members__") == 0)
4645 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004646 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004647#if PY_MAJOR_VERSION < 3
4648 return Py_FindMethod(WindowMethods, self, name);
4649#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004650 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004651#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004652}
4653
4654 static int
4655CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4656{
4657 if (strcmp(name, "line") == 0)
4658 {
4659 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4660 return -1;
4661
4662 return 0;
4663 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004664 else if (strcmp(name, "buffer") == 0)
4665 {
4666 int count;
4667
4668 if (value->ob_type != &BufferType)
4669 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004670 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004671 return -1;
4672 }
4673
4674 if (CheckBuffer((BufferObject *)(value)))
4675 return -1;
4676 count = ((BufferObject *)(value))->buf->b_fnum;
4677
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004678 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004679 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4680 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004681 if (VimTryEnd())
4682 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004683 PyErr_SetVim(_("failed to switch to given buffer"));
4684 return -1;
4685 }
4686
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004687 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004688 }
4689 else if (strcmp(name, "window") == 0)
4690 {
4691 int count;
4692
4693 if (value->ob_type != &WindowType)
4694 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004695 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004696 return -1;
4697 }
4698
4699 if (CheckWindow((WindowObject *)(value)))
4700 return -1;
4701 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4702
4703 if (!count)
4704 {
4705 PyErr_SetString(PyExc_ValueError,
4706 _("failed to find window in the current tab page"));
4707 return -1;
4708 }
4709
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004710 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004711 win_goto(((WindowObject *)(value))->win);
4712 if (((WindowObject *)(value))->win != curwin)
4713 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004714 if (VimTryEnd())
4715 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004716 PyErr_SetString(PyExc_RuntimeError,
4717 _("did not switch to the specified window"));
4718 return -1;
4719 }
4720
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004721 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004722 }
4723 else if (strcmp(name, "tabpage") == 0)
4724 {
4725 if (value->ob_type != &TabPageType)
4726 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004727 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004728 return -1;
4729 }
4730
4731 if (CheckTabPage((TabPageObject *)(value)))
4732 return -1;
4733
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004734 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004735 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4736 if (((TabPageObject *)(value))->tab != curtab)
4737 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004738 if (VimTryEnd())
4739 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004740 PyErr_SetString(PyExc_RuntimeError,
4741 _("did not switch to the specified tab page"));
4742 return -1;
4743 }
4744
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004745 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004746 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004747 else
4748 {
4749 PyErr_SetString(PyExc_AttributeError, name);
4750 return -1;
4751 }
4752}
4753
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004754static struct PyMethodDef CurrentMethods[] = {
4755 /* name, function, calling, documentation */
4756 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4757 { NULL, NULL, 0, NULL}
4758};
4759
Bram Moolenaardb913952012-06-29 12:54:53 +02004760 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004761init_range_cmd(exarg_T *eap)
4762{
4763 RangeStart = eap->line1;
4764 RangeEnd = eap->line2;
4765}
4766
4767 static void
4768init_range_eval(typval_T *rettv UNUSED)
4769{
4770 RangeStart = (PyInt) curwin->w_cursor.lnum;
4771 RangeEnd = RangeStart;
4772}
4773
4774 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004775run_cmd(const char *cmd, void *arg UNUSED
4776#ifdef PY_CAN_RECURSE
4777 , PyGILState_STATE *pygilstate UNUSED
4778#endif
4779 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004780{
4781 PyRun_SimpleString((char *) cmd);
4782}
4783
4784static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4785static int code_hdr_len = 30;
4786
4787 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004788run_do(const char *cmd, void *arg UNUSED
4789#ifdef PY_CAN_RECURSE
4790 , PyGILState_STATE *pygilstate
4791#endif
4792 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004793{
4794 PyInt lnum;
4795 size_t len;
4796 char *code;
4797 int status;
4798 PyObject *pyfunc, *pymain;
4799
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004800 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004801 {
4802 EMSG(_("cannot save undo information"));
4803 return;
4804 }
4805
4806 len = code_hdr_len + STRLEN(cmd);
4807 code = PyMem_New(char, len + 1);
4808 memcpy(code, code_hdr, code_hdr_len);
4809 STRCPY(code + code_hdr_len, cmd);
4810 status = PyRun_SimpleString(code);
4811 PyMem_Free(code);
4812
4813 if (status)
4814 {
4815 EMSG(_("failed to run the code"));
4816 return;
4817 }
4818
4819 status = 0;
4820 pymain = PyImport_AddModule("__main__");
4821 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004822#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004823 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004824#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004825
4826 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4827 {
4828 PyObject *line, *linenr, *ret;
4829
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004830#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004831 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004832#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004833 if (!(line = GetBufferLine(curbuf, lnum)))
4834 goto err;
4835 if (!(linenr = PyInt_FromLong((long) lnum)))
4836 {
4837 Py_DECREF(line);
4838 goto err;
4839 }
4840 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4841 Py_DECREF(line);
4842 Py_DECREF(linenr);
4843 if (!ret)
4844 goto err;
4845
4846 if (ret != Py_None)
4847 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4848 goto err;
4849
4850 Py_XDECREF(ret);
4851 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004852#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004853 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004854#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004855 }
4856 goto out;
4857err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004858#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004859 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004860#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004861 PyErr_PrintEx(0);
4862 PythonIO_Flush();
4863 status = 1;
4864out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004865#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004866 if (!status)
4867 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004868#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004869 Py_DECREF(pyfunc);
4870 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4871 if (status)
4872 return;
4873 check_cursor();
4874 update_curbuf(NOT_VALID);
4875}
4876
4877 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004878run_eval(const char *cmd, typval_T *rettv
4879#ifdef PY_CAN_RECURSE
4880 , PyGILState_STATE *pygilstate UNUSED
4881#endif
4882 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004883{
4884 PyObject *r;
4885
4886 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4887 if (r == NULL)
4888 {
4889 if (PyErr_Occurred() && !msg_silent)
4890 PyErr_PrintEx(0);
4891 EMSG(_("E858: Eval did not return a valid python object"));
4892 }
4893 else
4894 {
4895 if (ConvertFromPyObject(r, rettv) == -1)
4896 EMSG(_("E859: Failed to convert returned python object to vim value"));
4897 Py_DECREF(r);
4898 }
4899 PyErr_Clear();
4900}
4901
4902 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004903set_ref_in_py(const int copyID)
4904{
4905 pylinkedlist_T *cur;
4906 dict_T *dd;
4907 list_T *ll;
4908
4909 if (lastdict != NULL)
4910 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4911 {
4912 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4913 if (dd->dv_copyID != copyID)
4914 {
4915 dd->dv_copyID = copyID;
4916 set_ref_in_ht(&dd->dv_hashtab, copyID);
4917 }
4918 }
4919
4920 if (lastlist != NULL)
4921 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4922 {
4923 ll = ((ListObject *) (cur->pll_obj))->list;
4924 if (ll->lv_copyID != copyID)
4925 {
4926 ll->lv_copyID = copyID;
4927 set_ref_in_list(ll, copyID);
4928 }
4929 }
4930}
4931
4932 static int
4933set_string_copy(char_u *str, typval_T *tv)
4934{
4935 tv->vval.v_string = vim_strsave(str);
4936 if (tv->vval.v_string == NULL)
4937 {
4938 PyErr_NoMemory();
4939 return -1;
4940 }
4941 return 0;
4942}
4943
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004944 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004945pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004946{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004947 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004948 char_u *key;
4949 dictitem_T *di;
4950 PyObject *keyObject;
4951 PyObject *valObject;
4952 Py_ssize_t iter = 0;
4953
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004954 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004955 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004956
4957 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004958 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004959
4960 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4961 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004962 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004963
Bram Moolenaara03e6312013-05-29 22:49:26 +02004964 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004965 {
4966 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004967 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004968 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004969
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004970 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004971 {
4972 dict_unref(dict);
4973 return -1;
4974 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004975
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004976 if (*key == NUL)
4977 {
4978 dict_unref(dict);
4979 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004980 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004981 return -1;
4982 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004983
4984 di = dictitem_alloc(key);
4985
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004986 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004987
4988 if (di == NULL)
4989 {
4990 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004991 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004992 return -1;
4993 }
4994 di->di_tv.v_lock = 0;
4995
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004996 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004997 {
4998 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004999 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005000 return -1;
5001 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005002
5003 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005004 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02005005 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005006 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005007 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005008 PyErr_SetVim(_("failed to add key to dictionary"));
5009 return -1;
5010 }
5011 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005012
5013 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005014 return 0;
5015}
5016
5017 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005018pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005019{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005020 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005021 char_u *key;
5022 dictitem_T *di;
5023 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005024 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005025 PyObject *keyObject;
5026 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005027
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005028 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005029 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005030
5031 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005032 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005033
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005034 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005035 {
5036 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005037 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005038 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005039
5040 if (!(iterator = PyObject_GetIter(list)))
5041 {
5042 dict_unref(dict);
5043 Py_DECREF(list);
5044 return -1;
5045 }
5046 Py_DECREF(list);
5047
5048 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005049 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005050 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005051
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005052 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005053 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005054 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005055 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005056 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005057 return -1;
5058 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005059
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005060 if (*key == NUL)
5061 {
5062 Py_DECREF(keyObject);
5063 Py_DECREF(iterator);
5064 Py_XDECREF(todecref);
5065 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005066 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005067 return -1;
5068 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005069
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005070 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005071 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005072 Py_DECREF(keyObject);
5073 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005074 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005075 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005076 return -1;
5077 }
5078
5079 di = dictitem_alloc(key);
5080
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005081 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005082 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005083
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005084 if (di == NULL)
5085 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005086 Py_DECREF(iterator);
5087 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005088 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089 PyErr_NoMemory();
5090 return -1;
5091 }
5092 di->di_tv.v_lock = 0;
5093
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005094 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005095 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005096 Py_DECREF(iterator);
5097 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005098 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005099 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005100 return -1;
5101 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005102
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005103 Py_DECREF(valObject);
5104
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005105 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005106 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005107 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005108 dictitem_free(di);
5109 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005110 PyErr_SetVim(_("failed to add key to dictionary"));
5111 return -1;
5112 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005113 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005114 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005115 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116 return 0;
5117}
5118
5119 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005120pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005121{
5122 list_T *l;
5123
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005124 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005125 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005126
5127 tv->v_type = VAR_LIST;
5128 tv->vval.v_list = l;
5129
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005130 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005131 {
5132 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005133 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005134 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005135
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005136 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005137 return 0;
5138}
5139
Bram Moolenaardb913952012-06-29 12:54:53 +02005140typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5141
5142 static int
5143convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005144 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005145{
5146 PyObject *capsule;
5147 char hexBuf[sizeof(void *) * 2 + 3];
5148
5149 sprintf(hexBuf, "%p", obj);
5150
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005151# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005152 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005153# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005154 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005155# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005156 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005157 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005158# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005159 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005160# else
5161 capsule = PyCObject_FromVoidPtr(tv, NULL);
5162# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005163 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5164 {
5165 Py_DECREF(capsule);
5166 tv->v_type = VAR_UNKNOWN;
5167 return -1;
5168 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005169 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005170 {
5171 tv->v_type = VAR_UNKNOWN;
5172 return -1;
5173 }
5174 /* As we are not using copy_tv which increments reference count we must
5175 * do it ourself. */
5176 switch(tv->v_type)
5177 {
5178 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5179 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5180 }
5181 }
5182 else
5183 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005184 typval_T *v;
5185
5186# ifdef PY_USE_CAPSULE
5187 v = PyCapsule_GetPointer(capsule, NULL);
5188# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005189 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005190# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005191 copy_tv(v, tv);
5192 }
5193 return 0;
5194}
5195
5196 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005197ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5198{
5199 PyObject *lookup_dict;
5200 int r;
5201
5202 if (!(lookup_dict = PyDict_New()))
5203 return -1;
5204
5205 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5206 {
5207 tv->v_type = VAR_DICT;
5208 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5209 ++tv->vval.v_dict->dv_refcount;
5210 r = 0;
5211 }
5212 else if (PyDict_Check(obj))
5213 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
5214 else if (PyMapping_Check(obj))
5215 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5216 else
5217 {
5218 PyErr_SetString(PyExc_TypeError,
5219 _("unable to convert object to vim dictionary"));
5220 r = -1;
5221 }
5222 Py_DECREF(lookup_dict);
5223 return r;
5224}
5225
5226 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005227ConvertFromPyObject(PyObject *obj, typval_T *tv)
5228{
5229 PyObject *lookup_dict;
5230 int r;
5231
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005232 if (!(lookup_dict = PyDict_New()))
5233 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005234 r = _ConvertFromPyObject(obj, tv, lookup_dict);
5235 Py_DECREF(lookup_dict);
5236 return r;
5237}
5238
5239 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005240_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005241{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005242 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005243 {
5244 tv->v_type = VAR_DICT;
5245 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5246 ++tv->vval.v_dict->dv_refcount;
5247 }
5248 else if (obj->ob_type == &ListType)
5249 {
5250 tv->v_type = VAR_LIST;
5251 tv->vval.v_list = (((ListObject *)(obj))->list);
5252 ++tv->vval.v_list->lv_refcount;
5253 }
5254 else if (obj->ob_type == &FunctionType)
5255 {
5256 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5257 return -1;
5258
5259 tv->v_type = VAR_FUNC;
5260 func_ref(tv->vval.v_string);
5261 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005262 else if (PyBytes_Check(obj))
5263 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005264 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02005265
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005266 if (PyBytes_AsStringAndSize(obj, (char **) &result, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005267 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005268 if (result == NULL)
5269 return -1;
5270
5271 if (set_string_copy(result, tv) == -1)
5272 return -1;
5273
5274 tv->v_type = VAR_STRING;
5275 }
5276 else if (PyUnicode_Check(obj))
5277 {
5278 PyObject *bytes;
5279 char_u *result;
5280
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005281 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005282 if (bytes == NULL)
5283 return -1;
5284
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005285 if(PyBytes_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005286 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005287 if (result == NULL)
5288 return -1;
5289
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005290 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005291 {
5292 Py_XDECREF(bytes);
5293 return -1;
5294 }
5295 Py_XDECREF(bytes);
5296
5297 tv->v_type = VAR_STRING;
5298 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005299#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005300 else if (PyInt_Check(obj))
5301 {
5302 tv->v_type = VAR_NUMBER;
5303 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
5304 }
5305#endif
5306 else if (PyLong_Check(obj))
5307 {
5308 tv->v_type = VAR_NUMBER;
5309 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
5310 }
5311 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005312 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005313#ifdef FEAT_FLOAT
5314 else if (PyFloat_Check(obj))
5315 {
5316 tv->v_type = VAR_FLOAT;
5317 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5318 }
5319#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005320 else if (PyObject_HasAttrString(obj, "keys"))
5321 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005322 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005323 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005324 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005325 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005326 else
5327 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02005328 PyErr_SetString(PyExc_TypeError,
5329 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005330 return -1;
5331 }
5332 return 0;
5333}
5334
5335 static PyObject *
5336ConvertToPyObject(typval_T *tv)
5337{
5338 if (tv == NULL)
5339 {
5340 PyErr_SetVim(_("NULL reference passed"));
5341 return NULL;
5342 }
5343 switch (tv->v_type)
5344 {
5345 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005346 return PyBytes_FromString(tv->vval.v_string == NULL
5347 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005348 case VAR_NUMBER:
5349 return PyLong_FromLong((long) tv->vval.v_number);
5350#ifdef FEAT_FLOAT
5351 case VAR_FLOAT:
5352 return PyFloat_FromDouble((double) tv->vval.v_float);
5353#endif
5354 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005355 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005356 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005357 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005358 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005359 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005360 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005361 case VAR_UNKNOWN:
5362 Py_INCREF(Py_None);
5363 return Py_None;
5364 default:
5365 PyErr_SetVim(_("internal error: invalid value type"));
5366 return NULL;
5367 }
5368}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005369
5370typedef struct
5371{
5372 PyObject_HEAD
5373} CurrentObject;
5374static PyTypeObject CurrentType;
5375
5376 static void
5377init_structs(void)
5378{
5379 vim_memset(&OutputType, 0, sizeof(OutputType));
5380 OutputType.tp_name = "vim.message";
5381 OutputType.tp_basicsize = sizeof(OutputObject);
5382 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5383 OutputType.tp_doc = "vim message object";
5384 OutputType.tp_methods = OutputMethods;
5385#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005386 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5387 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005388 OutputType.tp_alloc = call_PyType_GenericAlloc;
5389 OutputType.tp_new = call_PyType_GenericNew;
5390 OutputType.tp_free = call_PyObject_Free;
5391#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005392 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5393 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005394#endif
5395
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005396 vim_memset(&IterType, 0, sizeof(IterType));
5397 IterType.tp_name = "vim.iter";
5398 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005399 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005400 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005401 IterType.tp_iter = (getiterfunc)IterIter;
5402 IterType.tp_iternext = (iternextfunc)IterNext;
5403 IterType.tp_dealloc = (destructor)IterDestructor;
5404 IterType.tp_traverse = (traverseproc)IterTraverse;
5405 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005406
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005407 vim_memset(&BufferType, 0, sizeof(BufferType));
5408 BufferType.tp_name = "vim.buffer";
5409 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005410 BufferType.tp_dealloc = (destructor)BufferDestructor;
5411 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005412 BufferType.tp_as_sequence = &BufferAsSeq;
5413 BufferType.tp_as_mapping = &BufferAsMapping;
5414 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5415 BufferType.tp_doc = "vim buffer object";
5416 BufferType.tp_methods = BufferMethods;
5417#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005418 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005419 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005420 BufferType.tp_alloc = call_PyType_GenericAlloc;
5421 BufferType.tp_new = call_PyType_GenericNew;
5422 BufferType.tp_free = call_PyObject_Free;
5423#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005424 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005425 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005426#endif
5427
5428 vim_memset(&WindowType, 0, sizeof(WindowType));
5429 WindowType.tp_name = "vim.window";
5430 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005431 WindowType.tp_dealloc = (destructor)WindowDestructor;
5432 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005433 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005434 WindowType.tp_doc = "vim Window object";
5435 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005436 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5437 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005438#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005439 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5440 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005441 WindowType.tp_alloc = call_PyType_GenericAlloc;
5442 WindowType.tp_new = call_PyType_GenericNew;
5443 WindowType.tp_free = call_PyObject_Free;
5444#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005445 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5446 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005447#endif
5448
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005449 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5450 TabPageType.tp_name = "vim.tabpage";
5451 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005452 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5453 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005454 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5455 TabPageType.tp_doc = "vim tab page object";
5456 TabPageType.tp_methods = TabPageMethods;
5457#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005458 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005459 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5460 TabPageType.tp_new = call_PyType_GenericNew;
5461 TabPageType.tp_free = call_PyObject_Free;
5462#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005463 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005464#endif
5465
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005466 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5467 BufMapType.tp_name = "vim.bufferlist";
5468 BufMapType.tp_basicsize = sizeof(BufMapObject);
5469 BufMapType.tp_as_mapping = &BufMapAsMapping;
5470 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005471 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005472 BufferType.tp_doc = "vim buffer list";
5473
5474 vim_memset(&WinListType, 0, sizeof(WinListType));
5475 WinListType.tp_name = "vim.windowlist";
5476 WinListType.tp_basicsize = sizeof(WinListType);
5477 WinListType.tp_as_sequence = &WinListAsSeq;
5478 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5479 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005480 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005481
5482 vim_memset(&TabListType, 0, sizeof(TabListType));
5483 TabListType.tp_name = "vim.tabpagelist";
5484 TabListType.tp_basicsize = sizeof(TabListType);
5485 TabListType.tp_as_sequence = &TabListAsSeq;
5486 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5487 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005488
5489 vim_memset(&RangeType, 0, sizeof(RangeType));
5490 RangeType.tp_name = "vim.range";
5491 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005492 RangeType.tp_dealloc = (destructor)RangeDestructor;
5493 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005494 RangeType.tp_as_sequence = &RangeAsSeq;
5495 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005496 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005497 RangeType.tp_doc = "vim Range object";
5498 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005499 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5500 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005501#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005502 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005503 RangeType.tp_alloc = call_PyType_GenericAlloc;
5504 RangeType.tp_new = call_PyType_GenericNew;
5505 RangeType.tp_free = call_PyObject_Free;
5506#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005507 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005508#endif
5509
5510 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5511 CurrentType.tp_name = "vim.currentdata";
5512 CurrentType.tp_basicsize = sizeof(CurrentObject);
5513 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5514 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005515 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005516#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005517 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5518 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005519#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005520 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5521 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005522#endif
5523
5524 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5525 DictionaryType.tp_name = "vim.dictionary";
5526 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005527 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005528 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005529 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005530 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005531 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5532 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005533 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5534 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5535 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005536#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005537 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5538 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005539#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005540 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5541 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005542#endif
5543
5544 vim_memset(&ListType, 0, sizeof(ListType));
5545 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005546 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005547 ListType.tp_basicsize = sizeof(ListObject);
5548 ListType.tp_as_sequence = &ListAsSeq;
5549 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005550 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005551 ListType.tp_doc = "list pushing modifications to vim structure";
5552 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005553 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005554 ListType.tp_new = (newfunc)ListConstructor;
5555 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005556#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005557 ListType.tp_getattro = (getattrofunc)ListGetattro;
5558 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005559#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005560 ListType.tp_getattr = (getattrfunc)ListGetattr;
5561 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005562#endif
5563
5564 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005565 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005566 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005567 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5568 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005569 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005570 FunctionType.tp_doc = "object that calls vim function";
5571 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005572 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005573 FunctionType.tp_new = (newfunc)FunctionConstructor;
5574 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005575#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005576 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005577#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005578 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005579#endif
5580
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005581 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5582 OptionsType.tp_name = "vim.options";
5583 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005584 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005585 OptionsType.tp_doc = "object for manipulating options";
5586 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005587 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5588 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5589 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005590
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005591 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5592 LoaderType.tp_name = "vim.Loader";
5593 LoaderType.tp_basicsize = sizeof(LoaderObject);
5594 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5595 LoaderType.tp_doc = "vim message object";
5596 LoaderType.tp_methods = LoaderMethods;
5597 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5598
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005599#if PY_MAJOR_VERSION >= 3
5600 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5601 vimmodule.m_name = "vim";
5602 vimmodule.m_doc = "Vim Python interface\n";
5603 vimmodule.m_size = -1;
5604 vimmodule.m_methods = VimMethods;
5605#endif
5606}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005607
5608#define PYTYPE_READY(type) \
5609 if (PyType_Ready(&type)) \
5610 return -1;
5611
5612 static int
5613init_types()
5614{
5615 PYTYPE_READY(IterType);
5616 PYTYPE_READY(BufferType);
5617 PYTYPE_READY(RangeType);
5618 PYTYPE_READY(WindowType);
5619 PYTYPE_READY(TabPageType);
5620 PYTYPE_READY(BufMapType);
5621 PYTYPE_READY(WinListType);
5622 PYTYPE_READY(TabListType);
5623 PYTYPE_READY(CurrentType);
5624 PYTYPE_READY(DictionaryType);
5625 PYTYPE_READY(ListType);
5626 PYTYPE_READY(FunctionType);
5627 PYTYPE_READY(OptionsType);
5628 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005629 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005630 return 0;
5631}
5632
5633 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005634init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005635{
5636 PyObject *path;
5637 PyObject *path_hook;
5638 PyObject *path_hooks;
5639
5640 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5641 return -1;
5642
5643 if (!(path_hooks = PySys_GetObject("path_hooks")))
5644 {
5645 PyErr_Clear();
5646 path_hooks = PyList_New(1);
5647 PyList_SET_ITEM(path_hooks, 0, path_hook);
5648 if (PySys_SetObject("path_hooks", path_hooks))
5649 {
5650 Py_DECREF(path_hooks);
5651 return -1;
5652 }
5653 Py_DECREF(path_hooks);
5654 }
5655 else if (PyList_Check(path_hooks))
5656 {
5657 if (PyList_Append(path_hooks, path_hook))
5658 {
5659 Py_DECREF(path_hook);
5660 return -1;
5661 }
5662 Py_DECREF(path_hook);
5663 }
5664 else
5665 {
5666 VimTryStart();
5667 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5668 "You should now do the following:\n"
5669 "- append vim.path_hook to sys.path_hooks\n"
5670 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5671 VimTryEnd(); /* Discard the error */
5672 Py_DECREF(path_hook);
5673 return 0;
5674 }
5675
5676 if (!(path = PySys_GetObject("path")))
5677 {
5678 PyErr_Clear();
5679 path = PyList_New(1);
5680 Py_INCREF(vim_special_path_object);
5681 PyList_SET_ITEM(path, 0, vim_special_path_object);
5682 if (PySys_SetObject("path", path))
5683 {
5684 Py_DECREF(path);
5685 return -1;
5686 }
5687 Py_DECREF(path);
5688 }
5689 else if (PyList_Check(path))
5690 {
5691 if (PyList_Append(path, vim_special_path_object))
5692 return -1;
5693 }
5694 else
5695 {
5696 VimTryStart();
5697 EMSG(_("Failed to set path: sys.path is not a list\n"
5698 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5699 VimTryEnd(); /* Discard the error */
5700 }
5701
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005702 return 0;
5703}
5704
5705static BufMapObject TheBufferMap =
5706{
5707 PyObject_HEAD_INIT(&BufMapType)
5708};
5709
5710static WinListObject TheWindowList =
5711{
5712 PyObject_HEAD_INIT(&WinListType)
5713 NULL
5714};
5715
5716static CurrentObject TheCurrent =
5717{
5718 PyObject_HEAD_INIT(&CurrentType)
5719};
5720
5721static TabListObject TheTabPageList =
5722{
5723 PyObject_HEAD_INIT(&TabListType)
5724};
5725
5726static struct numeric_constant {
5727 char *name;
5728 int value;
5729} numeric_constants[] = {
5730 {"VAR_LOCKED", VAR_LOCKED},
5731 {"VAR_FIXED", VAR_FIXED},
5732 {"VAR_SCOPE", VAR_SCOPE},
5733 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5734};
5735
5736static struct object_constant {
5737 char *name;
5738 PyObject *value;
5739} object_constants[] = {
5740 {"buffers", (PyObject *)(void *)&TheBufferMap},
5741 {"windows", (PyObject *)(void *)&TheWindowList},
5742 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5743 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005744
5745 {"Buffer", (PyObject *)&BufferType},
5746 {"Range", (PyObject *)&RangeType},
5747 {"Window", (PyObject *)&WindowType},
5748 {"TabPage", (PyObject *)&TabPageType},
5749 {"Dictionary", (PyObject *)&DictionaryType},
5750 {"List", (PyObject *)&ListType},
5751 {"Function", (PyObject *)&FunctionType},
5752 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005753 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005754};
5755
5756typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005757typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005758
5759#define ADD_OBJECT(m, name, obj) \
5760 if (add_object(m, name, obj)) \
5761 return -1;
5762
5763#define ADD_CHECKED_OBJECT(m, name, obj) \
5764 { \
5765 PyObject *value = obj; \
5766 if (!value) \
5767 return -1; \
5768 ADD_OBJECT(m, name, value); \
5769 }
5770
5771 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005772populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005773{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005774 int i;
5775 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005776 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005777 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005778
5779 for (i = 0; i < (int)(sizeof(numeric_constants)
5780 / sizeof(struct numeric_constant));
5781 ++i)
5782 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5783 PyInt_FromLong(numeric_constants[i].value));
5784
5785 for (i = 0; i < (int)(sizeof(object_constants)
5786 / sizeof(struct object_constant));
5787 ++i)
5788 {
5789 PyObject *value;
5790
5791 value = object_constants[i].value;
5792 Py_INCREF(value);
5793 ADD_OBJECT(m, object_constants[i].name, value);
5794 }
5795
5796 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5797 return -1;
5798 ADD_OBJECT(m, "error", VimError);
5799
Bram Moolenaara9922d62013-05-30 13:01:18 +02005800 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5801 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005802 ADD_CHECKED_OBJECT(m, "options",
5803 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005804
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005805 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005806 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005807 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005808
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005809 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005810 return -1;
5811 ADD_OBJECT(m, "_getcwd", py_getcwd)
5812
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005813 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005814 return -1;
5815 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005816 if (!(attr = get_attr(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005817 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005818 if (PyObject_SetAttrString(other_module, "chdir", attr))
5819 {
5820 Py_DECREF(attr);
5821 return -1;
5822 }
5823 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005824
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005825 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005826 {
5827 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005828 if (!(attr = get_attr(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005829 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005830 if (PyObject_SetAttrString(other_module, "fchdir", attr))
5831 {
5832 Py_DECREF(attr);
5833 return -1;
5834 }
5835 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005836 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02005837 else
5838 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02005839
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005840 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
5841 return -1;
5842
5843 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
5844
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005845 if (!(imp = PyImport_ImportModule("imp")))
5846 return -1;
5847
5848 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
5849 {
5850 Py_DECREF(imp);
5851 return -1;
5852 }
5853
5854 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
5855 {
5856 Py_DECREF(py_find_module);
5857 Py_DECREF(imp);
5858 return -1;
5859 }
5860
5861 Py_DECREF(imp);
5862
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005863 ADD_OBJECT(m, "_find_module", py_find_module);
5864 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005865
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005866 return 0;
5867}