blob: bd1d70434f6a27cd08b32c7528a02e486f6b4dd1 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020016#if PY_VERSION_HEX < 0x02050000
17typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
18#endif
19
Bram Moolenaar91805fc2011-06-26 04:01:44 +020020#ifdef FEAT_MBYTE
21# define ENC_OPT p_enc
22#else
23# define ENC_OPT "latin1"
24#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020025#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020026
Bram 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 Moolenaar170bf1a2010-07-24 23:51:45 +020063/*
64 * obtain a lock on the Vim data structures
65 */
66 static void
67Python_Lock_Vim(void)
68{
69}
70
71/*
72 * release a lock on the Vim data structures
73 */
74 static void
75Python_Release_Vim(void)
76{
77}
78
Bram Moolenaare9ba5162013-05-29 22:02:22 +020079/*
80 * The "todecref" argument holds a pointer to PyObject * that must be
81 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
82 * was needed to generate returned value is object.
83 *
84 * Use Py_XDECREF to decrement reference count.
85 */
86 static char_u *
87StringToChars(PyObject *object, PyObject **todecref)
88{
89 char_u *p;
90 PyObject *bytes = NULL;
91
92 if (PyBytes_Check(object))
93 {
94
95 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
96 return NULL;
97 if (p == NULL)
98 return NULL;
99
100 *todecref = NULL;
101 }
102 else if (PyUnicode_Check(object))
103 {
104 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
105 if (bytes == NULL)
106 return NULL;
107
108 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
109 return NULL;
110 if (p == NULL)
111 return NULL;
112
113 *todecref = bytes;
114 }
115 else
116 {
117 PyErr_SetString(PyExc_TypeError, _("object must be string"));
118 return NULL;
119 }
120
121 return (char_u *) p;
122}
123
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200124 static int
125add_string(PyObject *list, char *s)
126{
127 PyObject *string;
128
129 if (!(string = PyString_FromString(s)))
130 return -1;
131 if (PyList_Append(list, string))
132 {
133 Py_DECREF(string);
134 return -1;
135 }
136
137 Py_DECREF(string);
138 return 0;
139}
140
141 static PyObject *
142ObjectDir(PyObject *self, char **attributes)
143{
144 PyMethodDef *method;
145 char **attr;
146 PyObject *r;
147
148 if (!(r = PyList_New(0)))
149 return NULL;
150
151 if (self)
152 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
153 if (add_string(r, (char *) method->ml_name))
154 {
155 Py_DECREF(r);
156 return NULL;
157 }
158
159 for (attr = attributes ; *attr ; ++attr)
160 if (add_string(r, *attr))
161 {
162 Py_DECREF(r);
163 return NULL;
164 }
165
166#if PY_MAJOR_VERSION < 3
167 if (add_string(r, "__members__"))
168 {
169 Py_DECREF(r);
170 return NULL;
171 }
172#endif
173
174 return r;
175}
176
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200177/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200178 */
179
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200180/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200181typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200182
183static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200184
185typedef struct
186{
187 PyObject_HEAD
188 long softspace;
189 long error;
190} OutputObject;
191
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200192static char *OutputAttrs[] = {
193 "softspace",
194 NULL
195};
196
197 static PyObject *
198OutputDir(PyObject *self)
199{
200 return ObjectDir(self, OutputAttrs);
201}
202
Bram Moolenaar77045652012-09-21 13:46:06 +0200203 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200204OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200205{
206 if (val == NULL)
207 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200208 PyErr_SetString(PyExc_AttributeError,
209 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200210 return -1;
211 }
212
213 if (strcmp(name, "softspace") == 0)
214 {
215 if (!PyInt_Check(val))
216 {
217 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
218 return -1;
219 }
220
Bram Moolenaard6e39182013-05-21 18:30:34 +0200221 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200222 return 0;
223 }
224
225 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
226 return -1;
227}
228
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200229/* Buffer IO, we write one whole line at a time. */
230static garray_T io_ga = {0, 0, 1, 80, NULL};
231static writefn old_fn = NULL;
232
233 static void
234PythonIO_Flush(void)
235{
236 if (old_fn != NULL && io_ga.ga_len > 0)
237 {
238 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
239 old_fn((char_u *)io_ga.ga_data);
240 }
241 io_ga.ga_len = 0;
242}
243
244 static void
245writer(writefn fn, char_u *str, PyInt n)
246{
247 char_u *ptr;
248
249 /* Flush when switching output function. */
250 if (fn != old_fn)
251 PythonIO_Flush();
252 old_fn = fn;
253
254 /* Write each NL separated line. Text after the last NL is kept for
255 * writing later. */
256 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
257 {
258 PyInt len = ptr - str;
259
260 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
261 break;
262
263 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
264 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
265 fn((char_u *)io_ga.ga_data);
266 str = ptr + 1;
267 n -= len + 1;
268 io_ga.ga_len = 0;
269 }
270
271 /* Put the remaining text into io_ga for later printing. */
272 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
273 {
274 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
275 io_ga.ga_len += (int)n;
276 }
277}
278
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200279 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200280OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200281{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200282 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200283 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200284 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200285
Bram Moolenaar27564802011-09-07 19:30:21 +0200286 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200287 return NULL;
288
289 Py_BEGIN_ALLOW_THREADS
290 Python_Lock_Vim();
291 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
292 Python_Release_Vim();
293 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200294 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200295
296 Py_INCREF(Py_None);
297 return Py_None;
298}
299
300 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200301OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200302{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200303 PyObject *seq;
304 PyObject *iterator;
305 PyObject *item;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200306 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200307
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200308 if (!PyArg_ParseTuple(args, "O", &seq))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200309 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200310
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200311 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200312 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200313
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200314 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200316 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200317 PyInt len;
318
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200319 if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
Bram Moolenaardb913952012-06-29 12:54:53 +0200320 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200321 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200322 Py_DECREF(iterator);
323 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200324 return NULL;
325 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200326 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200327
328 Py_BEGIN_ALLOW_THREADS
329 Python_Lock_Vim();
330 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
331 Python_Release_Vim();
332 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200333 PyMem_Free(str);
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 */
358 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
359 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
360 {"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
396
397static PyObject *VimError;
398
399/* Check to see whether a Vim error has been reported, or a keyboard
400 * interrupt has been detected.
401 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200402
403 static void
404VimTryStart(void)
405{
406 ++trylevel;
407}
408
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200409 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200410VimTryEnd(void)
411{
412 --trylevel;
413 if (got_int)
414 {
415 PyErr_SetNone(PyExc_KeyboardInterrupt);
416 return 1;
417 }
418 else if (!did_throw)
419 return 0;
420 else if (PyErr_Occurred())
421 return 1;
422 else
423 {
424 PyErr_SetVim((char *) current_exception->value);
425 discard_current_exception();
426 return 1;
427 }
428}
429
430 static int
431VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200432{
433 if (got_int)
434 {
435 PyErr_SetNone(PyExc_KeyboardInterrupt);
436 return 1;
437 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200438 return 0;
439}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200440
441/* Vim module - Implementation
442 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200443
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200444 static PyObject *
445VimCommand(PyObject *self UNUSED, PyObject *args)
446{
447 char *cmd;
448 PyObject *result;
449
450 if (!PyArg_ParseTuple(args, "s", &cmd))
451 return NULL;
452
453 PyErr_Clear();
454
455 Py_BEGIN_ALLOW_THREADS
456 Python_Lock_Vim();
457
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200458 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200459 do_cmdline_cmd((char_u *)cmd);
460 update_screen(VALID);
461
462 Python_Release_Vim();
463 Py_END_ALLOW_THREADS
464
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200465 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200466 result = NULL;
467 else
468 result = Py_None;
469
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200470
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200471 Py_XINCREF(result);
472 return result;
473}
474
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200475/*
476 * Function to translate a typval_T into a PyObject; this will recursively
477 * translate lists/dictionaries into their Python equivalents.
478 *
479 * The depth parameter is to avoid infinite recursion, set it to 1 when
480 * you call VimToPython.
481 */
482 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200483VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200484{
485 PyObject *result;
486 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200487 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200488
489 /* Avoid infinite recursion */
490 if (depth > 100)
491 {
492 Py_INCREF(Py_None);
493 result = Py_None;
494 return result;
495 }
496
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200497 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200498 * then and we can use it again. */
499 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
500 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
501 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200502 sprintf(ptrBuf, "%p",
503 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
504 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200505
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200506 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200507 {
508 Py_INCREF(result);
509 return result;
510 }
511 }
512
513 if (our_tv->v_type == VAR_STRING)
514 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200515 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200516 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200517 }
518 else if (our_tv->v_type == VAR_NUMBER)
519 {
520 char buf[NUMBUFLEN];
521
522 /* For backwards compatibility numbers are stored as strings. */
523 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200524 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200525 }
526# ifdef FEAT_FLOAT
527 else if (our_tv->v_type == VAR_FLOAT)
528 {
529 char buf[NUMBUFLEN];
530
531 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200532 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200533 }
534# endif
535 else if (our_tv->v_type == VAR_LIST)
536 {
537 list_T *list = our_tv->vval.v_list;
538 listitem_T *curr;
539
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200540 if (list == NULL)
541 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200542
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200543 if (!(result = PyList_New(0)))
544 return NULL;
545
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200546 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200547 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200548 Py_DECREF(result);
549 return NULL;
550 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200551
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200552 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
553 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200554 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200555 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200556 Py_DECREF(result);
557 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200558 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200559 if (PyList_Append(result, newObj))
560 {
561 Py_DECREF(newObj);
562 Py_DECREF(result);
563 return NULL;
564 }
565 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200566 }
567 }
568 else if (our_tv->v_type == VAR_DICT)
569 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200570
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200571 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
572 long_u todo = ht->ht_used;
573 hashitem_T *hi;
574 dictitem_T *di;
575 if (our_tv->vval.v_dict == NULL)
576 return NULL;
577
578 if (!(result = PyDict_New()))
579 return NULL;
580
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200581 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200582 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200583 Py_DECREF(result);
584 return NULL;
585 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200586
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200587 for (hi = ht->ht_array; todo > 0; ++hi)
588 {
589 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200590 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200591 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200592
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200593 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200594 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200595 {
596 Py_DECREF(result);
597 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200598 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200599 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
600 {
601 Py_DECREF(result);
602 Py_DECREF(newObj);
603 return NULL;
604 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200605 }
606 }
607 }
608 else
609 {
610 Py_INCREF(Py_None);
611 result = Py_None;
612 }
613
614 return result;
615}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200616
617 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200618VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200619{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200620 char *expr;
621 typval_T *our_tv;
622 PyObject *result;
623 PyObject *lookup_dict;
624
625 if (!PyArg_ParseTuple(args, "s", &expr))
626 return NULL;
627
628 Py_BEGIN_ALLOW_THREADS
629 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200630 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200631 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200632 Python_Release_Vim();
633 Py_END_ALLOW_THREADS
634
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200635 if (VimTryEnd())
636 return NULL;
637
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200638 if (our_tv == NULL)
639 {
640 PyErr_SetVim(_("invalid expression"));
641 return NULL;
642 }
643
644 /* Convert the Vim type into a Python type. Create a dictionary that's
645 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200646 if (!(lookup_dict = PyDict_New()))
647 result = NULL;
648 else
649 {
650 result = VimToPython(our_tv, 1, lookup_dict);
651 Py_DECREF(lookup_dict);
652 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200653
654
655 Py_BEGIN_ALLOW_THREADS
656 Python_Lock_Vim();
657 free_tv(our_tv);
658 Python_Release_Vim();
659 Py_END_ALLOW_THREADS
660
661 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200662}
663
Bram Moolenaardb913952012-06-29 12:54:53 +0200664static PyObject *ConvertToPyObject(typval_T *);
665
666 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200667VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200668{
Bram Moolenaardb913952012-06-29 12:54:53 +0200669 char *expr;
670 typval_T *our_tv;
671 PyObject *result;
672
673 if (!PyArg_ParseTuple(args, "s", &expr))
674 return NULL;
675
676 Py_BEGIN_ALLOW_THREADS
677 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200678 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200679 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200680 Python_Release_Vim();
681 Py_END_ALLOW_THREADS
682
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200683 if (VimTryEnd())
684 return NULL;
685
Bram Moolenaardb913952012-06-29 12:54:53 +0200686 if (our_tv == NULL)
687 {
688 PyErr_SetVim(_("invalid expression"));
689 return NULL;
690 }
691
692 result = ConvertToPyObject(our_tv);
693 Py_BEGIN_ALLOW_THREADS
694 Python_Lock_Vim();
695 free_tv(our_tv);
696 Python_Release_Vim();
697 Py_END_ALLOW_THREADS
698
699 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200700}
701
702 static PyObject *
703VimStrwidth(PyObject *self UNUSED, PyObject *args)
704{
705 char *expr;
706
707 if (!PyArg_ParseTuple(args, "s", &expr))
708 return NULL;
709
Bram Moolenaara54bf402012-12-05 16:30:07 +0100710 return PyLong_FromLong(
711#ifdef FEAT_MBYTE
712 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
713#else
714 STRLEN(expr)
715#endif
716 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200717}
718
Bram Moolenaarf4258302013-06-02 18:20:17 +0200719 static PyObject *
720_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
721{
722 PyObject *r;
723 PyObject *newwd;
724 PyObject *todecref;
725 char_u *new_dir;
726
Bram Moolenaard4209d22013-06-05 20:34:15 +0200727 if (_chdir == NULL)
728 return NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200729 if (!(r = PyObject_Call(_chdir, args, kwargs)))
730 return NULL;
731
732 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
733 {
734 Py_DECREF(r);
735 return NULL;
736 }
737
738 if (!(new_dir = StringToChars(newwd, &todecref)))
739 {
740 Py_DECREF(r);
741 Py_DECREF(newwd);
742 return NULL;
743 }
744
745 VimTryStart();
746
747 if (vim_chdir(new_dir))
748 {
749 Py_DECREF(r);
750 Py_DECREF(newwd);
751 Py_XDECREF(todecref);
752
753 if (VimTryEnd())
754 return NULL;
755
756 PyErr_SetVim(_("failed to change directory"));
757 return NULL;
758 }
759
760 Py_DECREF(newwd);
761 Py_XDECREF(todecref);
762
763 post_chdir(FALSE);
764
765 if (VimTryEnd())
766 {
767 Py_DECREF(r);
768 return NULL;
769 }
770
771 return r;
772}
773
774 static PyObject *
775VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
776{
777 return _VimChdir(py_chdir, args, kwargs);
778}
779
780 static PyObject *
781VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
782{
783 return _VimChdir(py_fchdir, args, kwargs);
784}
785
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200786typedef struct {
787 PyObject *callable;
788 PyObject *result;
789} map_rtp_data;
790
791 static void
792map_rtp_callback(char_u *path, void *_data)
793{
794 void **data = (void **) _data;
795 PyObject *pathObject;
796 map_rtp_data *mr_data = *((map_rtp_data **) data);
797
798 if (!(pathObject = PyString_FromString((char *) path)))
799 {
800 *data = NULL;
801 return;
802 }
803
804 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
805 pathObject, NULL);
806
807 Py_DECREF(pathObject);
808
809 if (!mr_data->result || mr_data->result != Py_None)
810 *data = NULL;
811 else
812 {
813 Py_DECREF(mr_data->result);
814 mr_data->result = NULL;
815 }
816}
817
818 static PyObject *
819VimForeachRTP(PyObject *self UNUSED, PyObject *args)
820{
821 map_rtp_data data;
822
823 if (!PyArg_ParseTuple(args, "O", &data.callable))
824 return NULL;
825
826 data.result = NULL;
827
828 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
829
830 if (data.result == NULL)
831 {
832 if (PyErr_Occurred())
833 return NULL;
834 else
835 {
836 Py_INCREF(Py_None);
837 return Py_None;
838 }
839 }
840 return data.result;
841}
842
843/*
844 * _vim_runtimepath_ special path implementation.
845 */
846
847 static void
848map_finder_callback(char_u *path, void *_data)
849{
850 void **data = (void **) _data;
851 PyObject *list = *((PyObject **) data);
852 PyObject *pathObject1, *pathObject2;
853 char *pathbuf;
854 size_t pathlen;
855
856 pathlen = STRLEN(path);
857
858#if PY_MAJOR_VERSION < 3
859# define PY_MAIN_DIR_STRING "python2"
860#else
861# define PY_MAIN_DIR_STRING "python3"
862#endif
863#define PY_ALTERNATE_DIR_STRING "pythonx"
864
865#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
866 if (!(pathbuf = PyMem_New(char,
867 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
868 {
869 PyErr_NoMemory();
870 *data = NULL;
871 return;
872 }
873
874 mch_memmove(pathbuf, path, pathlen + 1);
875 add_pathsep((char_u *) pathbuf);
876
877 pathlen = STRLEN(pathbuf);
878 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
879 PYTHONX_STRING_LENGTH + 1);
880
881 if (!(pathObject1 = PyString_FromString(pathbuf)))
882 {
883 *data = NULL;
884 PyMem_Free(pathbuf);
885 return;
886 }
887
888 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
889 PYTHONX_STRING_LENGTH + 1);
890
891 if (!(pathObject2 = PyString_FromString(pathbuf)))
892 {
893 Py_DECREF(pathObject1);
894 PyMem_Free(pathbuf);
895 *data = NULL;
896 return;
897 }
898
899 PyMem_Free(pathbuf);
900
901 if (PyList_Append(list, pathObject1)
902 || PyList_Append(list, pathObject2))
903 *data = NULL;
904
905 Py_DECREF(pathObject1);
906 Py_DECREF(pathObject2);
907}
908
909 static PyObject *
910Vim_GetPaths(PyObject *self UNUSED)
911{
912 PyObject *r;
913
914 if (!(r = PyList_New(0)))
915 return NULL;
916
917 do_in_runtimepath(NULL, FALSE, &map_finder_callback, r);
918
919 if (PyErr_Occurred())
920 {
921 Py_DECREF(r);
922 return NULL;
923 }
924
925 return r;
926}
927
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200928/*
929 * Vim module - Definitions
930 */
931
932static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200933 /* name, function, calling, documentation */
934 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
935 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
936 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
937 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
938 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
939 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
940 {"foreach_rtp", VimForeachRTP, METH_VARARGS, "Call given callable for each path in &rtp"},
941#if PY_MAJOR_VERSION < 3
942 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200943#endif
944 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
945 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
946 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200947};
948
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200949/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200950 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200951 */
952
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200953static PyTypeObject IterType;
954
955typedef PyObject *(*nextfun)(void **);
956typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200957typedef int (*traversefun)(void *, visitproc, void *);
958typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200959
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200960/* Main purpose of this object is removing the need for do python
961 * initialization (i.e. PyType_Ready and setting type attributes) for a big
962 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200963
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200964typedef struct
965{
966 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200967 void *cur;
968 nextfun next;
969 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200970 traversefun traverse;
971 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200972} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200973
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200974 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200975IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
976 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200977{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200978 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200979
Bram Moolenaar774267b2013-05-21 20:51:59 +0200980 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200981 self->cur = start;
982 self->next = next;
983 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200984 self->traverse = traverse;
985 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200986
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200987 return (PyObject *)(self);
988}
989
990 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200991IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200992{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200993 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200994 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200995 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200996}
997
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200998 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200999IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001000{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001001 if (self->traverse != NULL)
1002 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001003 else
1004 return 0;
1005}
1006
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001007/* Mac OSX defines clear() somewhere. */
1008#ifdef clear
1009# undef clear
1010#endif
1011
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001012 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001013IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001014{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001015 if (self->clear != NULL)
1016 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001017 else
1018 return 0;
1019}
1020
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001021 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001022IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001023{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001024 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001025}
1026
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001027 static PyObject *
1028IterIter(PyObject *self)
1029{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001030 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001031 return self;
1032}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001033
Bram Moolenaardb913952012-06-29 12:54:53 +02001034typedef struct pylinkedlist_S {
1035 struct pylinkedlist_S *pll_next;
1036 struct pylinkedlist_S *pll_prev;
1037 PyObject *pll_obj;
1038} pylinkedlist_T;
1039
1040static pylinkedlist_T *lastdict = NULL;
1041static pylinkedlist_T *lastlist = NULL;
1042
1043 static void
1044pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1045{
1046 if (ref->pll_prev == NULL)
1047 {
1048 if (ref->pll_next == NULL)
1049 {
1050 *last = NULL;
1051 return;
1052 }
1053 }
1054 else
1055 ref->pll_prev->pll_next = ref->pll_next;
1056
1057 if (ref->pll_next == NULL)
1058 *last = ref->pll_prev;
1059 else
1060 ref->pll_next->pll_prev = ref->pll_prev;
1061}
1062
1063 static void
1064pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1065{
1066 if (*last == NULL)
1067 ref->pll_prev = NULL;
1068 else
1069 {
1070 (*last)->pll_next = ref;
1071 ref->pll_prev = *last;
1072 }
1073 ref->pll_next = NULL;
1074 ref->pll_obj = self;
1075 *last = ref;
1076}
1077
1078static PyTypeObject DictionaryType;
1079
1080typedef struct
1081{
1082 PyObject_HEAD
1083 dict_T *dict;
1084 pylinkedlist_T ref;
1085} DictionaryObject;
1086
Bram Moolenaara9922d62013-05-30 13:01:18 +02001087static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1088
1089#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1090
Bram Moolenaardb913952012-06-29 12:54:53 +02001091 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001092DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001093{
1094 DictionaryObject *self;
1095
Bram Moolenaara9922d62013-05-30 13:01:18 +02001096 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001097 if (self == NULL)
1098 return NULL;
1099 self->dict = dict;
1100 ++dict->dv_refcount;
1101
1102 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1103
1104 return (PyObject *)(self);
1105}
1106
Bram Moolenaara9922d62013-05-30 13:01:18 +02001107 static dict_T *
1108py_dict_alloc()
1109{
1110 dict_T *r;
1111
1112 if (!(r = dict_alloc()))
1113 {
1114 PyErr_NoMemory();
1115 return NULL;
1116 }
1117 ++r->dv_refcount;
1118
1119 return r;
1120}
1121
1122 static PyObject *
1123DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1124{
1125 DictionaryObject *self;
1126 dict_T *dict;
1127
1128 if (!(dict = py_dict_alloc()))
1129 return NULL;
1130
1131 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1132
1133 --dict->dv_refcount;
1134
1135 if (kwargs || PyTuple_Size(args))
1136 {
1137 PyObject *tmp;
1138 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1139 {
1140 Py_DECREF(self);
1141 return NULL;
1142 }
1143
1144 Py_DECREF(tmp);
1145 }
1146
1147 return (PyObject *)(self);
1148}
1149
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001150 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001151DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001152{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001153 pyll_remove(&self->ref, &lastdict);
1154 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001155
1156 DESTRUCTOR_FINISH(self);
1157}
1158
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001159static char *DictionaryAttrs[] = {
1160 "locked", "scope",
1161 NULL
1162};
1163
1164 static PyObject *
1165DictionaryDir(PyObject *self)
1166{
1167 return ObjectDir(self, DictionaryAttrs);
1168}
1169
Bram Moolenaardb913952012-06-29 12:54:53 +02001170 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001171DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001172{
1173 if (val == NULL)
1174 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001175 PyErr_SetString(PyExc_AttributeError,
1176 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001177 return -1;
1178 }
1179
1180 if (strcmp(name, "locked") == 0)
1181 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001182 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001183 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001184 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001185 return -1;
1186 }
1187 else
1188 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001189 int istrue = PyObject_IsTrue(val);
1190 if (istrue == -1)
1191 return -1;
1192 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001193 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001194 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001195 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001196 }
1197 return 0;
1198 }
1199 else
1200 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001201 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001202 return -1;
1203 }
1204}
1205
1206 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001207DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001208{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001209 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001210}
1211
Bram Moolenaara9922d62013-05-30 13:01:18 +02001212#define DICT_FLAG_HAS_DEFAULT 0x01
1213#define DICT_FLAG_POP 0x02
1214#define DICT_FLAG_NONE_DEFAULT 0x04
1215#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1216#define DICT_FLAG_RETURN_PAIR 0x10
1217
Bram Moolenaardb913952012-06-29 12:54:53 +02001218 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001219_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001220{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001221 PyObject *keyObject;
1222 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1223 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001224 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001225 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001226 dict_T *dict = self->dict;
1227 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001228 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001229
Bram Moolenaara9922d62013-05-30 13:01:18 +02001230 if (flags & DICT_FLAG_HAS_DEFAULT)
1231 {
1232 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1233 return NULL;
1234 }
1235 else
1236 keyObject = args;
1237
1238 if (flags & DICT_FLAG_RETURN_BOOL)
1239 defObject = Py_False;
1240
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001241 if (!(key = StringToChars(keyObject, &todecref)))
1242 return NULL;
1243
1244 if (*key == NUL)
1245 {
1246 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001247 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001248 return NULL;
1249 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001250
Bram Moolenaara9922d62013-05-30 13:01:18 +02001251 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001252
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001253 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001254
Bram Moolenaara9922d62013-05-30 13:01:18 +02001255 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001256 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001257 if (defObject)
1258 {
1259 Py_INCREF(defObject);
1260 return defObject;
1261 }
1262 else
1263 {
1264 PyErr_SetObject(PyExc_KeyError, keyObject);
1265 return NULL;
1266 }
1267 }
1268 else if (flags & DICT_FLAG_RETURN_BOOL)
1269 {
1270 Py_INCREF(Py_True);
1271 return Py_True;
1272 }
1273
1274 di = dict_lookup(hi);
1275
1276 if (!(r = ConvertToPyObject(&di->di_tv)))
1277 return NULL;
1278
1279 if (flags & DICT_FLAG_POP)
1280 {
1281 if (dict->dv_lock)
1282 {
1283 PyErr_SetVim(_("dict is locked"));
1284 Py_DECREF(r);
1285 return NULL;
1286 }
1287
1288 hash_remove(&dict->dv_hashtab, hi);
1289 dictitem_free(di);
1290 }
1291
Bram Moolenaara9922d62013-05-30 13:01:18 +02001292 return r;
1293}
1294
1295 static PyObject *
1296DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1297{
1298 return _DictionaryItem(self, keyObject, 0);
1299}
1300
1301 static int
1302DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1303{
1304 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1305 int r;
1306
1307 r = (rObj == Py_True);
1308
1309 Py_DECREF(Py_True);
1310
1311 return r;
1312}
1313
1314typedef struct
1315{
1316 hashitem_T *ht_array;
1317 long_u ht_used;
1318 hashtab_T *ht;
1319 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001320 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001321} dictiterinfo_T;
1322
1323 static PyObject *
1324DictionaryIterNext(dictiterinfo_T **dii)
1325{
1326 PyObject *r;
1327
1328 if (!(*dii)->todo)
1329 return NULL;
1330
1331 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1332 (*dii)->ht->ht_used != (*dii)->ht_used)
1333 {
1334 PyErr_SetString(PyExc_RuntimeError,
1335 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001336 return NULL;
1337 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001338
Bram Moolenaara9922d62013-05-30 13:01:18 +02001339 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1340 ++((*dii)->hi);
1341
1342 --((*dii)->todo);
1343
1344 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1345 return NULL;
1346
1347 return r;
1348}
1349
1350 static PyObject *
1351DictionaryIter(DictionaryObject *self)
1352{
1353 dictiterinfo_T *dii;
1354 hashtab_T *ht;
1355
1356 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1357 {
1358 PyErr_NoMemory();
1359 return NULL;
1360 }
1361
1362 ht = &self->dict->dv_hashtab;
1363 dii->ht_array = ht->ht_array;
1364 dii->ht_used = ht->ht_used;
1365 dii->ht = ht;
1366 dii->hi = dii->ht_array;
1367 dii->todo = dii->ht_used;
1368
1369 return IterNew(dii,
1370 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1371 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001372}
1373
1374 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001375DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001376{
1377 char_u *key;
1378 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001379 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001380 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001381 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001382
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001383 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001384 {
1385 PyErr_SetVim(_("dict is locked"));
1386 return -1;
1387 }
1388
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001389 if (!(key = StringToChars(keyObject, &todecref)))
1390 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001391
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001392 if (*key == NUL)
1393 {
1394 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001395 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001396 return -1;
1397 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001398
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001399 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001400
1401 if (valObject == NULL)
1402 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001403 hashitem_T *hi;
1404
Bram Moolenaardb913952012-06-29 12:54:53 +02001405 if (di == NULL)
1406 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001407 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001408 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001409 return -1;
1410 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001411 hi = hash_find(&dict->dv_hashtab, di->di_key);
1412 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001413 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001414 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001415 return 0;
1416 }
1417
1418 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001419 {
1420 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001421 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001422 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001423
1424 if (di == NULL)
1425 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001426 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001427 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001428 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001429 PyErr_NoMemory();
1430 return -1;
1431 }
1432 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001433 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001434
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001435 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001436 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001437 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001438 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001439 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001440 PyErr_SetVim(_("failed to add key to dictionary"));
1441 return -1;
1442 }
1443 }
1444 else
1445 clear_tv(&di->di_tv);
1446
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001447 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001448
1449 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001450 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001451 return 0;
1452}
1453
Bram Moolenaara9922d62013-05-30 13:01:18 +02001454typedef PyObject *(*hi_to_py)(hashitem_T *);
1455
Bram Moolenaardb913952012-06-29 12:54:53 +02001456 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001457DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001458{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001459 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001460 long_u todo = dict->dv_hashtab.ht_used;
1461 Py_ssize_t i = 0;
1462 PyObject *r;
1463 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001464 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001465
1466 r = PyList_New(todo);
1467 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1468 {
1469 if (!HASHITEM_EMPTY(hi))
1470 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001471 if (!(newObj = hiconvert(hi)))
1472 {
1473 Py_DECREF(r);
1474 return NULL;
1475 }
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02001476 PyList_SET_ITEM(r, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001477 --todo;
1478 ++i;
1479 }
1480 }
1481 return r;
1482}
1483
Bram Moolenaara9922d62013-05-30 13:01:18 +02001484 static PyObject *
1485dict_key(hashitem_T *hi)
1486{
1487 return PyBytes_FromString((char *)(hi->hi_key));
1488}
1489
1490 static PyObject *
1491DictionaryListKeys(DictionaryObject *self)
1492{
1493 return DictionaryListObjects(self, dict_key);
1494}
1495
1496 static PyObject *
1497dict_val(hashitem_T *hi)
1498{
1499 dictitem_T *di;
1500
1501 di = dict_lookup(hi);
1502 return ConvertToPyObject(&di->di_tv);
1503}
1504
1505 static PyObject *
1506DictionaryListValues(DictionaryObject *self)
1507{
1508 return DictionaryListObjects(self, dict_val);
1509}
1510
1511 static PyObject *
1512dict_item(hashitem_T *hi)
1513{
1514 PyObject *keyObject;
1515 PyObject *valObject;
1516 PyObject *r;
1517
1518 if (!(keyObject = dict_key(hi)))
1519 return NULL;
1520
1521 if (!(valObject = dict_val(hi)))
1522 {
1523 Py_DECREF(keyObject);
1524 return NULL;
1525 }
1526
1527 r = Py_BuildValue("(OO)", keyObject, valObject);
1528
1529 Py_DECREF(keyObject);
1530 Py_DECREF(valObject);
1531
1532 return r;
1533}
1534
1535 static PyObject *
1536DictionaryListItems(DictionaryObject *self)
1537{
1538 return DictionaryListObjects(self, dict_item);
1539}
1540
1541 static PyObject *
1542DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1543{
1544 dict_T *dict = self->dict;
1545
1546 if (dict->dv_lock)
1547 {
1548 PyErr_SetVim(_("dict is locked"));
1549 return NULL;
1550 }
1551
1552 if (kwargs)
1553 {
1554 typval_T tv;
1555
1556 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1557 return NULL;
1558
1559 VimTryStart();
1560 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1561 clear_tv(&tv);
1562 if (VimTryEnd())
1563 return NULL;
1564 }
1565 else
1566 {
1567 PyObject *object;
1568
1569 if (!PyArg_Parse(args, "(O)", &object))
1570 return NULL;
1571
1572 if (PyObject_HasAttrString(object, "keys"))
1573 return DictionaryUpdate(self, NULL, object);
1574 else
1575 {
1576 PyObject *iterator;
1577 PyObject *item;
1578
1579 if (!(iterator = PyObject_GetIter(object)))
1580 return NULL;
1581
1582 while ((item = PyIter_Next(iterator)))
1583 {
1584 PyObject *fast;
1585 PyObject *keyObject;
1586 PyObject *valObject;
1587 PyObject *todecref;
1588 char_u *key;
1589 dictitem_T *di;
1590
1591 if (!(fast = PySequence_Fast(item, "")))
1592 {
1593 Py_DECREF(iterator);
1594 Py_DECREF(item);
1595 return NULL;
1596 }
1597
1598 Py_DECREF(item);
1599
1600 if (PySequence_Fast_GET_SIZE(fast) != 2)
1601 {
1602 Py_DECREF(iterator);
1603 Py_DECREF(fast);
1604 PyErr_SetString(PyExc_ValueError,
1605 _("expected sequence element of size 2"));
1606 return NULL;
1607 }
1608
1609 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1610
1611 if (!(key = StringToChars(keyObject, &todecref)))
1612 {
1613 Py_DECREF(iterator);
1614 Py_DECREF(fast);
1615 return NULL;
1616 }
1617
1618 di = dictitem_alloc(key);
1619
1620 Py_XDECREF(todecref);
1621
1622 if (di == NULL)
1623 {
1624 Py_DECREF(fast);
1625 Py_DECREF(iterator);
1626 PyErr_NoMemory();
1627 return NULL;
1628 }
1629 di->di_tv.v_lock = 0;
1630 di->di_tv.v_type = VAR_UNKNOWN;
1631
1632 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1633
1634 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1635 {
1636 Py_DECREF(iterator);
1637 Py_DECREF(fast);
1638 dictitem_free(di);
1639 return NULL;
1640 }
1641
1642 Py_DECREF(fast);
1643
1644 if (dict_add(dict, di) == FAIL)
1645 {
1646 Py_DECREF(iterator);
1647 dictitem_free(di);
1648 PyErr_SetVim(_("failed to add key to dictionary"));
1649 return NULL;
1650 }
1651 }
1652
1653 Py_DECREF(iterator);
1654
1655 /* Iterator may have finished due to an exception */
1656 if (PyErr_Occurred())
1657 return NULL;
1658 }
1659 }
1660 Py_INCREF(Py_None);
1661 return Py_None;
1662}
1663
1664 static PyObject *
1665DictionaryGet(DictionaryObject *self, PyObject *args)
1666{
1667 return _DictionaryItem(self, args,
1668 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1669}
1670
1671 static PyObject *
1672DictionaryPop(DictionaryObject *self, PyObject *args)
1673{
1674 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1675}
1676
1677 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001678DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001679{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001680 hashitem_T *hi;
1681 PyObject *r;
1682 PyObject *valObject;
1683 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001684
Bram Moolenaarde71b562013-06-02 17:41:54 +02001685 if (self->dict->dv_hashtab.ht_used == 0)
1686 {
1687 PyErr_SetNone(PyExc_KeyError);
1688 return NULL;
1689 }
1690
1691 hi = self->dict->dv_hashtab.ht_array;
1692 while (HASHITEM_EMPTY(hi))
1693 ++hi;
1694
1695 di = dict_lookup(hi);
1696
1697 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001698 return NULL;
1699
Bram Moolenaarde71b562013-06-02 17:41:54 +02001700 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
1701 {
1702 Py_DECREF(valObject);
1703 return NULL;
1704 }
1705
1706 hash_remove(&self->dict->dv_hashtab, hi);
1707 dictitem_free(di);
1708
1709 return r;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001710}
1711
1712 static PyObject *
1713DictionaryHasKey(DictionaryObject *self, PyObject *args)
1714{
1715 PyObject *keyObject;
1716
1717 if (!PyArg_ParseTuple(args, "O", &keyObject))
1718 return NULL;
1719
1720 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1721}
1722
1723static PySequenceMethods DictionaryAsSeq = {
1724 0, /* sq_length */
1725 0, /* sq_concat */
1726 0, /* sq_repeat */
1727 0, /* sq_item */
1728 0, /* sq_slice */
1729 0, /* sq_ass_item */
1730 0, /* sq_ass_slice */
1731 (objobjproc) DictionaryContains, /* sq_contains */
1732 0, /* sq_inplace_concat */
1733 0, /* sq_inplace_repeat */
1734};
1735
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001736static PyMappingMethods DictionaryAsMapping = {
1737 (lenfunc) DictionaryLength,
1738 (binaryfunc) DictionaryItem,
1739 (objobjargproc) DictionaryAssItem,
1740};
1741
Bram Moolenaardb913952012-06-29 12:54:53 +02001742static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001743 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001744 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1745 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1746 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1747 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1748 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02001749 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001750 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001751 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1752 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001753};
1754
1755static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001756static PySequenceMethods ListAsSeq;
1757static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001758
1759typedef struct
1760{
1761 PyObject_HEAD
1762 list_T *list;
1763 pylinkedlist_T ref;
1764} ListObject;
1765
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001766#define NEW_LIST(list) ListNew(&ListType, list)
1767
Bram Moolenaardb913952012-06-29 12:54:53 +02001768 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001769ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001770{
1771 ListObject *self;
1772
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001773 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001774 if (self == NULL)
1775 return NULL;
1776 self->list = list;
1777 ++list->lv_refcount;
1778
1779 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1780
1781 return (PyObject *)(self);
1782}
1783
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001784 static list_T *
1785py_list_alloc()
1786{
1787 list_T *r;
1788
1789 if (!(r = list_alloc()))
1790 {
1791 PyErr_NoMemory();
1792 return NULL;
1793 }
1794 ++r->lv_refcount;
1795
1796 return r;
1797}
1798
1799 static int
1800list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1801{
1802 PyObject *iterator;
1803 PyObject *item;
1804 listitem_T *li;
1805
1806 if (!(iterator = PyObject_GetIter(obj)))
1807 return -1;
1808
1809 while ((item = PyIter_Next(iterator)))
1810 {
1811 if (!(li = listitem_alloc()))
1812 {
1813 PyErr_NoMemory();
1814 Py_DECREF(item);
1815 Py_DECREF(iterator);
1816 return -1;
1817 }
1818 li->li_tv.v_lock = 0;
1819 li->li_tv.v_type = VAR_UNKNOWN;
1820
1821 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1822 {
1823 Py_DECREF(item);
1824 Py_DECREF(iterator);
1825 listitem_free(li);
1826 return -1;
1827 }
1828
1829 Py_DECREF(item);
1830
1831 list_append(l, li);
1832 }
1833
1834 Py_DECREF(iterator);
1835
1836 /* Iterator may have finished due to an exception */
1837 if (PyErr_Occurred())
1838 return -1;
1839
1840 return 0;
1841}
1842
1843 static PyObject *
1844ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1845{
1846 list_T *list;
1847 PyObject *obj = NULL;
1848
1849 if (kwargs)
1850 {
1851 PyErr_SetString(PyExc_TypeError,
1852 _("list constructor does not accept keyword arguments"));
1853 return NULL;
1854 }
1855
1856 if (!PyArg_ParseTuple(args, "|O", &obj))
1857 return NULL;
1858
1859 if (!(list = py_list_alloc()))
1860 return NULL;
1861
1862 if (obj)
1863 {
1864 PyObject *lookup_dict;
1865
1866 if (!(lookup_dict = PyDict_New()))
1867 {
1868 list_unref(list);
1869 return NULL;
1870 }
1871
1872 if (list_py_concat(list, obj, lookup_dict) == -1)
1873 {
1874 Py_DECREF(lookup_dict);
1875 list_unref(list);
1876 return NULL;
1877 }
1878
1879 Py_DECREF(lookup_dict);
1880 }
1881
1882 return ListNew(subtype, list);
1883}
1884
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001885 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001886ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001887{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001888 pyll_remove(&self->ref, &lastlist);
1889 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001890
1891 DESTRUCTOR_FINISH(self);
1892}
1893
Bram Moolenaardb913952012-06-29 12:54:53 +02001894 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001895ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001896{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001897 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001898}
1899
1900 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001901ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001902{
1903 listitem_T *li;
1904
Bram Moolenaard6e39182013-05-21 18:30:34 +02001905 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001906 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001907 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001908 return NULL;
1909 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001910 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001911 if (li == NULL)
1912 {
1913 PyErr_SetVim(_("internal error: failed to get vim list item"));
1914 return NULL;
1915 }
1916 return ConvertToPyObject(&li->li_tv);
1917}
1918
1919#define PROC_RANGE \
1920 if (last < 0) {\
1921 if (last < -size) \
1922 last = 0; \
1923 else \
1924 last += size; \
1925 } \
1926 if (first < 0) \
1927 first = 0; \
1928 if (first > size) \
1929 first = size; \
1930 if (last > size) \
1931 last = size;
1932
1933 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001934ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001935{
1936 PyInt i;
1937 PyInt size = ListLength(self);
1938 PyInt n;
1939 PyObject *list;
1940 int reversed = 0;
1941
1942 PROC_RANGE
1943 if (first >= last)
1944 first = last;
1945
1946 n = last-first;
1947 list = PyList_New(n);
1948 if (list == NULL)
1949 return NULL;
1950
1951 for (i = 0; i < n; ++i)
1952 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001953 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001954 if (item == NULL)
1955 {
1956 Py_DECREF(list);
1957 return NULL;
1958 }
1959
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02001960 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001961 }
1962
1963 return list;
1964}
1965
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001966typedef struct
1967{
1968 listwatch_T lw;
1969 list_T *list;
1970} listiterinfo_T;
1971
1972 static void
1973ListIterDestruct(listiterinfo_T *lii)
1974{
1975 list_rem_watch(lii->list, &lii->lw);
1976 PyMem_Free(lii);
1977}
1978
1979 static PyObject *
1980ListIterNext(listiterinfo_T **lii)
1981{
1982 PyObject *r;
1983
1984 if (!((*lii)->lw.lw_item))
1985 return NULL;
1986
1987 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1988 return NULL;
1989
1990 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1991
1992 return r;
1993}
1994
1995 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001996ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001997{
1998 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001999 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002000
2001 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2002 {
2003 PyErr_NoMemory();
2004 return NULL;
2005 }
2006
2007 list_add_watch(l, &lii->lw);
2008 lii->lw.lw_item = l->lv_first;
2009 lii->list = l;
2010
2011 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002012 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2013 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002014}
2015
Bram Moolenaardb913952012-06-29 12:54:53 +02002016 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002017ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002018{
2019 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002020 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002021 listitem_T *li;
2022 Py_ssize_t length = ListLength(self);
2023
2024 if (l->lv_lock)
2025 {
2026 PyErr_SetVim(_("list is locked"));
2027 return -1;
2028 }
2029 if (index>length || (index==length && obj==NULL))
2030 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002031 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002032 return -1;
2033 }
2034
2035 if (obj == NULL)
2036 {
2037 li = list_find(l, (long) index);
2038 list_remove(l, li, li);
2039 clear_tv(&li->li_tv);
2040 vim_free(li);
2041 return 0;
2042 }
2043
2044 if (ConvertFromPyObject(obj, &tv) == -1)
2045 return -1;
2046
2047 if (index == length)
2048 {
2049 if (list_append_tv(l, &tv) == FAIL)
2050 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002051 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002052 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002053 return -1;
2054 }
2055 }
2056 else
2057 {
2058 li = list_find(l, (long) index);
2059 clear_tv(&li->li_tv);
2060 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002061 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002062 }
2063 return 0;
2064}
2065
2066 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002067ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002068{
2069 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002070 PyObject *iterator;
2071 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002072 listitem_T *li;
2073 listitem_T *next;
2074 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002075 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002076 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002077
2078 if (l->lv_lock)
2079 {
2080 PyErr_SetVim(_("list is locked"));
2081 return -1;
2082 }
2083
2084 PROC_RANGE
2085
2086 if (first == size)
2087 li = NULL;
2088 else
2089 {
2090 li = list_find(l, (long) first);
2091 if (li == NULL)
2092 {
2093 PyErr_SetVim(_("internal error: no vim list item"));
2094 return -1;
2095 }
2096 if (last > first)
2097 {
2098 i = last - first;
2099 while (i-- && li != NULL)
2100 {
2101 next = li->li_next;
2102 listitem_remove(l, li);
2103 li = next;
2104 }
2105 }
2106 }
2107
2108 if (obj == NULL)
2109 return 0;
2110
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002111 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002112 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002113
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002114 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002115 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002116 if (ConvertFromPyObject(item, &v) == -1)
2117 {
2118 Py_DECREF(iterator);
2119 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002120 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002121 }
2122 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002123 if (list_insert_tv(l, &v, li) == FAIL)
2124 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002125 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002126 PyErr_SetVim(_("internal error: failed to add item to list"));
2127 return -1;
2128 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002129 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002130 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002131 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02002132 return 0;
2133}
2134
2135 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002136ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002137{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002138 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002139 PyObject *lookup_dict;
2140
2141 if (l->lv_lock)
2142 {
2143 PyErr_SetVim(_("list is locked"));
2144 return NULL;
2145 }
2146
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002147 if (!(lookup_dict = PyDict_New()))
2148 return NULL;
2149
Bram Moolenaardb913952012-06-29 12:54:53 +02002150 if (list_py_concat(l, obj, lookup_dict) == -1)
2151 {
2152 Py_DECREF(lookup_dict);
2153 return NULL;
2154 }
2155 Py_DECREF(lookup_dict);
2156
2157 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002158 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002159}
2160
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002161static char *ListAttrs[] = {
2162 "locked",
2163 NULL
2164};
2165
2166 static PyObject *
2167ListDir(PyObject *self)
2168{
2169 return ObjectDir(self, ListAttrs);
2170}
2171
Bram Moolenaar66b79852012-09-21 14:00:35 +02002172 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002173ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002174{
2175 if (val == NULL)
2176 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002177 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002178 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002179 return -1;
2180 }
2181
2182 if (strcmp(name, "locked") == 0)
2183 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002184 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002185 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002186 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002187 return -1;
2188 }
2189 else
2190 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002191 int istrue = PyObject_IsTrue(val);
2192 if (istrue == -1)
2193 return -1;
2194 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002195 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002196 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002197 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002198 }
2199 return 0;
2200 }
2201 else
2202 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002203 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002204 return -1;
2205 }
2206}
2207
Bram Moolenaardb913952012-06-29 12:54:53 +02002208static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002209 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2210 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2211 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002212};
2213
2214typedef struct
2215{
2216 PyObject_HEAD
2217 char_u *name;
2218} FunctionObject;
2219
2220static PyTypeObject FunctionType;
2221
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002222#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2223
Bram Moolenaardb913952012-06-29 12:54:53 +02002224 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002225FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002226{
2227 FunctionObject *self;
2228
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002229 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2230
Bram Moolenaardb913952012-06-29 12:54:53 +02002231 if (self == NULL)
2232 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002233
2234 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002235 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002236 if (!translated_function_exists(name))
2237 {
2238 PyErr_SetString(PyExc_ValueError,
2239 _("unnamed function does not exist"));
2240 return NULL;
2241 }
2242 self->name = vim_strsave(name);
2243 func_ref(self->name);
2244 }
2245 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002246 if ((self->name = get_expanded_name(name,
2247 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2248 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002249 {
Bram Moolenaar018acca2013-05-30 13:37:28 +02002250 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2251 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002252 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002253
2254 return (PyObject *)(self);
2255}
2256
2257 static PyObject *
2258FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2259{
2260 PyObject *self;
2261 char_u *name;
2262
2263 if (kwargs)
2264 {
2265 PyErr_SetString(PyExc_TypeError,
2266 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002267 return NULL;
2268 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002269
2270 if (!PyArg_ParseTuple(args, "s", &name))
2271 return NULL;
2272
2273 self = FunctionNew(subtype, name);
2274
2275 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002276}
2277
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002278 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002279FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002280{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002281 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002282 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002283
2284 DESTRUCTOR_FINISH(self);
2285}
2286
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002287static char *FunctionAttrs[] = {
2288 "softspace",
2289 NULL
2290};
2291
2292 static PyObject *
2293FunctionDir(PyObject *self)
2294{
2295 return ObjectDir(self, FunctionAttrs);
2296}
2297
Bram Moolenaardb913952012-06-29 12:54:53 +02002298 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002299FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002300{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002301 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002302 typval_T args;
2303 typval_T selfdicttv;
2304 typval_T rettv;
2305 dict_T *selfdict = NULL;
2306 PyObject *selfdictObject;
2307 PyObject *result;
2308 int error;
2309
2310 if (ConvertFromPyObject(argsObject, &args) == -1)
2311 return NULL;
2312
2313 if (kwargs != NULL)
2314 {
2315 selfdictObject = PyDict_GetItemString(kwargs, "self");
2316 if (selfdictObject != NULL)
2317 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002318 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002319 {
2320 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002321 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002322 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002323 selfdict = selfdicttv.vval.v_dict;
2324 }
2325 }
2326
Bram Moolenaar71700b82013-05-15 17:49:05 +02002327 Py_BEGIN_ALLOW_THREADS
2328 Python_Lock_Vim();
2329
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002330 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002331 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002332
2333 Python_Release_Vim();
2334 Py_END_ALLOW_THREADS
2335
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002336 if (VimTryEnd())
2337 result = NULL;
2338 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002339 {
2340 result = NULL;
2341 PyErr_SetVim(_("failed to run function"));
2342 }
2343 else
2344 result = ConvertToPyObject(&rettv);
2345
Bram Moolenaardb913952012-06-29 12:54:53 +02002346 clear_tv(&args);
2347 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002348 if (selfdict != NULL)
2349 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002350
2351 return result;
2352}
2353
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002354 static PyObject *
2355FunctionRepr(FunctionObject *self)
2356{
2357 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2358}
2359
Bram Moolenaardb913952012-06-29 12:54:53 +02002360static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002361 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2362 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002363};
2364
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002365/*
2366 * Options object
2367 */
2368
2369static PyTypeObject OptionsType;
2370
2371typedef int (*checkfun)(void *);
2372
2373typedef struct
2374{
2375 PyObject_HEAD
2376 int opt_type;
2377 void *from;
2378 checkfun Check;
2379 PyObject *fromObj;
2380} OptionsObject;
2381
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002382 static int
2383dummy_check(void *arg UNUSED)
2384{
2385 return 0;
2386}
2387
2388 static PyObject *
2389OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2390{
2391 OptionsObject *self;
2392
Bram Moolenaar774267b2013-05-21 20:51:59 +02002393 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002394 if (self == NULL)
2395 return NULL;
2396
2397 self->opt_type = opt_type;
2398 self->from = from;
2399 self->Check = Check;
2400 self->fromObj = fromObj;
2401 if (fromObj)
2402 Py_INCREF(fromObj);
2403
2404 return (PyObject *)(self);
2405}
2406
2407 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002408OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002409{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002410 PyObject_GC_UnTrack((void *)(self));
2411 Py_XDECREF(self->fromObj);
2412 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002413}
2414
2415 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002416OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002417{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002418 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002419 return 0;
2420}
2421
2422 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002423OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002424{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002425 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002426 return 0;
2427}
2428
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002429 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002430OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002431{
2432 char_u *key;
2433 int flags;
2434 long numval;
2435 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002436 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002437
Bram Moolenaard6e39182013-05-21 18:30:34 +02002438 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002439 return NULL;
2440
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002441 if (!(key = StringToChars(keyObject, &todecref)))
2442 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002443
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002444 if (*key == NUL)
2445 {
2446 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002447 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002448 return NULL;
2449 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002450
2451 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002452 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002453
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002454 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002455
2456 if (flags == 0)
2457 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002458 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002459 return NULL;
2460 }
2461
2462 if (flags & SOPT_UNSET)
2463 {
2464 Py_INCREF(Py_None);
2465 return Py_None;
2466 }
2467 else if (flags & SOPT_BOOL)
2468 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002469 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002470 r = numval ? Py_True : Py_False;
2471 Py_INCREF(r);
2472 return r;
2473 }
2474 else if (flags & SOPT_NUM)
2475 return PyInt_FromLong(numval);
2476 else if (flags & SOPT_STRING)
2477 {
2478 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002479 {
2480 PyObject *r = PyBytes_FromString((char *) stringval);
2481 vim_free(stringval);
2482 return r;
2483 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002484 else
2485 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002486 PyErr_SetString(PyExc_RuntimeError,
2487 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002488 return NULL;
2489 }
2490 }
2491 else
2492 {
2493 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2494 return NULL;
2495 }
2496}
2497
2498 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002499set_option_value_err(key, numval, stringval, opt_flags)
2500 char_u *key;
2501 int numval;
2502 char_u *stringval;
2503 int opt_flags;
2504{
2505 char_u *errmsg;
2506
2507 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2508 {
2509 if (VimTryEnd())
2510 return FAIL;
2511 PyErr_SetVim((char *)errmsg);
2512 return FAIL;
2513 }
2514 return OK;
2515}
2516
2517 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002518set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2519 char_u *key;
2520 int numval;
2521 char_u *stringval;
2522 int opt_flags;
2523 int opt_type;
2524 void *from;
2525{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002526 win_T *save_curwin = NULL;
2527 tabpage_T *save_curtab = NULL;
2528 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002529 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002530
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002531 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002532 switch (opt_type)
2533 {
2534 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002535 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2536 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002537 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002538 if (VimTryEnd())
2539 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002540 PyErr_SetVim("Problem while switching windows.");
2541 return -1;
2542 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002543 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002544 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002545 if (r == FAIL)
2546 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002547 break;
2548 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002549 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002550 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002551 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002552 if (r == FAIL)
2553 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002554 break;
2555 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002556 r = set_option_value_err(key, numval, stringval, opt_flags);
2557 if (r == FAIL)
2558 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002559 break;
2560 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002561 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002562}
2563
2564 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002565OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002566{
2567 char_u *key;
2568 int flags;
2569 int opt_flags;
2570 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002571 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002572
Bram Moolenaard6e39182013-05-21 18:30:34 +02002573 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002574 return -1;
2575
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002576 if (!(key = StringToChars(keyObject, &todecref)))
2577 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002578
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002579 if (*key == NUL)
2580 {
2581 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002582 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002583 return -1;
2584 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002585
2586 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002587 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002588
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002589 if (flags == 0)
2590 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002591 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002592 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002593 return -1;
2594 }
2595
2596 if (valObject == NULL)
2597 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002598 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002599 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002600 PyErr_SetString(PyExc_ValueError,
2601 _("unable to unset global option"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002602 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002603 return -1;
2604 }
2605 else if (!(flags & SOPT_GLOBAL))
2606 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002607 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2608 "without global value"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002609 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002610 return -1;
2611 }
2612 else
2613 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002614 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002615 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002616 return 0;
2617 }
2618 }
2619
Bram Moolenaard6e39182013-05-21 18:30:34 +02002620 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002621
2622 if (flags & SOPT_BOOL)
2623 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002624 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002625
Bram Moolenaarb983f752013-05-15 16:11:50 +02002626 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002627 r = -1;
2628 else
2629 r = set_option_value_for(key, istrue, NULL,
2630 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002631 }
2632 else if (flags & SOPT_NUM)
2633 {
2634 int val;
2635
2636#if PY_MAJOR_VERSION < 3
2637 if (PyInt_Check(valObject))
2638 val = PyInt_AsLong(valObject);
2639 else
2640#endif
2641 if (PyLong_Check(valObject))
2642 val = PyLong_AsLong(valObject);
2643 else
2644 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002645 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002646 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002647 return -1;
2648 }
2649
2650 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002651 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002652 }
2653 else
2654 {
2655 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002656 PyObject *todecref;
2657
2658 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002659 r = set_option_value_for(key, 0, val, opt_flags,
2660 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002661 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002662 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002663 }
2664
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002665 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002666
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002667 return r;
2668}
2669
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002670static PyMappingMethods OptionsAsMapping = {
2671 (lenfunc) NULL,
2672 (binaryfunc) OptionsItem,
2673 (objobjargproc) OptionsAssItem,
2674};
2675
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002676/* Tabpage object
2677 */
2678
2679typedef struct
2680{
2681 PyObject_HEAD
2682 tabpage_T *tab;
2683} TabPageObject;
2684
2685static PyObject *WinListNew(TabPageObject *tabObject);
2686
2687static PyTypeObject TabPageType;
2688
2689 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002690CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002691{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002692 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002693 {
2694 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2695 return -1;
2696 }
2697
2698 return 0;
2699}
2700
2701 static PyObject *
2702TabPageNew(tabpage_T *tab)
2703{
2704 TabPageObject *self;
2705
2706 if (TAB_PYTHON_REF(tab))
2707 {
2708 self = TAB_PYTHON_REF(tab);
2709 Py_INCREF(self);
2710 }
2711 else
2712 {
2713 self = PyObject_NEW(TabPageObject, &TabPageType);
2714 if (self == NULL)
2715 return NULL;
2716 self->tab = tab;
2717 TAB_PYTHON_REF(tab) = self;
2718 }
2719
2720 return (PyObject *)(self);
2721}
2722
2723 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002724TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002725{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002726 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2727 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002728
2729 DESTRUCTOR_FINISH(self);
2730}
2731
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002732static char *TabPageAttrs[] = {
2733 "windows", "number", "vars", "window", "valid",
2734 NULL
2735};
2736
2737 static PyObject *
2738TabPageDir(PyObject *self)
2739{
2740 return ObjectDir(self, TabPageAttrs);
2741}
2742
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002743 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002744TabPageAttrValid(TabPageObject *self, char *name)
2745{
2746 PyObject *r;
2747
2748 if (strcmp(name, "valid") != 0)
2749 return NULL;
2750
2751 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2752 Py_INCREF(r);
2753 return r;
2754}
2755
2756 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002757TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002758{
2759 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002760 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002761 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002762 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002763 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002764 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002765 else if (strcmp(name, "window") == 0)
2766 {
2767 /* For current tab window.c does not bother to set or update tp_curwin
2768 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002769 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002770 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002771 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002772 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002773 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002774 else if (strcmp(name, "__members__") == 0)
2775 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002776 return NULL;
2777}
2778
2779 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002780TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002781{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002782 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002783 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002784 else
2785 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002786 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002787
2788 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002789 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2790 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002791 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002792 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002793 }
2794}
2795
2796static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002797 /* name, function, calling, documentation */
2798 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2799 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002800};
2801
2802/*
2803 * Window list object
2804 */
2805
2806static PyTypeObject TabListType;
2807static PySequenceMethods TabListAsSeq;
2808
2809typedef struct
2810{
2811 PyObject_HEAD
2812} TabListObject;
2813
2814 static PyInt
2815TabListLength(PyObject *self UNUSED)
2816{
2817 tabpage_T *tp = first_tabpage;
2818 PyInt n = 0;
2819
2820 while (tp != NULL)
2821 {
2822 ++n;
2823 tp = tp->tp_next;
2824 }
2825
2826 return n;
2827}
2828
2829 static PyObject *
2830TabListItem(PyObject *self UNUSED, PyInt n)
2831{
2832 tabpage_T *tp;
2833
2834 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2835 if (n == 0)
2836 return TabPageNew(tp);
2837
2838 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2839 return NULL;
2840}
2841
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002842/* Window object
2843 */
2844
2845typedef struct
2846{
2847 PyObject_HEAD
2848 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002849 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002850} WindowObject;
2851
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002852static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002853
2854 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002855CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002856{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002857 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002858 {
2859 PyErr_SetVim(_("attempt to refer to deleted window"));
2860 return -1;
2861 }
2862
2863 return 0;
2864}
2865
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002866 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002867WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002868{
2869 /* We need to handle deletion of windows underneath us.
2870 * If we add a "w_python*_ref" field to the win_T structure,
2871 * then we can get at it in win_free() in vim. We then
2872 * need to create only ONE Python object per window - if
2873 * we try to create a second, just INCREF the existing one
2874 * and return it. The (single) Python object referring to
2875 * the window is stored in "w_python*_ref".
2876 * On a win_free() we set the Python object's win_T* field
2877 * to an invalid value. We trap all uses of a window
2878 * object, and reject them if the win_T* field is invalid.
2879 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002880 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002881 * w_python_ref and w_python3_ref fields respectively.
2882 */
2883
2884 WindowObject *self;
2885
2886 if (WIN_PYTHON_REF(win))
2887 {
2888 self = WIN_PYTHON_REF(win);
2889 Py_INCREF(self);
2890 }
2891 else
2892 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002893 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002894 if (self == NULL)
2895 return NULL;
2896 self->win = win;
2897 WIN_PYTHON_REF(win) = self;
2898 }
2899
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002900 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2901
Bram Moolenaar971db462013-05-12 18:44:48 +02002902 return (PyObject *)(self);
2903}
2904
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002905 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002906WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002907{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002908 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002909 if (self->win && self->win != INVALID_WINDOW_VALUE)
2910 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002911 Py_XDECREF(((PyObject *)(self->tabObject)));
2912 PyObject_GC_Del((void *)(self));
2913}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002914
Bram Moolenaar774267b2013-05-21 20:51:59 +02002915 static int
2916WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2917{
2918 Py_VISIT(((PyObject *)(self->tabObject)));
2919 return 0;
2920}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002921
Bram Moolenaar774267b2013-05-21 20:51:59 +02002922 static int
2923WindowClear(WindowObject *self)
2924{
2925 Py_CLEAR(self->tabObject);
2926 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002927}
2928
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002929 static win_T *
2930get_firstwin(TabPageObject *tabObject)
2931{
2932 if (tabObject)
2933 {
2934 if (CheckTabPage(tabObject))
2935 return NULL;
2936 /* For current tab window.c does not bother to set or update tp_firstwin
2937 */
2938 else if (tabObject->tab == curtab)
2939 return firstwin;
2940 else
2941 return tabObject->tab->tp_firstwin;
2942 }
2943 else
2944 return firstwin;
2945}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002946static char *WindowAttrs[] = {
2947 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2948 "tabpage", "valid",
2949 NULL
2950};
2951
2952 static PyObject *
2953WindowDir(PyObject *self)
2954{
2955 return ObjectDir(self, WindowAttrs);
2956}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002957
Bram Moolenaar971db462013-05-12 18:44:48 +02002958 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002959WindowAttrValid(WindowObject *self, char *name)
2960{
2961 PyObject *r;
2962
2963 if (strcmp(name, "valid") != 0)
2964 return NULL;
2965
2966 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2967 Py_INCREF(r);
2968 return r;
2969}
2970
2971 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002972WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002973{
2974 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002975 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002976 else if (strcmp(name, "cursor") == 0)
2977 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002978 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002979
2980 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2981 }
2982 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002983 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002984#ifdef FEAT_WINDOWS
2985 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002986 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002987#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002988#ifdef FEAT_VERTSPLIT
2989 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002990 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002991 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002992 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002993#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002994 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002995 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002996 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002997 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2998 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002999 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003000 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003001 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003002 return NULL;
3003 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003004 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003005 }
3006 else if (strcmp(name, "tabpage") == 0)
3007 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003008 Py_INCREF(self->tabObject);
3009 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003010 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003011 else if (strcmp(name, "__members__") == 0)
3012 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003013 else
3014 return NULL;
3015}
3016
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003017 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003018WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003019{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003020 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003021 return -1;
3022
3023 if (strcmp(name, "buffer") == 0)
3024 {
3025 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
3026 return -1;
3027 }
3028 else if (strcmp(name, "cursor") == 0)
3029 {
3030 long lnum;
3031 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003032
3033 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
3034 return -1;
3035
Bram Moolenaard6e39182013-05-21 18:30:34 +02003036 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003037 {
3038 PyErr_SetVim(_("cursor position outside buffer"));
3039 return -1;
3040 }
3041
3042 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003043 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003044 return -1;
3045
Bram Moolenaard6e39182013-05-21 18:30:34 +02003046 self->win->w_cursor.lnum = lnum;
3047 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003048#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003049 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003050#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003051 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003052 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003053
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003054 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003055 return 0;
3056 }
3057 else if (strcmp(name, "height") == 0)
3058 {
3059 int height;
3060 win_T *savewin;
3061
3062 if (!PyArg_Parse(val, "i", &height))
3063 return -1;
3064
3065#ifdef FEAT_GUI
3066 need_mouse_correct = TRUE;
3067#endif
3068 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003069 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003070
3071 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003072 win_setheight(height);
3073 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003074 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003075 return -1;
3076
3077 return 0;
3078 }
3079#ifdef FEAT_VERTSPLIT
3080 else if (strcmp(name, "width") == 0)
3081 {
3082 int width;
3083 win_T *savewin;
3084
3085 if (!PyArg_Parse(val, "i", &width))
3086 return -1;
3087
3088#ifdef FEAT_GUI
3089 need_mouse_correct = TRUE;
3090#endif
3091 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003092 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003093
3094 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003095 win_setwidth(width);
3096 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003097 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003098 return -1;
3099
3100 return 0;
3101 }
3102#endif
3103 else
3104 {
3105 PyErr_SetString(PyExc_AttributeError, name);
3106 return -1;
3107 }
3108}
3109
3110 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003111WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003112{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003113 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003114 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003115 else
3116 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003117 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003118
Bram Moolenaar6d216452013-05-12 19:00:41 +02003119 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003120 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003121 (self));
3122 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003123 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003124 }
3125}
3126
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003127static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003128 /* name, function, calling, documentation */
3129 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3130 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003131};
3132
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003133/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003134 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003135 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003136
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003137static PyTypeObject WinListType;
3138static PySequenceMethods WinListAsSeq;
3139
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003140typedef struct
3141{
3142 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003143 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003144} WinListObject;
3145
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003146 static PyObject *
3147WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003148{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003149 WinListObject *self;
3150
3151 self = PyObject_NEW(WinListObject, &WinListType);
3152 self->tabObject = tabObject;
3153 Py_INCREF(tabObject);
3154
3155 return (PyObject *)(self);
3156}
3157
3158 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003159WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003160{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003161 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003162
3163 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003164 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003165 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003166 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003167
3168 DESTRUCTOR_FINISH(self);
3169}
3170
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003171 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003172WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003173{
3174 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003175 PyInt n = 0;
3176
Bram Moolenaard6e39182013-05-21 18:30:34 +02003177 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003178 return -1;
3179
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003180 while (w != NULL)
3181 {
3182 ++n;
3183 w = W_NEXT(w);
3184 }
3185
3186 return n;
3187}
3188
3189 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003190WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003191{
3192 win_T *w;
3193
Bram Moolenaard6e39182013-05-21 18:30:34 +02003194 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003195 return NULL;
3196
3197 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003198 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003199 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003200
3201 PyErr_SetString(PyExc_IndexError, _("no such window"));
3202 return NULL;
3203}
3204
3205/* Convert a Python string into a Vim line.
3206 *
3207 * The result is in allocated memory. All internal nulls are replaced by
3208 * newline characters. It is an error for the string to contain newline
3209 * characters.
3210 *
3211 * On errors, the Python exception data is set, and NULL is returned.
3212 */
3213 static char *
3214StringToLine(PyObject *obj)
3215{
3216 const char *str;
3217 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003218 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003219 PyInt len;
3220 PyInt i;
3221 char *p;
3222
3223 if (obj == NULL || !PyString_Check(obj))
3224 {
3225 PyErr_BadArgument();
3226 return NULL;
3227 }
3228
Bram Moolenaar19e60942011-06-19 00:27:51 +02003229 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
3230 str = PyString_AsString(bytes);
3231 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003232
3233 /*
3234 * Error checking: String must not contain newlines, as we
3235 * are replacing a single line, and we must replace it with
3236 * a single line.
3237 * A trailing newline is removed, so that append(f.readlines()) works.
3238 */
3239 p = memchr(str, '\n', len);
3240 if (p != NULL)
3241 {
3242 if (p == str + len - 1)
3243 --len;
3244 else
3245 {
3246 PyErr_SetVim(_("string cannot contain newlines"));
3247 return NULL;
3248 }
3249 }
3250
3251 /* Create a copy of the string, with internal nulls replaced by
3252 * newline characters, as is the vim convention.
3253 */
3254 save = (char *)alloc((unsigned)(len+1));
3255 if (save == NULL)
3256 {
3257 PyErr_NoMemory();
3258 return NULL;
3259 }
3260
3261 for (i = 0; i < len; ++i)
3262 {
3263 if (str[i] == '\0')
3264 save[i] = '\n';
3265 else
3266 save[i] = str[i];
3267 }
3268
3269 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003270 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003271
3272 return save;
3273}
3274
3275/* Get a line from the specified buffer. The line number is
3276 * in Vim format (1-based). The line is returned as a Python
3277 * string object.
3278 */
3279 static PyObject *
3280GetBufferLine(buf_T *buf, PyInt n)
3281{
3282 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3283}
3284
3285
3286/* Get a list of lines from the specified buffer. The line numbers
3287 * are in Vim format (1-based). The range is from lo up to, but not
3288 * including, hi. The list is returned as a Python list of string objects.
3289 */
3290 static PyObject *
3291GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3292{
3293 PyInt i;
3294 PyInt n = hi - lo;
3295 PyObject *list = PyList_New(n);
3296
3297 if (list == NULL)
3298 return NULL;
3299
3300 for (i = 0; i < n; ++i)
3301 {
3302 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3303
3304 /* Error check - was the Python string creation OK? */
3305 if (str == NULL)
3306 {
3307 Py_DECREF(list);
3308 return NULL;
3309 }
3310
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02003311 PyList_SET_ITEM(list, i, str);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003312 }
3313
3314 /* The ownership of the Python list is passed to the caller (ie,
3315 * the caller should Py_DECREF() the object when it is finished
3316 * with it).
3317 */
3318
3319 return list;
3320}
3321
3322/*
3323 * Check if deleting lines made the cursor position invalid.
3324 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3325 * deleted).
3326 */
3327 static void
3328py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3329{
3330 if (curwin->w_cursor.lnum >= lo)
3331 {
3332 /* Adjust the cursor position if it's in/after the changed
3333 * lines. */
3334 if (curwin->w_cursor.lnum >= hi)
3335 {
3336 curwin->w_cursor.lnum += extra;
3337 check_cursor_col();
3338 }
3339 else if (extra < 0)
3340 {
3341 curwin->w_cursor.lnum = lo;
3342 check_cursor();
3343 }
3344 else
3345 check_cursor_col();
3346 changed_cline_bef_curs();
3347 }
3348 invalidate_botline();
3349}
3350
Bram Moolenaar19e60942011-06-19 00:27:51 +02003351/*
3352 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003353 * in Vim format (1-based). The replacement line is given as
3354 * a Python string object. The object is checked for validity
3355 * and correct format. Errors are returned as a value of FAIL.
3356 * The return value is OK on success.
3357 * If OK is returned and len_change is not NULL, *len_change
3358 * is set to the change in the buffer length.
3359 */
3360 static int
3361SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3362{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003363 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003364 * There are three cases:
3365 * 1. NULL, or None - this is a deletion.
3366 * 2. A string - this is a replacement.
3367 * 3. Anything else - this is an error.
3368 */
3369 if (line == Py_None || line == NULL)
3370 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003371 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003372
3373 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003374 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003375
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003376 VimTryStart();
3377
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003378 if (u_savedel((linenr_T)n, 1L) == FAIL)
3379 PyErr_SetVim(_("cannot save undo information"));
3380 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3381 PyErr_SetVim(_("cannot delete line"));
3382 else
3383 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003384 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003385 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3386 deleted_lines_mark((linenr_T)n, 1L);
3387 }
3388
Bram Moolenaar105bc352013-05-17 16:03:57 +02003389 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003390
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003391 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003392 return FAIL;
3393
3394 if (len_change)
3395 *len_change = -1;
3396
3397 return OK;
3398 }
3399 else if (PyString_Check(line))
3400 {
3401 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003402 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003403
3404 if (save == NULL)
3405 return FAIL;
3406
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003407 VimTryStart();
3408
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003409 /* We do not need to free "save" if ml_replace() consumes it. */
3410 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003411 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003412
3413 if (u_savesub((linenr_T)n) == FAIL)
3414 {
3415 PyErr_SetVim(_("cannot save undo information"));
3416 vim_free(save);
3417 }
3418 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3419 {
3420 PyErr_SetVim(_("cannot replace line"));
3421 vim_free(save);
3422 }
3423 else
3424 changed_bytes((linenr_T)n, 0);
3425
Bram Moolenaar105bc352013-05-17 16:03:57 +02003426 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003427
3428 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003429 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003430 check_cursor_col();
3431
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003432 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003433 return FAIL;
3434
3435 if (len_change)
3436 *len_change = 0;
3437
3438 return OK;
3439 }
3440 else
3441 {
3442 PyErr_BadArgument();
3443 return FAIL;
3444 }
3445}
3446
Bram Moolenaar19e60942011-06-19 00:27:51 +02003447/* Replace a range of lines in the specified buffer. The line numbers are in
3448 * Vim format (1-based). The range is from lo up to, but not including, hi.
3449 * The replacement lines are given as a Python list of string objects. The
3450 * list is checked for validity and correct format. Errors are returned as a
3451 * value of FAIL. The return value is OK on success.
3452 * If OK is returned and len_change is not NULL, *len_change
3453 * is set to the change in the buffer length.
3454 */
3455 static int
3456SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3457{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003458 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003459 * There are three cases:
3460 * 1. NULL, or None - this is a deletion.
3461 * 2. A list - this is a replacement.
3462 * 3. Anything else - this is an error.
3463 */
3464 if (list == Py_None || list == NULL)
3465 {
3466 PyInt i;
3467 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003468 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003469
3470 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003471 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003472 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003473
3474 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3475 PyErr_SetVim(_("cannot save undo information"));
3476 else
3477 {
3478 for (i = 0; i < n; ++i)
3479 {
3480 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3481 {
3482 PyErr_SetVim(_("cannot delete line"));
3483 break;
3484 }
3485 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003486 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003487 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3488 deleted_lines_mark((linenr_T)lo, (long)i);
3489 }
3490
Bram Moolenaar105bc352013-05-17 16:03:57 +02003491 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003492
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003493 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003494 return FAIL;
3495
3496 if (len_change)
3497 *len_change = -n;
3498
3499 return OK;
3500 }
3501 else if (PyList_Check(list))
3502 {
3503 PyInt i;
3504 PyInt new_len = PyList_Size(list);
3505 PyInt old_len = hi - lo;
3506 PyInt extra = 0; /* lines added to text, can be negative */
3507 char **array;
3508 buf_T *savebuf;
3509
3510 if (new_len == 0) /* avoid allocating zero bytes */
3511 array = NULL;
3512 else
3513 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003514 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003515 if (array == NULL)
3516 {
3517 PyErr_NoMemory();
3518 return FAIL;
3519 }
3520 }
3521
3522 for (i = 0; i < new_len; ++i)
3523 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003524 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003525
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003526 if (!(line = PyList_GetItem(list, i)) ||
3527 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003528 {
3529 while (i)
3530 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003531 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003532 return FAIL;
3533 }
3534 }
3535
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003536 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003537 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003538
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003539 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003540 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003541
3542 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3543 PyErr_SetVim(_("cannot save undo information"));
3544
3545 /* If the size of the range is reducing (ie, new_len < old_len) we
3546 * need to delete some old_len. We do this at the start, by
3547 * repeatedly deleting line "lo".
3548 */
3549 if (!PyErr_Occurred())
3550 {
3551 for (i = 0; i < old_len - new_len; ++i)
3552 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3553 {
3554 PyErr_SetVim(_("cannot delete line"));
3555 break;
3556 }
3557 extra -= i;
3558 }
3559
3560 /* For as long as possible, replace the existing old_len with the
3561 * new old_len. This is a more efficient operation, as it requires
3562 * less memory allocation and freeing.
3563 */
3564 if (!PyErr_Occurred())
3565 {
3566 for (i = 0; i < old_len && i < new_len; ++i)
3567 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3568 == FAIL)
3569 {
3570 PyErr_SetVim(_("cannot replace line"));
3571 break;
3572 }
3573 }
3574 else
3575 i = 0;
3576
3577 /* Now we may need to insert the remaining new old_len. If we do, we
3578 * must free the strings as we finish with them (we can't pass the
3579 * responsibility to vim in this case).
3580 */
3581 if (!PyErr_Occurred())
3582 {
3583 while (i < new_len)
3584 {
3585 if (ml_append((linenr_T)(lo + i - 1),
3586 (char_u *)array[i], 0, FALSE) == FAIL)
3587 {
3588 PyErr_SetVim(_("cannot insert line"));
3589 break;
3590 }
3591 vim_free(array[i]);
3592 ++i;
3593 ++extra;
3594 }
3595 }
3596
3597 /* Free any left-over old_len, as a result of an error */
3598 while (i < new_len)
3599 {
3600 vim_free(array[i]);
3601 ++i;
3602 }
3603
3604 /* Free the array of old_len. All of its contents have now
3605 * been dealt with (either freed, or the responsibility passed
3606 * to vim.
3607 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003608 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003609
3610 /* Adjust marks. Invalidate any which lie in the
3611 * changed range, and move any in the remainder of the buffer.
3612 */
3613 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3614 (long)MAXLNUM, (long)extra);
3615 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3616
Bram Moolenaar105bc352013-05-17 16:03:57 +02003617 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003618 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3619
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003620 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003621 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003622
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003623 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003624 return FAIL;
3625
3626 if (len_change)
3627 *len_change = new_len - old_len;
3628
3629 return OK;
3630 }
3631 else
3632 {
3633 PyErr_BadArgument();
3634 return FAIL;
3635 }
3636}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003637
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003638/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003639 * The line number is in Vim format (1-based). The lines to be inserted are
3640 * given as a Python list of string objects or as a single string. The lines
3641 * to be added are checked for validity and correct format. Errors are
3642 * returned as a value of FAIL. The return value is OK on success.
3643 * If OK is returned and len_change is not NULL, *len_change
3644 * is set to the change in the buffer length.
3645 */
3646 static int
3647InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3648{
3649 /* First of all, we check the type of the supplied Python object.
3650 * It must be a string or a list, or the call is in error.
3651 */
3652 if (PyString_Check(lines))
3653 {
3654 char *str = StringToLine(lines);
3655 buf_T *savebuf;
3656
3657 if (str == NULL)
3658 return FAIL;
3659
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003660 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003661 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003662 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003663
3664 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3665 PyErr_SetVim(_("cannot save undo information"));
3666 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3667 PyErr_SetVim(_("cannot insert line"));
3668 else
3669 appended_lines_mark((linenr_T)n, 1L);
3670
3671 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003672 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003673 update_screen(VALID);
3674
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003675 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003676 return FAIL;
3677
3678 if (len_change)
3679 *len_change = 1;
3680
3681 return OK;
3682 }
3683 else if (PyList_Check(lines))
3684 {
3685 PyInt i;
3686 PyInt size = PyList_Size(lines);
3687 char **array;
3688 buf_T *savebuf;
3689
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003690 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003691 if (array == NULL)
3692 {
3693 PyErr_NoMemory();
3694 return FAIL;
3695 }
3696
3697 for (i = 0; i < size; ++i)
3698 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003699 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003700
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003701 if (!(line = PyList_GetItem(lines, i)) ||
3702 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003703 {
3704 while (i)
3705 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003706 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003707 return FAIL;
3708 }
3709 }
3710
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003711 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003712 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003713 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003714
3715 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3716 PyErr_SetVim(_("cannot save undo information"));
3717 else
3718 {
3719 for (i = 0; i < size; ++i)
3720 {
3721 if (ml_append((linenr_T)(n + i),
3722 (char_u *)array[i], 0, FALSE) == FAIL)
3723 {
3724 PyErr_SetVim(_("cannot insert line"));
3725
3726 /* Free the rest of the lines */
3727 while (i < size)
3728 vim_free(array[i++]);
3729
3730 break;
3731 }
3732 vim_free(array[i]);
3733 }
3734 if (i > 0)
3735 appended_lines_mark((linenr_T)n, (long)i);
3736 }
3737
3738 /* Free the array of lines. All of its contents have now
3739 * been freed.
3740 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003741 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003742
Bram Moolenaar105bc352013-05-17 16:03:57 +02003743 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003744 update_screen(VALID);
3745
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003746 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003747 return FAIL;
3748
3749 if (len_change)
3750 *len_change = size;
3751
3752 return OK;
3753 }
3754 else
3755 {
3756 PyErr_BadArgument();
3757 return FAIL;
3758 }
3759}
3760
3761/*
3762 * Common routines for buffers and line ranges
3763 * -------------------------------------------
3764 */
3765
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003766typedef struct
3767{
3768 PyObject_HEAD
3769 buf_T *buf;
3770} BufferObject;
3771
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003772 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003773CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003774{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003775 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003776 {
3777 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3778 return -1;
3779 }
3780
3781 return 0;
3782}
3783
3784 static PyObject *
3785RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3786{
3787 if (CheckBuffer(self))
3788 return NULL;
3789
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003790 if (end == -1)
3791 end = self->buf->b_ml.ml_line_count;
3792
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003793 if (n < 0)
3794 n += end - start + 1;
3795
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003796 if (n < 0 || n > end - start)
3797 {
3798 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3799 return NULL;
3800 }
3801
3802 return GetBufferLine(self->buf, n+start);
3803}
3804
3805 static PyObject *
3806RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3807{
3808 PyInt size;
3809
3810 if (CheckBuffer(self))
3811 return NULL;
3812
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003813 if (end == -1)
3814 end = self->buf->b_ml.ml_line_count;
3815
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003816 size = end - start + 1;
3817
3818 if (lo < 0)
3819 lo = 0;
3820 else if (lo > size)
3821 lo = size;
3822 if (hi < 0)
3823 hi = 0;
3824 if (hi < lo)
3825 hi = lo;
3826 else if (hi > size)
3827 hi = size;
3828
3829 return GetBufferLineList(self->buf, lo+start, hi+start);
3830}
3831
3832 static PyInt
3833RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3834{
3835 PyInt len_change;
3836
3837 if (CheckBuffer(self))
3838 return -1;
3839
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003840 if (end == -1)
3841 end = self->buf->b_ml.ml_line_count;
3842
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003843 if (n < 0)
3844 n += end - start + 1;
3845
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003846 if (n < 0 || n > end - start)
3847 {
3848 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3849 return -1;
3850 }
3851
3852 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3853 return -1;
3854
3855 if (new_end)
3856 *new_end = end + len_change;
3857
3858 return 0;
3859}
3860
Bram Moolenaar19e60942011-06-19 00:27:51 +02003861 static PyInt
3862RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3863{
3864 PyInt size;
3865 PyInt len_change;
3866
3867 /* Self must be a valid buffer */
3868 if (CheckBuffer(self))
3869 return -1;
3870
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003871 if (end == -1)
3872 end = self->buf->b_ml.ml_line_count;
3873
Bram Moolenaar19e60942011-06-19 00:27:51 +02003874 /* Sort out the slice range */
3875 size = end - start + 1;
3876
3877 if (lo < 0)
3878 lo = 0;
3879 else if (lo > size)
3880 lo = size;
3881 if (hi < 0)
3882 hi = 0;
3883 if (hi < lo)
3884 hi = lo;
3885 else if (hi > size)
3886 hi = size;
3887
3888 if (SetBufferLineList(self->buf, lo + start, hi + start,
3889 val, &len_change) == FAIL)
3890 return -1;
3891
3892 if (new_end)
3893 *new_end = end + len_change;
3894
3895 return 0;
3896}
3897
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003898
3899 static PyObject *
3900RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3901{
3902 PyObject *lines;
3903 PyInt len_change;
3904 PyInt max;
3905 PyInt n;
3906
3907 if (CheckBuffer(self))
3908 return NULL;
3909
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003910 if (end == -1)
3911 end = self->buf->b_ml.ml_line_count;
3912
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003913 max = n = end - start + 1;
3914
3915 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3916 return NULL;
3917
3918 if (n < 0 || n > max)
3919 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003920 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003921 return NULL;
3922 }
3923
3924 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3925 return NULL;
3926
3927 if (new_end)
3928 *new_end = end + len_change;
3929
3930 Py_INCREF(Py_None);
3931 return Py_None;
3932}
3933
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003934/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003935 */
3936
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003937static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003938static PySequenceMethods RangeAsSeq;
3939static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003940
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003941typedef struct
3942{
3943 PyObject_HEAD
3944 BufferObject *buf;
3945 PyInt start;
3946 PyInt end;
3947} RangeObject;
3948
3949 static PyObject *
3950RangeNew(buf_T *buf, PyInt start, PyInt end)
3951{
3952 BufferObject *bufr;
3953 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003954 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003955 if (self == NULL)
3956 return NULL;
3957
3958 bufr = (BufferObject *)BufferNew(buf);
3959 if (bufr == NULL)
3960 {
3961 Py_DECREF(self);
3962 return NULL;
3963 }
3964 Py_INCREF(bufr);
3965
3966 self->buf = bufr;
3967 self->start = start;
3968 self->end = end;
3969
3970 return (PyObject *)(self);
3971}
3972
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003973 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003974RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003975{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003976 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003977 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003978 PyObject_GC_Del((void *)(self));
3979}
3980
3981 static int
3982RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3983{
3984 Py_VISIT(((PyObject *)(self->buf)));
3985 return 0;
3986}
3987
3988 static int
3989RangeClear(RangeObject *self)
3990{
3991 Py_CLEAR(self->buf);
3992 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003993}
3994
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003995 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003997{
3998 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003999 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004000 return -1; /* ??? */
4001
Bram Moolenaard6e39182013-05-21 18:30:34 +02004002 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004003}
4004
4005 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004006RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004007{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004008 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004009}
4010
4011 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004012RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004013{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004014 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004015}
4016
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004017static char *RangeAttrs[] = {
4018 "start", "end",
4019 NULL
4020};
4021
4022 static PyObject *
4023RangeDir(PyObject *self)
4024{
4025 return ObjectDir(self, RangeAttrs);
4026}
4027
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004028 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004029RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004030{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004031 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004032}
4033
4034 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004035RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004036{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004037 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004038 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4039 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004040 else
4041 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004042 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004043
4044 if (name == NULL)
4045 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004046
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004047 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004048 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004049 }
4050}
4051
4052static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004053 /* name, function, calling, documentation */
4054 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004055 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4056 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004057};
4058
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004059static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004060static PySequenceMethods BufferAsSeq;
4061static PyMappingMethods BufferAsMapping;
4062
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004063 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004064BufferNew(buf_T *buf)
4065{
4066 /* We need to handle deletion of buffers underneath us.
4067 * If we add a "b_python*_ref" field to the buf_T structure,
4068 * then we can get at it in buf_freeall() in vim. We then
4069 * need to create only ONE Python object per buffer - if
4070 * we try to create a second, just INCREF the existing one
4071 * and return it. The (single) Python object referring to
4072 * the buffer is stored in "b_python*_ref".
4073 * Question: what to do on a buf_freeall(). We'll probably
4074 * have to either delete the Python object (DECREF it to
4075 * zero - a bad idea, as it leaves dangling refs!) or
4076 * set the buf_T * value to an invalid value (-1?), which
4077 * means we need checks in all access functions... Bah.
4078 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004079 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004080 * b_python_ref and b_python3_ref fields respectively.
4081 */
4082
4083 BufferObject *self;
4084
4085 if (BUF_PYTHON_REF(buf) != NULL)
4086 {
4087 self = BUF_PYTHON_REF(buf);
4088 Py_INCREF(self);
4089 }
4090 else
4091 {
4092 self = PyObject_NEW(BufferObject, &BufferType);
4093 if (self == NULL)
4094 return NULL;
4095 self->buf = buf;
4096 BUF_PYTHON_REF(buf) = self;
4097 }
4098
4099 return (PyObject *)(self);
4100}
4101
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004102 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004103BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004104{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004105 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4106 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004107
4108 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004109}
4110
Bram Moolenaar971db462013-05-12 18:44:48 +02004111 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004112BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004113{
4114 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004115 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004116 return -1; /* ??? */
4117
Bram Moolenaard6e39182013-05-21 18:30:34 +02004118 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004119}
4120
4121 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004122BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004123{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004124 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004125}
4126
4127 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004128BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004129{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004130 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004131}
4132
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004133static char *BufferAttrs[] = {
4134 "name", "number", "vars", "options", "valid",
4135 NULL
4136};
4137
4138 static PyObject *
4139BufferDir(PyObject *self)
4140{
4141 return ObjectDir(self, BufferAttrs);
4142}
4143
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004144 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004145BufferAttrValid(BufferObject *self, char *name)
4146{
4147 PyObject *r;
4148
4149 if (strcmp(name, "valid") != 0)
4150 return NULL;
4151
4152 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4153 Py_INCREF(r);
4154 return r;
4155}
4156
4157 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004158BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004159{
4160 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004161 return PyString_FromString((self->buf->b_ffname == NULL
4162 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004163 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004164 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004165 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004166 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004167 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004168 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4169 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004170 else if (strcmp(name, "__members__") == 0)
4171 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004172 else
4173 return NULL;
4174}
4175
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004176 static int
4177BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4178{
4179 if (CheckBuffer(self))
4180 return -1;
4181
4182 if (strcmp(name, "name") == 0)
4183 {
4184 char_u *val;
4185 aco_save_T aco;
4186 int r;
4187 PyObject *todecref;
4188
4189 if (!(val = StringToChars(valObject, &todecref)))
4190 return -1;
4191
4192 VimTryStart();
4193 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4194 aucmd_prepbuf(&aco, self->buf);
4195 r = rename_buffer(val);
4196 aucmd_restbuf(&aco);
4197 Py_XDECREF(todecref);
4198 if (VimTryEnd())
4199 return -1;
4200
4201 if (r == FAIL)
4202 {
4203 PyErr_SetVim(_("failed to rename buffer"));
4204 return -1;
4205 }
4206 return 0;
4207 }
4208 else
4209 {
4210 PyErr_SetString(PyExc_AttributeError, name);
4211 return -1;
4212 }
4213}
4214
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004215 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004216BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004217{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004218 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004219}
4220
4221 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004222BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004223{
4224 pos_T *posp;
4225 char *pmark;
4226 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004227 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004228
Bram Moolenaard6e39182013-05-21 18:30:34 +02004229 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004230 return NULL;
4231
4232 if (!PyArg_ParseTuple(args, "s", &pmark))
4233 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004234
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004235 if (STRLEN(pmark) != 1)
4236 {
4237 PyErr_SetString(PyExc_ValueError,
4238 _("mark name must be a single character"));
4239 return NULL;
4240 }
4241
4242 mark = *pmark;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004243 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004244 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004245 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004246 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004247 if (VimTryEnd())
4248 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004249
4250 if (posp == NULL)
4251 {
4252 PyErr_SetVim(_("invalid mark name"));
4253 return NULL;
4254 }
4255
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004256 if (posp->lnum <= 0)
4257 {
4258 /* Or raise an error? */
4259 Py_INCREF(Py_None);
4260 return Py_None;
4261 }
4262
4263 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4264}
4265
4266 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004267BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004268{
4269 PyInt start;
4270 PyInt end;
4271
Bram Moolenaard6e39182013-05-21 18:30:34 +02004272 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004273 return NULL;
4274
4275 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4276 return NULL;
4277
Bram Moolenaard6e39182013-05-21 18:30:34 +02004278 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004279}
4280
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004281 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004282BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004283{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004284 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004285 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004286 else
4287 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004288 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004289
4290 if (name == NULL)
4291 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004292
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004293 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004294 }
4295}
4296
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004297static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004298 /* name, function, calling, documentation */
4299 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4300 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4301 {"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 +02004302 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4303 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004304};
4305
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004306/*
4307 * Buffer list object - Implementation
4308 */
4309
4310static PyTypeObject BufMapType;
4311
4312typedef struct
4313{
4314 PyObject_HEAD
4315} BufMapObject;
4316
4317 static PyInt
4318BufMapLength(PyObject *self UNUSED)
4319{
4320 buf_T *b = firstbuf;
4321 PyInt n = 0;
4322
4323 while (b)
4324 {
4325 ++n;
4326 b = b->b_next;
4327 }
4328
4329 return n;
4330}
4331
4332 static PyObject *
4333BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4334{
4335 buf_T *b;
4336 int bnr;
4337
4338#if PY_MAJOR_VERSION < 3
4339 if (PyInt_Check(keyObject))
4340 bnr = PyInt_AsLong(keyObject);
4341 else
4342#endif
4343 if (PyLong_Check(keyObject))
4344 bnr = PyLong_AsLong(keyObject);
4345 else
4346 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004347 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004348 return NULL;
4349 }
4350
4351 b = buflist_findnr(bnr);
4352
4353 if (b)
4354 return BufferNew(b);
4355 else
4356 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004357 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004358 return NULL;
4359 }
4360}
4361
4362 static void
4363BufMapIterDestruct(PyObject *buffer)
4364{
4365 /* Iteration was stopped before all buffers were processed */
4366 if (buffer)
4367 {
4368 Py_DECREF(buffer);
4369 }
4370}
4371
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004372 static int
4373BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4374{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004375 if (buffer)
4376 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004377 return 0;
4378}
4379
4380 static int
4381BufMapIterClear(PyObject **buffer)
4382{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004383 if (*buffer)
4384 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004385 return 0;
4386}
4387
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004388 static PyObject *
4389BufMapIterNext(PyObject **buffer)
4390{
4391 PyObject *next;
4392 PyObject *r;
4393
4394 if (!*buffer)
4395 return NULL;
4396
4397 r = *buffer;
4398
4399 if (CheckBuffer((BufferObject *)(r)))
4400 {
4401 *buffer = NULL;
4402 return NULL;
4403 }
4404
4405 if (!((BufferObject *)(r))->buf->b_next)
4406 next = NULL;
4407 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4408 return NULL;
4409 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004410 /* Do not increment reference: we no longer hold it (decref), but whoever
4411 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004412 return r;
4413}
4414
4415 static PyObject *
4416BufMapIter(PyObject *self UNUSED)
4417{
4418 PyObject *buffer;
4419
4420 buffer = BufferNew(firstbuf);
4421 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004422 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4423 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004424}
4425
4426static PyMappingMethods BufMapAsMapping = {
4427 (lenfunc) BufMapLength,
4428 (binaryfunc) BufMapItem,
4429 (objobjargproc) 0,
4430};
4431
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004432/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004433 */
4434
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004435static char *CurrentAttrs[] = {
4436 "buffer", "window", "line", "range", "tabpage",
4437 NULL
4438};
4439
4440 static PyObject *
4441CurrentDir(PyObject *self)
4442{
4443 return ObjectDir(self, CurrentAttrs);
4444}
4445
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004446 static PyObject *
4447CurrentGetattr(PyObject *self UNUSED, char *name)
4448{
4449 if (strcmp(name, "buffer") == 0)
4450 return (PyObject *)BufferNew(curbuf);
4451 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004452 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004453 else if (strcmp(name, "tabpage") == 0)
4454 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004455 else if (strcmp(name, "line") == 0)
4456 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4457 else if (strcmp(name, "range") == 0)
4458 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004459 else if (strcmp(name, "__members__") == 0)
4460 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004461 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004462#if PY_MAJOR_VERSION < 3
4463 return Py_FindMethod(WindowMethods, self, name);
4464#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004465 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004466#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004467}
4468
4469 static int
4470CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4471{
4472 if (strcmp(name, "line") == 0)
4473 {
4474 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4475 return -1;
4476
4477 return 0;
4478 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004479 else if (strcmp(name, "buffer") == 0)
4480 {
4481 int count;
4482
4483 if (value->ob_type != &BufferType)
4484 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004485 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004486 return -1;
4487 }
4488
4489 if (CheckBuffer((BufferObject *)(value)))
4490 return -1;
4491 count = ((BufferObject *)(value))->buf->b_fnum;
4492
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004493 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004494 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4495 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004496 if (VimTryEnd())
4497 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004498 PyErr_SetVim(_("failed to switch to given buffer"));
4499 return -1;
4500 }
4501
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004502 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004503 }
4504 else if (strcmp(name, "window") == 0)
4505 {
4506 int count;
4507
4508 if (value->ob_type != &WindowType)
4509 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004510 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004511 return -1;
4512 }
4513
4514 if (CheckWindow((WindowObject *)(value)))
4515 return -1;
4516 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4517
4518 if (!count)
4519 {
4520 PyErr_SetString(PyExc_ValueError,
4521 _("failed to find window in the current tab page"));
4522 return -1;
4523 }
4524
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004525 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004526 win_goto(((WindowObject *)(value))->win);
4527 if (((WindowObject *)(value))->win != curwin)
4528 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004529 if (VimTryEnd())
4530 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004531 PyErr_SetString(PyExc_RuntimeError,
4532 _("did not switch to the specified window"));
4533 return -1;
4534 }
4535
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004536 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004537 }
4538 else if (strcmp(name, "tabpage") == 0)
4539 {
4540 if (value->ob_type != &TabPageType)
4541 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004542 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004543 return -1;
4544 }
4545
4546 if (CheckTabPage((TabPageObject *)(value)))
4547 return -1;
4548
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004549 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004550 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4551 if (((TabPageObject *)(value))->tab != curtab)
4552 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004553 if (VimTryEnd())
4554 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004555 PyErr_SetString(PyExc_RuntimeError,
4556 _("did not switch to the specified tab page"));
4557 return -1;
4558 }
4559
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004560 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004561 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004562 else
4563 {
4564 PyErr_SetString(PyExc_AttributeError, name);
4565 return -1;
4566 }
4567}
4568
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004569static struct PyMethodDef CurrentMethods[] = {
4570 /* name, function, calling, documentation */
4571 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4572 { NULL, NULL, 0, NULL}
4573};
4574
Bram Moolenaardb913952012-06-29 12:54:53 +02004575 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004576init_range_cmd(exarg_T *eap)
4577{
4578 RangeStart = eap->line1;
4579 RangeEnd = eap->line2;
4580}
4581
4582 static void
4583init_range_eval(typval_T *rettv UNUSED)
4584{
4585 RangeStart = (PyInt) curwin->w_cursor.lnum;
4586 RangeEnd = RangeStart;
4587}
4588
4589 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004590run_cmd(const char *cmd, void *arg UNUSED
4591#ifdef PY_CAN_RECURSE
4592 , PyGILState_STATE *pygilstate UNUSED
4593#endif
4594 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004595{
4596 PyRun_SimpleString((char *) cmd);
4597}
4598
4599static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4600static int code_hdr_len = 30;
4601
4602 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004603run_do(const char *cmd, void *arg UNUSED
4604#ifdef PY_CAN_RECURSE
4605 , PyGILState_STATE *pygilstate
4606#endif
4607 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004608{
4609 PyInt lnum;
4610 size_t len;
4611 char *code;
4612 int status;
4613 PyObject *pyfunc, *pymain;
4614
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004615 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004616 {
4617 EMSG(_("cannot save undo information"));
4618 return;
4619 }
4620
4621 len = code_hdr_len + STRLEN(cmd);
4622 code = PyMem_New(char, len + 1);
4623 memcpy(code, code_hdr, code_hdr_len);
4624 STRCPY(code + code_hdr_len, cmd);
4625 status = PyRun_SimpleString(code);
4626 PyMem_Free(code);
4627
4628 if (status)
4629 {
4630 EMSG(_("failed to run the code"));
4631 return;
4632 }
4633
4634 status = 0;
4635 pymain = PyImport_AddModule("__main__");
4636 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004637#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004638 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004639#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004640
4641 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4642 {
4643 PyObject *line, *linenr, *ret;
4644
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004645#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004646 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004647#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004648 if (!(line = GetBufferLine(curbuf, lnum)))
4649 goto err;
4650 if (!(linenr = PyInt_FromLong((long) lnum)))
4651 {
4652 Py_DECREF(line);
4653 goto err;
4654 }
4655 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4656 Py_DECREF(line);
4657 Py_DECREF(linenr);
4658 if (!ret)
4659 goto err;
4660
4661 if (ret != Py_None)
4662 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4663 goto err;
4664
4665 Py_XDECREF(ret);
4666 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004667#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004668 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004669#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004670 }
4671 goto out;
4672err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004673#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004674 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004675#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004676 PyErr_PrintEx(0);
4677 PythonIO_Flush();
4678 status = 1;
4679out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004680#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004681 if (!status)
4682 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004683#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004684 Py_DECREF(pyfunc);
4685 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4686 if (status)
4687 return;
4688 check_cursor();
4689 update_curbuf(NOT_VALID);
4690}
4691
4692 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004693run_eval(const char *cmd, typval_T *rettv
4694#ifdef PY_CAN_RECURSE
4695 , PyGILState_STATE *pygilstate UNUSED
4696#endif
4697 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004698{
4699 PyObject *r;
4700
4701 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4702 if (r == NULL)
4703 {
4704 if (PyErr_Occurred() && !msg_silent)
4705 PyErr_PrintEx(0);
4706 EMSG(_("E858: Eval did not return a valid python object"));
4707 }
4708 else
4709 {
4710 if (ConvertFromPyObject(r, rettv) == -1)
4711 EMSG(_("E859: Failed to convert returned python object to vim value"));
4712 Py_DECREF(r);
4713 }
4714 PyErr_Clear();
4715}
4716
4717 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004718set_ref_in_py(const int copyID)
4719{
4720 pylinkedlist_T *cur;
4721 dict_T *dd;
4722 list_T *ll;
4723
4724 if (lastdict != NULL)
4725 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4726 {
4727 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4728 if (dd->dv_copyID != copyID)
4729 {
4730 dd->dv_copyID = copyID;
4731 set_ref_in_ht(&dd->dv_hashtab, copyID);
4732 }
4733 }
4734
4735 if (lastlist != NULL)
4736 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4737 {
4738 ll = ((ListObject *) (cur->pll_obj))->list;
4739 if (ll->lv_copyID != copyID)
4740 {
4741 ll->lv_copyID = copyID;
4742 set_ref_in_list(ll, copyID);
4743 }
4744 }
4745}
4746
4747 static int
4748set_string_copy(char_u *str, typval_T *tv)
4749{
4750 tv->vval.v_string = vim_strsave(str);
4751 if (tv->vval.v_string == NULL)
4752 {
4753 PyErr_NoMemory();
4754 return -1;
4755 }
4756 return 0;
4757}
4758
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004759 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004760pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004761{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004762 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004763 char_u *key;
4764 dictitem_T *di;
4765 PyObject *keyObject;
4766 PyObject *valObject;
4767 Py_ssize_t iter = 0;
4768
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004769 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004770 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004771
4772 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004773 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004774
4775 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4776 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004777 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004778
Bram Moolenaara03e6312013-05-29 22:49:26 +02004779 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004780 {
4781 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004782 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004783 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004784
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004785 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004786 {
4787 dict_unref(dict);
4788 return -1;
4789 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004790
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004791 if (*key == NUL)
4792 {
4793 dict_unref(dict);
4794 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004795 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004796 return -1;
4797 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004798
4799 di = dictitem_alloc(key);
4800
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004801 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004802
4803 if (di == NULL)
4804 {
4805 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004806 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004807 return -1;
4808 }
4809 di->di_tv.v_lock = 0;
4810
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004811 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004812 {
4813 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004814 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004815 return -1;
4816 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004817
4818 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004819 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004820 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004821 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004822 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004823 PyErr_SetVim(_("failed to add key to dictionary"));
4824 return -1;
4825 }
4826 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004827
4828 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004829 return 0;
4830}
4831
4832 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004833pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004834{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004835 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004836 char_u *key;
4837 dictitem_T *di;
4838 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004839 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004840 PyObject *keyObject;
4841 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004842
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004843 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004844 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004845
4846 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004847 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004848
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004849 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004850 {
4851 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004852 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004853 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004854
4855 if (!(iterator = PyObject_GetIter(list)))
4856 {
4857 dict_unref(dict);
4858 Py_DECREF(list);
4859 return -1;
4860 }
4861 Py_DECREF(list);
4862
4863 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004864 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004865 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004866
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004867 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004868 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004869 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004870 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004871 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004872 return -1;
4873 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004874
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004875 if (*key == NUL)
4876 {
4877 Py_DECREF(keyObject);
4878 Py_DECREF(iterator);
4879 Py_XDECREF(todecref);
4880 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004881 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004882 return -1;
4883 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004884
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004885 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004886 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004887 Py_DECREF(keyObject);
4888 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004889 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004890 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004891 return -1;
4892 }
4893
4894 di = dictitem_alloc(key);
4895
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004896 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004897 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004898
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004899 if (di == NULL)
4900 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004901 Py_DECREF(iterator);
4902 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004903 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004904 PyErr_NoMemory();
4905 return -1;
4906 }
4907 di->di_tv.v_lock = 0;
4908
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004909 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004910 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004911 Py_DECREF(iterator);
4912 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004913 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004914 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004915 return -1;
4916 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004917
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004918 Py_DECREF(valObject);
4919
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004920 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004921 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004922 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004923 dictitem_free(di);
4924 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004925 PyErr_SetVim(_("failed to add key to dictionary"));
4926 return -1;
4927 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004928 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004929 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004930 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004931 return 0;
4932}
4933
4934 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004935pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004936{
4937 list_T *l;
4938
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004939 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004940 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004941
4942 tv->v_type = VAR_LIST;
4943 tv->vval.v_list = l;
4944
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004945 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004946 {
4947 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004948 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004949 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004950
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004951 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004952 return 0;
4953}
4954
Bram Moolenaardb913952012-06-29 12:54:53 +02004955typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4956
4957 static int
4958convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004959 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004960{
4961 PyObject *capsule;
4962 char hexBuf[sizeof(void *) * 2 + 3];
4963
4964 sprintf(hexBuf, "%p", obj);
4965
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004966# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004967 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004968# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004969 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004970# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004971 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004972 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004973# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004974 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004975# else
4976 capsule = PyCObject_FromVoidPtr(tv, NULL);
4977# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004978 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4979 {
4980 Py_DECREF(capsule);
4981 tv->v_type = VAR_UNKNOWN;
4982 return -1;
4983 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004984 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004985 {
4986 tv->v_type = VAR_UNKNOWN;
4987 return -1;
4988 }
4989 /* As we are not using copy_tv which increments reference count we must
4990 * do it ourself. */
4991 switch(tv->v_type)
4992 {
4993 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4994 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4995 }
4996 }
4997 else
4998 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004999 typval_T *v;
5000
5001# ifdef PY_USE_CAPSULE
5002 v = PyCapsule_GetPointer(capsule, NULL);
5003# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005004 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005005# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005006 copy_tv(v, tv);
5007 }
5008 return 0;
5009}
5010
5011 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005012ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5013{
5014 PyObject *lookup_dict;
5015 int r;
5016
5017 if (!(lookup_dict = PyDict_New()))
5018 return -1;
5019
5020 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5021 {
5022 tv->v_type = VAR_DICT;
5023 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5024 ++tv->vval.v_dict->dv_refcount;
5025 r = 0;
5026 }
5027 else if (PyDict_Check(obj))
5028 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
5029 else if (PyMapping_Check(obj))
5030 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5031 else
5032 {
5033 PyErr_SetString(PyExc_TypeError,
5034 _("unable to convert object to vim dictionary"));
5035 r = -1;
5036 }
5037 Py_DECREF(lookup_dict);
5038 return r;
5039}
5040
5041 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005042ConvertFromPyObject(PyObject *obj, typval_T *tv)
5043{
5044 PyObject *lookup_dict;
5045 int r;
5046
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005047 if (!(lookup_dict = PyDict_New()))
5048 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005049 r = _ConvertFromPyObject(obj, tv, lookup_dict);
5050 Py_DECREF(lookup_dict);
5051 return r;
5052}
5053
5054 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005055_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005056{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005057 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005058 {
5059 tv->v_type = VAR_DICT;
5060 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5061 ++tv->vval.v_dict->dv_refcount;
5062 }
5063 else if (obj->ob_type == &ListType)
5064 {
5065 tv->v_type = VAR_LIST;
5066 tv->vval.v_list = (((ListObject *)(obj))->list);
5067 ++tv->vval.v_list->lv_refcount;
5068 }
5069 else if (obj->ob_type == &FunctionType)
5070 {
5071 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5072 return -1;
5073
5074 tv->v_type = VAR_FUNC;
5075 func_ref(tv->vval.v_string);
5076 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005077 else if (PyBytes_Check(obj))
5078 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005079 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02005080
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005081 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
5082 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005083 if (result == NULL)
5084 return -1;
5085
5086 if (set_string_copy(result, tv) == -1)
5087 return -1;
5088
5089 tv->v_type = VAR_STRING;
5090 }
5091 else if (PyUnicode_Check(obj))
5092 {
5093 PyObject *bytes;
5094 char_u *result;
5095
Bram Moolenaardb913952012-06-29 12:54:53 +02005096 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
5097 if (bytes == NULL)
5098 return -1;
5099
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005100 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
5101 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005102 if (result == NULL)
5103 return -1;
5104
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005105 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005106 {
5107 Py_XDECREF(bytes);
5108 return -1;
5109 }
5110 Py_XDECREF(bytes);
5111
5112 tv->v_type = VAR_STRING;
5113 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005114#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005115 else if (PyInt_Check(obj))
5116 {
5117 tv->v_type = VAR_NUMBER;
5118 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
5119 }
5120#endif
5121 else if (PyLong_Check(obj))
5122 {
5123 tv->v_type = VAR_NUMBER;
5124 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
5125 }
5126 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005127 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005128#ifdef FEAT_FLOAT
5129 else if (PyFloat_Check(obj))
5130 {
5131 tv->v_type = VAR_FLOAT;
5132 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5133 }
5134#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005135 else if (PyObject_HasAttrString(obj, "keys"))
5136 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005137 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005138 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005139 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005140 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005141 else
5142 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02005143 PyErr_SetString(PyExc_TypeError,
5144 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005145 return -1;
5146 }
5147 return 0;
5148}
5149
5150 static PyObject *
5151ConvertToPyObject(typval_T *tv)
5152{
5153 if (tv == NULL)
5154 {
5155 PyErr_SetVim(_("NULL reference passed"));
5156 return NULL;
5157 }
5158 switch (tv->v_type)
5159 {
5160 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005161 return PyBytes_FromString(tv->vval.v_string == NULL
5162 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005163 case VAR_NUMBER:
5164 return PyLong_FromLong((long) tv->vval.v_number);
5165#ifdef FEAT_FLOAT
5166 case VAR_FLOAT:
5167 return PyFloat_FromDouble((double) tv->vval.v_float);
5168#endif
5169 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005170 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005171 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005172 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005173 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005174 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005175 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005176 case VAR_UNKNOWN:
5177 Py_INCREF(Py_None);
5178 return Py_None;
5179 default:
5180 PyErr_SetVim(_("internal error: invalid value type"));
5181 return NULL;
5182 }
5183}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005184
5185typedef struct
5186{
5187 PyObject_HEAD
5188} CurrentObject;
5189static PyTypeObject CurrentType;
5190
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005191#if PY_MAJOR_VERSION >= 3
5192typedef struct
5193{
5194 PyObject_HEAD
5195} FinderObject;
5196static PyTypeObject FinderType;
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005197#else
5198typedef struct
5199{
5200 PyObject_HEAD
5201 PyObject *module;
5202} LoaderObject;
5203static PyTypeObject LoaderType;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005204#endif
5205
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005206 static void
5207init_structs(void)
5208{
5209 vim_memset(&OutputType, 0, sizeof(OutputType));
5210 OutputType.tp_name = "vim.message";
5211 OutputType.tp_basicsize = sizeof(OutputObject);
5212 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5213 OutputType.tp_doc = "vim message object";
5214 OutputType.tp_methods = OutputMethods;
5215#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005216 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5217 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005218 OutputType.tp_alloc = call_PyType_GenericAlloc;
5219 OutputType.tp_new = call_PyType_GenericNew;
5220 OutputType.tp_free = call_PyObject_Free;
5221#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005222 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5223 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005224#endif
5225
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005226 vim_memset(&IterType, 0, sizeof(IterType));
5227 IterType.tp_name = "vim.iter";
5228 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005229 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005230 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005231 IterType.tp_iter = (getiterfunc)IterIter;
5232 IterType.tp_iternext = (iternextfunc)IterNext;
5233 IterType.tp_dealloc = (destructor)IterDestructor;
5234 IterType.tp_traverse = (traverseproc)IterTraverse;
5235 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005236
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005237 vim_memset(&BufferType, 0, sizeof(BufferType));
5238 BufferType.tp_name = "vim.buffer";
5239 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005240 BufferType.tp_dealloc = (destructor)BufferDestructor;
5241 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005242 BufferType.tp_as_sequence = &BufferAsSeq;
5243 BufferType.tp_as_mapping = &BufferAsMapping;
5244 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5245 BufferType.tp_doc = "vim buffer object";
5246 BufferType.tp_methods = BufferMethods;
5247#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005248 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005249 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005250 BufferType.tp_alloc = call_PyType_GenericAlloc;
5251 BufferType.tp_new = call_PyType_GenericNew;
5252 BufferType.tp_free = call_PyObject_Free;
5253#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005254 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005255 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005256#endif
5257
5258 vim_memset(&WindowType, 0, sizeof(WindowType));
5259 WindowType.tp_name = "vim.window";
5260 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005261 WindowType.tp_dealloc = (destructor)WindowDestructor;
5262 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005263 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005264 WindowType.tp_doc = "vim Window object";
5265 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005266 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5267 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005268#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005269 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5270 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005271 WindowType.tp_alloc = call_PyType_GenericAlloc;
5272 WindowType.tp_new = call_PyType_GenericNew;
5273 WindowType.tp_free = call_PyObject_Free;
5274#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005275 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5276 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005277#endif
5278
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005279 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5280 TabPageType.tp_name = "vim.tabpage";
5281 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005282 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5283 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005284 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5285 TabPageType.tp_doc = "vim tab page object";
5286 TabPageType.tp_methods = TabPageMethods;
5287#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005288 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005289 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5290 TabPageType.tp_new = call_PyType_GenericNew;
5291 TabPageType.tp_free = call_PyObject_Free;
5292#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005293 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005294#endif
5295
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005296 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5297 BufMapType.tp_name = "vim.bufferlist";
5298 BufMapType.tp_basicsize = sizeof(BufMapObject);
5299 BufMapType.tp_as_mapping = &BufMapAsMapping;
5300 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005301 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005302 BufferType.tp_doc = "vim buffer list";
5303
5304 vim_memset(&WinListType, 0, sizeof(WinListType));
5305 WinListType.tp_name = "vim.windowlist";
5306 WinListType.tp_basicsize = sizeof(WinListType);
5307 WinListType.tp_as_sequence = &WinListAsSeq;
5308 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5309 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005310 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005311
5312 vim_memset(&TabListType, 0, sizeof(TabListType));
5313 TabListType.tp_name = "vim.tabpagelist";
5314 TabListType.tp_basicsize = sizeof(TabListType);
5315 TabListType.tp_as_sequence = &TabListAsSeq;
5316 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5317 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005318
5319 vim_memset(&RangeType, 0, sizeof(RangeType));
5320 RangeType.tp_name = "vim.range";
5321 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005322 RangeType.tp_dealloc = (destructor)RangeDestructor;
5323 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005324 RangeType.tp_as_sequence = &RangeAsSeq;
5325 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005326 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005327 RangeType.tp_doc = "vim Range object";
5328 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005329 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5330 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005331#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005332 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005333 RangeType.tp_alloc = call_PyType_GenericAlloc;
5334 RangeType.tp_new = call_PyType_GenericNew;
5335 RangeType.tp_free = call_PyObject_Free;
5336#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005337 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005338#endif
5339
5340 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5341 CurrentType.tp_name = "vim.currentdata";
5342 CurrentType.tp_basicsize = sizeof(CurrentObject);
5343 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5344 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005345 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005346#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005347 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5348 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005349#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005350 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5351 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005352#endif
5353
5354 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5355 DictionaryType.tp_name = "vim.dictionary";
5356 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005357 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005358 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005359 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005360 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005361 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5362 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005363 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5364 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5365 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005366#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005367 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5368 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005369#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005370 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5371 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005372#endif
5373
5374 vim_memset(&ListType, 0, sizeof(ListType));
5375 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005376 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005377 ListType.tp_basicsize = sizeof(ListObject);
5378 ListType.tp_as_sequence = &ListAsSeq;
5379 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005380 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005381 ListType.tp_doc = "list pushing modifications to vim structure";
5382 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005383 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005384 ListType.tp_new = (newfunc)ListConstructor;
5385 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005386#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005387 ListType.tp_getattro = (getattrofunc)ListGetattro;
5388 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005389#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005390 ListType.tp_getattr = (getattrfunc)ListGetattr;
5391 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005392#endif
5393
5394 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005395 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005396 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005397 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5398 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005399 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005400 FunctionType.tp_doc = "object that calls vim function";
5401 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005402 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005403 FunctionType.tp_new = (newfunc)FunctionConstructor;
5404 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005405#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005406 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005407#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005408 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005409#endif
5410
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005411 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5412 OptionsType.tp_name = "vim.options";
5413 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005414 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005415 OptionsType.tp_doc = "object for manipulating options";
5416 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005417 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5418 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5419 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005420
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005421#if PY_MAJOR_VERSION >= 3
5422 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5423 vimmodule.m_name = "vim";
5424 vimmodule.m_doc = "Vim Python interface\n";
5425 vimmodule.m_size = -1;
5426 vimmodule.m_methods = VimMethods;
5427#endif
5428}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005429
5430#define PYTYPE_READY(type) \
5431 if (PyType_Ready(&type)) \
5432 return -1;
5433
5434 static int
5435init_types()
5436{
5437 PYTYPE_READY(IterType);
5438 PYTYPE_READY(BufferType);
5439 PYTYPE_READY(RangeType);
5440 PYTYPE_READY(WindowType);
5441 PYTYPE_READY(TabPageType);
5442 PYTYPE_READY(BufMapType);
5443 PYTYPE_READY(WinListType);
5444 PYTYPE_READY(TabListType);
5445 PYTYPE_READY(CurrentType);
5446 PYTYPE_READY(DictionaryType);
5447 PYTYPE_READY(ListType);
5448 PYTYPE_READY(FunctionType);
5449 PYTYPE_READY(OptionsType);
5450 PYTYPE_READY(OutputType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005451#if PY_MAJOR_VERSION >= 3
5452 PYTYPE_READY(FinderType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005453#else
5454 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005455#endif
5456 return 0;
5457}
5458
5459 static int
5460init_sys_path()
5461{
5462 PyObject *path;
5463 PyObject *path_hook;
5464 PyObject *path_hooks;
5465
5466 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5467 return -1;
5468
5469 if (!(path_hooks = PySys_GetObject("path_hooks")))
5470 {
5471 PyErr_Clear();
5472 path_hooks = PyList_New(1);
5473 PyList_SET_ITEM(path_hooks, 0, path_hook);
5474 if (PySys_SetObject("path_hooks", path_hooks))
5475 {
5476 Py_DECREF(path_hooks);
5477 return -1;
5478 }
5479 Py_DECREF(path_hooks);
5480 }
5481 else if (PyList_Check(path_hooks))
5482 {
5483 if (PyList_Append(path_hooks, path_hook))
5484 {
5485 Py_DECREF(path_hook);
5486 return -1;
5487 }
5488 Py_DECREF(path_hook);
5489 }
5490 else
5491 {
5492 VimTryStart();
5493 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5494 "You should now do the following:\n"
5495 "- append vim.path_hook to sys.path_hooks\n"
5496 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5497 VimTryEnd(); /* Discard the error */
5498 Py_DECREF(path_hook);
5499 return 0;
5500 }
5501
5502 if (!(path = PySys_GetObject("path")))
5503 {
5504 PyErr_Clear();
5505 path = PyList_New(1);
5506 Py_INCREF(vim_special_path_object);
5507 PyList_SET_ITEM(path, 0, vim_special_path_object);
5508 if (PySys_SetObject("path", path))
5509 {
5510 Py_DECREF(path);
5511 return -1;
5512 }
5513 Py_DECREF(path);
5514 }
5515 else if (PyList_Check(path))
5516 {
5517 if (PyList_Append(path, vim_special_path_object))
5518 return -1;
5519 }
5520 else
5521 {
5522 VimTryStart();
5523 EMSG(_("Failed to set path: sys.path is not a list\n"
5524 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5525 VimTryEnd(); /* Discard the error */
5526 }
5527
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005528 return 0;
5529}
5530
5531static BufMapObject TheBufferMap =
5532{
5533 PyObject_HEAD_INIT(&BufMapType)
5534};
5535
5536static WinListObject TheWindowList =
5537{
5538 PyObject_HEAD_INIT(&WinListType)
5539 NULL
5540};
5541
5542static CurrentObject TheCurrent =
5543{
5544 PyObject_HEAD_INIT(&CurrentType)
5545};
5546
5547static TabListObject TheTabPageList =
5548{
5549 PyObject_HEAD_INIT(&TabListType)
5550};
5551
5552static struct numeric_constant {
5553 char *name;
5554 int value;
5555} numeric_constants[] = {
5556 {"VAR_LOCKED", VAR_LOCKED},
5557 {"VAR_FIXED", VAR_FIXED},
5558 {"VAR_SCOPE", VAR_SCOPE},
5559 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5560};
5561
5562static struct object_constant {
5563 char *name;
5564 PyObject *value;
5565} object_constants[] = {
5566 {"buffers", (PyObject *)(void *)&TheBufferMap},
5567 {"windows", (PyObject *)(void *)&TheWindowList},
5568 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5569 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005570
5571 {"Buffer", (PyObject *)&BufferType},
5572 {"Range", (PyObject *)&RangeType},
5573 {"Window", (PyObject *)&WindowType},
5574 {"TabPage", (PyObject *)&TabPageType},
5575 {"Dictionary", (PyObject *)&DictionaryType},
5576 {"List", (PyObject *)&ListType},
5577 {"Function", (PyObject *)&FunctionType},
5578 {"Options", (PyObject *)&OptionsType},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005579#if PY_MAJOR_VERSION >= 3
5580 {"Finder", (PyObject *)&FinderType},
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005581#else
5582 {"Loader", (PyObject *)&LoaderType},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005583#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005584};
5585
5586typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005587typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005588
5589#define ADD_OBJECT(m, name, obj) \
5590 if (add_object(m, name, obj)) \
5591 return -1;
5592
5593#define ADD_CHECKED_OBJECT(m, name, obj) \
5594 { \
5595 PyObject *value = obj; \
5596 if (!value) \
5597 return -1; \
5598 ADD_OBJECT(m, name, value); \
5599 }
5600
5601 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005602populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005603{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005604 int i;
5605 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005606 PyObject *attr;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005607
5608 for (i = 0; i < (int)(sizeof(numeric_constants)
5609 / sizeof(struct numeric_constant));
5610 ++i)
5611 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5612 PyInt_FromLong(numeric_constants[i].value));
5613
5614 for (i = 0; i < (int)(sizeof(object_constants)
5615 / sizeof(struct object_constant));
5616 ++i)
5617 {
5618 PyObject *value;
5619
5620 value = object_constants[i].value;
5621 Py_INCREF(value);
5622 ADD_OBJECT(m, object_constants[i].name, value);
5623 }
5624
5625 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5626 return -1;
5627 ADD_OBJECT(m, "error", VimError);
5628
Bram Moolenaara9922d62013-05-30 13:01:18 +02005629 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5630 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005631 ADD_CHECKED_OBJECT(m, "options",
5632 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005633
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005634 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005635 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005636 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005637
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005638 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005639 return -1;
5640 ADD_OBJECT(m, "_getcwd", py_getcwd)
5641
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005642 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005643 return -1;
5644 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005645 if (!(attr = get_attr(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005646 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005647 if (PyObject_SetAttrString(other_module, "chdir", attr))
5648 {
5649 Py_DECREF(attr);
5650 return -1;
5651 }
5652 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005653
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005654 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005655 {
5656 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005657 if (!(attr = get_attr(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005658 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005659 if (PyObject_SetAttrString(other_module, "fchdir", attr))
5660 {
5661 Py_DECREF(attr);
5662 return -1;
5663 }
5664 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005665 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02005666 else
5667 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02005668
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005669 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
5670 return -1;
5671
5672 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
5673
5674#if PY_MAJOR_VERSION >= 3
5675 ADD_OBJECT(m, "_PathFinder", path_finder);
5676 ADD_CHECKED_OBJECT(m, "_find_module",
5677 (py_find_module = PyObject_GetAttrString(path_finder,
5678 "find_module")));
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005679#else
5680 ADD_OBJECT(m, "_find_module", py_find_module);
5681 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005682#endif
5683
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005684 return 0;
5685}